/* ============================================================
 *  Racing Game  –  Master Stylesheet
 * ============================================================ */

/* ---------- Reset & Base ---------- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --bg:       #0b0e17;
    --surface:  #141929;
    --card:     #1a2035;
    --border:   #252d44;
    --text:     #d4d8e8;
    --muted:    #6b7394;
    --accent:   #e94560;
    --accent2:  #0f3460;
    --green:    #2ecc71;
    --blue:     #3498db;
    --yellow:   #f1c40f;
    --radius:   8px;
}

body {
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
}

a { color: var(--blue); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---------- Utility ---------- */
.hidden { display: none !important; }

/* ---------- Buttons ---------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center;
    padding: 10px 22px;
    border: none; border-radius: var(--radius);
    font-size: 14px; font-weight: 600;
    cursor: pointer; transition: all .15s;
    color: #fff;
}
.btn:hover { filter: brightness(1.15); transform: translateY(-1px); }
.btn:active { transform: translateY(0); }

.btn-primary   { background: var(--accent); }
.btn-secondary { background: var(--accent2); }
.btn-success   { background: var(--green); }
.btn-danger    { background: #c0392b; }
.btn-small     { padding: 6px 14px; font-size: 12px; }
.btn:disabled  { opacity: .5; cursor: not-allowed; filter: none; transform: none; }

/* ---------- Forms ---------- */
.form-group { margin-bottom: 14px; }
.form-group label {
    display: block; margin-bottom: 4px;
    font-size: 13px; color: var(--muted); font-weight: 600;
}
.form-control {
    width: 100%; padding: 10px 14px;
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); color: var(--text);
    font-size: 14px; outline: none; transition: border-color .15s;
}
.form-control:focus { border-color: var(--accent); }
select.form-control { cursor: pointer; }

/* ---------- Cards ---------- */
.card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 24px;
}

/* ---------- Layout ---------- */
.container {
    max-width: 900px;
    margin: 0 auto;
    padding: 30px 20px;
}

/* ============================================================
 *  INDEX / LOBBY
 * ============================================================ */
.app-header {
    text-align: center;
    padding: 30px 0 10px;
}
.app-header h1 {
    font-size: 36px;
    background: linear-gradient(135deg, var(--accent), var(--yellow));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}
.app-header p { color: var(--muted); margin-top: 4px; }

.screen { animation: fadeIn .25s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* auth form */
.auth-box {
    max-width: 380px;
    margin: 40px auto;
}
.auth-box .tabs { display: flex; gap: 4px; margin-bottom: 20px; }
.auth-box .tab {
    flex: 1; padding: 10px; text-align: center;
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius) var(--radius) 0 0;
    cursor: pointer; font-weight: 600; color: var(--muted);
    transition: .15s;
}
.auth-box .tab.active {
    background: var(--card); color: var(--text);
    border-bottom-color: var(--card);
}

/* main menu */
.menu-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
    margin-top: 20px;
}
.menu-card {
    padding: 30px 24px;
    text-align: center;
    cursor: pointer;
    transition: .2s;
}
.menu-card:hover {
    border-color: var(--accent);
    transform: translateY(-3px);
}
.menu-card h3 { margin-bottom: 8px; color: var(--accent); }
.menu-card p { font-size: 13px; color: var(--muted); }

/* user bar */
.user-bar {
    display: flex; justify-content: space-between; align-items: center;
    padding: 12px 18px; margin-bottom: 20px;
    background: var(--surface); border-radius: var(--radius);
}
.user-bar .user-name { font-weight: 600; }
.user-bar .user-name span { color: var(--accent); }

/* match browser */
.match-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 12px;
}
.match-table th, .match-table td {
    padding: 10px 14px;
    text-align: left;
    border-bottom: 1px solid var(--border);
}
.match-table th {
    font-size: 12px; text-transform: uppercase;
    color: var(--muted); font-weight: 600;
}
.match-table tr:hover td { background: rgba(233,69,96,.05); }

.badge {
    display: inline-block; padding: 3px 10px;
    border-radius: 20px; font-size: 11px; font-weight: 700;
    text-transform: uppercase;
}
.badge-waiting  { background: rgba(52,152,219,.2); color: var(--blue); }
.badge-racing   { background: rgba(46,204,113,.2); color: var(--green); }
.badge-finished { background: rgba(149,165,166,.2); color: var(--muted); }
.badge-countdown { background: rgba(241,196,15,.2); color: var(--yellow); }

/* match lobby (in-match waiting) */
.lobby-players {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    margin: 16px 0;
}
.lobby-player {
    padding: 16px;
    background: var(--surface);
    border-radius: var(--radius);
    display: flex; align-items: center; gap: 12px;
    border: 2px solid var(--border);
}
.lobby-player .color-dot {
    width: 24px; height: 24px; border-radius: 50%;
}
.lobby-player.empty {
    opacity: .3;
    border-style: dashed;
}

/* ============================================================
 *  MAP EDITOR
 * ============================================================ */
.editor-layout {
    display: flex;
    height: 100vh;
    overflow: hidden;
}
.editor-sidebar {
    width: 240px;
    background: var(--surface);
    border-right: 1px solid var(--border);
    padding: 16px;
    overflow-y: auto;
    flex-shrink: 0;
}
.editor-sidebar h2 {
    font-size: 16px;
    margin-bottom: 16px;
    color: var(--accent);
}
.editor-main {
    flex: 1;
    position: relative;
    overflow: hidden;
    background: #080b12;
}
.editor-main canvas {
    display: block;
    cursor: crosshair;
}

.tile-palette {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    margin-bottom: 16px;
}
.tile-swatch {
    padding: 8px 4px;
    text-align: center;
    font-size: 11px;
    font-weight: 600;
    border-radius: 6px;
    cursor: pointer;
    border: 2px solid transparent;
    transition: .15s;
}
.tile-swatch:hover { filter: brightness(1.2); }
.tile-swatch.active { border-color: #fff; transform: scale(1.05); }

.editor-toolbar {
    position: absolute; top: 12px; left: 12px; right: 12px;
    display: flex; gap: 8px; align-items: center;
    z-index: 10;
    pointer-events: none;
}
.editor-toolbar > * { pointer-events: auto; }

.editor-info {
    position: absolute; bottom: 12px; left: 12px;
    font-size: 12px; color: var(--muted);
    background: rgba(0,0,0,.6); padding: 6px 12px;
    border-radius: 6px;
}

/* ============================================================
 *  GAME PAGE
 * ============================================================ */
.game-wrapper {
    width: 100vw; height: 100vh;
    overflow: hidden; position: relative;
    background: #080b12;
}
.game-wrapper canvas { display: block; }

/* HUD */
.hud {
    position: absolute;
    pointer-events: none;
    z-index: 10;
    font-weight: 700;
    text-shadow: 0 2px 6px rgba(0,0,0,.8);
}
/* HUD position utilities — referenced by HUD_ITEMS config in game.js */
.hud-top-left      { top: 16px; left: 16px; }
.hud-top-center     { top: 16px; left: 50%; transform: translateX(-50%); }
.hud-top-right      { top: 16px; right: 16px; }
.hud-below-center   { top: 50px; left: 50%; transform: translateX(-50%); }
.hud-bottom-left    { bottom: 16px; left: 16px; }
.hud-bottom-center  { bottom: 16px; left: 50%; transform: translateX(-50%); }
.hud-bottom-right   { bottom: 16px; right: 16px; }

/* overlays */
.game-overlay {
    position: absolute; inset: 0;
    display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    background: rgba(11,14,23,.92);
    z-index: 50;
    text-align: center;
}
.game-overlay h2 {
    font-size: 32px; margin-bottom: 16px;
    color: var(--accent);
}
.game-overlay .sub { color: var(--muted); margin-bottom: 20px; }

.countdown-number {
    font-size: 120px;
    font-weight: 900;
    color: var(--yellow);
    text-shadow: 0 0 40px rgba(241,196,15,.4);
    animation: pulse .5s ease infinite alternate;
}
.countdown-number.loading-phase {
    font-size: 72px;
    color: var(--blue);
    text-shadow: 0 0 30px rgba(52,152,219,.4);
    animation: pulse 1s ease infinite alternate;
}
@keyframes pulse { to { transform: scale(1.1); } }

/* results */
.results-table {
    margin: 20px auto;
    border-collapse: collapse;
    min-width: 300px;
}
.results-table th, .results-table td {
    padding: 10px 20px;
    border-bottom: 1px solid var(--border);
    text-align: left;
}
.results-table th { color: var(--muted); font-size: 12px; text-transform: uppercase; }
.results-table tr:first-child td { color: var(--yellow); font-weight: 700; }

/* toast */
.toast {
    position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
    padding: 12px 24px; border-radius: var(--radius);
    font-size: 14px; font-weight: 600; z-index: 999;
    animation: toastIn .3s ease;
}
.toast-error   { background: var(--accent); color: #fff; }
.toast-success { background: var(--green); color: #fff; }
@keyframes toastIn { from { opacity:0; transform: translateX(-50%) translateY(20px); } }

/* scrollbar */
::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

/* ============================================================
 *  TOUCH CONTROLS  (mobile on-screen buttons)
 * ============================================================ */
.touch-controls {
    position: absolute;
    inset: 0;
    z-index: 20;
    pointer-events: none;          /* let taps pass through to canvas */
    touch-action: none;            /* kill ALL browser gestures in this layer */
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}
.touch-group {
    position: absolute;
    bottom: 28px;
    display: flex;
    gap: 14px;
    pointer-events: auto;          /* buttons themselves are tappable */
    touch-action: none;
}
.touch-group-left  { left: 20px; }
.touch-group-right { right: 20px; }
.touch-group-right {
    flex-direction: column;
    align-items: center;
}
.touch-btn {
    width: 72px; height: 72px;
    border-radius: 50%;
    border: 2px solid rgba(255,255,255,.25);
    background: rgba(255,255,255,.10);
    color: rgba(255,255,255,.7);
    font-size: 28px;
    display: flex; align-items: center; justify-content: center;
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    touch-action: none;             /* critical: no browser gestures per-button */
    user-select: none;
    cursor: pointer;
    transition: background .08s;
    /* prevent any text-selection or context-menu on long press */
    -webkit-user-select: none;
}
.touch-btn.active {
    background: rgba(255,255,255,.30);
    border-color: rgba(255,255,255,.50);
    color: #fff;
}
.touch-btn.touch-gas   { background: rgba(46,204,113,.18);  border-color: rgba(46,204,113,.35); }
.touch-btn.touch-gas.active   { background: rgba(46,204,113,.45); border-color: rgba(46,204,113,.6); }
.touch-btn.touch-brake { background: rgba(231,76,60,.18);   border-color: rgba(231,76,60,.35); }
.touch-btn.touch-brake.active { background: rgba(231,76,60,.45); border-color: rgba(231,76,60,.6); }

/* responsive */
@media (max-width: 600px) {
    .menu-grid { grid-template-columns: 1fr; }
    .lobby-players { grid-template-columns: 1fr; }
}

/* Slightly smaller buttons on narrow phones */
@media (max-width: 400px) {
    .touch-btn { width: 62px; height: 62px; font-size: 22px; }
    .touch-group { gap: 10px; bottom: 18px; }
    .touch-group-left  { left: 12px; }
    .touch-group-right { right: 12px; }
}
