:root {
    color-scheme: light dark;
    --background: #f5f7f8;
    --surface: #ffffff;
    --surface-muted: #edf1f3;
    --text: #17212b;
    --text-muted: #5f6b76;
    --border: #d8dfe4;
    --accent: #315f8c;
    --accent-strong: #234b72;
    --success: #237a57;
    --warning: #a56a00;
    --danger: #b8324b;
    --focus: #0a78d1;
    --shadow: 0 12px 32px rgb(23 33 43 / 8%);
    --radius-large: 18px;
    --radius-medium: 12px;
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", sans-serif;
    font-size: 17px;
    line-height: 1.5;
    font-synthesis: none;
}

* { box-sizing: border-box; }

html {
    min-width: 0;
    background: var(--background);
    -webkit-text-size-adjust: 100%;
}

body {
    min-width: 0;
    min-height: 100vh;
    margin: 0;
    overflow-x: hidden;
    color: var(--text);
    background: var(--background);
}

a { color: var(--accent); }
a:hover { color: var(--accent-strong); }
img, svg { max-width: 100%; }

button, input, select, textarea {
    min-height: 44px;
    max-width: 100%;
    border: 1px solid var(--border);
    border-radius: 10px;
    color: var(--text);
    background: var(--surface);
    font: inherit;
}

button, input[type="button"], input[type="submit"] { cursor: pointer; }
button { padding: 0.55rem 0.9rem; }
input, select, textarea { padding: 0.65rem 0.75rem; }

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

[hidden] { display: none !important; }

.skip-link {
    position: fixed;
    z-index: 1000;
    top: 0.5rem;
    left: 0.5rem;
    padding: 0.65rem 0.85rem;
    border-radius: 8px;
    color: #fff;
    background: var(--accent-strong);
    transform: translateY(-150%);
}

.skip-link:focus { transform: translateY(0); }

.connection-notice {
    position: sticky;
    z-index: 200;
    top: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    padding: calc(0.65rem + env(safe-area-inset-top)) 1rem 0.65rem;
    color: #fff;
    background: #714500;
    font-size: 0.9rem;
    text-align: center;
}

.connection-notice button {
    flex: 0 0 auto;
    border-color: currentColor;
    color: inherit;
    background: transparent;
}

.app-header {
    display: flex;
    align-items: center;
    min-height: 64px;
    padding: calc(0.65rem + env(safe-area-inset-top)) max(1rem, env(safe-area-inset-right)) 0.65rem max(1rem, env(safe-area-inset-left));
    border-bottom: 1px solid var(--border);
    background: color-mix(in srgb, var(--surface) 94%, transparent);
}

.app-brand {
    display: inline-flex;
    align-items: center;
    gap: 0.55rem;
    min-height: 44px;
    color: var(--text);
    font-size: 1.08rem;
    font-weight: 700;
    text-decoration: none;
}

.app-navigation {
    position: fixed;
    z-index: 100;
    right: 0;
    bottom: 0;
    left: 0;
    padding: 0.35rem max(0.25rem, env(safe-area-inset-right)) calc(0.35rem + env(safe-area-inset-bottom)) max(0.25rem, env(safe-area-inset-left));
    border-top: 1px solid var(--border);
    background: color-mix(in srgb, var(--surface) 96%, transparent);
    box-shadow: 0 -8px 24px rgb(23 33 43 / 8%);
    backdrop-filter: blur(18px);
}

.app-navigation ul {
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    max-width: 720px;
    margin: 0 auto;
    padding: 0;
    list-style: none;
}

.app-navigation li { min-width: 0; }

.nav-link {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.12rem;
    min-width: 0;
    min-height: 52px;
    padding: 0.3rem 0.1rem;
    border-radius: 10px;
    color: var(--text-muted);
    font-size: clamp(0.55rem, 2.6vw, 0.72rem);
    font-weight: 600;
    line-height: 1.05;
    text-decoration: none;
    text-align: center;
}

.nav-link svg {
    width: 22px;
    height: 22px;
    fill: none;
    stroke: currentColor;
    stroke-linecap: round;
    stroke-linejoin: round;
    stroke-width: 1.8;
}

.nav-link span { max-width: 100%; white-space: normal; overflow-wrap: anywhere; }
.nav-link:hover { color: var(--accent); background: var(--surface-muted); }
.nav-link.active {
    color: var(--accent-strong);
    background: color-mix(in srgb, var(--accent) 12%, var(--surface));
    font-weight: 750;
}

.nav-link.active::after {
    position: absolute;
    top: 0.18rem;
    width: 1.15rem;
    height: 3px;
    border-radius: 999px;
    background: currentColor;
    content: "";
}

.app-main {
    width: min(1200px, calc(100% - 2rem));
    min-width: 0;
    margin: 1rem auto;
    padding: clamp(1rem, 3vw, 2rem);
    border: 1px solid var(--border);
    border-radius: var(--radius-large);
    background: var(--surface);
    box-shadow: var(--shadow);
}

/* Berichtsraster und Plotly-Flächen dürfen ihre Spalte nicht verbreitern. */
.app-main .report-grid,
.app-main .report-grid > *,
.app-main .quality-section,
.app-main .insulin-section,
.app-main .chart {
    min-width: 0;
    max-width: 100%;
}

.app-main .chart { width: 100%; }

body:not(.auth-page) .app-main { margin-bottom: calc(5.5rem + env(safe-area-inset-bottom)); }

h1, h2, h3 { line-height: 1.18; }
h1 { color: var(--text); font-size: clamp(1.75rem, 5vw, 2.35rem); }
h2 { font-size: clamp(1.3rem, 3vw, 1.65rem); }

.primary-button {
    border-color: var(--accent);
    color: #fff;
    background: var(--accent);
    font-weight: 700;
}

.primary-button:hover { color: #fff; background: var(--accent-strong); }

.auth-page {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    place-items: center;
    padding: max(1rem, env(safe-area-inset-top)) max(1rem, env(safe-area-inset-right)) max(1rem, env(safe-area-inset-bottom)) max(1rem, env(safe-area-inset-left));
}

.auth-page .app-main {
    width: min(420px, calc(100vw - 2rem));
    margin: auto;
    padding: clamp(1.4rem, 6vw, 2.25rem);
}

.auth-card h1 { margin: 0.7rem 0 0.4rem; text-align: left; }
.auth-card > p { color: var(--text-muted); }
.auth-brand img { display: block; }

.form-stack { display: grid; gap: 0.4rem; margin-top: 1.4rem; }
.form-stack label { margin-top: 0.55rem; font-weight: 650; }
.form-stack button { margin-top: 1rem; }

.message {
    padding: 0.8rem 0.95rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-medium);
}

.message-error { border-color: var(--danger); color: var(--danger); background: color-mix(in srgb, var(--danger) 9%, var(--surface)); }
.account-section { padding-top: 1.25rem; border-top: 1px solid var(--border); }

/* Einstellungen: Metadaten und Schlüssel bleiben auch bei großer Schrift im Rahmen. */
.settings-page { max-width: 800px; min-width: 0; margin: 0 auto; overflow-wrap: anywhere; }
.settings-section { margin-top: 1.5rem; padding: 1.25rem 0; border-top: 1px solid var(--border); }
.status-section + .status-section { margin-top: 1.75rem; }
.status-list { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.6fr); gap: 0.65rem 1.25rem; }
.status-list dt { font-weight: 650; }
.status-list dd { min-width: 0; margin: 0; }
.settings-page form, .settings-page input { min-width: 0; }
.settings-page button { min-width: 44px; white-space: normal; }
.action-link {
    display: inline-flex;
    align-items: center;
    min-height: 44px;
    min-width: 44px;
    max-width: 100%;
    padding: 0.55rem 0;
}
.receipt-list { margin: 1rem 0; padding: 0; list-style: none; }
.receipt { margin: 0.75rem 0; padding: 1rem; border: 1px solid var(--border); border-radius: var(--radius-medium); }
.receipt h4 { margin: 0 0 0.75rem; font-size: 1rem; }
.camaps-page li { margin-bottom: 0.75rem; }
.camaps-page form + form { margin-top: 0.75rem; }
.camaps-page code { white-space: normal; overflow-wrap: anywhere; }
.camaps-page input[type="file"] { width: 100%; }
.camaps-page input[type="file"]::file-selector-button {
    min-height: 44px; max-width: 100%; margin-right: 0.5rem;
    border: 1px solid var(--border); border-radius: 8px;
    color: var(--text); background: var(--surface-muted); font: inherit;
    white-space: normal;
}
.secret { padding: 1rem; border-radius: var(--radius-medium); background: color-mix(in srgb, var(--warning) 14%, var(--surface)); }
.settings-page [aria-busy="true"] { opacity: 0.75; }
@media (max-width: 600px) {
    .status-list { grid-template-columns: minmax(0, 1fr); gap: 0.25rem; }
    .status-list dd { margin-bottom: 0.75rem; }
    .settings-page button, .settings-page .action-link { width: 100%; }
}

/* Bestehende Berichte bleiben inhaltlich unverändert, folgen aber dem gemeinsamen Farbrahmen. */
.app-main .quality-item,
.app-main .date-range-container,
.app-main .hba1c-form,
.app-main .modal-content,
.app-main .period-card,
.app-main .notice {
    border-color: var(--border);
    color: var(--text);
    background-color: var(--surface-muted);
}

.app-main .error,
.app-main .notice.error {
    color: var(--danger);
    background-color: color-mix(in srgb, var(--danger) 9%, var(--surface));
}

.app-main .toggle-switch { min-height: 44px; }
.app-main .toggle-switch .toggle-slider { top: 9px; bottom: 9px; }

@media (min-width: 768px) {
    .app-header {
        flex-wrap: wrap;
        justify-content: space-between;
        gap: 1rem;
        padding-right: max(1.25rem, env(safe-area-inset-right));
        padding-left: max(1.25rem, env(safe-area-inset-left));
    }

    .app-navigation {
        position: static;
        padding: 0;
        border: 0;
        background: transparent;
        box-shadow: none;
        backdrop-filter: none;
    }

    .app-navigation ul { display: flex; flex-wrap: wrap; max-width: none; gap: 0.15rem; }
    .nav-link {
        flex-direction: row;
        gap: 0.4rem;
        min-height: 44px;
        padding: 0.55rem 0.7rem;
        font-size: 0.88rem;
        white-space: nowrap;
    }
    .nav-link svg { width: 19px; height: 19px; }
    .nav-link.active::after { top: auto; bottom: 0.18rem; }
    body:not(.auth-page) .app-main { margin-bottom: 2rem; }
}

@media (max-width: 420px) {
    .connection-notice { align-items: stretch; flex-direction: column; }
    .app-main { width: min(100% - 1rem, 1200px); padding: 0.9rem; border-radius: 14px; }
    .app-brand { font-size: 1rem; }
    .app-main .date-range-container { align-items: stretch; flex-wrap: wrap; }
    .app-main .date-picker-group { flex: 1 1 100%; min-width: 0; }
    .app-main .date-picker-group input { flex: 1 1 auto; min-width: 0; }
    .app-main .apply-dates-btn { width: 100%; }
}

@media (max-width: 350px) {
    .nav-link { font-size: 0.5rem; }
}

@media (prefers-color-scheme: dark) {
    :root {
        --background: #0f1418;
        --surface: #171d23;
        --surface-muted: #222a32;
        --text: #f3f6f8;
        --text-muted: #aeb8c2;
        --border: #35414b;
        --accent: #6ea6d7;
        --accent-strong: #9ac3e6;
        --success: #3fa77c;
        --warning: #e2a440;
        --danger: #eb6a7d;
        --focus: #73b9f3;
        --shadow: 0 14px 36px rgb(0 0 0 / 24%);
    }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        scroll-behavior: auto !important;
        transition-duration: 0.01ms !important;
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
    }
}
