/* === Reset & Base === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --bg: #0f1117;
    --bg-card: #1a1d27;
    --bg-card-hover: #22263a;
    --text: #e8e8ee;
    --text-muted: #8888a0;
    --primary: #6366f1;
    --primary-hover: #818cf8;
    --danger: #ef4444;
    --danger-hover: #dc2626;
    --success: #22c55e;
    --warning: #f59e0b;
    --border: #2a2d3a;
    --radius: 12px;
    --radius-sm: 8px;
}

html, body { height: 100%; }
body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    overflow-x: hidden;
}

/* === Toast === */
.toast-container {
    position: fixed; top: 20px; left: 50%; transform: translateX(-50%);
    z-index: 1000;
}
.toast {
    padding: 12px 24px; border-radius: var(--radius-sm);
    font-weight: 500; animation: slideDown 0.3s ease;
}
.toast.success { background: var(--success); color: #000; }
.toast.error { background: var(--danger); color: #fff; }
.toast.info { background: var(--primary); color: #fff; }

@keyframes slideDown {
    from { transform: translateY(-20px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

/* === Layout === */
.container { max-width: 1200px; margin: 0 auto; padding: 20px; min-height: 100vh; }

/* === Game Container (No-scroll viewport layout) === */
.game-container {
    height: 100vh;
    max-height: 100vh;
    min-height: unset;
    max-width: none;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    padding: 0 16px;
}

/* === Logo === */
.logo {
    font-size: 4rem; font-weight: 900; letter-spacing: 0.2em;
    background: linear-gradient(135deg, #e74c3c, #27ae60, #8e44ad);
    -webkit-background-clip: text; -webkit-text-fill-color: transparent;
    background-clip: text; text-align: center;
}
.logo-small {
    font-size: 1.5rem; font-weight: 900; letter-spacing: 0.15em;
    background: linear-gradient(135deg, #e74c3c, #27ae60, #8e44ad);
    -webkit-background-clip: text; -webkit-text-fill-color: transparent;
    background-clip: text;
}
.subtitle { text-align: center; color: var(--text-muted); margin-top: 4px; font-size: 1.1rem; }

/* === Buttons === */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    padding: 10px 20px; border: none; border-radius: var(--radius-sm);
    font-size: 0.95rem; font-weight: 600; cursor: pointer; transition: all 0.2s;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-ghost { background: transparent; color: var(--text-muted); }
.btn-ghost:hover { color: var(--text); background: var(--bg-card-hover); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover:not(:disabled) { background: var(--danger-hover); }
.btn-sm { padding: 6px 12px; font-size: 0.85rem; }
.btn-lg { padding: 14px 28px; font-size: 1.1rem; }

/* === Inputs === */
input[type="text"], input[type="password"], .select {
    width: 100%; padding: 12px 16px;
    background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius-sm);
    color: var(--text); font-size: 1rem; outline: none; transition: border-color 0.2s;
}
input:focus, .select:focus { border-color: var(--primary); }
.select { cursor: pointer; }

/* === Card (UI Card) === */
.card {
    background: var(--bg-card); border: 1px solid var(--border);
    border-radius: var(--radius); padding: 24px;
}

/* === Modal === */
.modal-overlay {
    position: fixed; inset: 0; background: rgba(0,0,0,0.6);
    display: flex; align-items: center; justify-content: center;
    z-index: 900;
}
.modal {
    background: var(--bg-card); border: 1px solid var(--border);
    border-radius: var(--radius); padding: 32px; width: 100%; max-width: 400px;
}
.modal h3 { margin-bottom: 16px; }
.modal-actions { display: flex; gap: 12px; justify-content: flex-end; margin-top: 8px; }

/* === Auth === */
.auth-container {
    display: flex; align-items: center; justify-content: center;
}
.auth-card {
    background: var(--bg-card); border: 1px solid var(--border);
    border-radius: var(--radius); padding: 40px; width: 100%; max-width: 420px;
}
.tab-switch {
    display: flex; gap: 0; margin: 24px 0 20px; border-radius: var(--radius-sm); overflow: hidden;
    border: 1px solid var(--border);
}
.tab-switch button {
    flex: 1; padding: 10px; border: none; background: var(--bg);
    color: var(--text-muted); cursor: pointer; font-weight: 600; transition: all 0.2s;
}
.tab-switch button.active { background: var(--primary); color: #fff; }
.auth-form { display: flex; flex-direction: column; gap: 12px; }

.spinner {
    display: inline-block; width: 18px; height: 18px;
    border: 2px solid rgba(255,255,255,0.3); border-top-color: #fff;
    border-radius: 50%; animation: spin 0.6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* === Lobby === */
.lobby-header {
    display: flex; justify-content: space-between; align-items: center;
    padding-bottom: 20px; border-bottom: 1px solid var(--border); margin-bottom: 24px;
}
.user-info { display: flex; align-items: center; gap: 12px; }
.lobby-content { display: flex; flex-direction: column; gap: 20px; }

/* Lobby Tabs */
.lobby-tabs {
    display: flex; gap: 0; border-radius: var(--radius-sm); overflow: hidden;
    border: 1px solid var(--border); margin-bottom: 4px;
}
.lobby-tabs button {
    flex: 1; padding: 10px 16px; border: none; background: var(--bg);
    color: var(--text-muted); cursor: pointer; font-weight: 600; transition: all 0.2s;
    font-size: 0.95rem;
}
.lobby-tabs button.active { background: var(--primary); color: #fff; }

.form-row { display: flex; gap: 16px; align-items: center; flex-wrap: wrap; margin-bottom: 16px; }
.toggle-label { display: flex; align-items: center; gap: 8px; cursor: pointer; }
.toggle-label input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--primary); }
.join-row { display: flex; gap: 12px; }
.code-input { text-transform: uppercase; letter-spacing: 0.15em; font-weight: 700; font-size: 1.1rem; text-align: center; }
.room-list-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
.room-list { display: flex; flex-direction: column; gap: 8px; }
.room-item {
    display: flex; justify-content: space-between; align-items: center;
    padding: 12px 16px; background: var(--bg); border-radius: var(--radius-sm);
    cursor: pointer; transition: background 0.2s;
}
.room-item:hover { background: var(--bg-card-hover); }
.room-item-info { display: flex; gap: 12px; align-items: center; }
.room-code { font-weight: 700; letter-spacing: 0.1em; font-family: monospace; font-size: 1.05rem; }
.room-host { color: var(--text-muted); }
.room-item-meta { display: flex; gap: 8px; align-items: center; }
.badge {
    padding: 3px 10px; border-radius: 20px; font-size: 0.75rem; font-weight: 600;
    background: var(--primary); color: #fff;
}
.badge-warn { background: var(--warning); color: #000; }
.badge-danger { background: var(--danger); color: #fff; }
.badge-host { background: var(--success); color: #000; font-size: 0.7rem; }
.player-count { color: var(--text-muted); font-size: 0.9rem; }
.empty-state { color: var(--text-muted); text-align: center; padding: 24px; }

/* === Scoreboard Table === */
.scoreboard-table { display: flex; flex-direction: column; gap: 6px; }
.scoreboard-row {
    display: flex; align-items: center; gap: 12px;
    padding: 10px 16px; background: var(--bg); border-radius: var(--radius-sm);
}
.scoreboard-row.top-3 { border-left: 3px solid var(--primary); }
.sb-rank { font-weight: 900; min-width: 32px; color: var(--text-muted); }
.scoreboard-row.top-3 .sb-rank { color: var(--primary); }
.sb-name { flex: 1; font-weight: 500; }
.sb-record { color: var(--success); font-weight: 600; font-size: 0.85rem; min-width: 70px; }
.sb-games { color: var(--text-muted); font-size: 0.85rem; margin-right: 12px; }
.sb-score { font-weight: 700; color: var(--primary); min-width: 80px; text-align: right; }

/* === Admin Panel === */
.admin-user-list { display: flex; flex-direction: column; gap: 6px; }
.admin-user-row {
    display: flex; justify-content: space-between; align-items: center;
    padding: 10px 16px; background: var(--bg); border-radius: var(--radius-sm);
}
.admin-user-info { display: flex; align-items: center; gap: 10px; }
.admin-username { font-weight: 600; }
.admin-date { color: var(--text-muted); font-size: 0.8rem; }

/* === Game Header === */
.game-header {
    display: flex; justify-content: space-between; align-items: center;
    padding: 8px 0; border-bottom: 1px solid var(--border);
    flex-wrap: wrap; gap: 8px; flex-shrink: 0;
}
.game-header-left, .game-header-center, .game-header-right {
    display: flex; align-items: center; gap: 12px;
}
.room-code-display { font-family: monospace; color: var(--text-muted); }
.deck-count { color: var(--text-muted); }
.current-user { font-weight: 600; }
.game-timer {
    font-family: monospace; font-weight: 700; font-size: 1.1rem;
    color: var(--text); background: var(--bg-card); border: 1px solid var(--border);
    padding: 2px 10px; border-radius: var(--radius-sm); letter-spacing: 0.05em;
}

/* === Game Lobby === */
.game-lobby { display: flex; justify-content: center; align-items: flex-start; padding-top: 40px; flex: 1; min-height: 0; overflow-y: auto; }
.waiting-card { text-align: center; max-width: 500px; width: 100%; }
.player-list { margin: 20px 0; display: flex; flex-direction: column; gap: 8px; }
.player-item {
    display: flex; align-items: center; gap: 10px;
    padding: 10px 16px; background: var(--bg); border-radius: var(--radius-sm);
}
.player-dot {
    width: 10px; height: 10px; border-radius: 50%;
    background: var(--text-muted);
}
.player-dot.online { background: var(--success); }
.share-code { margin: 20px 0; }
.big-code {
    display: block; font-size: 2.5rem; font-weight: 900; letter-spacing: 0.3em;
    font-family: monospace; color: var(--primary); margin-top: 8px;
}
.hint { color: var(--text-muted); font-style: italic; }

/* === Game Board === */
.game-board-container {
    display: flex; flex-direction: column; gap: 8px;
    flex: 1; min-height: 0; padding: 8px 0;
}
.game-board-layout {
    display: flex; gap: 16px; align-items: stretch;
    flex: 1; min-height: 0; justify-content: center;
}

/* In-Game Scoreboard */
.scoreboard {
    display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; flex-shrink: 0;
}
.score-item {
    display: flex; align-items: center; gap: 6px;
    padding: 4px 12px; background: var(--bg-card); border: 1px solid var(--border);
    border-radius: var(--radius-sm); transition: all 0.3s;
}
.score-item.is-me { border-color: var(--primary); }
.score-item.is-turn { border-color: var(--warning); background: rgba(245, 158, 11, 0.1); }
.score-name { font-weight: 500; }
.score-total-sets { color: var(--text-muted); font-size: 0.8rem; font-weight: 400; }
.score-value {
    font-weight: 900; font-size: 1.2rem; color: var(--primary);
    min-width: 28px; text-align: center;
}

/* Turn Indicator */
.turn-indicator {
    text-align: center; padding: 4px; font-weight: 600;
    color: var(--warning); flex-shrink: 0;
}

/* === Card Grid === */
.card-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(3, 1fr);
    gap: 8px;
    min-width: 0;
    min-height: 0;
    height: 100%;
    max-width: 100%;
}

/* === SET Card === */
.set-card {
    container-type: size;
    width: 100%;
    height: 100%;
    perspective: 800px; cursor: pointer;
    transition: transform 0.15s;
}
.set-card:hover { transform: scale(1.03); }

.card-inner {
    width: min(100cqw, calc(100cqh * 5 / 7));
    height: min(100cqh, calc(100cqw * 7 / 5));
    margin: auto;
    position: relative;
    transition: transform 0.4s;
    transform-style: preserve-3d;
}

.card-front, .card-back {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    backface-visibility: hidden;
    border-radius: var(--radius);
    border: 2px solid var(--border);
    display: flex; align-items: center; justify-content: center;
    background: var(--bg-card);
    transition: border-color 0.2s, box-shadow 0.2s;
    overflow: hidden;
    container-type: size;
}

.card-back {
    background: linear-gradient(135deg, #2a1a3a, #1a2a3a);
    border-color: #3a3a5a;
}
.card-back-pattern {
    font-size: 1.8rem; font-weight: 900; letter-spacing: 0.2em;
    opacity: 0.15; color: var(--text);
}

/* Face down flip */
.set-card.face-down .card-inner { transform: rotateY(180deg); }
.set-card.face-down .card-back { transform: rotateY(180deg); }

/* Selection */
.set-card.selected .card-front {
    border-color: var(--primary);
    box-shadow: 0 0 20px rgba(99, 102, 241, 0.4);
}

/* Flash animations */
.set-card.flash-green .card-front {
    border-color: var(--success);
    box-shadow: 0 0 25px rgba(34, 197, 94, 0.6);
    animation: flashPulse 0.5s ease;
}
.set-card.flash-red .card-front {
    border-color: var(--danger);
    box-shadow: 0 0 25px rgba(239, 68, 68, 0.6);
    animation: flashPulse 0.5s ease;
}

@keyframes flashPulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.05); }
}

/* Card symbols — rendered inline via JS */
.card-symbols {
    display: flex; flex-direction: column; align-items: center;
    justify-content: center;
    gap: 1cqh; padding: 0 12%;
    width: 100%; height: 100%;
}

.card-shape {
    height: 25cqh;
    flex-shrink: 0;
    width: auto;
    max-width: 100%;
    display: block;
}

/* === Found SETs Panel === */
.found-sets-panel {
    width: 180px; flex-shrink: 0;
    display: flex; flex-direction: column; gap: 8px;
    overflow-y: auto; min-height: 0;
}
.found-sets-title {
    font-size: 0.85rem; font-weight: 700; color: var(--text-muted);
    text-transform: uppercase; letter-spacing: 0.05em;
}
.found-set-group {
    background: var(--bg-card); border: 1px solid var(--border);
    border-radius: var(--radius-sm); padding: 8px;
}
.found-set-player {
    font-size: 0.75rem; color: var(--text-muted); display: block; margin-bottom: 6px;
}
.found-set-cards {
    display: flex; gap: 4px;
}
.mini-card {
    flex: 1; background: var(--bg); border: 1px solid var(--border);
    border-radius: 4px; padding: 4px;
    display: flex; align-items: center; justify-content: center;
}
.mini-card-inner {
    display: flex; flex-direction: column; align-items: center;
    gap: 2px; width: 100%;
}
.mini-card-inner .card-shape {
    width: 100%; height: auto;
}

/* === Game Over === */
.game-over { display: flex; justify-content: center; align-items: flex-start; padding-top: 40px; flex: 1; min-height: 0; overflow-y: auto; }
.game-over-card { text-align: center; max-width: 500px; width: 100%; }
.game-stats {
    display: flex; align-items: center; justify-content: center; gap: 8px;
    margin-bottom: 8px; font-size: 1.1rem;
}
.game-stats-label { color: var(--text-muted); }
.game-stats-value { font-weight: 700; font-family: monospace; font-size: 1.2rem; color: var(--text); }
.final-scores { margin: 24px 0; display: flex; flex-direction: column; gap: 8px; }
.final-score-item {
    display: flex; align-items: center; gap: 12px;
    padding: 12px 20px; background: var(--bg); border-radius: var(--radius-sm);
}
.final-score-item.winner {
    background: rgba(99, 102, 241, 0.15); border: 1px solid var(--primary);
}
.rank { font-weight: 900; font-size: 1.2rem; color: var(--text-muted); min-width: 32px; }
.final-score-item.winner .rank { color: var(--primary); }
.name { flex: 1; text-align: left; font-weight: 500; }
.score { font-weight: 700; color: var(--primary); }
.avg-time { font-size: 0.85rem; color: var(--text-muted); font-family: monospace; }

/* === Guide === */
.guide-card h2 { margin-bottom: 12px; }
.guide-card h3 { margin-top: 24px; margin-bottom: 8px; color: var(--primary); }
.guide-card h4 { margin-top: 16px; margin-bottom: 8px; }
.guide-card p { margin-bottom: 8px; line-height: 1.6; }
.guide-card ul { margin: 8px 0 12px 20px; line-height: 1.8; }
.guide-intro { color: var(--text-muted); font-size: 1.05rem; }

.guide-card-row {
    display: flex; gap: 12px; justify-content: center;
    margin: 16px 0 8px; flex-wrap: wrap;
}
.guide-example-card {
    width: 90px; height: 126px;
    background: var(--bg); border: 2px solid var(--border); border-radius: var(--radius-sm);
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 4px; padding: 10px;
    container-type: size;
}
.guide-example-card .card-shape {
    height: 25cqh; flex-shrink: 0; width: auto; max-width: 100%; display: block;
}
.guide-caption {
    text-align: center; color: var(--text-muted); font-size: 0.85rem; margin-bottom: 12px;
}
.guide-check {
    display: flex; flex-direction: column; gap: 4px; margin: 8px 0 16px;
    padding: 12px 16px; background: var(--bg); border-radius: var(--radius-sm);
}
.guide-check-yes { color: var(--success); }
.guide-check-yes::before { content: "\2713  "; }
.guide-check-no { color: var(--danger); }
.guide-check-no::before { content: "\2717  "; }
.guide-rule {
    padding: 12px 16px; background: rgba(99, 102, 241, 0.1);
    border-left: 3px solid var(--primary); border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
    margin: 12px 0;
}
.guide-note {
    font-style: italic; color: var(--warning); margin: 12px 0;
}
.guide-tip {
    margin-top: 20px; padding: 16px; background: rgba(34, 197, 94, 0.1);
    border-left: 3px solid var(--success); border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}

/* === Hint System === */
.hint-panel {
    display: flex; flex-direction: column; align-items: center;
    justify-content: center; flex-shrink: 0;
}
.btn-hint {
    display: inline-flex; align-items: center; gap: 6px;
    padding: 10px 20px; border: 2px solid var(--warning); border-radius: var(--radius-sm);
    background: rgba(245, 158, 11, 0.1); color: var(--warning);
    font-size: 0.95rem; font-weight: 700; cursor: pointer; transition: all 0.2s;
}
.btn-hint:hover { background: rgba(245, 158, 11, 0.2); }
.hint-count {
    font-size: 0.8rem; background: var(--warning); color: #000;
    padding: 1px 6px; border-radius: 10px; font-weight: 700;
}
.set-card.hint-highlight .card-front {
    border-color: var(--warning);
    box-shadow: 0 0 20px rgba(245, 158, 11, 0.5);
}

/* === SET Button === */
.set-call-bar {
    display: flex; align-items: center; justify-content: center; gap: 12px;
    flex-shrink: 0; width: 100%; max-width: 560px; margin: 0 auto;
}
.btn-set-call {
    min-width: 96px; padding: 10px 22px;
    font-size: 1.1rem; font-weight: 900; letter-spacing: 0.08em;
    background: var(--success); color: #000;
    font-variant-numeric: tabular-nums;
}
.btn-set-call:hover:not(:disabled) { filter: brightness(1.1); }
.btn-set-call:disabled { opacity: 1; cursor: default; }
.btn-set-call.is-mine { background: var(--primary); color: #fff; }
.btn-set-call.is-other { background: var(--bg-card); color: var(--text-muted); border: 1px solid var(--border); }
.set-call-info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 6px; }
.set-call-status { font-weight: 600; color: var(--text-muted); font-size: 0.9rem; }
.set-call-status.is-mine { color: var(--primary-hover); }
.set-call-progress {
    height: 6px; background: var(--bg-card); border-radius: 3px; overflow: hidden;
}
.set-call-progress-fill {
    height: 100%; background: var(--warning); transition: width 0.1s linear;
}
.set-call-progress-fill.is-mine { background: var(--primary); }
.card-grid.locked .set-card { cursor: default; }
.card-grid.locked .set-card:hover { transform: none; }

/* === Responsive === */
@media (max-width: 600px) {
    .container { padding: 12px; }
    .auth-card { padding: 24px; }
    .logo { font-size: 3rem; }
    .game-container { padding: 0 8px; }
    .game-board-layout { flex-direction: column; }
    .found-sets-panel { width: 100%; flex-direction: row; flex-wrap: wrap; max-height: 120px; }
    .found-set-group { flex: 1; min-width: 140px; }
    .card-grid { gap: 4px; }
    .form-row { flex-direction: column; }
    .join-row { flex-direction: column; }
    .game-header { font-size: 0.85rem; }
    .lobby-tabs { flex-direction: column; }
}
