/* ── Reset & base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --bg: #0f1117;
    --surface: #1a1d27;
    --surface-2: #232734;
    --border: #2d3145;
    --text: #e1e4ed;
    --text-muted: #8b8fa3;
    --accent: #6c7ee1;
    --good: #34d399;
    --good-bg: rgba(52, 211, 153, 0.12);
    --bad: #f87171;
    --bad-bg: rgba(248, 113, 113, 0.12);
    --warn: #fbbf24;
    --radius: 6px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    min-height: 100vh;
}

/* ── Header ── */
header {
    padding: 24px 32px 16px;
    border-bottom: 1px solid var(--border);
}
header h1 { font-size: 1.4rem; font-weight: 600; }
.subtitle { color: var(--text-muted); font-size: 0.85rem; margin-top: 2px; }
.header-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.nav-link {
    color: var(--accent);
    text-decoration: none;
    font-size: 0.85rem;
    padding: 6px 12px;
    border: 1px solid var(--accent);
    border-radius: var(--radius);
    transition: background 0.15s;
}
.nav-link:hover {
    background: rgba(108, 126, 225, 0.12);
}

/* ── Main ── */
main { padding: 16px 32px 48px; }

/* ── Controls ── */
.controls {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    margin-bottom: 16px;
    flex-wrap: wrap;
}
.controls-left, .controls-right {
    display: flex;
    align-items: center;
    gap: 8px;
}
.controls label {
    font-size: 0.85rem;
    color: var(--text-muted);
    white-space: nowrap;
}
#summary-text {
    font-size: 0.82rem;
    color: var(--text-muted);
}

select, input, textarea {
    background: var(--surface-2);
    border: 1px solid var(--border);
    color: var(--text);
    border-radius: var(--radius);
    padding: 6px 10px;
    font-size: 0.85rem;
    font-family: inherit;
}
select:focus, input:focus, textarea:focus {
    outline: none;
    border-color: var(--accent);
}

button {
    background: var(--surface-2);
    border: 1px solid var(--border);
    color: var(--text);
    border-radius: var(--radius);
    padding: 6px 12px;
    font-size: 0.85rem;
    cursor: pointer;
    transition: background 0.15s;
}
button:hover { background: var(--border); }
button:disabled { opacity: 0.4; cursor: default; }

.btn-primary {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}
.btn-primary:hover { background: #5a6dd0; }

.btn-secondary {
    background: transparent;
}

/* ── Add form ── */
.add-form {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: 16px;
}
.add-form h3 { font-size: 0.95rem; margin-bottom: 12px; }
.form-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}
.form-grid label {
    display: flex;
    flex-direction: column;
    font-size: 0.8rem;
    color: var(--text-muted);
    gap: 4px;
}
.span-2 { grid-column: span 2; }
.form-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 12px;
}

.hidden { display: none !important; }

/* ── Table ── */
.table-wrap { overflow-x: auto; }

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.82rem;
}
thead {
    position: sticky;
    top: 0;
    z-index: 2;
}
th {
    background: var(--surface);
    color: var(--text-muted);
    font-weight: 500;
    text-align: left;
    padding: 10px 12px;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}
td {
    padding: 8px 12px;
    border-bottom: 1px solid var(--border);
    vertical-align: top;
}
tr:hover td { background: var(--surface); }

.col-num { width: 40px; text-align: center; }
.col-title { width: 140px; }
.col-messages, .col-draft { max-width: 260px; }
.col-label { width: 64px; text-align: center; }
.col-mistake { max-width: 180px; }
.col-notes { min-width: 140px; }
.col-actions { width: 40px; text-align: center; }

.cell-truncate {
    max-width: 260px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    cursor: pointer;
}
.cell-truncate:hover { color: var(--accent); }

.empty-msg {
    text-align: center;
    color: var(--text-muted);
    padding: 40px 12px;
}

/* ── Badges ── */
.badge {
    display: inline-block;
    padding: 2px 10px;
    border-radius: 12px;
    font-size: 0.75rem;
    font-weight: 600;
    cursor: pointer;
    user-select: none;
    transition: opacity 0.15s;
}
.badge:hover { opacity: 0.8; }

.badge-good { background: var(--good-bg); color: var(--good); }
.badge-bad { background: var(--bad-bg); color: var(--bad); }

.notes-input {
    width: 100%;
    background: transparent;
    border: 1px solid transparent;
    color: var(--text);
    font-size: 0.82rem;
    padding: 4px 6px;
    border-radius: 4px;
    font-family: inherit;
}
.notes-input:focus {
    background: var(--surface-2);
    border-color: var(--accent);
    outline: none;
}
.notes-input::placeholder {
    color: var(--text-muted);
    opacity: 0.5;
}

/* ── Delete button ── */
.btn-delete {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 1rem;
    padding: 2px 6px;
    cursor: pointer;
}
.btn-delete:hover { color: var(--bad); }

/* ── Modal ── */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
}
.modal {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    width: 90%;
    max-width: 700px;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
}
.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
}
.modal-header h3 { font-size: 0.95rem; }
.modal-header button {
    background: none;
    border: none;
    font-size: 1.3rem;
    color: var(--text-muted);
    cursor: pointer;
}
.modal-body {
    padding: 20px;
    overflow-y: auto;
    white-space: pre-wrap;
    word-break: break-word;
    font-size: 0.82rem;
    line-height: 1.6;
    font-family: inherit;
    width: 100%;
    min-height: 300px;
    resize: vertical;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-sizing: border-box;
    background: var(--bg);
    color: var(--text);
}
.modal-footer {
    padding: 12px 20px;
    display: flex;
    justify-content: flex-end;
    border-top: 1px solid var(--border);
}

/* ── QA Tester page ── */
.qa-form {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: 16px;
}
.form-stack {
    display: flex;
    flex-direction: column;
    gap: 12px;
}
.form-stack label {
    display: flex;
    flex-direction: column;
    font-size: 0.8rem;
    color: var(--text-muted);
    gap: 4px;
}
.form-row-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}
.form-row-3 {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 12px;
}
.form-row-3 label {
    display: flex;
    flex-direction: column;
    font-size: 0.8rem;
    color: var(--text-muted);
    gap: 4px;
}
.text-muted { color: var(--text-muted); }

.qa-results {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: 16px;
}
.qa-results h3 { font-size: 0.95rem; margin-bottom: 12px; }

.qa-loading {
    color: var(--text-muted);
    font-size: 0.85rem;
    padding: 12px 0;
}

.mistake-list {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.mistake-item {
    background: var(--bad-bg);
    color: var(--bad);
    border: 1px solid rgba(248, 113, 113, 0.25);
    border-radius: var(--radius);
    padding: 10px 14px;
    font-size: 0.85rem;
    line-height: 1.5;
}

.qa-success {
    background: var(--good-bg);
    color: var(--good);
    border: 1px solid rgba(52, 211, 153, 0.25);
    border-radius: var(--radius);
    padding: 12px 16px;
    font-size: 0.85rem;
    font-weight: 500;
}

.qa-error {
    background: var(--bad-bg);
    color: var(--bad);
    border: 1px solid rgba(248, 113, 113, 0.25);
    border-radius: var(--radius);
    padding: 12px 16px;
    font-size: 0.85rem;
}

.add-to-eval {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: 16px;
}
.add-to-eval h3 { font-size: 0.95rem; margin-bottom: 12px; }

/* ── Section divider ── */
.section-divider {
    border: none;
    border-top: 1px solid var(--border);
    margin: 24px 0;
}

/* ── Eval runner ── */
.eval-runner {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: 16px;
}
.eval-runner h3 { font-size: 0.95rem; margin-bottom: 4px; }
.eval-runner .form-row-2 { margin-bottom: 0; }
.eval-runner label {
    display: flex;
    flex-direction: column;
    font-size: 0.8rem;
    color: var(--text-muted);
    gap: 4px;
}

.eval-run-results {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: 16px;
}
.eval-run-results h3 { font-size: 0.95rem; margin-bottom: 12px; }

/* ── Stats grid ── */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 12px;
}
.stat-card {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px;
    text-align: center;
}
.stat-value {
    font-size: 1.4rem;
    font-weight: 700;
    color: var(--text);
}
.stat-label {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-top: 2px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}
.stat-desc {
    font-size: 0.72rem;
    color: var(--text-muted);
    margin-top: 2px;
    font-style: italic;
}
.stat-good .stat-value { color: var(--good); }
.stat-tp { border-color: rgba(52, 211, 153, 0.3); }
.stat-tp .stat-value { color: var(--good); }
.stat-tn { border-color: rgba(52, 211, 153, 0.3); }
.stat-tn .stat-value { color: var(--good); }
.stat-fn { border-color: rgba(248, 113, 113, 0.3); }
.stat-fn .stat-value { color: var(--bad); }
.stat-fp { border-color: rgba(248, 113, 113, 0.3); }
.stat-fp .stat-value { color: var(--bad); }
.stats-timestamp {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-bottom: 8px;
}

/* ── Stats dashboard ── */
.stats-category-grid {
    display: flex;
    flex-direction: column;
    gap: 20px;
}
.stats-category-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
}
.stats-category-card h3 {
    font-size: 0.95rem;
    margin-bottom: 12px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.stats-category-card h3 span {
    font-size: 0.75rem;
    color: var(--text-muted);
    font-weight: 400;
}
.stats-empty {
    text-align: center;
    color: var(--text-muted);
    padding: 60px 20px;
    font-size: 0.9rem;
}
.stats-controls {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 20px;
}

/* ── Breakdown panel ── */
.breakdown-panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: 24px;
}
.breakdown-panel h3 {
    font-size: 0.95rem;
    margin-bottom: 14px;
}
.breakdown-tables {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 16px;
}
.breakdown-table {
    font-size: 0.8rem;
    border-collapse: collapse;
}
.breakdown-table th {
    background: var(--surface-2);
    color: var(--text-muted);
    font-weight: 600;
    text-align: left;
    padding: 8px 10px;
    border-bottom: 1px solid var(--border);
    font-size: 0.78rem;
    text-transform: uppercase;
    letter-spacing: 0.4px;
}
.breakdown-table td {
    padding: 6px 10px;
    border-bottom: 1px solid var(--border);
    vertical-align: top;
}
.tier-label {
    white-space: nowrap;
    font-weight: 600;
    font-size: 0.78rem;
    width: 72px;
}
.tier-count {
    display: inline-block;
    background: var(--surface-2);
    border-radius: 10px;
    font-size: 0.7rem;
    padding: 0 6px;
    margin-left: 4px;
    font-weight: 500;
    color: var(--text-muted);
}
.tier-great { color: var(--good); }
.tier-good { color: var(--warn); }
.tier-low { color: var(--bad); }
.tier-cats {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
}
.tier-cat {
    display: inline-block;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 2px 8px;
    font-size: 0.73rem;
    color: var(--text);
    line-height: 1.4;
}
.tier-cat em {
    font-style: normal;
    color: var(--text-muted);
    margin-left: 3px;
}

/* ── Per-entry breakdown ── */
.eval-entry-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.eval-entry-card {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 10px 14px;
    font-size: 0.82rem;
}
.eval-entry-clickable {
    cursor: pointer;
    transition: border-color 0.15s, background 0.15s;
}
.eval-entry-clickable:hover {
    border-color: var(--accent);
}
.eval-entry-wrong {
    border-color: rgba(248, 113, 113, 0.3);
    background: rgba(248, 113, 113, 0.04);
}
.eval-entry-correct {
    border-color: rgba(52, 211, 153, 0.15);
    background: rgba(52, 211, 153, 0.03);
}
.eval-entry-header {
    display: flex;
    align-items: center;
    gap: 8px;
}
.eval-entry-title {
    font-weight: 500;
    color: var(--text);
}
.entry-mistake-tag {
    font-size: 0.72rem;
    color: var(--text-muted);
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 1px 8px;
}
.eval-entry-verdict {
    color: var(--text-muted);
    font-size: 0.78rem;
    margin-left: auto;
    white-space: nowrap;
}
.eval-entry-mistakes {
    margin-top: 6px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}
.mistake-item-sm {
    color: var(--bad);
    font-size: 0.78rem;
    padding: 4px 8px;
    background: var(--bad-bg);
    border-radius: 4px;
}

/* ── Detail modal ── */
.modal-wide {
    max-width: 900px;
}
.modal-wide .modal-body {
    white-space: normal;
}
.detail-meta {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
}
.detail-section {
    margin-bottom: 16px;
}
.detail-label {
    font-size: 0.75rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 6px;
}
.detail-pre {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 12px;
    font-size: 0.82rem;
    line-height: 1.6;
    white-space: pre-wrap;
    word-break: break-word;
    max-height: 300px;
    overflow-y: auto;
    font-family: inherit;
    color: var(--text);
    margin: 0;
}
.detail-textarea {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 12px;
    font-size: 0.82rem;
    line-height: 1.6;
    white-space: pre-wrap;
    word-break: break-word;
    min-height: 120px;
    max-height: 300px;
    overflow-y: auto;
    font-family: inherit;
    color: var(--text);
    margin: 0;
    width: 100%;
    resize: vertical;
}
.detail-textarea:focus {
    outline: none;
    border-color: var(--accent);
}
.detail-edit-row {
    display: flex;
    gap: 12px;
    align-items: flex-end;
}
.detail-inline-label {
    display: flex;
    flex-direction: column;
    font-size: 0.75rem;
    color: var(--text-muted);
    gap: 4px;
}
.detail-inline-label select,
.detail-inline-label input {
    font-size: 0.82rem;
}
