/* 5x5 온라인 대전 — mobile first */
:root {
    --brand-1: #3498db;
    --brand-2: #2ecc71;
    --ink: #1e293b;
    --ink-2: #475569;
    --muted: #64748b;
    --line: #e2e8f0;
    --bg: #f1f5f9;
    --card: #ffffff;
    --p1: #4f46e5;
    --p1-soft: #e0e7ff;
    --p2: #ea580c;
    --p2-soft: #ffedd5;
    --danger: #dc2626;
    --ok: #16a34a;
    --focus: #f59e0b;
    --radius: 14px;
    --shadow: 0 6px 24px rgba(15, 23, 42, 0.08);
    --block-1: #fde2e4;
    --block-2: #dbeafe;
    --block-3: #dcfce7;
    --block-4: #fef3c7;
    --block-5: #ede9fe;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
    font-family: 'Noto Sans KR', system-ui, -apple-system, sans-serif;
    color: var(--ink);
    background: var(--bg);
    min-height: 100vh;
    line-height: 1.5;
}
button, input, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

/* 헤더 */
.site-header {
    display: grid;
    grid-template-columns: 44px 1fr auto;
    align-items: center;
    gap: 12px;
    padding: 14px 16px;
    padding-top: max(14px, env(safe-area-inset-top));
    background: linear-gradient(135deg, var(--brand-1), var(--brand-2));
    color: #fff;
}
.header-text { text-align: center; min-width: 0; }
.site-header h1 { font-size: 1.25rem; font-weight: 900; letter-spacing: -0.01em; }
.site-header p { font-size: 0.8rem; opacity: 0.92; }
@media (max-width: 380px) { .site-header p { display: none; } }
.icon-link {
    width: 44px; height: 44px; border-radius: 50%;
    display: grid; place-items: center;
    background: rgba(255, 255, 255, 0.2); color: #fff; text-decoration: none; font-size: 1.1rem;
}
.icon-link:hover { background: rgba(255, 255, 255, 0.32); }
.lang-switch { display: flex; gap: 4px; background: rgba(255, 255, 255, 0.18); border-radius: 999px; padding: 3px; }
.lang-switch a {
    color: #fff; text-decoration: none; font-size: 0.8rem; font-weight: 700;
    padding: 6px 10px; border-radius: 999px; min-width: 44px; text-align: center;
}
.lang-switch a.active { background: #fff; color: var(--brand-1); }

.page { max-width: 1040px; margin: 0 auto; padding: 16px; padding-bottom: max(24px, env(safe-area-inset-bottom)); }
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; }
.muted { color: var(--muted); }
.hint { color: var(--muted); font-size: 0.88rem; margin: 6px 0 12px; }

/* 버튼 */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    min-height: 44px; padding: 10px 18px; border-radius: 10px; border: 0;
    font-weight: 700; cursor: pointer; text-decoration: none; transition: transform 0.08s, filter 0.15s, background 0.15s;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--brand-1); color: #fff; }
.btn-primary:hover:not(:disabled) { filter: brightness(0.94); }
.btn-secondary { background: #e2e8f0; color: var(--ink); }
.btn-secondary:hover:not(:disabled) { background: #cbd5e1; }
.btn-ghost { background: transparent; color: var(--ink-2); border: 1px solid var(--line); }
.btn-ghost:hover { background: #f8fafc; }
.btn-ghost.danger { color: var(--danger); border-color: #fecaca; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-block { width: 100%; }
.btn-lg { min-height: 52px; font-size: 1.05rem; }
.link-btn { background: none; border: 0; color: var(--brand-1); cursor: pointer; text-decoration: underline; padding: 8px; font-size: 0.9rem; }

/* 로비 */
.lobby { display: grid; gap: 16px; max-width: 640px; margin: 0 auto; }
.lobby-top { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.lobby-top h2 { font-size: 1.2rem; }
.live-stats { display: inline-flex; align-items: center; gap: 8px; font-size: 0.82rem; color: var(--ink-2); background: #f8fafc; border: 1px solid var(--line); border-radius: 999px; padding: 4px 12px; }
.conn-dot { width: 9px; height: 9px; border-radius: 50%; background: #94a3b8; }
.conn-dot.on { background: var(--ok); box-shadow: 0 0 0 3px rgba(22, 163, 74, 0.18); }
.conn-dot.off { background: var(--danger); }
.field-label { display: block; font-weight: 700; font-size: 0.9rem; margin: 4px 0 6px; }
.field-label.small { font-size: 0.82rem; color: var(--ink-2); }
.text-input {
    width: 100%; min-height: 44px; padding: 10px 12px; border: 1.5px solid #cbd5e1; border-radius: 10px; background: #fff;
}
.text-input:focus { border-color: var(--brand-1); outline: none; box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2); }
.code-input { text-transform: uppercase; letter-spacing: 0.3em; font-family: 'Poppins', monospace; font-weight: 700; text-align: center; }

.tabs { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; background: #f1f5f9; border-radius: 12px; padding: 4px; margin: 16px 0 14px; }
.tab { min-height: 44px; border: 0; border-radius: 9px; background: transparent; font-weight: 700; color: var(--ink-2); cursor: pointer; }
.tab[aria-selected="true"] { background: #fff; color: var(--brand-1); box-shadow: 0 1px 4px rgba(15, 23, 42, 0.08); }

.friend-grid { display: grid; gap: 12px; }
.sub-card { border: 1px solid var(--line); border-radius: 12px; padding: 14px; display: grid; gap: 8px; }
.sub-card h3 { font-size: 1rem; }

.waiting { text-align: center; padding: 8px 0 4px; display: grid; justify-items: center; gap: 10px; }
.waiting-text { font-weight: 700; }
.spinner { width: 36px; height: 36px; border-radius: 50%; border: 4px solid #dbeafe; border-top-color: var(--brand-1); animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.invite { width: 100%; border: 2px dashed #93c5fd; background: #eff6ff; border-radius: 12px; padding: 14px; }
.invite-label { font-size: 0.8rem; color: var(--ink-2); font-weight: 700; }
.invite-code { font-family: 'Poppins', monospace; font-size: 2.2rem; font-weight: 700; letter-spacing: 0.25em; color: var(--p1); margin: 2px 0 10px; user-select: all; }
.invite-actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.lobby-status { min-height: 1.4em; margin-top: 10px; font-size: 0.9rem; font-weight: 500; text-align: center; }
.lobby-status.error { color: var(--danger); }
.lobby-status.ok { color: var(--ok); }

.rules summary { cursor: pointer; font-weight: 700; list-style: none; display: flex; align-items: center; gap: 8px; }
.rules summary::-webkit-details-marker { display: none; }
.rules ol { margin: 10px 0 0 1.2em; display: grid; gap: 6px; color: var(--ink-2); font-size: 0.92rem; }

/* 게임 */
.game-layout { display: grid; gap: 16px; }
.board-column { display: grid; gap: 10px; justify-items: center; width: 100%; max-width: 480px; margin: 0 auto; }
.side-column { display: grid; gap: 12px; align-content: start; }

.player-card {
    position: relative; width: 100%;
    display: grid; grid-template-columns: 40px 1fr auto; align-items: center; gap: 10px;
    background: var(--card); border-radius: 12px; padding: 10px 12px 14px; box-shadow: var(--shadow);
    border: 2px solid transparent; transition: border-color 0.2s, box-shadow 0.2s;
}
.player-card.active { border-color: currentColor; box-shadow: 0 0 0 4px rgba(79, 70, 229, 0.12), var(--shadow); }
.player-card.p1 { color: var(--p1); }
.player-card.p2 { color: var(--p2); }
.avatar { width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; font-weight: 900; color: #fff; background: currentColor; font-size: 0.85rem; }
.avatar { background: var(--muted); }
.player-card.p1 .avatar { background: var(--p1); }
.player-card.p2 .avatar { background: var(--p2); }
.pinfo { min-width: 0; color: var(--ink); }
.pname { font-weight: 800; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pstatus { font-size: 0.78rem; color: var(--muted); }
.pstatus.warn { color: var(--danger); font-weight: 700; }
.clock { font-family: 'Poppins', monospace; font-weight: 700; font-size: 1.35rem; color: var(--ink); font-variant-numeric: tabular-nums; }
.clock.low { color: var(--danger); animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0.55; } }
.clock-bar { position: absolute; left: 12px; right: 12px; bottom: 6px; height: 4px; border-radius: 4px; background: #eef2f7; overflow: hidden; }
.clock-bar span { display: block; height: 100%; width: 100%; background: currentColor; transition: width 0.2s linear; }

.turn-banner { min-height: 1.6em; font-weight: 800; text-align: center; font-size: 0.95rem; color: var(--ink-2); }
.turn-banner.mine { color: var(--ok); }

.board-wrap { position: relative; width: min(100%, 420px); }
.board {
    display: grid; grid-template-columns: repeat(5, 1fr);
    width: 100%; aspect-ratio: 1; border: 3px solid var(--ink); border-radius: 10px; overflow: hidden; background: #fff;
    touch-action: manipulation;
}
.cell {
    position: relative; border: 0; border-right: 1px solid #cbd5e1; border-bottom: 1px solid #cbd5e1;
    display: grid; place-items: center; cursor: pointer;
    font-family: 'Poppins', sans-serif; font-size: clamp(1.4rem, 7vw, 2.1rem); font-weight: 600; color: var(--ink);
    background: var(--cell-bg, #fff); transition: background 0.15s;
    -webkit-tap-highlight-color: transparent;
}
.cell.edge-r { border-right: 3px solid var(--ink); }
.cell.edge-b { border-bottom: 3px solid var(--ink); }
.cell.last-col { border-right: 0; }
.cell.last-row { border-bottom: 0; }
.cell.given { font-weight: 800; color: #0f172a; cursor: default; }
.cell.p1 { color: var(--p1); }
.cell.p2 { color: var(--p2); }
.cell.p1::after, .cell.p2::after { content: ""; position: absolute; top: 6px; right: 6px; width: 7px; height: 7px; border-radius: 50%; background: currentColor; opacity: 0.7; }
.cell.invalid { color: var(--danger); text-decoration: line-through; }
.cell.dead:not(.filled)::before {
    content: ""; position: absolute; inset: 22%;
    background: linear-gradient(45deg, transparent 46%, rgba(100, 116, 139, 0.35) 46% 54%, transparent 54%),
                linear-gradient(-45deg, transparent 46%, rgba(100, 116, 139, 0.35) 46% 54%, transparent 54%);
}
.cell.selected { box-shadow: inset 0 0 0 4px var(--focus); z-index: 1; }
.cell.last-move { animation: pop 0.45s ease-out; }
.cell.last-move::before { content: ""; position: absolute; inset: 3px; border-radius: 6px; border: 2px dashed currentColor; opacity: 0.55; }
.board.locked .cell:not(.filled) { cursor: not-allowed; }
@keyframes pop { 0% { transform: scale(0.7); } 70% { transform: scale(1.08); } 100% { transform: scale(1); } }

.pad { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; width: min(100%, 420px); }
.pad button {
    min-height: 54px; border-radius: 12px; border: 2px solid #cbd5e1; background: #fff; cursor: pointer;
    font-family: 'Poppins', sans-serif; font-size: 1.4rem; font-weight: 700; color: var(--ink);
    transition: background 0.12s, border-color 0.12s;
}
.pad button:hover:not(:disabled) { border-color: var(--brand-1); background: #eff6ff; }
.pad button.ok { border-color: var(--ok); background: #f0fdf4; color: #166534; }
.pad button:disabled { opacity: 0.4; cursor: not-allowed; }
.pad.has-sel button:disabled { opacity: 0.35; text-decoration: line-through; }
.pad-hint { min-height: 1.3em; font-size: 0.85rem; color: var(--muted); text-align: center; }

.game-actions { width: 100%; display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.emotes { display: flex; gap: 6px; flex-wrap: wrap; }
.emotes button { width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--line); background: #fff; font-size: 1.25rem; cursor: pointer; }
.emotes button:hover { background: #f8fafc; transform: scale(1.06); }
.post-game { width: 100%; display: flex; justify-content: center; }

.emote-layer { position: absolute; inset: 0; pointer-events: none; overflow: visible; }
.emote-float { position: absolute; left: 50%; font-size: 3.2rem; transform: translateX(-50%); animation: floatUp 1.8s ease-out forwards; text-shadow: 0 4px 10px rgba(0, 0, 0, 0.2); }
.emote-float.opp { top: 8%; }
.emote-float.me { bottom: 8%; }
@keyframes floatUp { 0% { opacity: 0; transform: translate(-50%, 20px) scale(0.6); } 20% { opacity: 1; transform: translate(-50%, 0) scale(1.1); } 100% { opacity: 0; transform: translate(-50%, -40px) scale(1); } }

.info-card { display: grid; gap: 6px; font-size: 0.9rem; }
.legend { display: inline-block; padding: 1px 8px; border-radius: 999px; font-weight: 800; font-size: 0.78rem; margin-left: 4px; }
.legend.p1 { background: var(--p1-soft); color: var(--p1); }
.legend.p2 { background: var(--p2-soft); color: var(--p2); }

.chat summary { cursor: pointer; font-weight: 700; list-style: none; display: flex; align-items: center; gap: 8px; }
.chat summary::-webkit-details-marker { display: none; }
.badge { background: var(--danger); color: #fff; border-radius: 999px; font-size: 0.72rem; padding: 0 7px; }
.chat-log { height: 180px; overflow-y: auto; margin: 10px 0; padding: 8px; background: #f8fafc; border-radius: 10px; display: grid; align-content: start; gap: 6px; font-size: 0.88rem; }
.msg { overflow-wrap: anywhere; }
.msg .who { font-weight: 800; margin-right: 6px; }
.msg.p1 .who { color: var(--p1); }
.msg.p2 .who { color: var(--p2); }
.msg.sys { color: var(--muted); font-style: italic; font-size: 0.82rem; }
.chat-form { display: grid; grid-template-columns: 1fr 48px; gap: 6px; }

/* 오버레이 */
.overlay { position: fixed; inset: 0; background: rgba(15, 23, 42, 0.55); display: grid; place-items: center; padding: 16px; z-index: 50; animation: fade 0.2s; }
@keyframes fade { from { opacity: 0; } }
.overlay-card { background: #fff; border-radius: 18px; padding: 24px 20px; width: min(100%, 420px); text-align: center; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3); display: grid; gap: 10px; justify-items: center; }
.overlay-card.small { width: min(100%, 360px); }
.result-icon { font-size: 3.2rem; line-height: 1; }
.overlay-card h2 { font-size: 1.6rem; font-weight: 900; }
.overlay-card h2.win { color: var(--ok); }
.overlay-card h2.lose { color: var(--danger); }
.result-reason { color: var(--ink-2); }
.result-clocks { font-family: 'Poppins', monospace; font-size: 0.88rem; color: var(--muted); }
.overlay-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; margin-top: 4px; }
.overlay-actions .btn { min-width: 130px; }
.btn.pulse { animation: pulseBtn 1.2s ease-in-out infinite; }
@keyframes pulseBtn { 50% { box-shadow: 0 0 0 6px rgba(52, 152, 219, 0.25); } }

.toast {
    position: fixed; left: 50%; bottom: max(20px, env(safe-area-inset-bottom)); transform: translateX(-50%);
    background: #0f172a; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 0.9rem; z-index: 60;
    max-width: calc(100% - 32px); box-shadow: 0 10px 30px rgba(0, 0, 0, 0.25); animation: fade 0.2s;
}

/* 넓은 화면 */
@media (min-width: 640px) {
    .site-header h1 { font-size: 1.6rem; }
    .site-header p { font-size: 0.95rem; }
    .friend-grid { grid-template-columns: 1fr 1fr; }
    .card { padding: 22px; }
}
@media (min-width: 900px) {
    .page { padding: 24px; }
    .game-layout { grid-template-columns: minmax(0, 1fr) 320px; align-items: start; }
    .board-column { max-width: 480px; }
    .chat-log { height: 320px; }
}
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation: none !important; transition: none !important; }
}
