/* =========================================================
   CỜ TƯỚNG ONLINE — STYLESHEET
   Gỗ, giấy cũ, chữ Hán. Giao diện tối giản: chỉ ba lối vào ở
   màn hình chính, còn lại nhường hết chỗ cho bàn cờ.
   ========================================================= */

:root {
    --font-display: 'Baloo 2', 'Nunito', system-ui, sans-serif;
    --font-main: 'Nunito', system-ui, sans-serif;
    --font-han: 'Noto Serif SC', 'Songti SC', 'SimSun', serif;

    --bg-dark: #1b120a;
    --card-bg: rgba(46, 30, 18, 0.86);
    --card-border: rgba(212, 170, 108, 0.32);

    --wood: #e8c88f;
    --wood-dark: #b98b4e;
    --wood-line: #6b4a24;
    --ink: #2a1b0e;

    --red: #c8321f;
    --red-soft: #e8674f;
    --black: #23252b;
    --black-soft: #4d525e;

    --gold: #f0c674;
    --paper: #f6e7c8;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

/* Thuộc tính hidden của HTML chỉ ẩn được nhờ style mặc định của trình duyệt,
   mà style đó thua bất kỳ khai báo display nào của mình — .countdown{display:flex}
   khiến lớp phủ đếm ngược không bao giờ tắt và chặn luôn click lên bàn cờ. */
[hidden] { display: none !important; }

body {
    font-family: var(--font-main);
    background-color: var(--bg-dark);
    color: var(--paper);
    min-height: 100vh;
    min-height: 100dvh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    align-items: center;
    position: relative;
}

.bg-paper {
    position: fixed;
    inset: 0;
    z-index: -1;
    background:
        radial-gradient(ellipse at 50% 0%, rgba(240, 198, 116, 0.16) 0%, transparent 55%),
        linear-gradient(180deg, #2a1c10 0%, #1b120a 60%, #120b05 100%);
}

body > .app-wrapper {
    height: 100vh;
    height: 100dvh;
    position: relative;
}

/* ---------- Thanh điều hướng ---------- */
.unified-nav {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: rgba(38, 25, 14, 0.86);
    z-index: 10;
}

.brand { display: flex; align-items: center; gap: 10px; }
.brand-icon { font-size: 1.5rem; }

/* Ảnh logo thật nếu có ở /xiangqi/icon.png; thiếu tệp thì thẻ img tự gỡ mình
   và quân cờ dựng bằng CSS bên dưới thế chỗ, nên không bao giờ hiện ảnh vỡ. */
.brand-img {
    height: 2.1em;
    width: auto;
    border-radius: 22%;
    display: block;
    flex-shrink: 0;
    box-shadow: 0 0.1em 0.3em rgba(0, 0, 0, 0.45);
}

/* Quân TƯỚNG đỏ dựng bằng CSS — biểu tượng của game.
   Khối trụ có bề dày thật: một lớp thành gỗ sẫm phía dưới, mặt trên vát sáng,
   vành đỏ khắc chìm và chữ 帥 nổi khối. Trước đây dùng emoji ♟️ — đó là quân
   tốt của CỜ VUA, sai hẳn loại cờ. */
.xq-piece {
    position: relative;
    display: inline-block;
    flex-shrink: 0;
    width: 1.72em;
    height: 1.96em;
    font-size: 0.6em;
    line-height: 1;
    vertical-align: middle;
}

/* thành quân cờ — phần tạo cảm giác dày */
.xq-piece::before {
    content: '';
    position: absolute;
    left: 0;
    right: 0;
    top: 0.24em;
    height: 1.72em;
    border-radius: 50%;
    background: linear-gradient(180deg, #c99c5f 0%, #ab7c40 46%, #7b5426 100%);
    box-shadow: 0 0.14em 0.22em rgba(0, 0, 0, 0.55);
}

/* mặt trên */
.xq-piece .xq-top {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    height: 1.72em;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: 'Noto Serif SC', 'Songti SC', 'SimSun', serif;
    font-weight: 900;
    font-size: 1.02em;
    color: #b8241a;
    background:
        radial-gradient(circle at 36% 26%, #fff9ea 0%, #f4dfb4 44%, #dcbb85 76%, #bd9560 100%);
    box-shadow:
        inset 0 0 0 0.09em #b8241a,
        inset 0 0 0 0.15em rgba(255, 247, 227, 0.9),
        inset 0 -0.14em 0.2em rgba(120, 84, 42, 0.4),
        inset 0 0.12em 0.18em rgba(255, 255, 255, 0.8);
    text-shadow:
        0 0.045em 0 rgba(255, 241, 214, 0.95),
        0 -0.03em 0.02em rgba(110, 18, 10, 0.55);
}

/* ---------- Nút ---------- */
.btn {
    font-family: var(--font-display);
    font-size: 1rem;
    font-weight: 700;
    padding: 9px 18px;
    border-radius: 12px;
    border: 1px solid var(--card-border);
    background: rgba(255, 255, 255, 0.07);
    color: var(--paper);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    transition: transform 0.15s ease, background 0.15s ease, border-color 0.15s ease;
}

.btn:hover { background: rgba(240, 198, 116, 0.16); border-color: var(--gold); transform: translateY(-2px); }
.btn:active { transform: translateY(0); }
.btn.muted { opacity: 0.55; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }

.btn-primary {
    background: linear-gradient(140deg, var(--red), #9d2415);
    border-color: rgba(255, 200, 160, 0.5);
    color: #fff;
    box-shadow: 0 6px 18px rgba(200, 50, 31, 0.35);
}

.btn-primary:hover { background: linear-gradient(140deg, #e04a33, #b02a19); color: #fff; }

.btn-small { padding: 7px 14px; font-size: 0.9rem; }

/* ---------- Khung màn hình ---------- */
.stage {
    flex: 1 1 auto;
    min-height: 0;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    overflow: hidden;
}

.screen {
    display: none;
    width: 100%;
    height: 100%;
    justify-content: center;
    align-items: center;
    overflow-y: auto;
    padding: 8px;
}

.screen.active { display: flex; }

/* ---------- Thẻ ở màn hình chính / phòng chờ ---------- */
.home-card {
    width: 100%;
    max-width: 460px;
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: 24px;
    padding: 26px 24px;
    text-align: center;
    box-shadow: 0 18px 50px rgba(0, 0, 0, 0.55);
    margin: auto;
}

.home-emblem {
    font-family: var(--font-han);
    font-size: 3.2rem;
    font-weight: 900;
    line-height: 1;
    color: var(--red-soft);
    text-shadow: 0 4px 18px rgba(200, 50, 31, 0.5);
    margin-bottom: 6px;
}

.home-title {
    font-family: var(--font-display);
    font-size: 1.7rem;
    font-weight: 800;
    letter-spacing: 1px;
    color: var(--gold);
}

.home-sub {
    color: #c9b18a;
    font-size: 0.92rem;
    margin: 6px 0 18px;
    line-height: 1.5;
}

.field { display: block; text-align: left; margin-bottom: 14px; }

.field-label {
    display: block;
    font-size: 0.78rem;
    font-weight: 800;
    letter-spacing: 0.5px;
    color: #b99a6b;
    text-transform: uppercase;
    margin-bottom: 6px;
}

.field-input {
    width: 100%;
    padding: 12px 16px;
    font-family: var(--font-main);
    font-size: 1rem;
    font-weight: 700;
    color: var(--paper);
    background: rgba(0, 0, 0, 0.28);
    border: 1px solid var(--card-border);
    border-radius: 14px;
    outline: none;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.field-input:focus { border-color: var(--gold); box-shadow: 0 0 0 3px rgba(240, 198, 116, 0.18); }
.field-input::placeholder { color: #8a7350; }

.setup-row { margin-bottom: 18px; text-align: left; }

.pill-row { display: flex; gap: 8px; flex-wrap: wrap; }

.pill {
    flex: 1 1 auto;
    font-family: var(--font-display);
    font-size: 0.9rem;
    font-weight: 700;
    padding: 8px 12px;
    border-radius: 30px;
    border: 1px solid var(--card-border);
    background: rgba(255, 255, 255, 0.05);
    color: #e2cfa8;
    cursor: pointer;
    transition: all 0.16s ease;
}

.pill:hover { background: rgba(240, 198, 116, 0.14); }

.pill.active {
    background: linear-gradient(140deg, rgba(240, 198, 116, 0.32), rgba(200, 50, 31, 0.24));
    border-color: var(--gold);
    color: #fff;
}

.home-actions { display: flex; flex-direction: column; gap: 10px; }
.home-actions .btn { justify-content: center; font-size: 1.05rem; padding: 12px 18px; }

.join-box { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.join-box .field-input { flex: 1 1 140px; }
.join-box .btn { flex: 0 0 auto; }
.join-box .pill-row { flex: 1 1 160px; }

.code-input {
    text-align: center;
    letter-spacing: 6px;
    font-family: var(--font-display);
    font-size: 1.3rem;
    text-transform: uppercase;
}

.home-note {
    font-size: 0.85rem;
    color: #b99a6b;
    margin-top: 12px;
    min-height: 1.2em;
    line-height: 1.5;
}

.home-note.warn { color: #ff9f8a; }

/* ---------- Mã phòng ---------- */
.code-display {
    font-family: var(--font-display);
    font-size: 2.6rem;
    font-weight: 800;
    letter-spacing: 10px;
    color: var(--gold);
    background: rgba(0, 0, 0, 0.3);
    border: 2px dashed var(--card-border);
    border-radius: 18px;
    padding: 14px 10px 14px 20px;
    margin: 6px 0 12px;
    user-select: all;
}

.code-actions { display: flex; gap: 10px; justify-content: center; margin-bottom: 18px; }

.lobby-players {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 14px;
    margin-bottom: 14px;
}

.lobby-seat {
    flex: 1 1 0;
    background: rgba(0, 0, 0, 0.25);
    border: 1px solid var(--card-border);
    border-radius: 16px;
    padding: 12px 10px;
    min-width: 0;
}

.seat-dot {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    margin: 0 auto 6px;
    box-shadow: inset 0 -2px 4px rgba(0, 0, 0, 0.35);
}

.seat-dot.red { background: linear-gradient(140deg, #e8674f, #a32213); }
.seat-dot.black { background: linear-gradient(140deg, #5b616e, #1d1f24); }

.seat-name {
    font-family: var(--font-display);
    font-weight: 800;
    font-size: 1rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.seat-tag { font-size: 0.7rem; color: #b99a6b; letter-spacing: 0.5px; }
.lobby-vs { font-family: var(--font-display); font-weight: 800; color: var(--gold); }

/* ---------- Bàn cờ ---------- */
.table {
    width: 100%;
    height: 100%;
    max-width: 620px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 4px;
}

.player-strip {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 8px 14px;
    border-radius: 14px;
    background: rgba(46, 30, 18, 0.7);
    border: 1px solid var(--card-border);
    transition: border-color 0.2s ease, background 0.2s ease;
}

.player-strip.turn {
    border-color: var(--gold);
    background: rgba(240, 198, 116, 0.16);
    box-shadow: 0 0 16px rgba(240, 198, 116, 0.25);
}

.player-strip.offline { opacity: 0.55; }

.ps-side { display: flex; align-items: center; gap: 9px; min-width: 0; }

.ps-dot {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    flex-shrink: 0;
    box-shadow: inset 0 -2px 3px rgba(0, 0, 0, 0.4);
}

.ps-dot.red { background: linear-gradient(140deg, #e8674f, #a32213); }
.ps-dot.black { background: linear-gradient(140deg, #5b616e, #1d1f24); }

.ps-name {
    font-family: var(--font-display);
    font-weight: 800;
    font-size: 1rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.ps-clock {
    font-family: var(--font-display);
    font-weight: 800;
    font-size: 1.35rem;
    font-variant-numeric: tabular-nums;
    padding: 2px 12px;
    border-radius: 10px;
    background: rgba(0, 0, 0, 0.32);
}

.ps-clock.low { color: #ff8a70; animation: clockPulse 1s ease-in-out infinite; }

@keyframes clockPulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.07); }
}

.board-wrap {
    flex: 1 1 auto;
    min-height: 0;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
}

#board {
    display: block;
    border-radius: 12px;
    box-shadow: 0 14px 40px rgba(0, 0, 0, 0.6);
    touch-action: none;
    cursor: pointer;
}

.board-msg {
    position: absolute;
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    padding: 6px 18px;
    border-radius: 20px;
    background: rgba(20, 12, 6, 0.9);
    border: 1px solid var(--gold);
    font-family: var(--font-display);
    font-weight: 800;
    font-size: 0.95rem;
    color: var(--gold);
    opacity: 0;
    transition: opacity 0.25s ease;
    pointer-events: none;
    white-space: nowrap;
}

.board-msg.show { opacity: 1; }

.countdown {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(20, 12, 6, 0.55);
    border-radius: 12px;
}

.countdown span {
    font-family: var(--font-display);
    font-size: 6rem;
    font-weight: 800;
    color: var(--gold);
    text-shadow: 0 6px 24px rgba(0, 0, 0, 0.7);
    animation: popIn 0.45s cubic-bezier(0.2, 1.4, 0.4, 1);
}

@keyframes popIn {
    0% { transform: scale(0.4); opacity: 0; }
    60% { transform: scale(1.15); opacity: 1; }
    100% { transform: scale(1); opacity: 1; }
}

.game-bar {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    min-height: 34px;
}

.captured {
    display: flex;
    gap: 3px;
    flex-wrap: wrap;
    font-family: var(--font-han);
    font-size: 0.95rem;
    line-height: 1;
    min-width: 0;
    opacity: 0.85;
}

.captured .cap {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: var(--wood);
    color: var(--red);
    font-size: 0.72rem;
    font-weight: 700;
    animation: capIn 0.35s cubic-bezier(0.2, 1.4, 0.4, 1);
}

.captured .cap.black { color: var(--black); }

@keyframes capIn {
    0% { transform: scale(0) rotate(-90deg); opacity: 0; }
    100% { transform: scale(1) rotate(0); opacity: 1; }
}

.game-buttons { display: flex; gap: 8px; flex-shrink: 0; }

/* ---------- Modal ---------- */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(12, 7, 3, 0.88);
    backdrop-filter: blur(8px);
    display: flex;
    overflow-y: auto;
    padding: 20px 16px;
    z-index: 100;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
}

.modal-overlay.active { opacity: 1; pointer-events: auto; }

.modal-card {
    background: linear-gradient(160deg, rgba(58, 38, 22, 0.97), rgba(30, 19, 10, 0.98));
    border: 1px solid var(--card-border);
    border-radius: 24px;
    padding: 28px 26px;
    max-width: 420px;
    width: 100%;
    margin: auto;
    text-align: center;
    box-shadow: 0 20px 55px rgba(0, 0, 0, 0.7);
    transform: scale(0.94);
    transition: transform 0.25s cubic-bezier(0.2, 1.3, 0.4, 1);
}

.modal-overlay.active .modal-card { transform: scale(1); }

.end-emblem { font-size: 3rem; margin-bottom: 6px; }

.modal-title {
    font-family: var(--font-display);
    font-size: 1.7rem;
    font-weight: 800;
    color: var(--gold);
    letter-spacing: 1px;
}

.modal-subtitle { color: #c9b18a; font-size: 0.95rem; margin: 8px 0 20px; line-height: 1.5; }

.end-buttons { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }

/* ---------- Thông báo nổi ---------- */
.toast-msg {
    position: fixed;
    left: 50%;
    bottom: 84px;
    transform: translateX(-50%) translateY(14px);
    max-width: calc(100vw - 32px);
    text-align: center;
    background: rgba(30, 19, 10, 0.96);
    border: 1px solid var(--gold);
    padding: 9px 22px;
    border-radius: 30px;
    font-family: var(--font-display);
    font-weight: 700;
    z-index: 200;
    opacity: 0;
    transition: all 0.25s ease;
    pointer-events: none;
}

.toast-msg.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* =========================================================
   RESPONSIVE
   ========================================================= */

@media (max-width: 620px) {
    .home-card { padding: 20px 16px; border-radius: 20px; }
    .home-emblem { font-size: 2.6rem; }
    .home-title { font-size: 1.4rem; }
    .code-display { font-size: 2rem; letter-spacing: 7px; }
    .ps-clock { font-size: 1.15rem; }
    .player-strip { padding: 6px 11px; }
}

@media (max-height: 560px) {
    body > .app-wrapper { padding: 6px 8px 8px; gap: 6px; }
    .app-wrapper > .unified-nav { padding: 4px 10px; border-radius: 14px; }
    .app-wrapper .nav-home { height: 28px; }
    .app-wrapper .nav-home img { height: 28px; }
    .app-wrapper .unified-nav .btn { padding: 5px 9px; font-size: 0.85rem; }
    .app-wrapper .unified-nav .brand-title { font-size: 1rem; }
    .app-wrapper > .site-footer { padding: 0; }
    .app-wrapper .lang-flag-btn { padding: 2px 6px; }
    .app-wrapper .lang-flag-btn img { width: 20px; height: 13px; }

    .home-card { padding: 12px 14px; }
    .home-emblem { display: none; }
    .home-title { font-size: 1.1rem; }
    .home-sub { font-size: 0.8rem; margin: 4px 0 10px; }
    .field { margin-bottom: 8px; }
    .setup-row { margin-bottom: 10px; }
    .home-actions { flex-direction: row; flex-wrap: wrap; }
    .home-actions .btn { flex: 1 1 30%; font-size: 0.9rem; padding: 8px 10px; }
    .code-display { font-size: 1.6rem; padding: 8px; margin: 4px 0 8px; }
    .lobby-players { margin-bottom: 8px; }

    .table { gap: 5px; }
    .player-strip { padding: 4px 10px; border-radius: 10px; }
    .ps-name { font-size: 0.85rem; }
    .ps-clock { font-size: 1rem; padding: 1px 8px; }
    .game-bar { min-height: 26px; }
    .btn-small { padding: 4px 9px; font-size: 0.78rem; }
    .countdown span { font-size: 3.4rem; }
    .captured .cap { width: 16px; height: 16px; font-size: 0.6rem; }
}

/* Điện thoại nằm ngang: bàn cờ cao gấp rưỡi bề ngang nên chỗ trống dồn hết
   sang hai bên; đưa hai thanh người chơi ra cạnh bàn cho đỡ phí chiều cao. */
@media (max-height: 480px) and (min-width: 640px) {
    .table {
        max-width: none;
        display: grid;
        /* Cột giữa để 'auto' thì bề rộng của nó lấy theo bề rộng canvas, mà
           canvas lại đo theo cột — hai bên chờ nhau nên bàn cờ co lại còn 164px.
           Chia phần cứng: hai thanh người chơi mỗi bên 1 phần, bàn cờ 2 phần. */
        grid-template-columns: minmax(100px, 1fr) 2fr minmax(100px, 1fr);
        /* Hàng phải chiếm trọn chiều cao: để auto thì nó co theo nội dung, mà
           board-wrap lại xin height:100% của chính hàng đó -> vòng luẩn quẩn,
           bàn cờ teo lại còn nửa màn hình. */
        grid-template-rows: minmax(0, 1fr);
        grid-template-areas: 'top board bottom';
        align-items: stretch;
        height: 100%;
    }
    #strip-top { grid-area: top; flex-direction: column; justify-content: center; gap: 6px; align-self: center; }
    #strip-bottom { grid-area: bottom; flex-direction: column; justify-content: center; gap: 6px; align-self: center; }
    .board-wrap { grid-area: board; min-height: 0; }
    .game-bar { display: none; }
}
