:root {
    color-scheme: light;
    --bg: #f4f6f8;
    --card: #ffffff;
    --text: #16202a;
    --muted: #66717d;
    --line: #dfe4e8;
    --brand: #5f35d5;
    --brand-dark: #4723b0;
    --brand-soft: #f0ebff;
    --success: #157347;
    --danger: #b42318;
    --shadow: 0 8px 28px rgba(26, 33, 45, .07);
}

* { box-sizing: border-box; }
body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button, input, textarea, select { font: inherit; }
button {
    border: 0;
    border-radius: 10px;
    padding: .72rem 1.05rem;
    background: var(--brand);
    color: white;
    font-weight: 700;
    cursor: pointer;
}
button:hover { background: var(--brand-dark); }
button:disabled { opacity: .55; cursor: wait; }
button.secondary { background: #eef1f4; color: var(--text); width: 100%; }

.topbar {
    min-height: 92px;
    padding: 1rem clamp(1rem, 4vw, 3rem);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    color: white;
    background: linear-gradient(120deg, #231a43, #5f35d5 62%, #7f5af0);
}
.topbar h1 { margin: .15rem 0 0; font-size: clamp(1.5rem, 3vw, 2.25rem); }
.eyebrow { font-size: .76rem; text-transform: uppercase; letter-spacing: .14em; opacity: .8; }
.status { border: 1px solid rgba(255,255,255,.3); border-radius: 99px; padding: .5rem .75rem; font-size: .84rem; }

main { max-width: 1600px; margin: 0 auto; padding: 1rem clamp(.7rem, 2.5vw, 2rem) 2rem; }
.tabs { display: flex; gap: .4rem; margin-bottom: 1rem; overflow-x: auto; }
.tab { color: var(--muted); background: transparent; }
.tab:hover { color: var(--text); background: #e8ebef; }
.tab.active { color: var(--brand); background: var(--brand-soft); }
.panel { display: none; }
.panel.active { display: grid; }
#tab-chat { grid-template-columns: minmax(230px, 280px) 1fr; gap: 1rem; min-height: calc(100vh - 175px); }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); }
.sidebar { padding: 1rem; align-self: start; }
.sidebar h2, .form-card h2 { margin: 0 0 .5rem; }
.muted { color: var(--muted); }
.sidebar p { font-size: .88rem; line-height: 1.45; }
.model-list { display: grid; gap: .45rem; margin: 1rem 0; max-height: 46vh; overflow-y: auto; overflow-x: hidden; }
.provider-title { margin: .2rem 0; color: var(--muted); font-size: .75rem; font-weight: 800; text-transform: uppercase; letter-spacing: .08em; }
.model-option { display: flex; gap: .55rem; align-items: flex-start; margin: 0; padding: .42rem .3rem; border-radius: 7px; font-size: .84rem; cursor: pointer; }
.model-option:hover { background: var(--brand-soft); }
.model-option.unavailable { opacity: .48; text-decoration: line-through; }
.model-option input { width: 18px; min-width: 18px; height: 18px; flex: 0 0 18px; margin: .05rem 0 0; padding: 0; accent-color: var(--brand); }
.model-option span { min-width: 0; line-height: 1.35; overflow-wrap: anywhere; word-break: break-word; }
details { border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); padding: .85rem 0; margin: 1rem 0; }
summary { cursor: pointer; font-weight: 700; }
label { display: grid; gap: .38rem; margin: .8rem 0; color: #34404c; font-size: .86rem; font-weight: 650; }
input, textarea, select {
    width: 100%;
    border: 1px solid #cbd2d9;
    border-radius: 10px;
    background: white;
    color: var(--text);
    padding: .72rem .78rem;
    outline: none;
}
textarea { resize: vertical; line-height: 1.48; }
input:focus, textarea:focus, select:focus { border-color: var(--brand); box-shadow: 0 0 0 3px rgba(95,53,213,.13); }

.workspace { min-width: 0; display: grid; grid-template-rows: 1fr auto; gap: 1rem; }
.results { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 390px), 1fr)); gap: 1rem; align-content: start; }
.empty-state { place-items: center; text-align: center; color: var(--muted); min-height: 360px; }
.empty-state h2 { color: var(--text); }
.answer-card { min-width: 0; overflow: hidden; }
.answer-head { display: flex; justify-content: space-between; gap: .8rem; align-items: center; padding: .8rem 1rem; border-bottom: 1px solid var(--line); background: #fbfbfc; }
.answer-model { font-weight: 800; overflow-wrap: anywhere; }
.answer-meta { color: var(--muted); font-size: .78rem; white-space: nowrap; }
.answer-body { padding: .35rem 1rem 1rem; line-height: 1.58; overflow-wrap: anywhere; }
.answer-body pre { overflow: auto; padding: .85rem; border-radius: 9px; background: #18212b; color: #f7f7f8; }
.answer-body code { font-family: "SFMono-Regular", Consolas, monospace; font-size: .9em; }
.answer-body :not(pre) > code { background: #edf0f3; padding: .12rem .3rem; border-radius: 4px; }
.answer-body table { width: 100%; border-collapse: collapse; margin: 1rem 0; font-size: .9rem; }
.answer-body th, .answer-body td { border: 1px solid #d9dee3; padding: .55rem .65rem; text-align: left; vertical-align: top; }
.answer-body th { background: #f0edfa; }
.answer-body tr:nth-child(even) td { background: #fafbfc; }
.answer-body img { display: block; max-width: 100%; height: auto; border-radius: 10px; margin: 1rem auto; }
.error { color: var(--danger); background: #fff2f0; border-radius: 8px; padding: .75rem; }
.loading { min-height: 120px; display: grid; place-items: center; color: var(--muted); }
.composer { padding: .75rem; position: sticky; bottom: .5rem; }
.composer textarea { border: 0; padding: .5rem; box-shadow: none; }
.composer-actions { display: flex; align-items: center; justify-content: space-between; gap: .8rem; padding: .25rem; }
.composer-actions .muted { font-size: .8rem; }
.document-bar { display: flex; align-items: center; gap: .65rem; padding: .15rem .25rem .55rem; border-bottom: 1px solid var(--line); }
.document-bar .muted { font-size: .78rem; }
.upload-button { flex: 0 0 auto; padding: .5rem .7rem; background: var(--brand-soft); color: var(--brand); font-size: .82rem; }
.upload-button:hover { background: #e4dcff; color: var(--brand-dark); }
.document-list { display: flex; flex-wrap: wrap; gap: .45rem; padding: .55rem .25rem 0; }
.document-list:empty { display: none; }
.document-chip { display: inline-flex; align-items: center; gap: .35rem; max-width: 100%; padding: .38rem .5rem; border: 1px solid #d7cff6; border-radius: 8px; background: #f7f4ff; color: #46368a; font-size: .78rem; }
.document-chip-name { max-width: 240px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.document-chip button { padding: 0 .2rem; background: transparent; color: #786a9f; font-size: 1.1rem; line-height: 1; }
.document-chip button:hover { background: transparent; color: var(--danger); }
.visually-hidden { position: absolute !important; width: 1px !important; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

.single-column { width: min(1050px, 100%); margin: 0 auto; display: grid; gap: 1rem; }
.form-card, .output-card { padding: clamp(1rem, 3vw, 1.5rem); }
.form-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: .75rem; }
.output-card { min-height: 120px; }
.generated-image { display: block; max-width: 100%; max-height: 720px; margin: 0 auto; border-radius: 12px; }
.rerank-row { display: grid; grid-template-columns: 3rem 1fr auto; gap: .8rem; padding: .8rem 0; border-bottom: 1px solid var(--line); align-items: start; }
.score { color: var(--brand); font-weight: 800; }

@media (max-width: 820px) {
    #tab-chat { grid-template-columns: 1fr; }
    .sidebar { position: static; }
    .model-list { max-height: 240px; }
    .form-grid { grid-template-columns: 1fr; }
    .topbar { align-items: flex-start; }
    .status { max-width: 50%; text-align: right; }
}
