/* Risk Console - plain admin styling. No gradients, no decoration. */

:root {
    --font-sans: -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "PingFang SC", sans-serif;
    --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;

    --bg: #f5f6f7;
    --surface: #ffffff;
    --surface-alt: #fafafa;
    --border: #d8dadd;
    --border-strong: #b9bdc2;
    --text: #1b1d20;
    --text-muted: #5c6269;
    --text-faint: #82888f;

    --accent: #1f5fbf;
    --accent-text: #ffffff;
    --accent-soft: #e8effa;

    --pending: #6b7280;
    --pending-bg: #eceef1;
    --running: #1f5fbf;
    --running-bg: #e8effa;
    --success: #17794a;
    --success-bg: #e4f2ea;
    --failed: #b3261e;
    --failed-bg: #fbe9e8;
    --cancelled: #8a6116;
    --cancelled-bg: #fbf1de;
    --critical: #7f1d1d;
    --critical-bg: #f7e2e2;

    --radius: 6px;
    --shadow-modal: 0 8px 32px rgba(0, 0, 0, 0.18);
    --sidebar-width: 240px;
}

[data-theme="dark"] {
    --bg: #16181b;
    --surface: #1e2125;
    --surface-alt: #24282d;
    --border: #343940;
    --border-strong: #464d55;
    --text: #e8eaed;
    --text-muted: #a4abb3;
    --text-faint: #848b93;

    --accent: #6fa3ef;
    --accent-text: #10161f;
    --accent-soft: #1d2b3f;

    --pending: #9ba1a9;
    --pending-bg: #2a2e34;
    --running: #6fa3ef;
    --running-bg: #1d2b3f;
    --success: #57c493;
    --success-bg: #16301f;
    --failed: #f08a82;
    --failed-bg: #3a1e1c;
    --cancelled: #e0b45e;
    --cancelled-bg: #3a2e15;
    --critical: #f2a09a;
    --critical-bg: #431b1b;

    --shadow-modal: 0 8px 32px rgba(0, 0, 0, 0.5);
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]):not([data-theme="dark"]) {
        --bg: #16181b;
        --surface: #1e2125;
        --surface-alt: #24282d;
        --border: #343940;
        --border-strong: #464d55;
        --text: #e8eaed;
        --text-muted: #a4abb3;
        --text-faint: #848b93;
        --accent: #6fa3ef;
        --accent-text: #10161f;
        --accent-soft: #1d2b3f;
        --pending: #9ba1a9;
        --pending-bg: #2a2e34;
        --running: #6fa3ef;
        --running-bg: #1d2b3f;
        --success: #57c493;
        --success-bg: #16301f;
        --failed: #f08a82;
        --failed-bg: #3a1e1c;
        --cancelled: #e0b45e;
        --cancelled-bg: #3a2e15;
        --critical: #f2a09a;
        --critical-bg: #431b1b;
        --shadow-modal: 0 8px 32px rgba(0, 0, 0, 0.5);
    }
}

* { box-sizing: border-box; }

/* The hidden attribute must win over any display rule below (.btn, .panel ...). */
[hidden] { display: none !important; }

html, body {
    margin: 0;
    padding: 0;
}

body {
    font-family: var(--font-sans);
    font-size: 14px;
    line-height: 1.5;
    color: var(--text);
    background: var(--bg);
    -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4 { margin: 0; font-weight: 600; }
p { margin: 0 0 8px; }

a { color: var(--accent); }

:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.numeric,
td.numeric,
th.numeric {
    font-variant-numeric: tabular-nums;
    text-align: right;
}

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
}

/* ---------------------------------------------------------------- layout */
.app-shell {
    display: flex;
    min-height: 100vh;
}

.sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: var(--sidebar-width);
    display: flex;
    flex-direction: column;
    background: var(--surface);
    border-right: 1px solid var(--border);
    z-index: 40;
}

.sidebar-brand {
    padding: 18px 20px;
    font-size: 15px;
    font-weight: 600;
    border-bottom: 1px solid var(--border);
}

.sidebar-nav {
    flex: 1;
    padding: 8px;
    overflow-y: auto;
}

.nav-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    width: 100%;
    padding: 8px 12px;
    margin-bottom: 2px;
    text-align: left;
    font: inherit;
    color: var(--text-muted);
    background: none;
    border: 1px solid transparent;
    border-radius: var(--radius);
    cursor: pointer;
}

.nav-badge {
    padding: 0 7px;
    font-size: 11px;
    font-weight: 600;
    line-height: 18px;
    color: var(--failed);
    background: var(--failed-bg);
    border-radius: 999px;
    font-variant-numeric: tabular-nums;
}

.nav-item:hover { background: var(--surface-alt); }

.nav-item[aria-current="page"] {
    color: var(--text);
    background: var(--accent-soft);
    border-color: var(--border);
    font-weight: 600;
}

.sidebar-footer {
    padding: 12px;
    border-top: 1px solid var(--border);
}

.sidebar-user {
    font-size: 12px;
    color: var(--text-muted);
    word-break: break-all;
    margin-bottom: 8px;
}

.main {
    flex: 1;
    min-width: 0;
    margin-left: var(--sidebar-width);
    display: flex;
    flex-direction: column;
}

.topbar {
    position: sticky;
    top: 0;
    z-index: 30;
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 0 24px;
    height: 56px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

.topbar h1 {
    font-size: 16px;
    flex: 1;
    min-width: 0;
}

.menu-toggle { display: none; }

.server-status {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    color: var(--text-muted);
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--pending);
}

.status-dot.is-online { background: var(--success); }
.status-dot.is-offline { background: var(--failed); }

.content {
    width: 100%;
    max-width: 1440px;
    padding: 24px;
}

.page[hidden] { display: none; }

.page-section { margin-bottom: 28px; }

.section-head {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 12px;
    margin-bottom: 12px;
}

.section-head h2 {
    font-size: 14px;
    flex: 1;
    min-width: 0;
}

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

/* ----------------------------------------------------------------- cards */
.panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.panel-body { padding: 16px; }

.stat-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 12px;
    margin-bottom: 28px;
}

.stat-tile {
    padding: 14px 16px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.stat-label {
    font-size: 12px;
    color: var(--text-muted);
    margin-bottom: 6px;
}

.stat-value {
    font-size: 24px;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

/* ---------------------------------------------------------------- tables */
.table-wrap {
    overflow-x: auto;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.data-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}

.data-table th,
.data-table td {
    padding: 8px 12px;
    text-align: left;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}

.data-table th {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-muted);
    background: var(--surface-alt);
    white-space: nowrap;
}

.data-table tbody tr:last-child td { border-bottom: none; }

.data-table .col-actions {
    white-space: nowrap;
    text-align: right;
}

.empty-row td {
    padding: 24px 12px;
    color: var(--text-muted);
    text-align: center;
}

.mono {
    font-family: var(--font-mono);
    font-size: 12px;
}

.truncate {
    display: block;
    max-width: 260px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.row-sub {
    display: block;
    margin-top: 2px;
    font-size: 12px;
}

.cell-wrap {
    min-width: 220px;
    max-width: 480px;
    white-space: normal;
    word-break: break-word;
}

.cell-wrap .field-help { margin-top: 2px; }

/* ----------------------------------------------------------------- pills */
.pill {
    display: inline-block;
    padding: 1px 8px;
    font-size: 12px;
    font-weight: 600;
    border-radius: 999px;
    border: 1px solid transparent;
    white-space: nowrap;
}

.pill-pending { color: var(--pending); background: var(--pending-bg); }
.pill-running { color: var(--running); background: var(--running-bg); }
.pill-success { color: var(--success); background: var(--success-bg); }
.pill-failed { color: var(--failed); background: var(--failed-bg); }
.pill-cancelled { color: var(--cancelled); background: var(--cancelled-bg); }
.pill-critical { color: var(--critical); background: var(--critical-bg); }
.pill-neutral { color: var(--text-muted); background: var(--surface-alt); border-color: var(--border); }

/* -------------------------------------------------------------- progress */
.progress {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 120px;
}

.progress-track {
    flex: 1;
    height: 6px;
    min-width: 60px;
    background: var(--surface-alt);
    border: 1px solid var(--border);
    border-radius: 999px;
    overflow: hidden;
}

.progress-fill {
    display: block;
    height: 100%;
    background: var(--running);
}

.progress-fill.is-success { background: var(--success); }
.progress-fill.is-failed { background: var(--failed); }
.progress-fill.is-cancelled { background: var(--cancelled); }

.progress-value {
    font-size: 12px;
    font-variant-numeric: tabular-nums;
    color: var(--text-muted);
    min-width: 34px;
    text-align: right;
}

/* --------------------------------------------------------------- buttons */
.btn {
    display: inline-block;
    padding: 6px 12px;
    font: inherit;
    font-size: 13px;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius);
    cursor: pointer;
}

.btn:hover { background: var(--surface-alt); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }

.btn-primary {
    color: var(--accent-text);
    background: var(--accent);
    border-color: var(--accent);
}

.btn-primary:hover { background: var(--accent); opacity: 0.9; }

.btn-danger { color: var(--failed); border-color: var(--failed); }
.btn-danger:hover { background: var(--failed-bg); }

.btn-sm { padding: 3px 8px; font-size: 12px; }

.btn-link {
    padding: 0;
    color: var(--accent);
    background: none;
    border: none;
    font: inherit;
    font-size: 13px;
    cursor: pointer;
    text-decoration: underline;
}

.row-actions {
    display: inline-flex;
    gap: 6px;
    flex-wrap: wrap;
    justify-content: flex-end;
}

/* ----------------------------------------------------------------- forms */
.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 16px;
}

.field { display: block; }

.field label,
.field > .field-label {
    display: block;
    font-size: 13px;
    font-weight: 600;
    margin-bottom: 4px;
}

.field input[type="text"],
.field input[type="email"],
.field input[type="password"],
.field input[type="datetime-local"],
.field input[type="number"],
.field textarea,
.search-input,
.field select {
    width: 100%;
    padding: 6px 8px;
    font: inherit;
    font-size: 13px;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius);
}

.field textarea {
    resize: vertical;
    min-height: 64px;
}

.field input[readonly] {
    color: var(--text-muted);
    background: var(--surface-alt);
}

.search-input {
    width: auto;
    min-width: 220px;
    padding: 3px 8px;
    font-size: 12px;
}

.field-help {
    display: block;
    margin-top: 4px;
    font-size: 12px;
    color: var(--text-muted);
}

.checkbox-field {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
}

.checkbox-field label {
    margin: 0;
    font-weight: 400;
}

.form-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 16px;
    padding-top: 16px;
    border-top: 1px solid var(--border);
}

.form-error {
    margin-top: 12px;
    padding: 8px 12px;
    font-size: 13px;
    color: var(--failed);
    background: var(--failed-bg);
    border: 1px solid var(--failed);
    border-radius: var(--radius);
}

.form-error[hidden] { display: none; }

.preset-row {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
    margin-bottom: 12px;
}

/* ---------------------------------------------------------------- modals */
.modal {
    position: fixed;
    inset: 0;
    z-index: 60;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding: 24px 16px;
    background: rgba(0, 0, 0, 0.45);
    overflow-y: auto;
    opacity: 1;
    transition: opacity 150ms linear;
}

.modal[hidden] { display: none; }

.modal-dialog {
    width: 100%;
    max-width: 640px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-modal);
}

.modal-dialog.is-wide { max-width: 1100px; }

.modal-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 16px;
    border-bottom: 1px solid var(--border);
}

.modal-header h2 {
    flex: 1;
    min-width: 0;
    font-size: 15px;
}

.modal-body { padding: 16px; }

.modal-section {
    padding: 16px;
    border-top: 1px solid var(--border);
}

.modal-footer {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 12px 16px;
    border-top: 1px solid var(--border);
}

.modal-footer .spacer { margin-left: auto; }

.icon-btn {
    padding: 4px 10px;
    font: inherit;
    font-size: 16px;
    line-height: 1;
    color: var(--text-muted);
    background: none;
    border: 1px solid transparent;
    border-radius: var(--radius);
    cursor: pointer;
}

.icon-btn:hover {
    background: var(--surface-alt);
    border-color: var(--border);
}

/* ------------------------------------------------------------ task modal */
.detail-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 12px;
    margin-bottom: 16px;
}

.detail-label {
    font-size: 12px;
    color: var(--text-muted);
}

.detail-value { font-size: 13px; }

.step-list {
    list-style: none;
    margin: 0 0 16px;
    padding: 0;
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.step-list li {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border-bottom: 1px solid var(--border);
}

.step-list li:last-child { border-bottom: none; }

.step-name { flex: 1; min-width: 140px; font-size: 13px; }
.step-message { flex-basis: 100%; font-size: 12px; color: var(--text-muted); }

.log-box {
    max-height: 240px;
    overflow: auto;
    padding: 10px 12px;
    font-family: var(--font-mono);
    font-size: 12px;
    white-space: pre-wrap;
    word-break: break-word;
    background: var(--surface-alt);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.error-box {
    padding: 10px 12px;
    margin-bottom: 16px;
    font-size: 13px;
    white-space: pre-wrap;
    color: var(--failed);
    background: var(--failed-bg);
    border: 1px solid var(--failed);
    border-radius: var(--radius);
}

/* -------------------------------------------------- cases and players */
.subhead {
    margin: 16px 0 8px;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-muted);
}

.timeline {
    list-style: none;
    margin: 0 0 16px;
    padding: 0;
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.timeline li {
    padding: 8px 12px;
    border-bottom: 1px solid var(--border);
    font-size: 13px;
}

.timeline li:last-child { border-bottom: none; }

.timeline-meta {
    margin-bottom: 2px;
    font-size: 12px;
    color: var(--text-muted);
}

.timeline-text {
    white-space: pre-wrap;
    word-break: break-word;
}

.detail-grid dt,
.detail-grid dd { margin: 0; }

#playerDetail .table-wrap { margin-bottom: 8px; }
#playerDetail .subhead { margin-top: 20px; }

/* ---------------------------------------------------------------- viewer */
.viewer-layout {
    display: grid;
    grid-template-columns: 240px 1fr;
    /* A definite height plus minmax(0, 1fr) is what lets the two panes scroll
       independently; max-height alone lets the grid row grow past the box and
       the parent then clips it (nothing scrolls). */
    grid-template-rows: minmax(0, 1fr);
    height: calc(100vh - 200px);
    min-height: 420px;
}

.viewer-files {
    min-height: 0;
    overflow-y: auto;
    padding: 8px;
    border-right: 1px solid var(--border);
    background: var(--surface-alt);
}

.viewer-file {
    display: block;
    width: 100%;
    padding: 6px 10px;
    margin-bottom: 2px;
    text-align: left;
    font: inherit;
    color: var(--text-muted);
    background: none;
    border: 1px solid transparent;
    border-radius: var(--radius);
    cursor: pointer;
}

.viewer-file:hover { background: var(--surface); }

.viewer-file.is-active {
    color: var(--text);
    background: var(--surface);
    border-color: var(--border);
}

.viewer-file-name { display: block; font-size: 13px; font-weight: 600; }
.viewer-file-meta { display: block; font-size: 11px; color: var(--text-faint); }

.viewer-content {
    min-height: 0;
    overflow: auto;
    padding: 16px;
}

.viewer-empty,
.viewer-note {
    color: var(--text-muted);
    font-size: 13px;
}

.viewer-pre {
    margin: 0;
    font-family: var(--font-mono);
    font-size: 12px;
    white-space: pre-wrap;
    word-break: break-word;
}

.viewer-frame {
    width: 100%;
    height: 70vh;
    min-height: 420px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: #ffffff;
}

.viewer-table-wrap {
    overflow-x: auto;
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.markdown-body { font-size: 14px; }
.markdown-body h1 { font-size: 20px; margin: 0 0 12px; }
.markdown-body h2 { font-size: 17px; margin: 20px 0 10px; }
.markdown-body h3 { font-size: 15px; margin: 16px 0 8px; }
.markdown-body table { border-collapse: collapse; font-size: 13px; }
.markdown-body th,
.markdown-body td { padding: 6px 10px; border: 1px solid var(--border); }
.markdown-body pre {
    padding: 10px;
    overflow-x: auto;
    background: var(--surface-alt);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}
.markdown-body code { font-family: var(--font-mono); font-size: 12px; }

/* JSON tree */
.json-tree { font-family: var(--font-mono); font-size: 12px; }
.json-node { margin-left: 12px; }
.json-row { margin-left: 12px; padding: 1px 0; }
.json-children.is-collapsed { display: none; }

.json-toggle {
    display: block;
    padding: 1px 0;
    font: inherit;
    color: var(--text);
    background: none;
    border: none;
    cursor: pointer;
    text-align: left;
}

.json-caret::before { content: "\25BE"; display: inline-block; width: 12px; color: var(--text-faint); }
.json-toggle.is-collapsed .json-caret::before { content: "\25B8"; }

.json-key { color: var(--accent); }
.json-colon { color: var(--text-faint); }
.json-string { color: var(--success); }
.json-number { color: var(--cancelled); }
.json-boolean { color: var(--running); }
.json-null { color: var(--text-faint); }
.json-bracket { color: var(--text-faint); }
.json-summary { color: var(--text-faint); margin: 0 4px; }

/* ---------------------------------------------------------------- toasts */
.toast-region {
    position: fixed;
    right: 16px;
    bottom: 16px;
    z-index: 80;
    display: flex;
    flex-direction: column;
    gap: 8px;
    max-width: 360px;
}

.toast {
    padding: 10px 12px;
    font-size: 13px;
    background: var(--surface);
    border: 1px solid var(--border-strong);
    border-left-width: 3px;
    border-radius: var(--radius);
    box-shadow: var(--shadow-modal);
}

.toast-success { border-left-color: var(--success); }
.toast-error { border-left-color: var(--failed); }
.toast-info { border-left-color: var(--running); }

.toast-title { font-weight: 600; }
.toast-message { color: var(--text-muted); }

/* ------------------------------------------------------------ share page */
.share-page {
    max-width: 1440px;
    margin: 0 auto;
    padding: 24px;
}

.share-header {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 8px;
    margin-bottom: 16px;
}

.share-header h1 { font-size: 18px; }
.share-header .share-name { font-family: var(--font-mono); font-size: 13px; color: var(--text-muted); }

.share-panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
}

/* ----------------------------------------------------------- responsive */
@media (max-width: 900px) {
    .sidebar {
        transform: translateX(-100%);
        box-shadow: var(--shadow-modal);
    }

    .sidebar.is-open { transform: translateX(0); }

    .main { margin-left: 0; }

    .menu-toggle { display: inline-block; }

    .content { padding: 16px; }

    .sidebar-backdrop {
        position: fixed;
        inset: 0;
        z-index: 35;
        background: rgba(0, 0, 0, 0.4);
    }

    .sidebar-backdrop[hidden] { display: none; }

    .viewer-layout {
        grid-template-columns: 1fr;
        grid-template-rows: auto minmax(0, 1fr);
        height: calc(100vh - 120px);
        min-height: 0;
    }

    .viewer-files {
        border-right: none;
        border-bottom: 1px solid var(--border);
        max-height: 180px;
    }

    .truncate { max-width: 140px; }
}

@media (min-width: 901px) {
    .sidebar-backdrop { display: none; }
}

/* Multi-select game list and inline clear link */
.field select[multiple] { height: auto; min-height: 168px; padding: 4px; }
.field select[multiple] option { padding: 4px 6px; }
.link-button { background: none; border: 0; padding: 0; color: var(--accent, #1d4ed8); cursor: pointer; font: inherit; text-decoration: underline; }
