/* Screen palettes never change the colors of printed financial documents. */
@media screen {
    :root {
        color-scheme: light;
        --theme-control-bg: #ffffff;
        --theme-control-ink: #253950;
        --theme-control-muted: #66778d;
        --theme-control-border: #d8e3ec;
        --theme-control-hover: #f2f7fa;
        --theme-control-active: #e0f5f6;
        --theme-control-accent: #057e87;
        --theme-menu-shadow: 0 12px 36px #10203b24;
    }

    html[data-theme="light"] {
        color-scheme: light;
    }

    html[data-theme="dark"] {
        color-scheme: dark;
        --canvas: #0d1422;
        --surface: #162132;
        --ink: #edf3fb;
        --muted: #b1c0d3;
        --border: #33455d;
        --teal: #60d2c8;
        --teal-dark: #8be3db;
        --teal-light: #173e45;
        --navy: #edf4ff;
        --shadow: 0 5px 22px #00000021;
        --theme-raised: #1e2c40;
        --theme-hover: #26384e;
        --theme-input: #101b2b;
        --theme-accent-text: #7ee3d8;
        --theme-button-ink: #0d2428;
        --theme-banner: linear-gradient(120deg, #18384b, #193b40);
        --theme-track: #34465f;
        --theme-success-bg: #153d36;
        --theme-success-ink: #93e8c9;
        --theme-warning-bg: #45371e;
        --theme-warning-ink: #ffdb83;
        --theme-danger-bg: #492936;
        --theme-danger-ink: #ffb1ba;
        --theme-info-bg: #203b55;
        --theme-info-ink: #afd8ff;
        --theme-danger-button: #ff8797;
        --theme-danger-hover: #ffacb7;
        --theme-danger-button-ink: #3f0711;
        --theme-chart-blue: #85b6ff;
        --theme-chart-negative: #ff8797;
        --theme-menu-shadow: 0 15px 40px #00000055;
    }

    html[data-theme="ocean"] {
        color-scheme: dark;
        --canvas: #071e33;
        --surface: #0b2b43;
        --ink: #ebf8ff;
        --muted: #abcddd;
        --border: #2d5570;
        --teal: #63d6fa;
        --teal-dark: #92e5ff;
        --teal-light: #113f56;
        --navy: #f0fbff;
        --shadow: 0 5px 24px #00142535;
        --theme-raised: #103850;
        --theme-hover: #174560;
        --theme-input: #072238;
        --theme-accent-text: #94e4ff;
        --theme-button-ink: #083346;
        --theme-banner: linear-gradient(120deg, #10436a, #0d535b);
        --theme-track: #325773;
        --theme-success-bg: #13443f;
        --theme-success-ink: #97f0cf;
        --theme-warning-bg: #483d23;
        --theme-warning-ink: #ffe194;
        --theme-danger-bg: #482d42;
        --theme-danger-ink: #ffbbc8;
        --theme-info-bg: #154563;
        --theme-info-ink: #b7e6ff;
        --theme-danger-button: #ff97a8;
        --theme-danger-hover: #ffbbc8;
        --theme-danger-button-ink: #421220;
        --theme-chart-blue: #83aaff;
        --theme-chart-negative: #ff97a8;
        --theme-menu-shadow: 0 15px 40px #00101e66;
    }

    html[data-theme="sand"] {
        color-scheme: light;
        --canvas: #f5f0e7;
        --surface: #fffdf7;
        --ink: #352d23;
        --muted: #6b5e4b;
        --border: #d9cbb6;
        --teal: #8a571f;
        --teal-dark: #704416;
        --teal-light: #f2e4cf;
        --navy: #44331f;
        --shadow: 0 5px 22px #68431b08;
        --theme-raised: #f5eee2;
        --theme-hover: #eee4d4;
        --theme-input: #fffefa;
        --theme-accent-text: #754814;
        --theme-button-ink: #ffffff;
        --theme-banner: linear-gradient(120deg, #eedfc7, #f5ead7);
        --theme-track: #d9cdbb;
        --theme-success-bg: #e5efdf;
        --theme-success-ink: #315d38;
        --theme-warning-bg: #f6e7c3;
        --theme-warning-ink: #735018;
        --theme-danger-bg: #f8e4dc;
        --theme-danger-ink: #8b3b36;
        --theme-info-bg: #e4edf0;
        --theme-info-ink: #375f72;
        --theme-danger-button: #a8413c;
        --theme-danger-hover: #8d312e;
        --theme-danger-button-ink: #ffffff;
        --theme-chart-blue: #49799a;
        --theme-chart-negative: #a8413c;
        --theme-menu-shadow: 0 12px 36px #63461f24;
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) {
        --theme-control-bg: var(--surface);
        --theme-control-ink: var(--ink);
        --theme-control-muted: var(--muted);
        --theme-control-border: var(--border);
        --theme-control-hover: var(--theme-hover);
        --theme-control-active: var(--teal-light);
        --theme-control-accent: var(--theme-accent-text);
        accent-color: var(--teal);
    }

    /* Theme controls use their own tokens so the existing light UI is unchanged. */
    .theme-picker {
        position: relative;
        flex: none;
    }

    .theme-picker[open] {
        z-index: 80;
    }

    .theme-toggle {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 7px;
        min-width: 36px;
        min-height: 36px;
        padding: 7px 10px;
        border: 1px solid var(--theme-control-border);
        border-radius: 7px;
        background: var(--theme-control-bg);
        color: var(--theme-control-ink);
        font-size: 12px;
        line-height: 1.5;
        list-style: none;
        cursor: pointer;
        user-select: none;
        white-space: nowrap;
    }

    .theme-toggle::-webkit-details-marker {
        display: none;
    }

    .theme-toggle::marker {
        content: "";
    }

    .theme-toggle:hover,
    .theme-picker[open] > .theme-toggle {
        background: var(--theme-control-hover);
        border-color: var(--theme-control-accent);
    }

    .theme-toggle:focus-visible,
    .theme-option:focus-visible {
        outline: 3px solid var(--theme-control-accent);
        outline-offset: 3px;
    }

    .theme-toggle .icon {
        width: 18px;
        height: 18px;
    }

    .theme-menu {
        position: absolute;
        top: calc(100% + 10px);
        right: 0;
        width: 268px;
        max-width: calc(100vw - 24px);
        padding: 16px;
        border: 1px solid var(--theme-control-border);
        border-radius: 12px;
        background: var(--theme-control-bg);
        color: var(--theme-control-ink);
        box-shadow: var(--theme-menu-shadow);
        text-align: left;
    }

    .theme-menu-title {
        margin: 0 0 12px;
        font-size: 13px;
        font-weight: 650;
    }

    .theme-options {
        display: grid;
        gap: 7px;
        min-width: 0;
    }

    .theme-option {
        display: flex;
        align-items: center;
        gap: 10px;
        min-width: 0;
        min-height: 43px;
        width: 100%;
        padding: 9px 11px;
        border: 1px solid var(--theme-control-border);
        border-radius: 8px;
        background: var(--theme-control-bg);
        color: var(--theme-control-ink);
        font-size: 12px;
        line-height: 1.6;
        text-align: left;
        white-space: normal;
    }

    .theme-option:hover {
        background: var(--theme-control-hover);
    }

    .theme-option[aria-pressed="true"] {
        border-color: var(--theme-control-accent);
        background: var(--theme-control-active);
        color: var(--theme-control-accent);
        font-weight: 650;
        box-shadow: inset 0 0 0 1px var(--theme-control-accent);
    }

    .theme-option > span:not(.theme-swatch):not(.theme-check) {
        min-width: 0;
        overflow-wrap: anywhere;
    }

    .theme-check {
        margin-left: auto;
        padding-left: 4px;
        color: var(--theme-control-accent);
        font-weight: 750;
        visibility: hidden;
        flex: none;
    }

    .theme-option[aria-pressed="true"] .theme-check {
        visibility: visible;
    }

    .theme-swatch {
        display: inline-block;
        width: 23px;
        height: 23px;
        flex: none;
        border: 1px solid #8193a380;
        border-radius: 6px;
        box-shadow: inset 0 0 0 2px #ffffff33;
    }

    [data-theme-swatch="light"] {
        background: linear-gradient(135deg, #ffffff 52%, #c2eceb 52%);
    }

    [data-theme-swatch="dark"] {
        background: linear-gradient(135deg, #162132 52%, #60d2c8 52%);
    }

    [data-theme-swatch="system"] {
        background: linear-gradient(135deg, #ffffff 50%, #162132 50%);
    }

    [data-theme-swatch="ocean"] {
        background: linear-gradient(135deg, #10436a 52%, #63d6fa 52%);
    }

    [data-theme-swatch="sand"] {
        background: linear-gradient(135deg, #f5ead7 52%, #b17b3d 52%);
    }

    .theme-menu > small,
    .theme-menu > .small,
    .theme-menu > .theme-hint {
        display: block;
        margin-top: 12px;
        color: var(--theme-control-muted);
        font-size: 11px;
        line-height: 1.7;
        overflow-wrap: anywhere;
    }

    .login-languages {
        display: flex;
        align-items: center;
        gap: 12px;
        z-index: 5;
    }

    .settings-theme {
        border-top: 1px solid var(--border);
        padding: 26px;
        margin-bottom: 20px;
    }

    .settings-theme > p {
        max-width: 680px;
        margin-top: 7px;
        color: var(--muted);
        font-size: 12px;
        line-height: 1.8;
    }

    .settings-theme-options {
        grid-template-columns: repeat(auto-fit, minmax(145px, 1fr));
        gap: 10px;
        margin-top: 19px;
    }

    /* Shared palette overrides cover the original fixed light colors. */
    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) :is(.sidebar, .topbar, .panel, .document, .login-page) {
        background: var(--surface);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) :is(.sidebar-footer, .form-footer, .document-header) {
        border-color: var(--border);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) :is(.nav-link, .breadcrumbs strong, .field label, .detail-grid dd, .document-meta strong, .empty-state h3) {
        color: var(--ink);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) :is(.page-heading p, .metric-label, .list-meta, .field-help, .checkbox-label, .login-security, .app-footer, .chart-labels, .chart-scale, .language-icon, .password-toggle, .document-meta p, .document-total div, .font-preview-sample .font-preview-digits) {
        color: var(--muted);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) :is(.nav-link:hover, .languages a:hover, .icon-button:hover, .button.secondary:hover, tbody tr:hover) {
        background: var(--theme-hover);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) :is(.nav-link.active, .languages a.active, .avatar, .metric-icon) {
        background: var(--teal-light);
        color: var(--theme-accent-text);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) :is(.nav-link.active .icon, .table-link, .audit-details summary) {
        color: var(--theme-accent-text);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) :is(a, button, summary):focus-visible {
        outline-color: var(--theme-accent-text);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) .skip-link {
        color: var(--theme-button-ink);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) :is(.icon-button, .button.secondary, .page-number) {
        background: var(--surface);
        color: var(--ink);
        border-color: var(--border);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) .button:not(.secondary):not(.danger),
    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) .page-number.active {
        background: var(--teal);
        color: var(--theme-button-ink);
        border-color: var(--teal);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) .button:not(.secondary):not(.danger):hover {
        background: var(--teal-dark);
        border-color: var(--teal-dark);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) .button.danger {
        background: var(--theme-danger-button);
        color: var(--theme-danger-button-ink);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) .button.danger:hover {
        background: var(--theme-danger-hover);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) :is(.welcome-banner, .login-art) {
        background: var(--theme-banner);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) .login-art h1 {
        color: var(--ink);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) :is(.welcome-copy p, .login-art p, .login-art-footer) {
        color: var(--muted);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) .metric:nth-child(2) .metric-icon {
        background: var(--theme-info-bg);
        color: var(--theme-info-ink);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) .chart-area {
        background: repeating-linear-gradient(to bottom, transparent 0, transparent 43px, var(--border) 44px, var(--border) 45px);
        border-bottom-color: var(--border);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) :is(.chart-empty h3, .chart-empty p, .chart-empty .icon) {
        background: var(--surface);
        color: var(--muted);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) .dot.gray {
        background: var(--theme-track);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) .dot.blue {
        background: var(--theme-chart-blue);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) .chart-svg rect[fill="#009ca6"] {
        fill: var(--teal);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) .chart-svg rect[fill="#438eec"] {
        fill: var(--theme-chart-blue);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) .chart-svg rect[fill="#d15464"] {
        fill: var(--theme-chart-negative);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) .donut {
        background: conic-gradient(var(--teal) calc(var(--occupancy, 0) * 1%), var(--theme-track) 0);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) .donut::before {
        background: var(--surface);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) th {
        background: var(--theme-raised);
        color: var(--muted);
        border-color: var(--border);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) td {
        color: var(--ink);
        border-color: var(--border);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) .status {
        background: var(--theme-raised);
        color: var(--muted);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) :is(.status-active, .status-paid, .status-completed, .status-available, .alert:not(.error):not(.info)) {
        background: var(--theme-success-bg);
        color: var(--theme-success-ink);
        border-color: var(--theme-success-ink);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) :is(.status-pending, .status-partial, .status-open) {
        background: var(--theme-warning-bg);
        color: var(--theme-warning-ink);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) :is(.status-overdue, .status-unpaid, .status-urgent, .alert.error) {
        background: var(--theme-danger-bg);
        color: var(--theme-danger-ink);
        border-color: var(--theme-danger-ink);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) :is(.status-occupied, .status-in_progress, .alert.info) {
        background: var(--theme-info-bg);
        color: var(--theme-info-ink);
        border-color: var(--theme-info-ink);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) :is(.field-error, .required) {
        color: var(--theme-danger-ink);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) .warning-text {
        color: var(--theme-warning-ink);
    }

    html[data-theme="sand"] .warning-text {
        color: #8a571f;
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) :is(.empty-state .empty-icon, .audit-metadata, .document-note, .bank-qr-preview) {
        background: var(--theme-raised);
        color: var(--muted);
        border-color: var(--border);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) :is(.input, input:not([type="checkbox"]):not([type="radio"]), select, textarea) {
        background: var(--theme-input);
        color: var(--ink);
        border-color: var(--border);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) :is(input, textarea)::placeholder {
        color: var(--muted);
        opacity: 1;
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) :is(input, select, textarea):focus {
        border-color: var(--teal);
        box-shadow: 0 0 0 3px var(--teal-light);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) .field :is(input, select, textarea)[aria-invalid="true"] {
        border-color: var(--theme-danger-ink);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) input:-webkit-autofill {
        -webkit-text-fill-color: var(--ink);
        -webkit-box-shadow: 0 0 0 1000px var(--theme-input) inset;
        caret-color: var(--ink);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) .settings-bank-qr input[type="file"]::file-selector-button {
        background: var(--teal-light);
        color: var(--theme-accent-text);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) .font-preview {
        background: var(--theme-raised);
        border-color: var(--border);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) .activity-metric.selected {
        background: var(--teal-light);
        border-color: var(--teal);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) .activity-metric:hover {
        border-color: var(--teal);
        box-shadow: var(--shadow);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) .document-total .grand-total {
        color: var(--ink);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) details.panel > summary[style] {
        color: var(--muted) !important;
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) .invoice-payment-instructions {
        background: var(--theme-raised);
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) .invoice-payment-details strong {
        color: var(--ink);
    }

    /* QR quiet zones stay white; only the surrounding application is themed. */
    .invoice-bank-qr,
    .bank-qr-preview-frame,
    .invoice-bank-qr img,
    .bank-qr-preview-frame img {
        background: #ffffff;
        color: #26374b;
    }

    .invoice-bank-qr figcaption,
    .bank-qr-preview-frame .bank-qr-empty {
        color: #52647a;
    }

    html:is([data-theme="dark"], [data-theme="ocean"], [data-theme="sand"]) .drawer-overlay.open {
        background: #020b18a6;
    }
}

@media screen and (max-width: 1100px) {
    .theme-toggle-label {
        display: none;
    }

    .theme-toggle {
        width: 36px;
        padding: 7px;
    }
}

@media screen and (max-width: 760px) {
    .settings-theme {
        padding: 22px 18px;
    }

    .settings-theme-options {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media screen and (max-width: 600px) {
    .theme-menu {
        position: fixed;
        top: 74px;
        right: 12px;
        width: min(284px, calc(100vw - 24px));
        max-height: calc(100vh - 90px);
        max-height: calc(100dvh - 90px);
        overflow-y: auto;
        overscroll-behavior: contain;
    }

    .login-languages {
        gap: 9px;
    }
}

@media screen and (max-width: 430px) {
    .mobile-brand > span:last-child {
        display: none;
    }

    .top-actions {
        gap: 6px;
    }

    .topbar {
        gap: 8px;
    }
}

@media print {
    html {
        color-scheme: light;
    }

    html,
    body {
        background: #ffffff !important;
        color: #000000 !important;
    }

    .theme-picker,
    .settings-theme,
    .theme-menu {
        display: none !important;
    }

    .document,
    .document *,
    .panel,
    th,
    td {
        background: transparent !important;
        color: #000000 !important;
        box-shadow: none !important;
        text-shadow: none !important;
    }

    .document .status {
        border: 1px solid #777777;
    }

    .invoice-payment-instructions,
    .invoice-bank-qr,
    .invoice-bank-qr img,
    .bank-qr-preview-frame,
    .bank-qr-preview-frame img {
        background: #ffffff !important;
        color: #000000 !important;
    }
}
