:root {
    --font-primary: "Lato", "Helvetica Neue", Arial, sans-serif;
    --font-zoom: 1;
}

* {
    box-sizing: border-box;
}

html {
    background: var(--color-surface-page);
}

/* Seitenlayout und Grundfarben */
body[class*="template-"] {
    margin: 0;
    padding-top: env(safe-area-inset-top, 0px);
    font-family: var(--font-primary);
    color: var(--color-text-primary);
    background: radial-gradient(circle at top left, var(--color-surface-card) 0%, var(--color-surface-page) 55%, var(--color-surface-page) 100%);
    line-height: 1.6;
    overflow-x: hidden;

    & .wrapper {
        max-width: 900px;
        margin: 0 auto 80px;
        background: var(--color-surface-card);
        padding: 36px 42px 64px;
        border: 1px solid var(--color-border-default);
        box-shadow: 0 18px 40px var(--color-shadow-card);
        border-radius: 8px;
    }

    & .base-doc-heading + .wrapper {
        margin-top: 0;
    }

    & .base-hero {
        position: relative;
        padding: 0 0 26px;
        margin-bottom: 12px;
    }

    & .base-hero__band {
        background: var(--color-header-band-bg);
        height: 64px;
        overflow: hidden;
        border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    }

    & .base-hero__toolbar {
        max-width: 900px;
        margin: 0 auto;
        padding: 12px 42px 0;
        display: flex;
        justify-content: flex-end;
    }

    & .base-toolbar {
        display: flex;
        align-items: center;
        gap: 6px;
    }

    & .base-search-btn {
        background: var(--color-surface-card);
        color: var(--color-text-primary);
        border: 1px solid var(--color-border-default);
        padding: 0;
        width: 34px;
        height: 34px;
        border-radius: 999px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        box-shadow: 0 6px 14px rgba(0, 0, 0, 0.12);
        transition: color 0.2s ease, border-color 0.2s ease, background-color 0.2s ease;
        flex-shrink: 0;

        & svg { width: 18px; height: 18px; display: block; }
        &:hover { border-color: var(--color-brand-primary); background-color: var(--color-surface-page); }
    }

    & .base-settings-wrap {
        position: relative;
    }

    & .base-settings-btn {
        background: var(--color-surface-card);
        color: var(--color-text-primary);
        border: 1px solid var(--color-border-default);
        padding: 0;
        width: 34px;
        height: 34px;
        border-radius: 999px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        box-shadow: 0 6px 14px rgba(0, 0, 0, 0.12);
        transition: color 0.2s ease, border-color 0.2s ease, background-color 0.2s ease;

        & svg {
            width: 18px;
            height: 18px;
            display: block;
        }

        &:hover {
            border-color: var(--color-brand-primary);
            background-color: var(--color-surface-page);
        }
    }

    & .base-settings-panel {
        position: absolute;
        top: calc(100% + 8px);
        right: 0;
        background: var(--color-surface-card);
        border: 1px solid var(--color-border-default);
        border-radius: 12px;
        padding: 12px 14px;
        box-shadow: 0 8px 24px rgba(0, 0, 0, 0.14);
        display: flex;
        flex-direction: column;
        gap: 10px;
        z-index: 500;
        min-width: 170px;

        &[hidden] { display: none; }
    }

    & .base-settings-row {
        display: flex;
        align-items: center;
        gap: 8px;
    }

    & .base-settings-font-small,
    & .base-settings-font-large {
        background: none;
        border: none;
        font-weight: 700;
        color: var(--color-text-muted);
        line-height: 1;
        flex-shrink: 0;
        cursor: pointer;
        padding: 2px 4px;
        border-radius: 4px;
        transition: color 0.15s ease;

        &:hover:not(:disabled) { color: var(--color-brand-primary); }
        &:disabled { opacity: 0.3; cursor: default; }
    }

    & .base-settings-font-small { font-size: 11px; }
    & .base-settings-font-large { font-size: 17px; }

    & .base-settings-font-pct {
        font-size: 11px;
        color: var(--color-text-muted);
        text-align: center;
        margin-top: -4px;
    }

    & .base-font-slider {
        flex: 1;
        -webkit-appearance: none;
        appearance: none;
        height: 4px;
        border-radius: 2px;
        background: linear-gradient(
            to right,
            var(--color-brand-primary) 0%,
            var(--color-brand-primary) var(--slider-fill, 50%),
            var(--color-border-default) var(--slider-fill, 50%)
        );
        outline: none;
        cursor: pointer;

        &::-webkit-slider-thumb {
            -webkit-appearance: none;
            width: 16px;
            height: 16px;
            border-radius: 50%;
            background: var(--color-brand-primary);
            cursor: pointer;
        }

        &::-moz-range-thumb {
            width: 16px;
            height: 16px;
            border-radius: 50%;
            border: none;
            background: var(--color-brand-primary);
            cursor: pointer;
        }
    }

    & .base-theme-toggle {
        background: var(--color-surface-page);
        color: var(--color-text-primary);
        border: 1px solid var(--color-border-default);
        padding: 0;
        width: 30px;
        height: 30px;
        border-radius: 999px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        transition: color 0.2s ease, border-color 0.2s ease, background-color 0.2s ease;

        &:hover {
            border-color: var(--color-brand-primary);
        }
    }

    & .base-theme-toggle__icon svg {
        width: 16px;
        height: 16px;
        display: block;
    }

    & .base-hero__content {
        max-width: 900px;
        margin: -18px auto 0;
        padding: 0 42px;
        display: flex;
        align-items: flex-end;
        justify-content: space-between;
        gap: 24px;
    }

    & .base-doc-heading {
        max-width: 900px;
        margin: 0 auto 20px;
        border: 1px solid var(--color-border-default);
        border-left: 6px solid var(--color-brand-primary);
        border-radius: 8px;
        padding: 16px 20px;
        background: var(--color-surface-card);
        box-shadow: 0 12px 22px rgba(0, 0, 0, 0.08);
    }

    & .base-doc-heading__top {
        display: flex;
        align-items: center;
        flex-wrap: wrap;
        gap: 12px 20px;
    }

    & .base-doc-brand-link {
        display: flex;
        align-items: center;
        gap: 12px;
        text-decoration: none;
        border-bottom: none;
        color: inherit;
        flex-shrink: 0;

        &:hover {
            border-bottom: none;
        }
    }

    & .base-doc-back-icon {
        display: block;
        width: 20px;
        height: 20px;
        color: var(--color-text-muted);
        flex-shrink: 0;
    }

    & .base-doc-sitename {
        display: none;
    }

    & .base-doc-titles {
        flex: 1;
        min-width: 0;
    }

    & .base-doc-logo {
        width: 70px;
        height: auto;
    }

    & .base-doc-date {
        font-size: 13px;
        color: var(--color-text-muted);
        white-space: nowrap;
    }

    & .base-doc-actions {
        display: flex;
        flex-direction: column;
        align-items: flex-end;
        gap: 6px;
    }

    & .base-hero__links {
        margin-top: 0;
        display: flex;
        gap: 10px;
        justify-content: flex-end;
        flex-wrap: wrap;
        align-items: center;
    }

    /* Header-Buttons (Home/PDF/Quelle) */
    & .base-hero__links a {
        font-family: var(--font-primary);
        font-size: 12px;
        letter-spacing: 0.08em;
        text-transform: uppercase;
        text-decoration: none;
        padding: 6px 12px;
        border: 1px solid var(--color-brand-primary);
        color: #ffffff;
        background: var(--color-brand-primary);
        transition: all 0.2s ease;

        &:hover {
            background: var(--color-brand-contrast);
            border-color: var(--color-brand-contrast);
            color: #ffffff;
        }
    }

    & .base-home-button {
        margin-bottom: 0;
    }

    /* Ueberschriften: Farbe wird ueber --color-brand-primary gesteuert */
    & h1,
    & h2,
    & h3,
    & h4 {
        font-family: var(--font-primary);
        color: var(--color-brand-primary);
        margin-top: 30px;
        margin-bottom: 12px;
        line-height: 1.2;
        font-weight: 700;
    }

    & h1 {
        font-size: calc(26px * var(--font-zoom));
        border-bottom: 2px solid var(--color-brand-primary);
        padding-bottom: 8px;
    }

    & h2 { font-size: calc(22px * var(--font-zoom)); }
    & h3 { font-size: calc(19px * var(--font-zoom)); }
    & h4 { font-size: calc(17px * var(--font-zoom)); }

    & .base-doc-title {
        font-family: "Oswald", var(--font-primary);
        font-size: 30px;
        letter-spacing: 0.01em;
        color: var(--color-brand-primary);
        text-transform: none;
        font-weight: 500;
    }

    & .base-doc-subtitle {
        margin-top: 6px;
        font-size: 15px;
        color: var(--color-text-muted);
    }

    & p,
    & li {
        font-size: calc(17px * var(--font-zoom));
    }

    /* Fliesstext-Links */
    & a {
        color: var(--color-brand-primary);
        text-decoration: none;
        border-bottom: 1px solid var(--color-link-underline);

        &:hover {
            color: var(--color-brand-primary-hover);
            border-bottom-color: var(--color-brand-primary-hover);
        }
    }

    /* Tabellen: Header nutzt die Markenfarbe */
    & table {
        width: 100%;
        border-collapse: collapse;
        margin-bottom: 20px;
    }

    & th,
    & td {
        padding: 10px 12px;
        border: 1px solid var(--color-border-default);
        text-align: left;
        overflow-wrap: anywhere;
        word-break: break-word;
        font-size: calc(15px * var(--font-zoom));
    }

    & th {
        background: var(--color-brand-primary);
        color: #ffffff;
        font-family: "acumin-pro-extra-condensed", "Work Sans", var(--font-primary);
        letter-spacing: 0.04em;
    }

    & tr:nth-child(even) {
        background: rgba(var(--color-brand-rgb), 0.06);

        :root[data-theme='dark'] & {
            background: rgba(var(--color-brand-rgb), 0.08);
        }
    }

    /* Inhaltsverzeichnis (TOC) */
    & #markdown-toc {
        counter-reset: item;
        list-style-type: none;
        padding-left: 0;
        margin-bottom: 40px;

        & ul {
            counter-reset: item;
            margin: 0;
            padding-left: 0.95rem;
        }

        & li {
            counter-increment: item;
            margin: 8px 0;
            font-size: calc(16px * var(--font-zoom));
            list-style: none;
        }

        & li a::before {
            content: counters(item, ".") ". ";
            color: var(--color-brand-primary);
        }

        & a {
            text-decoration: none;
            color: var(--color-brand-primary);

            &:hover {
                color: var(--color-brand-primary-hover);
            }
        }
    }

    &.section-prefix #markdown-toc li a::before {
        content: "\00A7 " counters(item, ".") " ";
    }

    & ul,
    & ol {
        margin: 0 0 16px;
        padding-left: 1.2rem;
    }

    & ul ul,
    & ol ol,
    & ul ol,
    & ol ul {
        margin: 4px 0 10px;
        padding-left: 0.95rem;
    }

    & li {
        margin-bottom: 8px;
    }

    & img {
        max-width: 100%;
        height: auto;
    }

    & #back-to-top {
        position: fixed;
        bottom: 20px;
        right: 20px;
        background-color: var(--color-brand-primary);
        color: white;
        padding: 10px 15px;
        border-radius: 5px;
        text-decoration: none;
        font-size: 12px;
        letter-spacing: 0.08em;
        text-transform: uppercase;
        box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
        display: none;
        transition: background-color 0.3s ease;
        font-family: var(--font-primary);

        &:hover {
            background-color: var(--color-brand-contrast);
        }
    }

    /* Automatische Nummerierung fuer h1-h4 (nur wenn section_numbering gesetzt) */
    &.section-arabic,
    &.section-prefix {
        counter-reset: section;
        & h1 { counter-reset: subsection; }
        & h2 { counter-reset: subsubsection; }
        & h3 { counter-reset: subsubsubsection; }
        & :is(h1, h2, h3, h4)::before { color: var(--color-brand-primary); margin-right: 6px; }
    }

    &.section-arabic {
        & h1::before { counter-increment: section; content: counter(section) ". "; }
        & h2::before { counter-increment: subsection; content: counter(section) "." counter(subsection) " "; }
        & h3::before { counter-increment: subsubsection; content: counter(section) "." counter(subsection) "." counter(subsubsection) " "; }
        & h4::before { counter-increment: subsubsubsection; content: counter(section) "." counter(subsection) "." counter(subsubsection) "." counter(subsubsubsection) " "; }
    }

    &.section-prefix {
        & h1::before { counter-increment: section; content: "\00A7 " counter(section) " "; }
        & h2::before { counter-increment: subsection; content: "\00A7 " counter(section) "." counter(subsection) " "; }
        & h3::before { counter-increment: subsubsection; content: "\00A7 " counter(section) "." counter(subsection) "." counter(subsubsection) " "; }
        & h4::before { counter-increment: subsubsubsection; content: "\00A7 " counter(section) "." counter(subsection) "." counter(subsubsection) "." counter(subsubsubsection) " "; }
    }

    & .base-heading-link {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 26px;
        height: 26px;
        margin-left: 8px;
        border-radius: 999px;
        border: 1px solid transparent;
        color: var(--color-text-muted);
        text-decoration: none;
        opacity: 0;
        transform: translateY(-1px);
        transition: opacity 0.2s ease, color 0.2s ease, border-color 0.2s ease, background-color 0.2s ease;

        &:focus {
            opacity: 1;
        }

        &:hover {
            opacity: 1;
            color: var(--color-brand-primary);
            border-color: var(--color-brand-primary);
            background-color: rgba(var(--color-brand-rgb), 0.08);
        }

        & svg {
            width: 16px;
            height: 16px;
            display: block;
        }
    }

    & :is(h1, h2, h3, h4):hover .base-heading-link {
        opacity: 1;
    }

    /* Responsive Anpassungen */
    @media (max-width: 860px) {
        & .base-doc-heading {
            margin: 0 16px 20px;
        }

        & .base-hero__content {
            flex-direction: column;
            align-items: flex-start;
            padding: 0 24px;
        }

        & .wrapper {
            margin: 0 16px 60px;
            padding: 28px 24px 48px;
        }

        & .base-hero__toolbar {
            padding: 12px 24px 0;
        }
    }

    @media (max-width: 540px) {
        & .base-hero {
            padding: 0 0 16px;
            margin-bottom: 8px;
        }

        & .base-hero__band {
            height: 46px;
        }

        & .base-hero__toolbar {
            padding: 8px 8px 0;
        }

        & .base-hero__content {
            margin: -10px auto 0;
            padding: 0 16px;
        }

        & .base-doc-heading {
            margin: 0 0 16px;
            border-radius: 0;
            border-left-width: 4px;
            box-shadow: none;
        }

        & .wrapper {
            margin: 0 0 36px;
            padding: 16px 12px 28px;
            border-radius: 0;
            border-left: none;
            border-right: none;
            box-shadow: none;
        }

        & .base-doc-brand-link {
            flex: 1;
        }

        & .base-doc-back-icon {
            width: 36px;
            height: 36px;
        }

        /* Startseite: Logo groß, Titel inline neben Logo (kein doppelter Seitenname) */
        & .base-doc-heading--home {
            & .base-doc-sitename { display: none; }
            & .base-doc-logo { width: 70px; }
            & .base-doc-brand-link { flex: none; }
            & .base-doc-titles { order: unset; width: auto; flex: 1; }
        }

        & .base-doc-sitename {
            display: block;
            font-size: 17px;
            font-weight: 700;
            color: var(--color-text-primary);
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        & .base-doc-logo {
            width: 36px;
        }

        & .base-doc-titles {
            order: 10;
            width: 100%;
            flex: none;
        }

        & .base-doc-title {
            font-size: 24px;
        }

        & .base-doc-actions {
            order: 11;
            align-items: flex-start;
            width: 100%;
        }

        & .base-hero__links {
            justify-content: flex-start;
        }

        & p,
        & li {
            font-size: calc(16px * var(--font-zoom));
            line-height: 1.42;
            overflow-wrap: anywhere;
            word-break: break-word;
        }

        & h1,
        & h2,
        & h3,
        & h4 {
            margin-top: 22px;
            margin-bottom: 8px;
        }

        & h1 { font-size: calc(23px * var(--font-zoom)); }
        & h2 { font-size: calc(20px * var(--font-zoom)); }
        & h3 { font-size: calc(18px * var(--font-zoom)); }
        & h4 { font-size: calc(16px * var(--font-zoom)); }

        & ul,
        & ol {
            margin: 0 0 12px;
            padding-left: 1.5rem;
        }

        & ul ul,
        & ol ol,
        & ul ol,
        & ol ul {
            margin: 4px 0 8px;
            padding-left: 0.75rem;
        }

        & li {
            margin-bottom: 4px;
        }

        & #back-to-top {
            right: 10px;
            bottom: 10px;
            padding: 8px 10px;
            font-size: 11px;
        }

        & .base-settings-panel {
            min-width: 210px;
            padding: 14px 16px;
            gap: 0;
        }

        & .base-settings-row {
            padding: 8px 0;
            justify-content: space-between;
        }

        & .base-settings-font-pct {
            margin-top: 2px;
        }

        & .base-settings-label {
            font-size: 14px;
        }

        & .base-theme-toggle {
            width: 34px;
            height: 34px;
        }
    }
}

/* Safe-area Abdeckung – nur in PWA (standalone) sichtbar */
#safe-area-cover {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: env(safe-area-inset-top, 0px);
    z-index: 10001;
}

@media (display-mode: standalone) {
    #safe-area-cover {
        background: var(--color-header-band-bg);
    }
}

/* Scroll-Fortschrittsbalken */
#scroll-progress {
    position: fixed;
    top: env(safe-area-inset-top, 0px);
    left: 0;
    width: 0%;
    height: 3px;
    background: var(--color-brand-primary);
    z-index: 10000;
    transition: width 0.1s linear;
}

/* Link-kopiert-Toast */
#base-toast {
    position: fixed;
    bottom: 32px;
    left: 50%;
    transform: translateX(-50%) translateY(12px);
    background: var(--color-brand-primary);
    color: #fff;
    padding: 10px 20px;
    border-radius: 8px;
    font-size: 0.875rem;
    font-weight: 500;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.2s ease, transform 0.2s ease;
    z-index: 9999;
    white-space: nowrap;

    &.base-toast--visible {
        opacity: 1;
        transform: translateX(-50%) translateY(0);
    }
}

#staging-banner {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: #b45309;
    color: #fff;
    text-align: center;
    font-size: 0.8rem;
    font-weight: 600;
    padding: 6px 16px;
    padding-bottom: calc(6px + env(safe-area-inset-bottom, 0px));
    z-index: 9998;
    letter-spacing: 0.03em;
}

body:has(#staging-banner) #back-to-top {
    bottom: 52px;
}

/* Suche: Button im Toolbar nur auf Desktop, im Panel nur auf Mobile */
@media (max-width: 540px) {
    .base-search-btn { display: none !important; }
}
@media (min-width: 541px) {
    .base-settings-search-row { display: none !important; }
}

/* Suchleiste */
#base-search-bar[hidden] { display: none !important; }

#base-search-bar {
    position: fixed;
    bottom: calc(env(safe-area-inset-bottom, 0px) + 12px);
    left: 50%;
    transform: translateX(-50%);
    background: var(--color-surface-card);
    border: 1px solid var(--color-border-default);
    border-radius: 12px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.18);
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 6px 8px;
    z-index: 9999;
    min-width: min(480px, calc(100vw - 32px));
}

@media (max-width: 540px) {
    #base-search-bar {
        bottom: auto;
        top: calc(env(safe-area-inset-top, 0px) + 8px);
        border-radius: 10px;
    }
}

#base-search-input {
    flex: 1;
    border: none;
    outline: none;
    background: transparent;
    font-family: var(--font-primary);
    font-size: 16px; /* min 16px verhindert Auto-Zoom auf iOS */
    color: var(--color-text-primary);
    padding: 4px 8px;
    min-width: 0;

    &::placeholder { color: var(--color-text-muted); }
    &::-webkit-search-cancel-button { display: none; }
}

#base-search-count {
    font-size: 12px;
    color: var(--color-text-muted);
    white-space: nowrap;
    padding: 0 4px;
    min-width: 52px;
    text-align: center;
}

#base-search-prev,
#base-search-next,
#base-search-close {
    background: none;
    border: none;
    padding: 4px;
    width: 30px;
    height: 30px;
    border-radius: 6px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--color-text-muted);
    transition: background-color 0.15s ease, color 0.15s ease;
    flex-shrink: 0;

    & svg { width: 16px; height: 16px; display: block; }
    &:hover { background: var(--color-surface-page); color: var(--color-text-primary); }
}

#base-search-close {
    color: var(--color-text-muted);
    margin-left: 2px;
}

mark.base-search-mark {
    background: rgba(255, 200, 0, 0.4);
    color: inherit;
    border-radius: 2px;
    padding: 0 1px;
}

mark.base-search-mark--active {
    background: rgba(255, 160, 0, 0.7);
    outline: 2px solid rgba(255, 140, 0, 0.8);
    outline-offset: 1px;
}
