:root {
    --bg: #f3f6fa;
    --surface: #ffffff;
    --border: #d6dde6;
    --text: #0e1f3a;
    --muted: #5b6a82;
    --primary: #1a3a6b;
    --primary-hover: #0f2750;
    --primary-soft: rgba(26, 58, 107, 0.08);
    --primary-ring: rgba(26, 58, 107, 0.20);
    --error: #b91c1c;
    --success: #047857;
    --warn: #b45309;
    --radius: 10px;
    --shadow: 0 1px 3px rgba(14, 31, 58, 0.06), 0 4px 12px rgba(14, 31, 58, 0.05);
}

html[data-theme="dark"] {
    --bg: #0c1a32;
    --surface: #142540;
    --border: #283e64;
    --text: #dbe4f3;
    --muted: #8194b2;
    --primary: #5a82d0;
    --primary-hover: #4067b6;
    --primary-soft: rgba(90, 130, 208, 0.12);
    --primary-ring: rgba(90, 130, 208, 0.26);
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.4), 0 4px 14px rgba(0, 0, 0, 0.28);
}

@media (prefers-color-scheme: dark) {
    html:not([data-theme="light"]) {
        --bg: #0c1a32;
        --surface: #142540;
        --border: #283e64;
        --text: #dbe4f3;
        --muted: #8194b2;
        --primary: #5a82d0;
        --primary-hover: #4067b6;
        --primary-soft: rgba(90, 130, 208, 0.12);
        --primary-ring: rgba(90, 130, 208, 0.26);
        --shadow: 0 1px 3px rgba(0, 0, 0, 0.4), 0 4px 14px rgba(0, 0, 0, 0.28);
    }
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.55;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    font-feature-settings: 'cv11', 'ss01';
}

/* ----- Login ----- */
.login-body {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
}
.login-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 32px 28px;
    width: 100%;
    max-width: 400px;
}
.login-card h1 { margin: 0 0 4px 0; font-size: 22px; }
.login-card form { display: flex; flex-direction: column; gap: 12px; margin-top: 16px; }

/* ----- Topbar ----- */
.topbar {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 12px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: sticky;
    top: 0;
    z-index: 10;
}
.topbar h1 { font-size: 18px; margin: 0; font-weight: 600; }
.topbar-actions { display: flex; gap: 6px; align-items: center; }
#theme-toggle { font-size: 16px; padding: 6px 10px; }

/* ----- Layout ----- */
.container {
    max-width: 920px;
    margin: 0 auto;
    padding: 20px 16px 60px;
    display: flex;
    flex-direction: column;
    gap: 18px;
}

.settings, .input-area, .progress, .result, .history-section, .clarification {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    box-shadow: var(--shadow);
}

.history-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
}
.history-header h3 { margin: 0; font-size: 16px; }
.small-btn { padding: 4px 10px; font-size: 13px; }
.history-list {
    display: flex;
    flex-direction: column;
    gap: 6px;
    max-height: 320px;
    overflow-y: auto;
}
.history-item {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 12px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    cursor: pointer;
    transition: border-color 0.12s, background 0.12s;
}
.history-item:hover {
    border-color: var(--primary);
    background: var(--primary-soft);
}
.history-item-main {
    flex: 1;
    min-width: 0;
}
.history-item-title {
    font-weight: 600;
    font-size: 14px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.history-item-preview {
    font-size: 12px;
    color: var(--muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-top: 2px;
}
.history-item-meta {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    font-size: 11px;
    color: var(--muted);
    white-space: nowrap;
    flex-shrink: 0;
}
.history-item-scenario {
    display: inline-block;
    padding: 1px 6px;
    border-radius: 4px;
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    background: var(--border);
    color: var(--text);
}
.history-item-delete {
    color: var(--muted);
    cursor: pointer;
    padding: 0 4px;
    font-size: 14px;
}
.history-item-delete:hover { color: var(--error); }
.history-section.collapsed .history-list,
.history-section.collapsed #history-empty { display: none; }

/* Clarification panel */
.clarification {
    border-left: 4px solid var(--warn);
}
.clarification h3 { margin: 0 0 6px; font-size: 16px; }
.clarify-q {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin: 12px 0;
}
.clarify-q label { font-weight: 600; font-size: 14px; }
.clarify-q textarea { min-height: 60px; }
.clarification-actions {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
    flex-wrap: wrap;
    margin-top: 8px;
}

/* Settings checkbox row */
.settings-row-inline { flex-direction: row; align-items: center; }
.checkbox-row {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
    cursor: pointer;
    user-select: none;
}
.checkbox-row input { width: auto; }

.settings-row {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-bottom: 12px;
}
.block-label {
    font-size: 13px;
    font-weight: 600;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

/* ----- Inputs ----- */
input[type="text"],
input[type="password"],
textarea,
select {
    width: 100%;
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 10px 12px;
    font-size: 15px;
    font-family: inherit;
}
textarea { resize: vertical; min-height: 90px; }

input:focus, textarea:focus, select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-ring);
}

/* ----- Buttons ----- */
button {
    cursor: pointer;
    font-family: inherit;
    font-size: 15px;
    padding: 10px 16px;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    transition: background 0.12s, border-color 0.12s, transform 0.05s;
}
button:hover { background: var(--bg); }
button:active { transform: scale(0.98); }
button.primary {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}
button.primary:hover { background: var(--primary-hover); }
button.primary:disabled { background: var(--muted); cursor: not-allowed; border-color: var(--muted); }
button.ghost { background: transparent; border-color: transparent; color: var(--muted); }
button.ghost:hover { color: var(--text); background: var(--bg); }

/* ----- Chip group (scenario) ----- */
.chip-group { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
    display: inline-flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 2px;
    padding: 10px 14px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg);
    cursor: pointer;
    transition: border-color 0.12s, background 0.12s;
    min-width: 120px;
}
.chip:hover { border-color: var(--primary); }
.chip.active { border-color: var(--primary); background: var(--primary-soft); }
.chip-name { font-weight: 600; font-size: 14px; }
.chip-cost { font-size: 12px; color: var(--muted); }

.chip-stat {
    margin-left: auto;
    cursor: default;
    justify-content: center;
    align-items: center;
    flex-direction: row;
    min-width: 110px;
}
.chip-stat:hover { border-color: var(--border); background: var(--bg); }
.chip-stat .chip-cost {
    color: var(--text);
    font-weight: 700;
    font-size: 20px;
    letter-spacing: 0.01em;
}

/* ----- Input area controls ----- */
.input-controls {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 12px;
    align-items: center;
}
.control {
    padding: 8px 14px;
    border-radius: 8px;
    font-size: 14px;
}
.file-label { display: inline-flex; align-items: center; gap: 4px; }
#submit-btn { margin-left: auto; min-width: 130px; }

.control.recording {
    background: var(--error);
    color: white;
    border-color: var(--error);
    animation: pulse 1.2s infinite;
}
@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.7; }
}

/* ----- Progress ----- */
.progress h3 { margin: 0 0 12px; font-size: 16px; }
.event {
    padding: 8px 10px;
    border-left: 3px solid var(--border);
    margin-bottom: 6px;
    font-size: 14px;
    background: var(--bg);
    border-radius: 0 6px 6px 0;
}
.event.round { border-left-color: var(--primary); }
.event.arbiter { border-left-color: var(--warn); }
.event.consensus { border-left-color: var(--success); font-weight: 600; }
.event-time { color: var(--muted); font-size: 12px; margin-right: 6px; }

.model-lines {
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin-top: 6px;
    padding-left: 4px;
}
.model-line {
    font-size: 13px;
    padding: 3px 0;
}
.model-line.ok { color: var(--success); }
.model-line.fail { color: var(--error); }
.model-line b { color: var(--text); }

/* ----- Result ----- */
.result-header {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 12px;
}
.result-header h2 { margin: 0; font-size: 20px; }

.final-answer {
    background: var(--bg);
    border: 2px solid var(--primary);
    border-radius: 8px;
    padding: 18px;
    white-space: pre-wrap;
    line-height: 1.6;
    font-size: 15px;
}
.final-answer.empty {
    border-color: var(--warn);
    color: var(--muted);
    font-style: italic;
}

.result-actions {
    display: flex;
    gap: 8px;
    margin-top: 14px;
    flex-wrap: wrap;
}
.result-action-btn {
    font-size: 14px;
    padding: 8px 14px;
}

.follow-up-banner {
    padding: 10px 12px;
    margin-bottom: 10px;
    background: var(--primary-soft);
    border: 1px solid var(--primary);
    border-radius: 8px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    font-size: 13px;
}
.follow-up-text {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.follow-up-text span {
    color: var(--muted);
    font-style: italic;
}

.rounds { margin-top: 14px; }
.rounds summary {
    cursor: pointer;
    padding: 8px 0;
    color: var(--muted);
    font-size: 14px;
}
.round-block {
    margin-top: 10px;
    padding: 10px;
    background: var(--bg);
    border-radius: 8px;
}
.round-block h4 { margin: 0 0 8px; font-size: 14px; color: var(--muted); }
.model-response {
    border-top: 1px solid var(--border);
    padding: 8px 0;
    font-size: 14px;
}
.model-response:first-of-type { border-top: none; }
.model-name { font-weight: 600; font-size: 13px; margin-bottom: 4px; }
.model-content { white-space: pre-wrap; color: var(--text); }
.model-content.error { color: var(--error); }
.model-meta { font-size: 12px; color: var(--muted); margin-top: 4px; }

/* ----- Utilities ----- */
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--error); }
.hidden { display: none !important; }

@media (max-width: 600px) {
    .container { padding: 12px 10px 60px; }
    .topbar { padding: 10px 12px; }

    /* Scenarios — 2 in a row */
    #scenario-options .chip:not(.chip-stat) {
        flex: 1 1 calc(50% - 4px);
        min-width: 0;
    }
    /* Stat chip — same 50% width as scenarios, sits next to Премиум */
    .chip-stat {
        flex: 1 1 calc(50% - 4px);
        margin-left: 0;
        min-width: 0;
    }
    .chip-stat .chip-cost { font-size: 18px; }

    /* Length modes — one per row so descriptions don't wrap awkwardly */
    #length-options .chip {
        flex: 1 1 100%;
        min-width: 0;
    }

    #submit-btn { width: 100%; margin-left: 0; }
    .input-controls { flex-direction: column; align-items: stretch; }
    .input-controls .control { width: 100%; justify-content: center; }

    /* Topbar: theme toggle slightly tighter */
    #theme-toggle { font-size: 14px; padding: 5px 8px; }
}
