/* Grundlayout der UI-Basis (docs/UI_STANDARD.md §3.1).
 *
 * Kaskaden-Ebene "io.base": CSS einer App ohne @layer gewinnt immer. Element-Regeln sind
 * bewusst knapp (Box-Modell, Schrift, Fokus), damit eine bestehende Seite beim Einbinden nicht
 * umkippt; alles Weitere steht in io-*-Klassen. Nur Tokens, keine festen Werte (R5).
 */
@layer io.tokens, io.base;

@layer io.base {
    /* hidden gilt immer — auch gegen display-Regeln der Komponenten (.io-placeholder, .io-row …) */
    [hidden] { display: none !important; }

    *, *::before, *::after { box-sizing: border-box; }

    body {
        margin: 0;
        background: var(--page-bg);
        color: var(--text-primary);
        font-family: var(--font-body);
        line-height: 1.45;
    }

    h1, h2, h3 { font-family: var(--font-display); line-height: 1.2; }
    a { color: var(--accent); }
    code, kbd, samp { font-family: var(--font-mono); font-size: 0.92em; }

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

    /* ---------- Layout ---------- */
    .io-container { max-width: 1200px; margin: 0 auto; padding: var(--space-l) var(--space-m); }
    .io-stack > * + * { margin-top: var(--space-m); }
    .io-row { display: flex; flex-wrap: wrap; gap: var(--space-s); align-items: center; }
    .io-grid { display: grid; gap: var(--space-m); grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }

    .io-card {
        display: block;                /* Komponenten sind eigene Elemente — von Haus aus inline */
        background: var(--card-bg);
        border: 1px solid var(--border-color);
        border-radius: var(--card-radius);
        box-shadow: var(--card-shadow);
        padding: var(--space-m);
    }

    /* ---------- Text ---------- */
    .io-muted { color: var(--text-secondary); }
    .io-positive { color: var(--positive); }
    .io-negative { color: var(--negative); }
    .io-num { font-variant-numeric: tabular-nums; text-align: right; }

    .io-note {
        padding: var(--space-s) var(--space-m);
        border-radius: var(--card-radius);
        background: var(--surface-2);
        color: var(--text-secondary);
    }
    .io-note.io-warning { background: var(--warning-bg); color: var(--warning-text); }
    .io-note.io-error { background: var(--negative-bg); color: var(--negative-text); }

    /* Lade-/Leer-/Fehlerzustand einer Komponente (IoElement.renderPlaceholder) */
    .io-placeholder {
        display: flex;
        align-items: center;
        justify-content: center;
        min-height: 3em;
        padding: var(--space-m);
        color: var(--text-muted);
        font-size: 0.9em;
    }
    .io-placeholder.io-error { color: var(--negative-text); }

    /* ---------- Bedienelemente ---------- */
    .io-btn {
        display: inline-flex;
        align-items: center;
        gap: var(--space-xs);
        padding: 6px 14px;
        border: 1px solid var(--btn-quiet-border);
        border-radius: 6px;
        background: var(--btn-quiet-bg);
        color: var(--btn-quiet-fg);
        font: inherit;
        cursor: pointer;
    }
    .io-btn:hover { border-color: var(--accent); }
    .io-btn[disabled] { opacity: 0.55; cursor: not-allowed; }
    .io-btn.io-primary {
        background: var(--btn-primary-bg);
        color: var(--btn-primary-fg);
        border-color: var(--btn-primary-border);
    }
    .io-btn.io-danger {
        background: var(--btn-danger-bg);
        color: var(--btn-danger-fg);
        border-color: transparent;
    }
    .io-btn[aria-pressed="true"] { background: var(--selected-bg); color: var(--selected-fg); }

    .io-input, .io-select {
        padding: 6px 10px;
        border: 1px solid var(--border-color);
        border-radius: 6px;
        background: var(--card-bg);
        color: var(--text-primary);
        font: inherit;
    }

    .io-card-title {
        margin: 0 0 var(--space-m);
        font-size: 0.8rem;
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: 0.04em;
        color: var(--text-secondary);
    }

    /* Schlüssel-Wert-Zeilen in Karten */
    .io-kv { display: flex; justify-content: space-between; gap: var(--space-m); padding: 5px 0; border-bottom: 1px solid var(--border-color); }
    .io-kv:last-child { border-bottom: none; }
    .io-kv > :first-child { color: var(--text-secondary); }
    .io-kv > :last-child { font-variant-numeric: tabular-nums; text-align: right; overflow-wrap: anywhere; }
    /* Langer Wert (Liste, Pfad): unter das Etikett statt rechtsbündig daneben */
    .io-kv.io-kv-block { flex-direction: column; gap: var(--space-xs); }
    .io-kv.io-kv-block > :last-child { text-align: left; }

    /* Unterblock in einer Karte (z. B. ein Cache in der Ressourcen-Karte) */
    .io-subcard { background: var(--surface-2); border-radius: 6px; padding: var(--space-s) var(--space-m); }
    .io-subcard + .io-subcard { margin-top: var(--space-s); }
    .io-subcard-title { font-weight: 600; margin-bottom: var(--space-xs); overflow-wrap: anywhere; }

    .io-pill {
        display: inline-block;
        padding: 1px 8px;
        border-radius: 10px;
        background: var(--surface-2);
        color: var(--text-secondary);
        font-size: 0.75rem;
        font-weight: 400;
        text-transform: none;
        letter-spacing: 0;
    }
    .io-pill.io-ok { background: var(--positive-bg); color: var(--positive-text); }
    .io-pill.io-warn { background: var(--warning-bg); color: var(--warning-text); }
    .io-pill.io-err { background: var(--negative-bg); color: var(--negative-text); }

    .io-dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: var(--text-muted); }
    .io-dot.io-ok { background: var(--positive); }
    .io-dot.io-err { background: var(--negative); }

    /* ---------- Seitenrahmen <io-page> ---------- */
    io-page { display: block; }
    .io-page-header {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        gap: var(--space-s) var(--space-m);
        padding: var(--space-m) var(--space-l);
        border-bottom: 1px solid var(--border-color);
    }
    .io-page-title { margin: 0; font-size: 1.15rem; font-weight: 600; }
    .io-page-nav { display: flex; flex-wrap: wrap; gap: 4px; }
    .io-page-nav a {
        padding: 5px 10px;
        border: 1px solid transparent;
        border-radius: 6px;
        color: var(--text-secondary);
        font-size: 0.9em;
        text-decoration: none;
    }
    .io-page-nav a:hover { color: var(--text-primary); border-color: var(--border-color); }
    .io-page-nav a.io-active { color: var(--text-primary); background: var(--surface-2); border-color: var(--border-color); }
    .io-page-spacer { flex: 1; }
    .io-page-status, .io-page-auto { display: inline-flex; align-items: center; gap: var(--space-s); color: var(--text-secondary); font-size: 0.9em; }
    .io-page-stamp { font-size: 0.85em; }

    /* ---------- Rückmeldungen <io-toast> ---------- */
    io-toast {
        position: fixed;
        right: var(--space-l);
        bottom: var(--space-l);
        z-index: 100;
        display: flex;
        flex-direction: column;
        align-items: flex-end;
        gap: var(--space-s);
        max-width: calc(100vw - 2 * var(--space-l));
    }
    .io-toast-msg {
        max-width: 460px;
        padding: 10px 16px;
        border: 1px solid var(--border-color);
        border-left: 3px solid var(--accent);
        border-radius: 8px;
        background: var(--card-bg);
        color: var(--text-primary);
        box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
        font-size: 0.9em;
    }
    .io-toast-msg.io-ok { border-left-color: var(--positive); }
    .io-toast-msg.io-err { border-left-color: var(--negative); }

    /* ---------- Formulare ---------- */
    textarea.io-input { width: 100%; min-height: 4.5em; resize: vertical; }
    /* Feld = Etikett über dem Eingabefeld; .io-field-wide geht über die ganze Formular-Breite */
    .io-field { display: flex; flex-direction: column; gap: var(--space-xs); min-width: 0; }
    .io-field > span:first-child { color: var(--text-secondary); font-size: 0.85em; font-weight: 600; }
    .io-field > .io-input, .io-field > .io-select { width: 100%; }
    .io-field > small { color: var(--text-muted); font-size: 0.8em; }
    .io-form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: var(--space-m); }
    .io-form-grid > .io-field-wide { grid-column: 1 / -1; }
    .io-form-actions { display: flex; gap: var(--space-s); justify-content: flex-end; margin-top: var(--space-l); }

    /* Vorschlagsliste unter einem Eingabefeld (z. B. Instrument-Suche) */
    .io-suggest { position: relative; }
    .io-suggest-list {
        position: absolute; left: 0; right: 0; top: 100%; z-index: 20;
        max-height: 280px; overflow-y: auto; margin-top: 2px;
        background: var(--card-bg); border: 1px solid var(--border-color); border-radius: 6px;
        box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
    }
    .io-suggest-item { display: block; width: 100%; padding: 8px 10px; border: 0; background: none; color: var(--text-primary); font: inherit; text-align: left; cursor: pointer; }
    .io-suggest-item:hover, .io-suggest-item[aria-selected="true"] { background: var(--surface-2); }

    /* ---------- Dialog: natives <dialog class="io-dialog"> mit showModal() ---------- */
    dialog.io-dialog {
        width: min(720px, calc(100vw - 2 * var(--space-m)));
        max-height: calc(100vh - 2 * var(--space-l));
        padding: var(--space-l);
        border: 1px solid var(--border-color);
        border-radius: var(--card-radius);
        background: var(--card-bg);
        color: var(--text-primary);
        box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
    }
    dialog.io-dialog.io-dialog-wide { width: min(980px, calc(100vw - 2 * var(--space-m))); }
    dialog.io-dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
    .io-dialog-head { display: flex; align-items: center; justify-content: space-between; gap: var(--space-m); margin-bottom: var(--space-m); }
    .io-dialog-head h3 { margin: 0; font-size: 1.1rem; }

    /* ---------- Tabelle ---------- */
    .io-table { width: 100%; border-collapse: collapse; font-size: 0.95em; }
    .io-table th, .io-table td { padding: 8px 10px; border-bottom: 1px solid var(--border-color); text-align: left; }
    .io-table thead th { background: var(--table-head-bg); color: var(--table-head-fg); font-weight: 600; }
    .io-table tbody tr:hover { background: var(--surface-2); }
}

/* ---------- App-Kopf <io-app-header> mit Navigationsmenü ----------
 * Bewusst AUSSERHALB von @layer io.base: Der Kopf steht auch auf älteren Seiten, deren CSS
 * `* { margin: 0; padding: 0 }` ohne Ebene setzt — das schlüge jede Ebenen-Regel. Ohne Ebene
 * gewinnt hier die Klassen-Spezifität gegen `*`; App-CSS kann weiterhin überschreiben. */
io-app-header { display: block; position: sticky; top: 0; z-index: 100; margin-bottom: var(--space-l); }
.io-app-header-bar {
    display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--space-m);
    padding: 18px 26px;
    background: var(--card-bg);
    border: 1px solid var(--header-border, var(--border-color));
    border-radius: var(--card-radius);
    box-shadow: var(--card-shadow);
}
.io-app-header-title { flex: 1 1 260px; min-width: 0; }
.io-app-header-title h1 { margin: 0 0 4px; font-size: clamp(1.4rem, 3vw, 2rem); color: var(--text-primary); }
.io-app-header-title p { margin: 0; color: var(--text-secondary); font-size: 0.9rem; }
.io-app-header-tools { display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: var(--space-s) 12px; }
.io-app-header-tools > .io-btn { min-width: 42px; min-height: 42px; justify-content: center; font-size: 1rem; }
.io-app-header-extra { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.io-app-header-extra:empty { display: none; }
.io-app-header-user { font-size: 0.9rem; white-space: nowrap; }
.io-app-header-tools > .io-app-header-logout { background: var(--btn-danger-bg); color: var(--btn-danger-fg); border-color: transparent; font-weight: 600; }
@media (max-width: 600px) {
    .io-app-header-bar { padding: 12px 14px; }
    .io-app-header-user > .io-muted { display: none; }
}

.io-app-header-overlay { position: fixed; inset: 0; z-index: 999; background: rgba(0, 0, 0, 0.5); }
.io-app-header-drawer {
    position: fixed; top: 0; right: 0; z-index: 1000;
    width: min(320px, 90vw); height: 100vh; overflow-y: auto;
    background: var(--card-bg); color: var(--text-primary);
    box-shadow: -2px 0 12px rgba(0, 0, 0, 0.3);
    transform: translateX(105%); transition: transform 0.25s ease-in-out;
    visibility: hidden;
}
.io-app-header-drawer.io-open { transform: none; visibility: visible; }
.io-app-header-drawer-head { display: flex; align-items: center; justify-content: space-between; padding: var(--space-m) 20px; border-bottom: 1px solid var(--border-color); }
.io-app-header-section { padding: var(--space-s) 0; border-bottom: 1px solid var(--border-color); }
.io-app-header-section-title { padding: var(--space-s) 20px; font-size: 0.75rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-secondary); }
.io-app-header-link { display: flex; align-items: center; gap: 12px; padding: 10px 20px; color: var(--text-primary); text-decoration: none; }
.io-app-header-link > span:first-child { width: 24px; text-align: center; font-size: 1.15rem; }
.io-app-header-link:hover { background: var(--surface-2); }
.io-app-header-link.io-active { background: var(--selected-bg); color: var(--selected-fg); }
.io-app-header-link.io-app-header-logout { color: var(--danger); font-weight: 600; }
.io-app-header-appearance { display: flex; flex-direction: column; gap: var(--space-s); padding: 0 20px var(--space-s); }
.io-app-header .io-btn { padding: 6px 14px; margin: 0; }
