/* 九九乘法心算大挑戰 網頁版 — 復古街機風格
   畫面固定為 960×720，由 ninenine.js 依視窗大小等比例縮放置中。 */

@font-face {
    font-family: "DigifaceWide";
    src: url("../font/digifaw.ttf") format("truetype");
}

:root {
    --ui: "Microsoft JhengHei", "微軟正黑體", "PingFang TC", "Noto Sans TC", sans-serif;
    --digit: "DigifaceWide", "Consolas", monospace;
    --bg: #050a2e;
    --panel: #0b1450;
    --panel-2: #111d6b;
    --line: #2a3cb0;
    --text: #e8ecff;
    --muted: #8f9ad8;
    --cyan: #3fe6ff;
    --yellow: #ffe14a;
    --red: #ff4d5e;
    --green: #5dff9b;
    --orange: #ff9a2e;
    --lilac: #b8a2ff;
}

html, body {
    margin: 0;
    height: 100%;
    background: radial-gradient(ellipse at center, #0d1a5c 0%, var(--bg) 60%, #01030f 100%);
    overflow: hidden;
    color: var(--text);
    font-family: var(--ui);
    -webkit-tap-highlight-color: transparent;
}

#stage {
    position: absolute;
    left: 0;
    top: 0;
    width: 960px;
    height: 720px;
    transform-origin: 0 0;
    box-sizing: border-box;
    padding: 16px;
}

/* 街機螢幕的掃描線 */
#stage::after {
    content: "";
    position: absolute;
    inset: 0;
    pointer-events: none;
    background: repeating-linear-gradient(0deg, rgba(0, 0, 0, .12) 0 1px, transparent 1px 3px);
}

/* ---------- 共用元件 ---------- */

.card {
    position: absolute;
    box-sizing: border-box;
    background: linear-gradient(180deg, var(--panel-2), var(--panel));
    border: 2px solid var(--line);
    border-radius: 10px;
    box-shadow: 0 0 0 2px #000, 0 6px 18px rgba(0, 0, 0, .5), inset 0 0 24px rgba(63, 230, 255, .06);
}

.card-title {
    font-weight: 900;
    font-size: 20px;
    letter-spacing: 2px;
    color: var(--yellow);
    text-shadow: 0 0 8px rgba(255, 225, 74, .5);
}

.btn {
    box-sizing: border-box;
    border: none;
    border-radius: 8px;
    font-family: var(--ui);
    font-weight: 900;
    font-size: 22px;
    letter-spacing: 2px;
    color: #10163d;
    background: var(--yellow);
    box-shadow: 0 5px 0 #a88a12, 0 6px 12px rgba(0, 0, 0, .5);
    cursor: pointer;
    transition: transform .05s, box-shadow .05s, filter .1s;
    white-space: nowrap;
}
.btn:hover:not(:disabled) { filter: brightness(1.08); }
.btn:active:not(:disabled) { transform: translateY(4px); box-shadow: 0 1px 0 #a88a12, 0 2px 6px rgba(0, 0, 0, .5); }
.btn:disabled { filter: grayscale(.85) brightness(.6); cursor: default; }
.btn.green { background: var(--green); box-shadow: 0 5px 0 #23a35a, 0 6px 12px rgba(0, 0, 0, .5); }
.btn.cyan { background: var(--cyan); box-shadow: 0 5px 0 #1c8fa3, 0 6px 12px rgba(0, 0, 0, .5); }
.btn.red { background: var(--red); color: #fff; box-shadow: 0 5px 0 #a3243a, 0 6px 12px rgba(0, 0, 0, .5); }
.btn.orange { background: var(--orange); box-shadow: 0 5px 0 #a85d12, 0 6px 12px rgba(0, 0, 0, .5); }
.btn.ghost { background: transparent; color: var(--text); border: 2px solid var(--line); box-shadow: none; }
.btn.small { font-size: 14px; letter-spacing: 0; border-radius: 6px; box-shadow: 0 3px 0 #a88a12; }

.digit { font-family: var(--digit); }

/* ---------- 第一頁 ---------- */

.lobby-title {
    position: absolute;
    left: 0;
    right: 0;
    top: 26px;
    text-align: center;
}
.lobby-title h1 {
    margin: 0;
    font-size: 62px;
    font-weight: 900;
    letter-spacing: 8px;
    color: var(--yellow);
    text-shadow: 0 0 6px #ff9a2e, 0 0 22px rgba(255, 154, 46, .8), 4px 4px 0 #a3243a;
}
.lobby-title .sub {
    margin-top: 4px;
    font: 18px var(--digit);
    letter-spacing: 6px;
    color: var(--cyan);
    text-shadow: 0 0 8px rgba(63, 230, 255, .7);
}

#playerCard { left: 40px; top: 170px; width: 400px; height: 290px; padding: 20px 24px; }
.field { display: flex; align-items: center; gap: 10px; margin-top: 16px; font-size: 20px; font-weight: 700; }
.field > label { width: 64px; color: var(--cyan); }
.field .unit { color: var(--muted); font-size: 18px; }
.field select, .field input {
    box-sizing: border-box;
    height: 40px;
    border: 2px solid var(--line);
    border-radius: 6px;
    background: #050a2e;
    color: var(--yellow);
    font: 700 20px var(--ui);
    text-align: center;
    text-align-last: center;
    padding: 0 6px;
}
.field select:focus, .field input:focus { outline: none; border-color: var(--cyan); box-shadow: 0 0 8px rgba(63, 230, 255, .6); }
.field select option { background: #050a2e; color: var(--yellow); }
#name { width: 200px; text-align: left; }

#help {
    position: absolute;
    left: 24px;
    right: 24px;
    bottom: 20px;
    height: 70px;
    box-sizing: border-box;
    padding: 8px 12px;
    border: 2px dashed var(--line);
    border-radius: 8px;
    background: rgba(0, 0, 0, .35);
    color: var(--red);
    font-weight: 700;
    font-size: 17px;
    line-height: 1.5;
    white-space: pre-line;
}

#freeCard { left: 470px; top: 170px; width: 450px; height: 290px; padding: 16px 20px; }
#freeRooms { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; margin-top: 14px; }
.tile {
    height: 100px;
    border-radius: 10px;
    border: 2px solid var(--line);
    background: #050a2e;
    color: var(--text);
    font-family: var(--ui);
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    transition: transform .08s, border-color .1s, box-shadow .1s;
}
.tile:hover { border-color: var(--cyan); box-shadow: 0 0 12px rgba(63, 230, 255, .5); transform: translateY(-2px); }
.tile.selected { border-color: var(--orange); box-shadow: 0 0 14px rgba(255, 154, 46, .8); }
.tile .icon { font-size: 34px; line-height: 1; }
.tile .name { font-size: 19px; font-weight: 900; color: var(--yellow); }
.tile .sec { font: 13px var(--digit); color: var(--cyan); }

#testCard { left: 40px; top: 480px; width: 880px; height: 160px; padding: 16px 20px; }
#testCard .buttons { display: flex; justify-content: center; margin-top: 18px; }
#testCard .btn { width: 360px; height: 70px; font-size: 24px; }
#testCard .btn.selected { outline: 3px solid var(--orange); outline-offset: 3px; }

.lobby-footer { position: absolute; left: 40px; right: 40px; top: 660px; display: flex; align-items: center; justify-content: space-between; }
#version { font: 16px var(--digit); color: var(--muted); letter-spacing: 2px; }
#btnEnd { width: 150px; height: 44px; }

/* ---------- 比賽頁 ---------- */

#roomBar {
    position: absolute;
    left: 16px;
    right: 16px;
    top: 16px;
    height: 52px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 26px;
    font-weight: 900;
    letter-spacing: 4px;
    box-shadow: 0 0 0 2px #000, 0 0 16px rgba(0, 0, 0, .6);
}
#roomBar.free { background: linear-gradient(90deg, #1430d8, #3050ff, #1430d8); color: var(--yellow); }
#roomBar.test { background: linear-gradient(90deg, #6e5cd8, #9a8cff, #6e5cd8); color: #fff; }

.info-card { left: 16px; width: 280px; height: 178px; padding: 8px 14px; }
#netCard { top: 84px; border-color: #1f9e52; }
#champCard { top: 272px; border-color: #3050ff; }
#challCard { top: 460px; border-color: #7a7a9a; }
.info-head { display: flex; justify-content: space-between; align-items: center; }
.info-head .score { font: 24px var(--digit); color: var(--red); text-shadow: 0 0 8px rgba(255, 77, 94, .7); }
.info-rows { display: grid; grid-template-columns: 48px 1fr; gap: 2px 6px; margin: 4px 0 0; font-size: 14px; line-height: 20px; }
.info-rows dt { margin: 0; color: var(--muted); font-weight: 700; }
.info-rows dd { margin: 0; color: var(--text); font-weight: 700; background: rgba(0, 0, 0, .35); border-radius: 4px; padding: 0 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#netCard .info-rows dd { color: #c9ffdd; }

#playCard { left: 312px; top: 84px; width: 632px; height: 554px; padding: 16px 20px; }
.score-row { display: flex; align-items: center; gap: 16px; }
.score-box { flex: 1; height: 74px; border-radius: 8px; background: #000; border: 2px solid var(--red); display: flex; flex-direction: column; align-items: center; justify-content: center; }
.score-box .cap { font-size: 13px; color: var(--muted); font-weight: 700; letter-spacing: 2px; }
.score-box .val { font: 40px var(--digit); color: var(--cyan); text-shadow: 0 0 10px rgba(63, 230, 255, .8); line-height: 1.1; }
#btnQuery { width: 150px; height: 60px; font-size: 20px; }

.question { display: flex; align-items: center; justify-content: center; gap: 10px; height: 260px; margin-top: 16px; background: #000; border-radius: 10px; border: 2px solid #1a2470; }
.question .op { width: 110px; text-align: center; font: 900 150px Arial, sans-serif; color: var(--red); text-shadow: 0 0 16px rgba(255, 77, 94, .7); line-height: 1; }
.question .sym { font: 900 90px Arial, sans-serif; color: #fff; line-height: 1; }
#echo {
    width: 180px;
    height: 150px;
    box-sizing: border-box;
    border: 3px solid #333a70;
    border-radius: 10px;
    background: #05081f;
    color: var(--yellow);
    font: 900 120px Arial, sans-serif;
    text-align: center;
    padding: 0;
    outline: none;
    caret-color: var(--yellow);
    text-shadow: 0 0 14px rgba(255, 225, 74, .6);
}
#echo.active { border-color: var(--yellow); box-shadow: 0 0 16px rgba(255, 225, 74, .6); }
#echo.wrong { color: var(--red); }

#bar { height: 14px; margin-top: 14px; border-radius: 7px; background: #1a1f45; overflow: hidden; }
#barFill { height: 100%; width: 0; background: linear-gradient(90deg, #ff4d5e, #ffe14a 40%, #5dff9b); }
#netMessage { height: 26px; margin-top: 8px; text-align: center; color: var(--yellow); font-weight: 700; font-size: 17px; }

.status-row { display: grid; grid-template-columns: 1.2fr 1fr 1fr; gap: 12px; margin-top: 8px; }
.status { height: 62px; border-radius: 8px; background: #000; border: 2px solid #1a2470; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.status .cap { font-size: 13px; color: var(--muted); font-weight: 700; letter-spacing: 2px; }
.status .val { font: 26px var(--digit); color: var(--cyan); line-height: 1.2; }
.status .val.text { font: 900 22px var(--ui); }
#syncStatus { cursor: pointer; }
#syncStatus .val.ok { color: var(--green); }
#syncStatus .val.no { color: var(--orange); }

.control-row { position: absolute; left: 16px; right: 16px; top: 656px; display: flex; align-items: center; gap: 14px; }
.control-row .btn { height: 48px; width: 150px; }
.control-row .spacer { flex: 1; }
#serverLabel { font: 14px var(--digit); color: var(--muted); min-width: 120px; text-align: right; }
#btnSound { width: 110px; }
#btnSound.off { background: #3a3f66; color: var(--muted); box-shadow: 0 5px 0 #20244a; }

/* ---------- 對話框 ---------- */

#overlay {
    position: absolute;
    inset: 0;
    background: rgba(1, 3, 15, .7);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 100;
}
#overlay.show { display: flex; }

.dialog {
    min-width: 380px;
    max-width: 700px;
    background: linear-gradient(180deg, var(--panel-2), var(--panel));
    border: 2px solid var(--cyan);
    border-radius: 12px;
    box-shadow: 0 0 0 2px #000, 0 0 30px rgba(63, 230, 255, .35);
    overflow: hidden;
}
.dialog-title { padding: 10px 18px; font-weight: 900; font-size: 20px; color: var(--yellow); background: rgba(0, 0, 0, .35); letter-spacing: 2px; }
.dialog-body { padding: 18px 24px; font-size: 18px; font-weight: 700; line-height: 1.65; white-space: pre-line; }
.dialog-body input {
    display: block;
    width: 100%;
    box-sizing: border-box;
    margin-top: 12px;
    height: 44px;
    border: 2px solid var(--line);
    border-radius: 6px;
    background: #050a2e;
    color: var(--yellow);
    font: 22px var(--digit);
    letter-spacing: 2px;
    text-align: center;
    text-transform: uppercase;
    outline: none;
}
.dialog-body input:focus { border-color: var(--cyan); }
.dialog-buttons { display: flex; justify-content: center; gap: 14px; padding: 4px 20px 20px; }
.dialog-buttons .btn { min-width: 120px; height: 44px; font-size: 19px; }

.sync-grid { display: grid; grid-template-columns: 110px 330px; gap: 8px 12px; font-size: 16px; }
.sync-grid .cap { color: var(--cyan); text-align: right; }
.sync-grid .val { background: rgba(0, 0, 0, .4); border-radius: 4px; padding: 2px 10px; font: 15px var(--ui); min-height: 22px; }
.sync-bar { height: 10px; margin-top: 14px; border-radius: 5px; background: #1a1f45; overflow: hidden; }
.sync-bar > div { height: 100%; width: 0; background: var(--cyan); transition: width .2s; }
.sync-msg { color: var(--yellow); text-align: center; margin-top: 10px; }

.board-head { display: flex; align-items: center; gap: 16px; margin-bottom: 12px; }
.board-head .icon { font-size: 54px; }
.board-head .info { color: var(--cyan); font-size: 16px; line-height: 1.5; }
.board-tabs { display: flex; gap: 10px; margin-left: auto; }
.board-tabs .btn { min-width: 0; width: 110px; height: 40px; font-size: 17px; }
.board-tabs .btn.selected { background: var(--orange); }
.board-wrap { height: 330px; overflow-y: auto; background: rgba(0, 0, 0, .45); border: 2px solid var(--line); border-radius: 8px; }
.board-table { width: 640px; border-collapse: collapse; font-size: 15px; font-weight: 700; }
.board-table th { position: sticky; top: 0; background: #1a2470; color: var(--yellow); padding: 6px 4px; }
.board-table td { text-align: center; padding: 5px 4px; border-bottom: 1px solid rgba(42, 60, 176, .5); }
.board-table td.score { font-family: var(--digit); color: var(--cyan); }
.board-table tr:nth-child(1) td.rank { color: var(--yellow); }
.board-status { color: var(--yellow); margin-top: 10px; font-size: 15px; }

/* ---------- 觸控裝置（手機橫放、平板）：精簡版面 ----------
   ninenine.js 在觸控裝置加上 body.touch，並讓畫面高度跟著螢幕比例變（--sh，400~720），
   所以這裡的版面用格線排列，不使用桌面版的絕對座標。 */

#keypad { display: none; }

body.touch {
    touch-action: manipulation;
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
}
body.touch #stage { display: grid; gap: 8px; padding: 8px; }

/* 第一頁 */
body.touch.lobby #stage { grid-template-columns: 340px 1fr; grid-template-rows: auto 1fr auto auto; gap: 8px 14px; padding: 8px 16px; }
body.touch.lobby .card { position: static; }
body.touch.lobby .lobby-title { position: static; grid-column: 1 / 3; }
body.touch.lobby .lobby-title h1 { font-size: 34px; letter-spacing: 4px; text-shadow: 0 0 6px #ff9a2e, 2px 2px 0 #a3243a; }
body.touch.lobby .lobby-title .sub { display: none; }
body.touch.lobby #playerCard { grid-column: 1; grid-row: 2; width: auto; height: auto; padding: 12px 16px; }
body.touch.lobby #freeCard { grid-column: 2; grid-row: 2 / 4; width: auto; height: auto; padding: 12px 16px; display: flex; flex-direction: column; }
body.touch.lobby #testCard { grid-column: 1; grid-row: 3; width: auto; height: auto; padding: 12px 16px; }
body.touch.lobby #help { display: none; }
body.touch.lobby .field { margin-top: 10px; }
body.touch.lobby #freeRooms { flex: 1; grid-auto-rows: 1fr; gap: 10px; margin-top: 8px; }
body.touch.lobby .tile { height: auto; }
body.touch.lobby #testCard .buttons { margin-top: 8px; }
body.touch.lobby #testCard .btn { width: 100%; height: 52px; font-size: 22px; }
body.touch.lobby .lobby-footer { position: static; grid-column: 1 / 3; grid-row: 4; }

/* 比賽室：左邊題目，右邊數字鍵盤；三個資訊卡片（網路霸主、本機霸主、挑戰者）省略 */
body.touch.room { --op: clamp(80px, calc(var(--sh, 432px) * .22), 120px); }
body.touch.room #stage { grid-template-columns: 1fr 330px; grid-template-rows: 44px minmax(0, 1fr) 54px; }
body.touch.room #roomBar { position: static; grid-column: 1 / 3; height: auto; font-size: 20px; letter-spacing: 3px; }
body.touch.room .info-card { display: none; }
body.touch.room #playCard { position: static; grid-column: 1; grid-row: 2; width: auto; height: auto; min-height: 0; padding: 10px 14px; display: flex; flex-direction: column; }
body.touch.room .score-box { height: 54px; }
body.touch.room .score-box .val { font-size: 30px; }
body.touch.room #btnQuery { width: 110px; height: 46px; font-size: 16px; }
body.touch.room .question { flex: 1; height: auto; min-height: 0; margin-top: 8px; gap: 6px; }
body.touch.room .question .op { font-size: var(--op); width: calc(var(--op) * .75); }
body.touch.room .question .sym { font-size: calc(var(--op) * .6); }
body.touch.room #echo { font-size: calc(var(--op) * .8); width: calc(var(--op) * 1.25); height: var(--op); }
body.touch.room #bar { margin-top: 8px; }
body.touch.room #netMessage { height: 20px; margin-top: 4px; font-size: 15px; }
body.touch.room .status-row { margin-top: 4px; }
body.touch.room .status { height: 44px; }
body.touch.room .status .cap { font-size: 12px; }
body.touch.room .status .val { font-size: 20px; }
body.touch.room .status .val.text { font-size: 18px; }
body.touch.room #keypad { display: grid; grid-column: 2; grid-row: 2; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(4, 1fr); gap: 8px; }
body.touch.room #keypad .btn { height: auto; min-height: 0; font-size: 36px; letter-spacing: 0; padding: 0; }
body.touch.room #keypad .btn.red, body.touch.room #keypad .btn.green { font-size: 24px; }
body.touch.room .control-row { position: static; grid-column: 1 / 3; grid-row: 3; gap: 10px; }
body.touch.room .control-row .btn { flex: 1; width: auto; height: 100%; font-size: 18px; letter-spacing: 1px; }
body.touch.room #btnSound { flex: 0 0 110px; }
body.touch.room #serverLabel, body.touch.room #btnProbe, body.touch.room .control-row .spacer { display: none; }

/* 對話框：不超出較矮的畫面 */
body.touch .dialog { min-width: 0; max-width: calc(100% - 32px); max-height: calc(var(--sh, 432px) - 24px); display: flex; flex-direction: column; }
body.touch .dialog-body { overflow: auto; min-height: 0; padding: 12px 20px; font-size: 16px; line-height: 1.5; }
body.touch .dialog-buttons { padding-bottom: 14px; }
body.touch .board-wrap { height: min(330px, calc(var(--sh, 432px) - 290px)); min-height: 110px; }

/* 手機直放時畫面太小，請使用者橫放 */
@media (orientation: portrait) and (max-width: 600px) {
    body.touch::before {
        content: "請將手機橫放，畫面才夠大";
        position: fixed;
        inset: 0;
        z-index: 9999;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 24px;
        text-align: center;
        background: var(--bg);
        color: var(--yellow);
        font: 900 22px/1.6 var(--ui);
    }
}
