/* Adaptive mode follows the OS. Keep overrides after component styles so
   legacy light surfaces cannot be combined with the adaptive light text. */
@media (prefers-color-scheme: dark) {
    body.pref-theme-system {
        color-scheme: dark;
        --bg: #171915;
        --panel: #20231e;
        --card: #20231e;
        --paper: #20231e;
        --paper-deep: #2b3028;
        --field: #2b3028;
        --surface-subtle: #2b3028;
        --surface: #161914;
        --ink: #f7f3e9;
        --muted: #c7c3b8;
        --border: #555b50;
        --line-soft: #555b50;
        --line: #a6ad9f;
        --green: #a6d5a5;
        --field-green: var(--green);
        --blue: #afc9e8;
        --steel: var(--blue);
        --danger: #ffaaa2;
        --red: var(--danger);
        --intent-surface: var(--panel);
    }

    /* Solid and translucent cream insets on home, module and record screens. */
    .pref-theme-system :is(
        .dashboard-crop-snapshot, .dashboard-stat-row, .module-dashboard-metrics,
        .module-financial-summary, .weather-card, .weather-plan-note,
        .weather-location-note, .weather-details, .weather-alert,
        .weather-insight-chip, .weather-insight-chip summary,
        .module-crop-command, .module-bee-pulse, .module-poultry-pulse,
        .module-tree-pulse, .module-livestock-pulse, .module-crop-pulse,
        .bee-apiary-snapshot-card, .bee-apiary-glance, .bee-hive-glance-card,
        .bee-insight-card, .bee-inspection-read, .tree-card-main:hover,
        .tree-getting-started li > span, .demo-production-banner,
        .demo-production-boundary, .demo-record-boundary,
        .planner-agenda-item, .planner-suggestion, .planner-catchup,
        .planner-catchup > div a, .planner-season-empty, .planner-quiet-empty,
        .planner-timeline-filters, .planner-timeline-canvas,
        .planner-linear:fullscreen, .planner-linear.is-focus-mode,
        .planner-year-operation > summary, .harvest-selection-recommendation,
        .harvest-selection-open-note, .harvest-selection-state,
        .harvest-selection-summary, .harvest-entry-card,
        .optional-cookie-notice, .privacy-choice-panel,
        .privacy-action:not(.primary), .privacy-text-action
    ) {
        background: var(--paper-deep) !important;
        color: var(--ink);
        border-color: var(--line-soft);
    }

    .pref-theme-system :is(.dashboard-crop-art-item, .module-crop-command-visual) {
        background: var(--paper-deep);
        border-color: var(--line-soft);
    }
    .pref-theme-system :is(.module-dashboard-metrics span + span,
        .dashboard-stat-row .dashboard-stat-cell + .dashboard-stat-cell) {
        border-color: var(--line-soft);
    }

    /* The header always uses a dark surface, independently of page colors. */
    body.pref-theme-system > header { color: var(--ink); }
    body.pref-theme-system header a { color: var(--ink) !important; }
    body.pref-theme-system header a:hover,
    body.pref-theme-system header a:focus-visible { color: var(--orange) !important; }
    /* The normal header rule makes the back link a light control. In dark mode
       it should read as part of the dark shell instead of flashing white. */
    body.pref-theme-system header .app-back-button,
    body.pref-theme-system header .app-back-button:visited {
        background: var(--paper-deep) !important;
        border: 1px solid var(--line-soft) !important;
        border-radius: 7px !important;
        color: var(--ink) !important;
        text-decoration: none;
    }
    body.pref-theme-system header .app-back-button:hover,
    body.pref-theme-system header .app-back-button:focus-visible {
        background: var(--field) !important;
        border-color: var(--orange) !important;
        color: var(--ink) !important;
    }
    :where(body.pref-theme-system) button { background: var(--field); }
    :where(body.pref-theme-system) button:hover { background: var(--paper-deep); }
    .pref-theme-system :is(.status-item-card.needs-action .status-item-type,
        .status-item-card.needs-action .status-attribution) { color: var(--orange); }
    body.pref-theme-system header .site-nav-menu > summary {
        background: var(--paper-deep);
        color: var(--ink) !important;
        -webkit-text-fill-color: var(--ink);
        border-color: var(--line-soft);
    }

    body.pref-theme-system header .site-nav-menu nav :is(a, button),
    .pref-theme-system :is(.secondary-button, .button-link:not(.primary):not(.danger),
        .layout-open-action) {
        background: var(--field) !important;
        color: var(--ink) !important;
        border-color: var(--line-soft) !important;
    }
    .pref-theme-system .field-help-disclosure > span { color: var(--ink); }
    .pref-theme-system .dashboard-layout-header { color: var(--ink); }
    .pref-theme-system .dashboard-layout-header h3 { color: var(--ink); }

    .pref-theme-system :is(.planner-domain, .planner-item-open,
        .planner-filter-actions a, .planner-timeline-controls > summary,
        .planner-individual-suggestions summary, .planner-operation-actions .text-button,
        .planner-timeline-filters legend, .planner-timeline-filters > label) {
        color: var(--ink);
    }
    .pref-theme-system .planner-page-shell :is(.planner-catchup-context,
        .planner-catchup > div a small, .planner-agenda-meta .planner-domain) {
        color: var(--muted) !important;
        background: var(--paper-deep);
    }
    .pref-theme-system .planner-page-shell :is(.planner-catchup > div a,
        .planner-catchup > div a > b, .planner-agenda-item .planner-item-open) {
        color: var(--ink) !important;
    }

    .pref-theme-system :is(.tree-getting-started li.is-complete > span,
        .bee-apiary-money-icon, .poultry-action-tile--care > span,
        .output-season-picker a.is-current, .output-season-picker a:hover,
        .output-season-picker a:focus-visible) {
        background: #3e6443 !important;
        color: #fffdf8 !important;
    }

    /* Keep filled action colors distinct from the brighter status text. */
    .pref-theme-system :is(button, .button-link, .secondary-button) {
        --green: #3e6443;
        --danger: #9b302c;
        --red: var(--danger);
    }
    .pref-theme-system :is(input, select, textarea) {
        background: var(--field);
        color: var(--ink);
        border-color: var(--line-soft);
    }
    .pref-theme-system :is(input, textarea)::placeholder {
        color: var(--muted);
        opacity: 1;
    }

    /* Main module operations boards share one resilient dark surface. These
       views used several older cream and translucent treatments, which made
       metrics and action labels disappear against adaptive dark mode. */
    .pref-theme-system :is(
        .bee-command-center, .crop-operations,
        .livestock-command-center, .poultry-command-center
    ) {
        background: var(--panel) !important;
        border-color: var(--line-soft) !important;
        box-shadow: 0 10px 28px rgba(0, 0, 0, .24);
        color: var(--ink) !important;
    }

    .pref-theme-system :is(
        .bee-command-center-heading, .crop-operations > header,
        .livestock-command-heading, .poultry-command-heading
    ) {
        background: var(--panel) !important;
        border-color: var(--line-soft) !important;
        color: var(--ink) !important;
    }

    .pref-theme-system :is(
        .bee-cockpit, .crop-operations-main,
        .livestock-cockpit, .poultry-cockpit,
        .crop-metric-rail, .livestock-command-metrics,
        .poultry-command-metrics
    ) {
        background: var(--paper-deep) !important;
        border-color: var(--line-soft) !important;
        color: var(--ink) !important;
    }

    .pref-theme-system :is(
        .bee-cockpit > *, .crop-operations-main > *,
        .crop-metric, .livestock-cockpit > *, .poultry-cockpit > *,
        .livestock-command-metrics > *, .poultry-command-metrics > *
    ) {
        background: transparent !important;
        border-color: var(--line-soft) !important;
        color: var(--ink) !important;
    }

    .pref-theme-system :is(
        .bee-command-center, .crop-operations,
        .livestock-command-center, .poultry-command-center
    ) :is(strong, b, h3, span, small, a) {
        color: inherit;
    }

    .pref-theme-system :is(
        .bee-command-center, .crop-operations,
        .livestock-command-center, .poultry-command-center
    ) :is(small, .eyebrow, .crop-metric span, .crop-metric small) {
        color: var(--muted) !important;
    }

    .pref-theme-system :is(
        .bee-command-center, .crop-operations,
        .livestock-command-center, .poultry-command-center
    ) :is(.button-link, .secondary-button, .crop-inline-action) {
        background: var(--field) !important;
        border-color: var(--line-soft) !important;
        color: var(--ink) !important;
    }

    .pref-theme-system .crop-priority { background: #35291f !important; color: var(--ink) !important; }
    .pref-theme-system .crop-priority :is(small, b) { color: var(--ink) !important; }

    /* Explicit action surfaces keep the controls readable even where older
       module rules set a light card or a light text color. */
    .pref-theme-system .form-heading-actions .secondary-button,
    .pref-theme-system .bee-apiary-card nav a {
        background: var(--field) !important;
        border-color: var(--line-soft) !important;
        color: var(--ink) !important;
    }

    .pref-theme-system .form-heading-actions .secondary-button:first-child,
    .pref-theme-system .bee-apiary-card nav .button-link {
        background: #3e6443 !important;
        border-color: #3e6443 !important;
        color: #fffdf8 !important;
    }

    /* Management controls are plain links in the markup, so they need an
       explicit surface too. Without it iOS can render a light button with
       inherited light text in this otherwise dark workspace. */
    .pref-theme-system .harvest-manage-row nav a {
        background: var(--field) !important;
        border: 1px solid var(--line-soft) !important;
        border-radius: 7px;
        color: var(--ink) !important;
        min-height: 2.45rem;
        padding: .48rem .72rem;
        text-decoration: none;
    }
    .pref-theme-system .harvest-manage-row nav .danger-link {
        background: #4b2825 !important;
        border-color: #d88078 !important;
        color: #ffe5df !important;
    }
}
