:root {
    --bg: #0f172a;
    --panel: #1e293b;
    --panel-2: #243044;
    --border: #334155;
    --text: #e2e8f0;
    --muted: #94a3b8;
    --accent: #fbbf24;
    --bad: #f87171;
}
@media (prefers-color-scheme: light) {
    :root {
        --bg: #f8fafc;
        --panel: #ffffff;
        --panel-2: #f1f5f9;
        --border: #e2e8f0;
        --text: #0f172a;
        --muted: #64748b;
        --accent: #b45309;
        --bad: #dc2626;
    }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.55;
    -webkit-font-smoothing: antialiased;
}

.topbar {
    background: var(--panel);
    border-bottom: 1px solid var(--border);
    padding: 0.85rem 1.3rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 0.7rem;
}
.brand {
    color: var(--accent);
    font-weight: 700;
    font-size: 1.15rem;
    text-decoration: none;
    letter-spacing: 0.02em;
}
.logout-form {
    display: flex;
    align-items: center;
    gap: 0.8rem;
    margin: 0;
}
.user-info { color: var(--muted); font-size: 0.9rem; }

main {
    max-width: 960px;
    margin: 2rem auto;
    padding: 0 1.3rem;
}

.card {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 1.6rem 1.7rem;
}
.card-narrow {
    max-width: 380px;
    margin: 3rem auto 0;
}
.card h1 {
    margin: 0 0 0.6rem;
    font-size: 1.55rem;
    letter-spacing: -0.01em;
}
.muted { color: var(--muted); }

.error {
    background: color-mix(in srgb, var(--bad) 18%, var(--panel));
    border: 1px solid var(--bad);
    color: var(--bad);
    padding: 0.6rem 0.85rem;
    border-radius: 9px;
    margin-bottom: 1rem;
    font-size: 0.92rem;
}

.login-form {
    display: flex;
    flex-direction: column;
    gap: 0.9rem;
    margin-top: 0.8rem;
}
.login-form label {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
    font-size: 0.85rem;
    color: var(--muted);
}
.login-form input {
    appearance: none;
    border: 1px solid var(--border);
    background: var(--panel-2);
    color: var(--text);
    padding: 0.6rem 0.75rem;
    border-radius: 9px;
    font: inherit;
    font-size: 1rem;
}
.login-form input:focus {
    outline: none;
    border-color: var(--accent);
}

.btn-primary, .btn-secondary {
    appearance: none;
    cursor: pointer;
    font: inherit;
    font-weight: 600;
    border-radius: 9px;
    padding: 0.55rem 1rem;
}
.btn-primary {
    border: 1px solid var(--accent);
    background: var(--accent);
    color: #1a1a1a;
    font-size: 0.95rem;
    margin-top: 0.4rem;
}
.btn-primary:hover { filter: brightness(1.08); }
.btn-secondary {
    border: 1px solid var(--border);
    background: var(--panel-2);
    color: var(--text);
    font-size: 0.85rem;
    padding: 0.42rem 0.9rem;
}
.btn-secondary:hover { border-color: var(--accent); }

/* ----- Phase 2: Navigation + Quellen-Tabelle ----- */

.mainnav {
    display: flex;
    gap: 0.4rem;
    flex: 1;
    margin-left: 1.2rem;
}
.mainnav a {
    color: var(--muted);
    text-decoration: none;
    padding: 0.35rem 0.7rem;
    border-radius: 7px;
    font-size: 0.92rem;
}
.mainnav a:hover {
    color: var(--text);
    background: var(--panel-2);
}

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    margin-bottom: 1rem;
}
.card-header h1 { margin: 0; font-size: 1.4rem; }
.btn-link { text-decoration: none; display: inline-block; }

.card-narrow-wide { max-width: 560px; margin: 1.5rem auto 0; }

.table-wrap { overflow-x: auto; }

.sources-table {
    width: 100%;
    table-layout: fixed;
    border-collapse: collapse;
    font-size: 0.93rem;
}
.sources-table th:nth-child(1), .sources-table td:nth-child(1) { width: 4.5rem; }  /* Typ */
.sources-table th:nth-child(2), .sources-table td:nth-child(2) { overflow-wrap: anywhere; }  /* Titel / Quelle */
.sources-table th:nth-child(3), .sources-table td:nth-child(3) { width: 6rem; }    /* Status */
.sources-table th:nth-child(4), .sources-table td:nth-child(4) { width: 4.5rem; }  /* Seiten */
.sources-table th:nth-child(5), .sources-table td:nth-child(5) { width: 4.5rem; }  /* Chunks */
.sources-table th:nth-child(6), .sources-table td:nth-child(6) { width: 7rem; }    /* Hinzugefügt */
.sources-table th:nth-child(7), .sources-table td:nth-child(7) { width: 6rem; }    /* Löschen */
.sources-table th, .sources-table td {
    text-align: left;
    padding: 0.6rem 0.7rem;
    border-bottom: 1px solid var(--border);
    vertical-align: top;
}
.sources-table th {
    color: var(--muted);
    font-weight: 600;
    font-size: 0.82rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}
.sources-table td.num, .sources-table th.num { text-align: right; font-variant-numeric: tabular-nums; }
.sources-table tr:last-child td { border-bottom: none; }

.status {
    display: inline-block;
    padding: 0.18rem 0.55rem;
    border-radius: 999px;
    font-size: 0.78rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    border: 1px solid transparent;
}
.status-pending    { background: color-mix(in srgb, var(--muted) 18%, transparent); color: var(--muted);  border-color: var(--border); }
.status-processing { background: color-mix(in srgb, var(--accent) 18%, transparent); color: var(--accent); border-color: var(--accent); }
.status-done       { background: color-mix(in srgb, #22c55e 18%, transparent);       color: #22c55e;       border-color: #22c55e; }
.status-error      { background: color-mix(in srgb, var(--bad) 18%, transparent);    color: var(--bad);    border-color: var(--bad); }

.row-error {
    color: var(--bad);
    font-size: 0.82rem;
    margin-top: 0.25rem;
    max-width: 32ch;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.row-url {
    font-size: 0.82rem;
    margin-top: 0.2rem;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.row-url a {
    color: var(--muted);
    text-decoration: none;
}
.row-url a:hover { color: var(--accent); text-decoration: underline; }

.kind-badge {
    display: inline-block;
    padding: 0.18rem 0.5rem;
    border-radius: 6px;
    font-size: 0.78rem;
    font-weight: 600;
    border: 1px solid var(--border);
    background: var(--panel-2);
    white-space: nowrap;
}
.kind-book { color: var(--accent); }
.kind-web { color: #38bdf8; }

.btn-danger {
    appearance: none;
    cursor: pointer;
    font: inherit;
    font-size: 0.82rem;
    font-weight: 600;
    border-radius: 7px;
    padding: 0.35rem 0.7rem;
    background: transparent;
    color: var(--bad);
    border: 1px solid color-mix(in srgb, var(--bad) 40%, var(--border));
}
.btn-danger:hover {
    background: color-mix(in srgb, var(--bad) 15%, transparent);
}

/* ----- Phase 4: Frage & Antwort ----- */

.ask-form {
    display: flex;
    flex-direction: column;
    gap: 0.7rem;
    margin-top: 1rem;
}
.ask-model-row {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    flex-wrap: wrap;
    padding: 0.55rem 0.8rem;
    background: var(--panel-2);
    border: 1px solid var(--border);
    border-radius: 9px;
}
.ask-model-label {
    flex: 0 0 auto;
    font-weight: 600;
    font-size: 0.9rem;
    color: var(--text);
    letter-spacing: 0.01em;
}
/* Modell-Bild-Picker: Trigger-Knopf zeigt die aktuelle Auswahl kompakt an. */
.model-trigger {
    flex: 0 1 auto;
    min-width: 13em;
    display: inline-flex;
    align-items: center;
    gap: 0.6rem;
    background-color: var(--bg);
    color: var(--text);
    border: 1px solid var(--border);
    padding: 0.35rem 0.55rem;
    border-radius: 8px;
    font: inherit;
    cursor: pointer;
    text-align: left;
    transition: border-color 0.15s ease;
}
.model-trigger:hover,
.model-trigger:focus-visible { outline: none; border-color: var(--accent); }
.model-trigger-img,
.model-trigger-icon {
    width: 46px;
    height: 31px;
    flex: 0 0 auto;
    border-radius: 4px;
    background: var(--panel-2);
}
.model-trigger-img { object-fit: cover; }
.model-trigger-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: var(--muted);
    font-size: 1.1rem;
}
.model-trigger-text { display: flex; flex-direction: column; line-height: 1.15; min-width: 0; }
.model-trigger-name { font-weight: 600; font-size: 0.95rem; }
.model-trigger-years { font-size: 0.78rem; }
.model-trigger-caret { margin-left: auto; padding-left: 0.4rem; color: var(--muted); font-size: 0.8rem; }

/* Einklappbares Bild-Raster zur Modellwahl. */
.model-grid {
    margin: 0.6rem 0 0;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 0.6rem;
    max-height: 58vh;
    overflow-y: auto;
    padding: 0.45rem;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--panel-2);
}
.model-card {
    position: relative;
    display: flex;
    flex-direction: column;
    gap: 0.1rem;
    padding: 0.35rem;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 9px;
    color: var(--text);
    font: inherit;
    cursor: pointer;
    text-align: left;
    transition: border-color 0.12s ease;
}
.model-card:hover { border-color: var(--accent); }
.model-card:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.model-card[aria-selected="true"] {
    border-color: var(--accent);
    box-shadow: inset 0 0 0 1px var(--accent);
}
.model-card-img {
    width: 100%;
    aspect-ratio: 3 / 2;
    object-fit: cover;
    border-radius: 6px;
    background: var(--panel-2);
    display: block;
}
.model-card-all {
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--muted);
    font-size: 1.8rem;
}
.model-card-name { font-weight: 600; font-size: 0.9rem; margin-top: 0.2rem; }
.model-card-years { font-size: 0.78rem; }
.model-card-check {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    width: 1.4rem;
    height: 1.4rem;
    display: none;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    background: var(--accent);
    color: var(--bg);
    font-size: 0.85rem;
    font-weight: 700;
}
.model-card[aria-selected="true"] .model-card-check { display: flex; }
.ask-model-hint {
    flex: 1 1 14em;
    font-size: 0.85rem;
    line-height: 1.35;
}
.ask-input-row {
    display: flex;
    gap: 0.7rem;
    align-items: stretch;
}
.ask-form textarea {
    flex: 1;
    appearance: none;
    border: 1px solid var(--border);
    background: var(--panel-2);
    color: var(--text);
    padding: 0.65rem 0.8rem;
    border-radius: 9px;
    font: inherit;
    font-size: 1rem;
    line-height: 1.45;
    resize: vertical;
    min-height: 2.6rem;
}
.ask-form textarea:focus {
    outline: none;
    border-color: var(--accent);
}
.ask-form .btn-primary {
    margin-top: 0;
    align-self: stretch;
    padding: 0 1.1rem;
}

.ask-status {
    margin-top: 0.9rem;
    font-size: 0.9rem;
    font-style: italic;
}

.ask-cost {
    margin: 0.5rem 0 0;
    font-size: 0.82rem;
}
.ask-cost #ask-cost-value {
    color: var(--accent);
    font-weight: 600;
}

.ask-answer {
    margin-top: 1.4rem;
    background: var(--panel-2);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 1rem 1.1rem;
}
.ask-answer h2 {
    margin: 0 0 0.5rem;
    font-size: 1rem;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    font-weight: 600;
}
.answer-text {
    white-space: pre-wrap;
    font-size: 1rem;
    line-height: 1.6;
    word-wrap: break-word;
}

/* Zweitantwort "Was würde Claude sagen" — vom strikt quellenbasierten Pfad
   abgesetzt durch einen Akzent-Rand, da sie auch Allgemeinwissen nutzen darf. */
.ask-claude {
    border-left: 3px solid var(--accent);
}
.ask-claude h2 {
    color: var(--accent);
}

.ask-sources {
    margin-top: 1.2rem;
}
.ask-sources h2 {
    margin: 0 0 0.6rem;
    font-size: 1rem;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    font-weight: 600;
}
.sources-ol {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    flex-direction: column;
    gap: 0.55rem;
}
.source-item {
    display: grid;
    grid-template-columns: auto 1fr;
    grid-template-rows: auto auto;
    gap: 0.15rem 0.7rem;
    background: var(--panel-2);
    border: 1px solid var(--border);
    border-radius: 9px;
    padding: 0.55rem 0.8rem;
    font-size: 0.92rem;
}
.source-tag {
    grid-row: 1 / span 2;
    align-self: start;
    display: inline-block;
    background: var(--accent);
    color: #1a1a1a;
    font-weight: 700;
    border-radius: 6px;
    padding: 0.15rem 0.5rem;
    font-size: 0.78rem;
    letter-spacing: 0.04em;
    min-width: 2.4em;
    text-align: center;
}
.source-meta { font-size: 0.93rem; }
.source-page { color: var(--muted); }
.source-sim { font-size: 0.82rem; margin-left: 0.3rem; }
.source-excerpt {
    grid-column: 2;
    font-size: 0.85rem;
    line-height: 1.45;
    max-height: 4.4em;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
}
.source-actions {
    grid-column: 2;
    display: flex;
    gap: 0.45rem;
    margin-top: 0.45rem;
    flex-wrap: wrap;
}
.btn-source {
    font-size: 0.78rem;
    padding: 0.28rem 0.65rem;
    text-decoration: none;
    line-height: 1.3;
}
.source-preview {
    grid-column: 1 / -1;
    margin-top: 0.6rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    overflow: hidden;
    background: var(--bg);
}
.source-preview iframe {
    width: 100%;
    height: 540px;
    border: 0;
    display: block;
    background: #fff;
}

/* ----- Anleitung-Seite ----- */

.help-card {
    line-height: 1.65;
}
.help-card h2 {
    margin: 2rem 0 0.6rem;
    padding-top: 0.6rem;
    font-size: 1.2rem;
    border-top: 1px solid var(--border);
    scroll-margin-top: 1rem;
}
.help-card h2:first-of-type {
    margin-top: 1.4rem;
}
.help-card p, .help-card ul, .help-card ol {
    margin: 0.5rem 0 0.9rem;
}
.help-card ul, .help-card ol {
    padding-left: 1.4rem;
}
.help-card li { margin-bottom: 0.3rem; }
.help-card code {
    background: var(--panel-2);
    padding: 0.05rem 0.35rem;
    border-radius: 4px;
    font-size: 0.88em;
}
.help-card kbd {
    background: var(--panel-2);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 0.05rem 0.4rem;
    font: inherit;
    font-size: 0.82em;
    box-shadow: 0 1px 0 var(--border);
}
.help-card a {
    color: var(--accent);
    text-decoration: none;
}
.help-card a:hover { text-decoration: underline; }

.help-toc {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem 0.55rem;
    background: var(--panel-2);
    border: 1px solid var(--border);
    border-radius: 9px;
    padding: 0.7rem 0.9rem;
    margin: 1rem 0 0;
    font-size: 0.88rem;
}
.help-toc a {
    color: var(--muted);
    text-decoration: none;
    padding: 0.15rem 0.45rem;
    border-radius: 5px;
}
.help-toc a:hover {
    background: var(--panel);
    color: var(--accent);
}

/* ----- Phase 3: Web-Quellen UI im Upload-Formular ----- */

.upload-section-h {
    margin: 1.4rem 0 0.4rem;
    font-size: 1.05rem;
    color: var(--text);
    font-weight: 600;
}
.upload-section-h:first-of-type { margin-top: 0.6rem; }
.upload-divider {
    border: none;
    border-top: 1px solid var(--border);
    margin: 1.8rem 0 0;
}
.muted.small { font-size: 0.85rem; margin-top: 0.3rem; }
.muted.small code {
    background: var(--panel-2);
    padding: 0.05rem 0.3rem;
    border-radius: 4px;
    font-size: 0.85em;
}

/* ============================================================
   Responsive — Tablet (<= 900 px) und Smartphone (<= 600 px)
   ============================================================ */

/* Touch-Targets ueberall mind. 40 px hoch (Apple HIG 44pt-ish).
   Greift auf jedem Geraet mit `pointer: coarse` (Finger statt Maus). */
@media (pointer: coarse) {
    .btn-primary, .btn-secondary, .btn-danger, .btn-source {
        min-height: 40px;
        padding-top: 0.5rem;
        padding-bottom: 0.5rem;
    }
    input, textarea, select {
        font-size: 16px;  /* verhindert iOS-Auto-Zoom beim Focus */
    }
}

@media (max-width: 900px) {
    main { padding: 0 0.9rem; margin: 1.2rem auto; }
    .card { padding: 1.1rem 1.2rem; border-radius: 12px; }
    .card h1 { font-size: 1.35rem; }
    .topbar { padding: 0.7rem 1rem; gap: 0.5rem 0.8rem; }
    .mainnav { margin-left: 0.4rem; gap: 0.1rem; flex-wrap: wrap; }
    .mainnav a { padding: 0.32rem 0.55rem; font-size: 0.88rem; }
}

@media (max-width: 600px) {
    main { padding: 0 0.6rem; margin: 0.8rem auto; }
    .card { padding: 0.9rem 0.95rem; }
    .card-narrow { margin-top: 1.5rem; }
    .card h1, .card-header h1 { font-size: 1.2rem; }
    .card-header { flex-wrap: wrap; gap: 0.6rem; }

    /* Topbar: Brand links, Logout rechts, Nav darunter in voller Breite */
    .topbar { padding: 0.6rem 0.8rem; }
    .brand { font-size: 1.05rem; }
    .mainnav {
        order: 3;
        flex: 0 0 100%;
        margin-left: 0;
        border-top: 1px solid var(--border);
        padding-top: 0.5rem;
        gap: 0.15rem;
    }
    .user-info { display: none; }  /* spart Platz; Login bleibt via Abmelden-Button erkennbar */

    /* Frage-Form: Button unter die Textarea */
    .ask-form { gap: 0.55rem; }
    .ask-input-row { flex-direction: column; gap: 0.55rem; }
    .ask-form .btn-primary { width: 100%; padding: 0.7rem 1rem; }
    .ask-model-row { padding: 0.5rem 0.6rem; gap: 0.5rem; }
    .model-trigger { width: 100%; min-width: 0; }
    .ask-model-hint { flex-basis: 100%; font-size: 0.8rem; }
    /* Bild-Raster auf dem Smartphone genau 2 Spalten, voll wischbar */
    .model-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 0.5rem;
        max-height: 62vh;
        -webkit-overflow-scrolling: touch;
    }

    /* Antwort + Quellen kompakter */
    .ask-answer { padding: 0.85rem 0.9rem; }
    .answer-text { font-size: 0.97rem; }
    .source-item {
        padding: 0.5rem 0.65rem;
        gap: 0.1rem 0.55rem;
    }
    .source-tag { min-width: 2em; font-size: 0.72rem; padding: 0.12rem 0.4rem; }
    .source-meta { font-size: 0.88rem; }
    .source-excerpt { font-size: 0.82rem; }
    .source-actions { gap: 0.35rem; }
    .source-preview iframe { height: 360px; }

    /* Sources-Tabelle: sekundaere Spalten ausblenden (Seiten, Chunks,
       Hinzugefuegt). Was bleibt: Typ, Titel/URL, Status, Loeschen. */
    .sources-table { font-size: 0.88rem; }
    .sources-table th, .sources-table td { padding: 0.45rem 0.4rem; }
    .sources-table th:nth-child(4),
    .sources-table td:nth-child(4),
    .sources-table th:nth-child(5),
    .sources-table td:nth-child(5),
    .sources-table th:nth-child(6),
    .sources-table td:nth-child(6) {
        display: none;
    }
    .row-url { max-width: 22ch; }

    /* Login-Karte: nutzt volle Breite statt 380 px fix */
    .card-narrow { max-width: 100%; }

    /* Upload-Form: Inputs auf voller Breite */
    .upload-section-h { font-size: 0.98rem; }

    /* Anleitung-TOC: 2-Spalten-Grid statt freier Flex */
    .help-toc { font-size: 0.85rem; padding: 0.55rem 0.7rem; }
    .help-toc a { padding: 0.2rem 0.4rem; }
    .help-card h2 { font-size: 1.08rem; margin-top: 1.4rem; }
}

@media (max-width: 380px) {
    /* Sehr schmal (z.B. iPhone SE im Portrait) — letzte Reserve */
    main { padding: 0 0.4rem; }
    .card { padding: 0.75rem 0.8rem; }
    .mainnav a { font-size: 0.84rem; padding: 0.3rem 0.45rem; }
    .source-preview iframe { height: 280px; }
}

/* ----- Rückfragen (Clarification) ----- */
.ask-clarify {
    background: color-mix(in srgb, var(--accent) 12%, var(--panel));
    border: 1px solid color-mix(in srgb, var(--accent) 45%, var(--border));
    border-radius: 10px;
    padding: 0.85rem 1rem;
    margin-bottom: 1rem;
}
.ask-clarify-msg { margin: 0 0 0.7rem; font-weight: 600; }
.ask-clarify-options {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
    margin-bottom: 0.6rem;
}
.clarify-chip { cursor: pointer; }
.ask-clarify-options:empty { margin-bottom: 0; }
.ask-clarify-form { display: flex; gap: 0.5rem; }
.ask-clarify-form input {
    flex: 1;
    border: 1px solid var(--border);
    background: var(--panel-2);
    color: var(--text);
    border-radius: 9px;
    padding: 0.45rem 0.7rem;
    font: inherit;
    font-size: 0.95rem;
}
.ask-clarify-form input:focus { outline: none; border-color: var(--accent); }
.ask-clarify-skip {
    margin-top: 0.7rem;
    background: none;
    border: none;
    color: var(--muted);
    cursor: pointer;
    text-decoration: underline;
    font: inherit;
    font-size: 0.85rem;
    padding: 0;
}
.ask-clarify-skip:hover { color: var(--text); }
@media (max-width: 640px) {
    .ask-clarify-form { flex-direction: column; }
}
