/* DCA-Planer — helles, schlichtes Dashboard, System-Fonts, keine externen Ressourcen. */

:root {
    --bg: #f4f6fa;
    --card-bg: #ffffff;
    --border: #e2e6ee;
    --border-strong: #cbd3e1;
    --text: #1b2230;
    --text-muted: #6b7688;
    --text-faint: #9aa4b5;
    --accent: #2f6df6;
    --accent-dark: #1f56d3;
    --accent-soft: #eaf1ff;
    --row-start-bg: #f7f8fb;
    --green: #1a7f47;
    --green-bg: #e6f5ec;
    --orange: #a35a06;
    --orange-bg: #fdf0dd;
    --red: #b3261e;
    --red-bg: #fceceb;
    --radius: 14px;
    --radius-sm: 8px;
    --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

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

body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--text);
    font-family: var(--font);
    font-size: 15px;
    line-height: 1.5;
    -webkit-text-size-adjust: 100%;
}

.page {
    max-width: 1220px;
    margin: 0 auto;
    padding: 28px 20px 48px;
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
}

/* ---------- Header ---------- */

.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    flex-wrap: wrap;
}

.brand {
    display: flex;
    align-items: baseline;
    gap: 10px;
}

.brand h1 {
    margin: 0;
    font-size: 1.5rem;
    font-weight: 650;
    letter-spacing: -0.01em;
}

.version {
    padding: 2px 8px;
    border-radius: 999px;
    background: var(--accent-soft);
    color: var(--accent-dark);
    font-size: 0.75rem;
    font-weight: 600;
}

.lead {
    margin: 10px 0 24px;
    max-width: 68ch;
    color: var(--text-muted);
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.save-status {
    font-size: 0.8rem;
    color: var(--text-muted);
    white-space: nowrap;
}

.save-status.is-unsaved {
    color: var(--orange);
}

.save-status.is-takenover {
    color: var(--accent-dark);
}

.hint-block {
    margin: 0 0 24px;
}

/* ---------- Karten ---------- */

.card {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: 0 1px 2px rgba(23, 33, 54, 0.04);
    padding: 20px 22px 22px;
    margin-bottom: 20px;
}

.card h2 {
    margin: 0 0 16px;
    font-size: 1rem;
    font-weight: 650;
    letter-spacing: 0.01em;
}

/* Kartenkopf mit abgeleitetem Wert rechts: in der Eingabezeile darunter stehen
   dadurch nur Felder, die der Nutzer selbst befüllt. */
.card-head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 12px 20px;
    flex-wrap: wrap;
    margin-bottom: 16px;
}

.card-head h2 {
    margin: 0;
}

.card-head-value {
    display: flex;
    align-items: baseline;
    gap: 8px;
    margin: 0;
}

.card-head-label {
    font-size: 0.78rem;
    font-weight: 550;
    color: var(--text-muted);
}

.card-head-value output {
    font-family: var(--font-mono);
    font-variant-numeric: tabular-nums;
    font-size: 0.95rem;
}

/* Richtungs-Umschalter (Long/Short) im Kartenkopf statt in der Feldzeile: fünf
   Elemente nebeneinander passen dort auf dem Desktop nicht mehr in eine Reihe.
   Bewusst in der Akzentfarbe und nicht grün/rot — diese Farben gehören in
   dieser App der Break-Even-Ampel und hätten hier eine andere Bedeutung. */
.direction {
    display: flex;
    align-items: baseline;
    gap: 8px;
    margin-right: auto;
}

.direction-segments {
    /* position: relative, damit die ausgeblendeten Radios darin ihren Bezugsrahmen
       haben — sonst liegen sie über dem ersten Label und fangen dessen Klicks ab. */
    position: relative;
    display: flex;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    overflow: hidden;
}

/* Radios bleiben fokussierbar und für Screenreader vorhanden, sichtbar sind die
   Labels (gleiche Technik wie .sr-only, aber ohne die Klasse zu überladen). */
.direction-segments input {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
}

.direction-segments label {
    padding: 4px 14px;
    font-size: 0.85rem;
    font-weight: 550;
    color: var(--text-muted);
    background: #fff;
    cursor: pointer;
    user-select: none;
}

.direction-segments label + input + label {
    border-left: 1px solid var(--border-strong);
}

.direction-segments label:hover {
    background: var(--accent-soft);
}

.direction-segments input:checked + label {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}

/* Fokus liegt auf dem unsichtbaren Radio — sichtbar wird er am Label. */
.direction-segments input:focus-visible + label {
    outline: 2px solid var(--accent);
    outline-offset: -2px;
}

/* ---------- Buttons ---------- */

.btn {
    appearance: none;
    border: 1px solid var(--border-strong);
    background: #fff;
    color: var(--text);
    font: inherit;
    font-weight: 550;
    padding: 8px 14px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background-color 0.12s ease, border-color 0.12s ease;
}

.btn:hover {
    background: #f2f4f8;
}

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

.btn-primary:hover {
    background: var(--accent-dark);
    border-color: var(--accent-dark);
}

.btn:focus-visible,
.num-input:focus-visible,
.text-input:focus-visible,
.btn-delete:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.btn:disabled {
    color: var(--text-faint);
    border-color: var(--border);
    background: #f7f8fb;
    cursor: not-allowed;
}

.btn-primary:disabled {
    background: #f7f8fb;
    border-color: var(--border);
}

.btn-icon {
    font-size: 1.05rem;
    line-height: 1;
    padding: 8px 12px;
}

.session-count {
    display: inline-block;
    min-width: 18px;
    padding: 0 5px;
    margin-left: 4px;
    border-radius: 999px;
    background: var(--accent-soft);
    color: var(--accent-dark);
    font-size: 0.75rem;
    font-weight: 650;
    text-align: center;
}

.btn:disabled .session-count {
    background: var(--border);
    color: var(--text-faint);
}

.btn-mini {
    appearance: none;
    font: inherit;
    font-size: 0.78rem;
    padding: 2px 8px;
    border: 1px solid var(--border-strong);
    border-radius: 6px;
    background: #fff;
    color: var(--text);
    cursor: pointer;
}

.btn-mini.btn-danger {
    border-color: var(--red);
    background: var(--red);
    color: #fff;
}

.btn-delete {
    appearance: none;
    border: 1px solid transparent;
    background: transparent;
    color: var(--text-faint);
    font-size: 1.1rem;
    line-height: 1;
    padding: 4px 9px;
    border-radius: var(--radius-sm);
    cursor: pointer;
}

.btn-delete:hover {
    background: var(--red-bg);
    color: var(--red);
}

/* ---------- Session-Panel ---------- */

.sessions-wrap {
    position: relative;
}

.sessions-panel {
    position: absolute;
    top: calc(100% + 6px);
    right: 0;
    z-index: 20;
    width: 320px;
    max-width: calc(100vw - 32px);
    padding: 6px;
    background: var(--card-bg);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius);
    box-shadow: 0 10px 30px rgba(23, 33, 54, 0.14);
}

.sessions-panel[hidden] {
    display: none;
}

.session-list {
    list-style: none;
    margin: 0;
    padding: 0;
    max-height: 320px;   /* ab ca. 8 Einträgen scrollbar */
    overflow-y: auto;
}

.session-item {
    position: relative;
    display: flex;
    align-items: center;
    flex-wrap: wrap;   /* die Übernahme-Rückfrage belegt eine eigene Zeile */
    gap: 4px;
    border-radius: var(--radius-sm);
}

.session-item.is-current {
    background: var(--accent-soft);
}

.session-open {
    appearance: none;
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 8px 10px;
    border: 0;
    border-radius: var(--radius-sm);
    background: transparent;
    font: inherit;
    text-align: left;
    color: var(--text);
    cursor: pointer;
}

.session-open:hover {
    background: var(--row-start-bg);
}

.session-name {
    font-weight: 600;
}

.session-meta {
    font-size: 0.76rem;
    color: var(--text-muted);
}

/* „in anderem Tab geöffnet": Punkt trägt die Aufmerksamkeit, der Text die
   Bedeutung — Farbe allein wäre mehrdeutig und für Screenreader unsichtbar. */
.session-elsewhere {
    color: var(--accent-dark);
}

.session-elsewhere[hidden] {
    display: none;
}

.session-elsewhere::before {
    content: " · ";
    color: var(--text-muted);
}

.session-dot {
    display: inline-block;
    width: 6px;
    height: 6px;
    margin-right: 5px;
    border-radius: 50%;
    background: var(--accent);
}

.session-open-confirm {
    flex: 1 0 100%;
    padding: 8px 10px 10px;
    border-top: 1px solid var(--border);
    font-size: 0.78rem;
}

.session-open-confirm[hidden] {
    display: none;
}

.session-open-confirm p {
    margin: 0 0 8px;
    color: var(--text-muted);
}

.session-open-actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 6px;
}

.btn-mini.btn-accent {
    border-color: var(--accent);
    background: var(--accent);
    color: #fff;
}

.session-delete {
    flex: 0 0 auto;
}

.session-confirm {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 6px;
    padding: 0 8px;
    border-radius: var(--radius-sm);
    background: var(--card-bg);
    font-size: 0.8rem;
}

.session-confirm[hidden] {
    display: none;
}

.session-new {
    appearance: none;
    display: block;
    width: 100%;
    margin-top: 6px;
    padding: 9px 10px;
    border: 0;
    border-top: 1px solid var(--border);
    border-radius: 0 0 var(--radius-sm) var(--radius-sm);
    background: transparent;
    font: inherit;
    font-weight: 550;
    color: var(--accent-dark);
    text-align: left;
    cursor: pointer;
}

.session-new:hover {
    background: var(--accent-soft);
}

/* ---------- Dialog und Toast ---------- */

.app-dialog {
    max-width: 420px;
    padding: 22px;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius);
    background: var(--card-bg);
    color: var(--text);
}

.app-dialog::backdrop {
    background: rgba(23, 33, 54, 0.42);
}

.dialog-message {
    margin: 0 0 18px;
}

.dialog-actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 8px;
}

.toast {
    position: fixed;
    right: 20px;
    bottom: 20px;
    z-index: 30;
    padding: 10px 16px;
    border-radius: var(--radius-sm);
    background: var(--text);
    color: #fff;
    font-size: 0.88rem;
    box-shadow: 0 8px 24px rgba(23, 33, 54, 0.2);
    opacity: 0;
    transform: translateY(6px);
    transition: opacity 0.2s ease, transform 0.2s ease;
}

.toast.is-visible {
    opacity: 1;
    transform: translateY(0);
}

/* ---------- Eingabefelder ---------- */

.num-input {
    width: 100%;
    font-family: var(--font-mono);
    font-size: 0.95rem;
    text-align: right;
    padding: 8px 10px;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    background: #fff;
    color: var(--text);
}

.text-input {
    width: 100%;
    font: inherit;
    padding: 8px 10px;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    background: #fff;
    color: var(--text);
}

.num-input::placeholder,
.text-input::placeholder {
    color: var(--text-faint);
}

.num-input.is-invalid {
    border-color: var(--red);
    background: var(--red-bg);
}

.start-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 16px;
    align-items: end;
}

/* Breiten am Inhalt ausgerichtet: Asset und Strategie sind auf 20 Zeichen
   begrenzt, die Zahlenfelder brauchen mehr Platz. */
@media (min-width: 761px) {
    .start-grid {
        grid-template-columns: 0.8fr 0.8fr 1fr 1fr;
    }
}

.field {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.field label {
    font-size: 0.8rem;
    font-weight: 550;
    color: var(--text-muted);
}

.hint {
    margin: 14px 0 0;
    padding: 9px 12px;
    border-radius: var(--radius-sm);
    background: var(--orange-bg);
    color: var(--orange);
    font-size: 0.85rem;
}

/* ---------- Tabelle ---------- */

.table-wrap {
    position: relative;   /* Bezugsrahmen für .sr-only in der Kopfzeile: sonst
                             verlässt der absolut positionierte Span den
                             scrollbaren Bereich und verbreitert die Seite */
    overflow-x: auto;
    margin: 0 -6px 16px;
    padding: 0 6px;
}

table.stages {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
}

table.stages th,
table.stages td {
    padding: 9px 10px;
    text-align: right;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}

table.stages thead th {
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-muted);
    border-bottom: 1px solid var(--border-strong);
}

table.stages .col-stage {
    text-align: left;
    font-weight: 600;
    width: 1%;
}

table.stages .col-action {
    width: 1%;
    padding-left: 2px;
    padding-right: 2px;
}

table.stages .col-input {
    width: 13%;
    min-width: 120px;
}

.num {
    font-family: var(--font-mono);
    font-variant-numeric: tabular-nums;
    font-feature-settings: "tnum";
}

tr.row-start {
    background: var(--row-start-bg);
    color: var(--text-muted);
}

tr.row-start .col-stage {
    color: var(--text-muted);
}

tr.is-incomplete .col-num {
    color: var(--text-faint);
}

tbody tr:last-child th,
tbody tr:last-child td {
    border-bottom: none;
}

/* Break-Even-Ampel: Farbe ist Zusatzinformation, der Wert steht immer im Klartext. */
.be-badge {
    display: inline-block;
    padding: 1px 7px;
    border-radius: 6px;
    font-weight: 600;
}

.be-low {
    color: var(--green);
    background: var(--green-bg);
}

.be-mid {
    color: var(--orange);
    background: var(--orange-bg);
}

.be-high {
    color: var(--red);
    background: var(--red-bg);
}

/* ---------- Zusammenfassung ---------- */

.summary {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
    gap: 12px;
    margin: 0;
}

.summary-item {
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--row-start-bg);
}

.summary dt {
    font-size: 0.78rem;
    font-weight: 550;
    color: var(--text-muted);
}

.summary dd {
    margin: 4px 0 0;
    font-size: 1.02rem;
    font-weight: 600;
}

/* ---------- Footer ---------- */

.app-footer {
    margin-top: 8px;
    color: var(--text-faint);
    font-size: 0.8rem;
    text-align: center;
}

/* ---------- Schmale Viewports: Tabelle als Karten je Stufe ---------- */

@media (max-width: 760px) {
    .page {
        padding: 20px 14px 40px;
    }

    .app-header {
        align-items: flex-start;
    }

    .header-actions {
        width: 100%;
    }

    .save-status {
        order: 3;
        width: 100%;
    }

    /* Bezugsrahmen ist hier der Header, nicht der Button: sonst ragt das
       Panel über den rechten Bildschirmrand hinaus. */
    .app-header {
        position: relative;
    }

    .sessions-wrap {
        position: static;
    }

    .sessions-panel {
        left: 0;
        right: 0;
        width: auto;
        max-width: none;
    }

    .toast {
        right: 14px;
        left: 14px;
        text-align: center;
    }

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

    table.stages thead {
        display: none;
    }

    table.stages,
    table.stages tbody,
    table.stages tr,
    table.stages th,
    table.stages td {
        display: block;
        width: auto;
    }

    table.stages tr {
        position: relative;
        border: 1px solid var(--border);
        border-radius: var(--radius-sm);
        padding: 10px 12px;
        margin-bottom: 12px;
    }

    table.stages th,
    table.stages td {
        border-bottom: none;
        padding: 4px 0;
        white-space: normal;
    }

    table.stages .col-stage {
        font-size: 0.78rem;
        text-transform: uppercase;
        letter-spacing: 0.04em;
        color: var(--text-muted);
        margin-bottom: 4px;
    }

    table.stages tbody td[data-label] {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 12px;
    }

    table.stages tbody td[data-label]::before {
        content: attr(data-label);
        font-family: var(--font);
        font-size: 0.8rem;
        font-weight: 550;
        color: var(--text-muted);
    }

    /* Prozentbreiten des Desktop-Layouts überschreiben — sie sind
       spezifischer als das width:auto der Block-Darstellung oben. */
    table.stages .col-stage,
    table.stages .col-input,
    table.stages .col-action {
        width: auto;
        min-width: 0;
    }

    table.stages .num-input {
        flex: 0 0 55%;
        width: 55%;
    }

    table.stages .col-action {
        position: absolute;
        top: 6px;
        right: 6px;
        padding: 0;
    }

    tr.row-start .col-action {
        display: none;
    }
}
