:root {
    --blue: #1d4ed8;
    --blue-dark: #1e3a8a;
    --ink: #0f172a;
    --muted: #64748b;
    --line: #e2e8f0;
    --bg: #f1f5f9;
    --ok: #15803d;
    --warn: #b45309;
    --danger: #dc2626;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
    font-family: "Segoe UI", system-ui, Arial, sans-serif;
    background: var(--bg);
    color: var(--ink);
    font-size: 15px;
}
a { color: var(--blue); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 24px; margin: 0; }
h2 { font-size: 17px; margin: 0 0 10px; }
h3 { font-size: 16px; margin: 0 0 6px; }
.mono { font-family: Consolas, "Courier New", monospace; }
.muted { color: var(--muted); font-size: 13px; }
.center { text-align: center; }

/* ---------------- top bar ---------------- */
.topbar {
    display: flex; align-items: center; gap: 18px;
    background: var(--blue-dark); color: #fff;
    padding: 0 18px; min-height: 54px; flex-wrap: wrap;
    position: sticky; top: 0; z-index: 60;
}
.brand { color: #fff; font-weight: 700; font-size: 17px; letter-spacing: .3px; }
.brand:hover { text-decoration: none; }
.nav { display: flex; gap: 4px; flex-wrap: wrap; flex: 1; }
.nav-link {
    color: #dbeafe; padding: 8px 10px; border-radius: 6px; font-size: 14px;
}
.nav-link:hover { background: rgba(255,255,255,.12); text-decoration: none; color: #fff; }
.nav-link.active { background: var(--blue); color: #fff; }
.nav-right { display: flex; align-items: center; gap: 10px; }
.who { font-size: 13px; color: #dbeafe; display: flex; align-items: center; gap: 8px; }

/* ---------------- layout ---------------- */
.container { max-width: 1180px; margin: 0 auto; padding: 20px 16px 60px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.panel { background: #fff; border: 1px solid var(--line); border-radius: 10px; padding: 18px; margin-bottom: 18px; }
.panel.narrow { max-width: 560px; }
.panel-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.panel p:last-child { margin-bottom: 0; }

/* ---------------- buttons ---------------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center;
    border: 1px solid transparent; border-radius: 8px; cursor: pointer;
    padding: 9px 16px; font-size: 14px; font-weight: 600; background: #e2e8f0; color: var(--ink);
}
.btn:hover { filter: brightness(.96); text-decoration: none; }
.btn-primary { background: var(--blue); color: #fff; }
.btn-outline { background: #fff; border-color: var(--line); color: var(--ink); }
.btn-danger { background: #fff; border-color: var(--danger); color: var(--danger); }
.btn-ghost { background: rgba(255,255,255,.14); color: #fff; }
.btn-sm { padding: 5px 10px; font-size: 13px; border-radius: 6px; }
.btn-lg { padding: 12px 22px; font-size: 16px; }
.btn-block { width: 100%; }
.btn-row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-top: 14px; }
.btn-row.separated { margin-top: 18px; border-top: 1px dashed var(--line); padding-top: 14px; }
.inline-form { display: inline; }

/* ---------------- forms ---------------- */
.field { margin-bottom: 14px; }
.field label { display: block; font-weight: 600; font-size: 13px; margin-bottom: 5px; }
.field input, .field select, .field textarea {
    width: 100%; padding: 9px 10px; border: 1px solid #cbd5e1; border-radius: 8px;
    font-size: 15px; font-family: inherit; background: #fff;
}
.field input:focus, .field select:focus, .field textarea:focus {
    outline: 2px solid #bfdbfe; border-color: var(--blue);
}
.field input.readonly, .field input[readonly] { background: #f8fafc; color: var(--muted); }
.field .hint { display: block; color: var(--muted); font-size: 12px; margin-top: 4px; }
.field-error { color: var(--danger); font-size: 12.5px; display: block; margin-top: 3px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 0 18px; }
.form-grid .span2 { grid-column: span 2; }
.checkbox-field label { font-weight: 600; display: flex; gap: 8px; align-items: center; }
.checkbox-field input { width: auto; }
.narrow { max-width: 160px; }
textarea { font-family: Consolas, monospace; font-size: 13px; }

.search-bar { display: flex; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; }
.search-bar input {
    flex: 1; min-width: 0; padding: 13px 14px; font-size: 17px;
    border: 1px solid #cbd5e1; border-radius: 10px;
}
.search-bar input:focus { outline: 2px solid #bfdbfe; border-color: var(--blue); }
.build-stamp { text-align: center; color: #94a3b8; font-size: 11px; padding: 4px 0 12px; }
.search-bar select {
    padding: 13px 10px; font-size: 15px; background: #fff;
    border: 1px solid #cbd5e1; border-radius: 10px;
}
.search-bar select:focus { outline: 2px solid #bfdbfe; border-color: var(--blue); }

/* ---------------- full-screen QR scan overlay ---------------- */
.scan-overlay {
    position: fixed; inset: 0; z-index: 300;
    display: none; flex-direction: column;
    background: #0f172a;
}
.scan-overlay.open { display: flex; }
.scan-overlay-head {
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
    padding: 12px 14px;
    padding-top: max(12px, env(safe-area-inset-top));
    color: #fff;
}
.scan-title { font-weight: 700; font-size: 16px; }
.scan-cancel {
    background: rgba(255,255,255,.14); color: #fff;
    border: 1px solid rgba(255,255,255,.4); border-radius: 8px;
    padding: 10px 18px; font-size: 15px; font-weight: 600;
    min-height: 44px; cursor: pointer;
}
.scan-cancel:hover { background: rgba(255,255,255,.26); }
.scan-overlay-body { flex: 1; position: relative; overflow: hidden; }
.scan-target { position: absolute; inset: 0; }
.scan-target video {
    width: 100% !important; height: 100% !important;
    object-fit: contain; display: block; background: #000;
}
.scan-overlay-hint {
    color: #cbd5e1; text-align: center; font-size: 14px;
    padding: 12px 16px;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
}
.scan-msg {
    position: absolute; z-index: 2; left: 16px; right: 16px; top: 50%;
    transform: translateY(-50%);
    background: #fff; color: #7f1d1d;
    border: 1px solid #fecaca; border-radius: 10px;
    padding: 14px 16px; font-size: 15px; line-height: 1.5; text-align: center;
    box-shadow: 0 10px 30px rgba(0,0,0,.4);
}

/* ---------------- tables ---------------- */
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th, .table td { padding: 9px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
.table thead th { background: #f8fafc; font-size: 12.5px; text-transform: uppercase; letter-spacing: .4px; color: var(--muted); }
.table tbody tr:hover { background: #f8fbff; }
.table .num { text-align: right; }
.table tfoot th { background: #f8fafc; }
.actions { white-space: nowrap; text-align: right; }
.actions .inline-form { margin-left: 6px; }
.total-cell { font-size: 17px; }

/* ---------------- tags / badges ---------------- */
.tag { display: inline-block; background: #eef2ff; color: #3730a3; border-radius: 999px; padding: 2px 10px; font-size: 12.5px; font-weight: 600; white-space: nowrap; }
.tag-green { background: #dcfce7; color: var(--ok); }
.tag-grey { background: #f1f5f9; color: var(--muted); }
.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip { font-size: 12px; font-weight: 700; border-radius: 999px; padding: 4px 12px; }
.chip-ok { background: #dcfce7; color: var(--ok); }
.chip-off { background: #fee2e2; color: var(--danger); }
.role-badge { display: inline-block; font-size: 11.5px; font-weight: 700; border-radius: 6px; padding: 2px 8px; text-transform: uppercase; letter-spacing: .4px; }
.role-admin { background: #fee2e2; color: #991b1b; }
.role-judge { background: #dbeafe; color: #1e40af; }
.role-volunteer { background: #dcfce7; color: #166534; }
.position {
    display: inline-flex; align-items: center; justify-content: center;
    min-width: 30px; height: 30px; border-radius: 50%; font-weight: 800; background: #f1f5f9;
}
.pos-1 { background: #fde68a; color: #92400e; }
.pos-2 { background: #e2e8f0; color: #334155; }
.pos-3 { background: #fed7aa; color: #9a3412; }
tr.rank-1 td { background: #fffbeb; }
tr.rank-2 td { background: #f8fafc; }
tr.rank-3 td { background: #fff7ed; }

/* ---------------- alerts ---------------- */
.alert { border-radius: 10px; padding: 12px 14px; margin-bottom: 16px; font-size: 14.5px; }
.alert:empty, .field-error:empty, .validation-summary-valid { display: none; }
.alert-success { background: #dcfce7; border: 1px solid #bbf7d0; color: #14532d; }
.alert-error { background: #fee2e2; border: 1px solid #fecaca; color: #7f1d1d; }
.alert-warn { background: #fef3c7; border: 1px solid #fde68a; color: #78350f; }

/* ---------------- dashboard ---------------- */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 14px; margin-bottom: 18px; }
.stat { background: #fff; border: 1px solid var(--line); border-radius: 10px; padding: 18px; text-align: center; }
.stat-value { font-size: 34px; font-weight: 800; color: var(--blue); }
.stat-label { color: var(--muted); font-size: 13px; margin-top: 2px; }
.card-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 14px; }
.card {
    display: block; background: #fff; border: 1px solid var(--line); border-radius: 10px;
    padding: 18px; color: var(--ink); transition: box-shadow .15s, transform .15s;
}
.card:hover { text-decoration: none; box-shadow: 0 6px 18px rgba(15, 23, 42, .08); transform: translateY(-2px); }
.card h3 { color: var(--blue); }
.card p { margin: 0; color: var(--muted); font-size: 13.5px; }

/* ---------------- participant / judging ---------------- */
.participant-strip {
    display: flex; gap: 26px; flex-wrap: wrap; background: #fff;
    border: 1px solid var(--line); border-radius: 10px; padding: 14px 18px; margin-bottom: 16px;
}
.participant-strip .k { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .5px; display: block; }
.big-code { font-size: 22px; font-weight: 800; color: var(--blue); }
.participant-card { display: flex; gap: 24px; justify-content: space-between; flex-wrap: wrap; align-items: center; }
.participant-main { display: flex; gap: 18px; align-items: center; flex-wrap: wrap; }
.big-serial {
    font-size: 30px; font-weight: 800; background: #eef2ff; color: #3730a3;
    border-radius: 10px; padding: 10px 16px; letter-spacing: 1px;
}
.participant-actions { text-align: center; }
.issued-box { background: #f8fafc; border: 1px dashed #cbd5e1; border-radius: 10px; padding: 14px 22px; }
.label { font-size: 11px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); }
.marks-input { max-width: 150px; text-align: center; font-size: 16px; padding: 8px; }
.marks-table td, .marks-table th { padding: 10px; }
.steps { margin: 0; padding-left: 20px; color: var(--muted); line-height: 1.7; }
.steps strong { color: var(--ink); }

/* ---------------- search result cards (mobile swaps the results table for these) ---------------- */
.result-list { display: none; }
.result-card {
    display: block; background: #fff; border: 1px solid var(--line);
    border-radius: 10px; padding: 12px 14px; color: var(--ink);
}
.result-card:active { background: #f8fafc; }
.rc-top { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.rc-serial {
    font-weight: 800; color: #3730a3; background: #eef2ff;
    border-radius: 8px; padding: 3px 9px; font-size: 15px; letter-spacing: 1px;
}
.rc-name { font-weight: 700; font-size: 16px; }
.rc-meta { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 14px; margin-top: 8px; font-size: 13.5px; }
.rc-item .k { display: block; font-size: 10.5px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); }
.rc-go {
    display: inline-block; margin-top: 10px; background: var(--blue); color: #fff;
    border-radius: 8px; padding: 9px 16px; font-weight: 600; font-size: 14px;
}
.rc-actions { display: flex; align-items: center; gap: 8px; margin-top: 10px; flex-wrap: wrap; }

/* ---------------- settings / setup ---------------- */
.status-line { border-radius: 8px; padding: 10px 12px; font-weight: 600; font-size: 14px; }
.status-line.ok { background: #dcfce7; color: #14532d; }
.status-line.bad { background: #fee2e2; color: #7f1d1d; }
.switch { display: inline-flex; align-items: center; gap: 8px; font-weight: 600; font-size: 14px; }
.switch input { width: auto; }
.info-strip {
    display: flex; gap: 26px; flex-wrap: wrap; background: #f8fafc; border: 1px solid var(--line);
    border-radius: 8px; padding: 10px 14px; margin-top: 6px; font-size: 13.5px; color: var(--muted);
}
.info-strip .k { font-weight: 700; color: var(--ink); margin-right: 4px; }

/* ---------------- login ---------------- */
.login-page {
    min-height: 100vh; display: flex; align-items: center; justify-content: center;
    background: linear-gradient(135deg, var(--blue-dark) 0%, #1d4ed8 100%); padding: 20px;
}
.login-card {
    background: #fff; border-radius: 14px; padding: 34px 30px; width: 100%; max-width: 400px;
    box-shadow: 0 20px 50px rgba(0,0,0,.25);
}
.login-brand { font-size: 22px; font-weight: 800; text-align: center; color: var(--blue-dark); }
.login-sub { text-align: center; color: var(--muted); font-size: 13px; margin: 4px 0 20px; }
.login-help { text-align: center; color: var(--muted); font-size: 12.5px; margin-top: 16px; }

/* ------------------------------------------------ busy progress overlay */
.busy-overlay {
    position: fixed;
    inset: 0;
    z-index: 200;
    display: none;
    align-items: center;
    justify-content: center;
    background: rgba(15, 23, 42, 0.45);
}
.busy-overlay.show { display: flex; }
.busy-card {
    background: #fff;
    border: 1px solid #cbd5e1;
    border-radius: 10px;
    padding: 18px 22px;
    width: min(320px, 82vw);
    box-shadow: 0 10px 30px rgba(15, 23, 42, 0.25);
}
.busy-track {
    height: 8px;
    border-radius: 999px;
    background: #e2e8f0;
    overflow: hidden;
}
.busy-fill {
    height: 100%;
    width: 45%;
    border-radius: 999px;
    background: var(--blue);
    animation: busy-slide 1s ease-in-out infinite alternate;
}
@keyframes busy-slide {
    from { transform: translateX(-100%); }
    to   { transform: translateX(240%); }
}
.busy-text { margin-top: 10px; font-size: 14px; color: #334155; }

/* ------------------------------------------------ mobile (<= 760px) */
@media (max-width: 760px) {
    /* --- header: compact brand + one swipeable nav row --- */
    .topbar { padding: 6px 10px; gap: 8px; min-height: 46px; }
    .brand { font-size: 15px; }
    .who-name { display: none; }
    .who { font-size: 12px; }
    .nav {
        order: 3; width: 100%; flex: 1 0 100%;
        flex-wrap: nowrap; overflow-x: auto; -webkit-overflow-scrolling: touch;
        scrollbar-width: none; margin: 0 -10px; padding: 0 10px 4px;
    }
    .nav::-webkit-scrollbar { display: none; }
    .nav-link { white-space: nowrap; padding: 7px 9px; font-size: 13px; }

    /* --- page chrome --- */
    .container { padding: 14px 12px 56px; }
    h1 { font-size: 20px; }
    h2 { font-size: 16px; }
    .panel { padding: 14px; overflow-x: auto; -webkit-overflow-scrolling: touch; }
    .panel.narrow { max-width: none; }
    .page-head { gap: 8px; }

    /* --- tables: tighter cells, dense tables swipe horizontally --- */
    .table th, .table td { padding: 7px 7px; font-size: 13.5px; }
    .table.wide { min-width: 560px; }
    .marks-input { max-width: 110px; }

    /* --- search results: tap-friendly stacked cards instead of a swipeable table --- */
    .results-panel table { display: none; }
    .results-panel { overflow-x: visible; }
    .result-list { display: grid; gap: 10px; }

    /* --- stacked editable tables (Setup): one labelled block per row --- */
    .table.stacked,
    .table.stacked tbody,
    .table.stacked tr,
    .table.stacked td { display: block; width: 100%; }
    .table.stacked thead { display: none; }
    .table.stacked tr {
        border: 1px solid var(--line); border-radius: 10px; background: #fff;
        padding: 10px 12px; margin-bottom: 10px;
    }
    .table.stacked td {
        border: none; padding: 5px 0;
        display: flex; align-items: center; gap: 10px;
    }
    .table.stacked td::before {
        content: attr(data-label);
        flex: 0 0 44%;
        font-size: 11px; font-weight: 600; text-transform: uppercase;
        letter-spacing: .5px; color: var(--muted);
    }
    .table.stacked td input { flex: 1 1 auto; min-width: 0; font-size: 16px; padding: 9px 10px; }
    .table.stacked td.num::before { content: none; }
    .table.stacked td.num { justify-content: flex-start; gap: 6px; color: var(--muted); font-weight: 700; }
    .table.stacked td.row-actions { justify-content: flex-end; padding-top: 8px; }
    .table.stacked td.row-actions::before { content: none; }

    /* --- judge marks tables: one labelled block per parameter --- */
    .table.marks-table { min-width: 0; }
    .table.marks-table thead { display: none; }
    .table.marks-table tbody, .table.marks-table tfoot { display: block; width: 100%; }
    .table.marks-table tbody tr {
        border: 1px solid var(--line); border-radius: 10px; background: #fff;
        padding: 10px 12px; margin-bottom: 10px;
    }
    .table.marks-table tbody td {
        display: flex; align-items: center; gap: 10px;
        border: none; padding: 5px 0; width: auto;
    }
    .table.marks-table tbody td::before {
        content: attr(data-label);
        flex: 0 0 42%;
        font-size: 11px; font-weight: 600; text-transform: uppercase;
        letter-spacing: .5px; color: var(--muted);
    }
    .table.marks-table tbody td:not([data-label])::before { content: none; }
    .table.marks-table tbody td.num { justify-content: flex-start; color: var(--ink); font-weight: 600; }
    .table.marks-table .marks-input { flex: 1 1 auto; max-width: none; min-width: 0; min-height: 46px; }
    .table.marks-table tfoot tr { display: flex; align-items: center; justify-content: space-between; padding: 10px 6px 2px; }
    .table.marks-table tfoot th { border: none; padding: 0; background: transparent; font-size: 14px; }
    .table.marks-table tfoot th.total-cell { font-size: 17px; }

    /* --- judge action buttons: full-width stack --- */
    .judge-actions { flex-direction: column; align-items: stretch; }
    .judge-actions .btn { width: 100%; justify-content: center; }
    .judge-actions .inline-form { display: block; width: 100%; }
    .judge-actions .inline-form .btn { width: 100%; }

    /* --- forms: 16px inputs stop iOS auto-zoom, single-column grid --- */
    .form-grid { grid-template-columns: 1fr; }
    .form-grid .span2 { grid-column: span 1; }
    .field input, .field select, .field textarea { font-size: 16px; padding: 10px; }
    .field label { font-size: 13px; }

    /* --- search / scan bars: input full row, buttons share the next row --- */
    .search-bar { gap: 8px; }
    .search-bar input { flex: 1 1 100%; font-size: 16px; padding: 12px; }
    .search-bar select { flex: 1 1 calc(50% - 4px); min-height: 44px; font-size: 16px; }
    .search-bar > .btn { flex: 1 1 calc(50% - 4px); min-height: 44px; }
    .search-bar > .btn:only-of-type { flex: 1 1 100%; }

    /* --- touch targets --- */
    .btn { padding: 11px 14px; min-height: 44px; font-size: 15px; }
    .btn-sm { padding: 8px 11px; min-height: 36px; font-size: 13px; }
    .btn-lg { padding: 12px 16px; }
    .btn-row { gap: 8px; }

    /* --- dashboard / cards --- */
    .stat-grid { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; }
    .stat { padding: 12px; }
    .stat-value { font-size: 26px; }
    .card-grid { grid-template-columns: 1fr; }
    .card { padding: 14px; }

    /* --- participant blocks --- */
    .participant-strip { gap: 10px 16px; padding: 12px 14px; }
    .participant-card, .participant-main { gap: 14px; }
    .participant-actions { width: 100%; text-align: left; }
    .big-serial { font-size: 24px; padding: 8px 12px; }
    .big-code { font-size: 19px; }
    .issued-box { padding: 12px 14px; }
    .info-strip { gap: 8px 16px; }

    /* --- login --- */
    .login-card { padding: 24px 18px; }
}

@media print {
    .topbar, .btn, .search-bar, .no-print { display: none !important; }
    body { background: #fff; }
    .panel { border: 0; padding: 0; }
}
