/*
 * exam 系統樣式基礎
 *
 * 設計方向：清新爽朗——大量留白、白底卡片、淺藍點綴、圓角與極輕的陰影。
 * 主色沿用 Foxlink 標誌藍 #0E5C93。
 *
 * 刻意不使用 CDN 字型／框架：教室現場網路不一定穩，全部走系統字型與自家 CSS，
 * 離線也能正常顯示（前端策略比照 FoxlinkSSO：Blade + 靜態資產，不進 Vite/npm 流程）。
 */

:root {
    /* 品牌色 */
    --brand: #0E5C93;
    --brand-dark: #0A4570;
    --brand-mid: #2F80B9;
    --brand-soft: #E9F2F9;
    --brand-softer: #F4F9FC;

    /* 中性色 */
    --bg: #F5F8FA;
    --surface: #FFFFFF;
    --border: #E3EAF0;
    --text: #1F2A37;
    --text-muted: #66788A;
    --text-faint: #93A3B3;

    /* 狀態色 */
    --success: #17864F;
    --success-soft: #E8F5EE;
    --danger: #C3352B;
    --danger-soft: #FBECEA;
    --warn: #A9701A;
    --warn-soft: #FDF3E2;

    --radius-sm: 8px;
    --radius: 14px;
    --radius-lg: 20px;
    --shadow-sm: 0 1px 2px rgba(16, 42, 66, .06);
    --shadow: 0 2px 4px rgba(16, 42, 66, .05), 0 12px 28px -18px rgba(16, 42, 66, .35);
    --ring: 0 0 0 3px rgba(14, 92, 147, .18);
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC", "Noto Sans",
        "PingFang TC", "Microsoft JhengHei", "Helvetica Neue", Arial, sans-serif;
    font-size: 16px;
    line-height: 1.65;
    -webkit-font-smoothing: antialiased;
}

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

h1, h2, h3 { margin: 0 0 .5rem; line-height: 1.35; font-weight: 700; letter-spacing: -.01em; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1.02rem; }
p { margin: 0 0 1rem; }

/* ── 版面 ──────────────────────────────────────────────── */

.page {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

.page-center {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: clamp(16px, 5vw, 48px) 16px;
}

.container { width: 100%; max-width: 1160px; margin: 0 auto; padding: 0 20px; }
.stack > * + * { margin-top: 1rem; }

/* 員工端頂部：白底 + 一條品牌色細線，維持清爽 */
.topbar {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow-sm);
}

.topbar-inner {
    max-width: 1160px;
    margin: 0 auto;
    padding: 14px 20px;
    display: flex;
    align-items: center;
    gap: 16px;
}

.brand-logo { height: 30px; width: auto; display: block; }

.brand-divider {
    width: 1px;
    height: 26px;
    background: var(--border);
}

.brand-title {
    font-size: .98rem;
    font-weight: 600;
    color: var(--brand-dark);
    letter-spacing: .01em;
}

.brand-sub {
    font-size: .8rem;
    color: var(--text-muted);
    font-weight: 400;
}

.topbar-spacer { flex: 1; }

/* ── 卡片 ──────────────────────────────────────────────── */

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: clamp(20px, 4vw, 32px);
}

.card-narrow { width: 100%; max-width: 420px; }
.card-head { margin-bottom: 1.25rem; }
.card-head p { color: var(--text-muted); margin: .25rem 0 0; font-size: .92rem; }

/* 入場資訊條：讓考生一眼確認自己在正確的廠區與場次 */
.context-bar {
    display: flex;
    flex-wrap: wrap;
    gap: 8px 14px;
    padding: 12px 16px;
    background: var(--brand-softer);
    border: 1px solid var(--brand-soft);
    border-radius: var(--radius-sm);
    font-size: .88rem;
    color: var(--brand-dark);
}

.context-bar strong { font-weight: 600; }

/* ── 表單 ──────────────────────────────────────────────── */

.field { margin-bottom: 1.1rem; }

.label {
    display: block;
    margin-bottom: .4rem;
    font-size: .9rem;
    font-weight: 600;
    color: var(--text);
}

.input {
    width: 100%;
    padding: 12px 14px;
    font-size: 1.05rem;
    font-family: inherit;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    transition: border-color .15s ease, box-shadow .15s ease;
}

.input:focus {
    outline: none;
    border-color: var(--brand-mid);
    box-shadow: var(--ring);
}

.input::placeholder { color: var(--text-faint); }

/* 工號、後四碼在手機上輸入，字距拉開比較好核對 */
.input-code { letter-spacing: .12em; font-variant-numeric: tabular-nums; }

.field-hint { margin: .35rem 0 0; font-size: .82rem; color: var(--text-muted); }
.field-error { margin: .35rem 0 0; font-size: .85rem; color: var(--danger); }

.captcha-row { display: flex; gap: 10px; align-items: flex-start; }
.captcha-row .input { flex: 1; }
.captcha-img { border: 1px solid var(--border); border-radius: var(--radius-sm); height: 46px; }

/* ── 按鈕 ──────────────────────────────────────────────── */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px 20px;
    font-size: 1rem;
    font-family: inherit;
    font-weight: 600;
    line-height: 1.2;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background-color .15s ease, border-color .15s ease, color .15s ease;
}

.btn:hover { text-decoration: none; }
.btn:focus-visible { outline: none; box-shadow: var(--ring); }

.btn-primary { background: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-dark); color: #fff; }

.btn-ghost { background: transparent; color: var(--brand); border-color: var(--border); }
.btn-ghost:hover { background: var(--brand-softer); color: var(--brand-dark); }

.btn-block { width: 100%; }
.btn-sm { padding: 7px 12px; font-size: .86rem; }

/* ── 提示與狀態 ─────────────────────────────────────────── */

.alert {
    padding: 12px 16px;
    border-radius: var(--radius-sm);
    font-size: .92rem;
    border: 1px solid transparent;
}

.alert-info { background: var(--brand-soft); border-color: #CFE3F1; color: var(--brand-dark); }
.alert-error { background: var(--danger-soft); border-color: #F2D2CE; color: var(--danger); }
.alert-warn { background: var(--warn-soft); border-color: #EEDFC2; color: var(--warn); }
.alert-success { background: var(--success-soft); border-color: #C9E5D6; color: var(--success); }

.badge {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 999px;
    font-size: .78rem;
    font-weight: 600;
}

.badge-brand { background: var(--brand-soft); color: var(--brand-dark); }
.badge-success { background: var(--success-soft); color: var(--success); }
.badge-muted { background: #EEF2F6; color: var(--text-muted); }

/* ── 語言切換 ───────────────────────────────────────────── */

.lang-switch { display: flex; align-items: center; gap: 6px; margin: 0; }

.lang-switch select { min-width: 8.5rem; }

/* 螢幕閱讀器用的標籤：視覺上隱藏但不影響可及性 */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* ── 頁尾 ──────────────────────────────────────────────── */

.footer {
    padding: 20px;
    text-align: center;
    font-size: .8rem;
    color: var(--text-faint);
}

/* ── 後台版面 ───────────────────────────────────────────── */

.admin-shell { display: flex; min-height: 100vh; }

.sidebar {
    width: 244px;
    flex-shrink: 0;
    background: var(--brand);
    color: #fff;
    padding: 20px 14px;
    display: flex;
    flex-direction: column;
    gap: 18px;
}

.sidebar-brand {
    background: #fff;
    border-radius: var(--radius-sm);
    padding: 12px 14px;
    text-align: center;
}

.sidebar-brand img { height: 26px; width: auto; }

.sidebar-title {
    font-size: .78rem;
    font-weight: 600;
    color: rgba(255, 255, 255, .62);
    letter-spacing: .08em;
    text-transform: uppercase;
    padding: 0 10px;
}

.sidebar-nav { display: flex; flex-direction: column; gap: 2px; }

.sidebar-nav a {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    color: rgba(255, 255, 255, .88);
    font-size: .93rem;
}

.sidebar-nav a:hover { background: rgba(255, 255, 255, .12); color: #fff; text-decoration: none; }
.sidebar-nav a.is-active { background: #fff; color: var(--brand-dark); font-weight: 600; }

.sidebar-nav .tag {
    font-size: .68rem;
    padding: 2px 7px;
    border-radius: 999px;
    background: rgba(255, 255, 255, .18);
    color: rgba(255, 255, 255, .82);
    font-weight: 600;
}

.sidebar-foot { margin-top: auto; font-size: .76rem; color: rgba(255, 255, 255, .55); padding: 0 10px; }

.admin-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.admin-topbar {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 14px 24px;
    display: flex;
    align-items: center;
    gap: 14px;
}

.admin-content { padding: 24px; }

/* ── 統計卡與表格 ───────────────────────────────────────── */

.grid { display: grid; gap: 16px; }
.grid-stats { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }

.stat {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 18px 20px;
    box-shadow: var(--shadow-sm);
}

.stat-label { font-size: .84rem; color: var(--text-muted); margin-bottom: 6px; }
.stat-value { font-size: 1.9rem; font-weight: 700; color: var(--brand-dark); line-height: 1.1; }

.table { width: 100%; border-collapse: collapse; font-size: .92rem; }
.table th, .table td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); }
.table th { color: var(--text-muted); font-weight: 600; font-size: .84rem; }

.list-plain { margin: 0; padding-left: 1.15rem; color: var(--text-muted); font-size: .92rem; }
.list-plain li { margin-bottom: .35rem; }

.text-muted { color: var(--text-muted); }
.text-center { text-align: center; }
.mt-0 { margin-top: 0; }
.mb-0 { margin-bottom: 0; }

@media (max-width: 720px) {
    .admin-shell { flex-direction: column; }
    .admin-content { padding: 16px; }

    /*
     * 頁首那一列要換行。
     *
     * 這裡有五個東西（標題／廠區切換器／語系／管理者名／登出），窄螢幕擠不下；
     * 不換行的話 flex 會硬壓縮，中文的管理者名稱被擠成一字一行，登出鈕還會溢出畫面外
     * 並且讓整頁多出一條水平捲軸。
     */
    .admin-topbar { flex-wrap: wrap; gap: 10px 12px; padding: 12px 16px; }

    /* 換行之後這個彈簧會獨佔一整行，反而把後面的東西推下去 */
    .topbar-spacer { display: none; }

    /*
     * 側邊選單改成「可橫向捲動的一列」。
     *
     * 直向展開的話 12 個項目就是 750px 高，主內容要捲掉快兩個螢幕才看得到——
     * 而監考人員用手機開後台就是要馬上看到監考頁或員工解鎖。
     * 選單本身橫捲，logo 與標題在窄螢幕讓位，把寬度全留給項目。
     */
    .sidebar {
        width: 100%;
        flex-direction: row;
        align-items: center;
        gap: 10px;
        padding: 10px 12px;
    }

    .sidebar-brand,
    .sidebar-title,
    .sidebar-foot { display: none; }

    .sidebar-body { flex: 1; min-width: 0; }

    .sidebar-nav {
        flex-direction: row;
        overflow-x: auto;
        gap: 6px;
        /* 捲動時項目不要被截半個字 */
        scroll-snap-type: x proximity;
    }

    .sidebar-nav a {
        white-space: nowrap;
        padding: 8px 12px;
        scroll-snap-align: start;
    }

    /*
     * 表格不要被硬壓縮。
     *
     * 欄位多的表格（成績列表有 11 欄）擠進 468px 的話，每一欄都窄到把中文字排成
     * 一個字一行，列高會拉到很長、反而更難讀。給一個下限寬度，讓外層那層
     * overflow-x 的容器去橫向捲動——欄寬維持正常，使用者用滑的看後面幾欄。
     *
     * 820px 是實測挑的：以最寬的成績列表（11 欄）為準，考卷名稱欄的寬度／列高
     * 在 640px 是 63px／240px（每個詞一行），720px 是 102px／118px，
     * 820px 是 151px／94px，再往上橫滑的距離就不划算了。
     */
    .table { min-width: 820px; }
}

/* ── 後台廠區切換器 ─────────────────────────────────────── */

.site-switch { display: flex; align-items: center; gap: 8px; margin: 0; }

.site-switch-label { font-size: .82rem; color: var(--text-muted); }

.input-inline {
    width: auto;
    padding: 6px 10px;
    font-size: .9rem;
    background: var(--surface);
}

/* ── 後台表單／清單 ─────────────────────────────────────── */

/*
 * 表單卡片寬度：欄位橫排時窄卡片會把字擠在一起，所以放寬到 1040px；
 * 真正的斷行交給 .field-row 的 min-width 決定，視窗變窄時欄位自己會換行。
 */
.card-form { width: 100%; max-width: 1040px; }
.card-form + .card-form { margin-top: 20px; }

.filter-bar {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 16px;
    padding-bottom: 16px;
    border-bottom: 1px solid var(--border);
}

.field-row { display: flex; flex-wrap: wrap; gap: 16px 20px; }
/* 16rem 是「不會把中文標籤與提示擠成兩三行」的下限，比原本的 12rem 寬 */
.field-row > .field { flex: 1 1 16rem; min-width: 16rem; }

textarea.input { resize: vertical; font-family: inherit; line-height: 1.6; }

.option-row {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 8px;
}

.option-row .input { flex: 1; }

.option-correct {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: .85rem;
    color: var(--text-muted);
    white-space: nowrap;
}

/* ── 分頁（共用元件 components/pagination）───────────────── */

.pagination-bar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-top: 18px;
    padding-top: 16px;
    border-top: 1px solid var(--border);
}

.pagination-summary {
    font-size: .84rem;
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
}

.pagination {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    list-style: none;
    padding: 0;
    margin: 0;
}

.page-link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 2.25rem;
    height: 2.25rem;
    padding: 0 .7rem;
    font-size: .88rem;
    font-weight: 500;
    font-variant-numeric: tabular-nums;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    transition: background-color .12s ease, border-color .12s ease, color .12s ease;
}

a.page-link:hover {
    background: var(--brand-softer);
    border-color: var(--brand-mid);
    color: var(--brand-dark);
    text-decoration: none;
}

a.page-link:focus-visible { outline: none; box-shadow: var(--ring); }

.page-link.is-active {
    background: var(--brand);
    border-color: var(--brand);
    color: #fff;
    font-weight: 600;
}

.page-link.is-disabled { color: var(--text-faint); background: var(--bg); cursor: default; }

/* 省略號不需要框線，只是視覺上的間隔 */
.page-link.is-gap { border-color: transparent; background: transparent; color: var(--text-faint); }

@media (max-width: 640px) {
    .pagination-bar { justify-content: center; }
    .pagination-summary { width: 100%; text-align: center; }
}

.checkbox-cell { width: 2.5rem; text-align: center; }

/* ── 作答頁 ────────────────────────────────────────────── */

.exam-bar {
    position: sticky;
    top: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-sm);
    margin-bottom: 16px;
}

.exam-timer {
    font-size: 1.3rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    color: var(--brand-dark);
}

.exam-timer.is-warning { color: var(--danger); }

.exam-progress { font-size: .85rem; color: var(--text-muted); }

.exam-save-state { font-size: .8rem; color: var(--text-faint); margin-left: auto; }
.exam-save-state.is-error { color: var(--danger); }

.question-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
    padding: 20px;
    margin-bottom: 14px;
}

.question-head {
    display: flex;
    gap: 10px;
    align-items: baseline;
    margin-bottom: 4px;
}

.question-no { font-weight: 700; color: var(--brand); }
.question-score { font-size: .8rem; color: var(--text-muted); margin-left: auto; }
.question-text { font-size: 1.05rem; margin-bottom: 12px; white-space: pre-wrap; }

.option-label {
    display: flex;
    gap: 10px;
    align-items: flex-start;
    padding: 11px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    margin-bottom: 8px;
    cursor: pointer;
    transition: border-color .12s ease, background-color .12s ease;
}

.option-label:hover { border-color: var(--brand-mid); background: var(--brand-softer); }
.option-label.is-checked { border-color: var(--brand); background: var(--brand-soft); }
.option-label input { margin-top: .25rem; }

.result-score {
    font-size: 3.4rem;
    font-weight: 700;
    line-height: 1.1;
    color: var(--brand-dark);
}

.result-score.is-failed { color: var(--danger); }
.result-score.is-passed { color: var(--success); }

.exam-wide { width: 100%; max-width: 760px; }

/* ── 登入頁的分隔線（帳密 / SSO）───────────────────────── */

.login-divider {
    display: flex;
    align-items: center;
    gap: 12px;
    margin: 18px 0 14px;
    color: var(--text-faint);
    font-size: .82rem;
}

.login-divider::before,
.login-divider::after {
    content: "";
    flex: 1;
    height: 1px;
    background: var(--border);
}

.badge-danger { background: var(--danger-soft); color: var(--danger); }

/* ── 場次 QR code 與列印 ────────────────────────────────── */

.qr-print-area { margin: 8px 0 12px; }
.qr-image { width: 100%; max-width: 320px; height: auto; }
.qr-print-title,
.qr-print-url { display: none; }

.alert-muted { background: #EEF2F6; border-color: var(--border); color: var(--text-muted); }

/*
 * 列印時只留 QR 卡片：現場要把它印出來貼在座位，
 * 側欄、導覽、按鈕印出來只是浪費紙。
 */
@media print {
    .sidebar,
    .admin-topbar,
    .btn,
    .alert,
    .field-hint,
    .text-muted { display: none !important; }

    .admin-shell,
    .admin-main,
    .admin-content { display: block; padding: 0; margin: 0; }

    .grid { display: block; }

    .card {
        border: 0;
        box-shadow: none;
        padding: 0;
        page-break-inside: avoid;
    }

    /* 只印 QR 卡片本身 */
    .admin-content > .grid > .card:not(#qr-card) { display: none; }

    #qr-card h2 { display: none; }

    .qr-print-title {
        display: block;
        font-size: 20pt;
        font-weight: 700;
        margin-bottom: 12pt;
    }

    .qr-image { max-width: 12cm; }

    .qr-print-url {
        display: block;
        margin-top: 10pt;
        font-size: 10pt;
        word-break: break-all;
    }
}

/* ── 廠區 × 角色 指派矩陣 ───────────────────────────────── */

/*
 * 表格預設會把標題欄壓到最窄，中文標題就變成一個字一行。
 * 標題不換行 + 給勾選欄固定寬度，欄位太多時整個表格水平捲動即可。
 */
.role-matrix th {
    white-space: nowrap;
    text-align: center;
    vertical-align: bottom;
    color: var(--text);
    font-size: .88rem;
}

.role-matrix th:first-child { text-align: left; }

.role-matrix td { text-align: center; }

.role-matrix th:not(:first-child),
.role-matrix td:not(:first-child) { width: 6.5rem; }

.role-matrix-site {
    text-align: left !important;
    font-weight: 600;
    white-space: nowrap;
}

.role-matrix tbody tr:hover { background: var(--brand-softer); }

/* 整格可點：滑鼠不用瞄準小方框 */
.role-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 2.2rem;
    margin: -6px 0;
    cursor: pointer;
}

.role-toggle input { width: 1.05rem; height: 1.05rem; cursor: pointer; }

/* ── 角色的權限勾選清單 ─────────────────────────────────── */

.permission-row {
    display: flex;
    gap: 10px;
    align-items: flex-start;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    margin-bottom: 8px;
    cursor: pointer;
}

.permission-row:hover { border-color: var(--brand-mid); background: var(--brand-softer); }
.permission-row input { margin-top: .3rem; }

/* 權限代碼給工程師對照程式裡的 can() 用，視覺上收斂一點 */
.permission-code {
    display: block;
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    font-size: .78rem;
    color: var(--text-faint);
}

/* ── 後台的作答明細 ────────────────────────────────────── */

.answer-option {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    margin-bottom: 6px;
}

/* 正解一律標出來（給查驗用），考生選的另外標，兩者可能重疊 */
.answer-option.is-correct { border-color: #C9E5D6; background: var(--success-soft); }
.answer-option.is-selected { border-color: var(--brand-mid); }
.answer-option.is-selected.is-correct { border-color: var(--success); }

.answer-marks { display: flex; gap: 4px; flex-shrink: 0; min-width: 7rem; }

.activity-props {
    display: inline-block;
    max-width: 34rem;
    font-size: .78rem;
    color: var(--text-muted);
    word-break: break-all;
}

/* ── 題目正確率 ────────────────────────────────────────── */

.rate-bar {
    position: relative;
    height: 1.5rem;
    background: #EEF2F6;
    border-radius: 999px;
    overflow: hidden;
}

.rate-fill { height: 100%; background: var(--success); opacity: .35; }
.rate-bar.is-low .rate-fill { background: var(--danger); opacity: .3; }

.rate-label {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: .8rem;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    color: var(--text);
}

.rate-bar.is-low .rate-label { color: var(--danger); }
