:root {
    --surface-alt: #eef3ef;
    --control: #d1ded6;
    --focus: #327c56;
    --on-accent: #fff;
}

:root[data-theme="dark"] {
    color-scheme: dark;
    --ink: #e5efe9;
    --muted: #afc4b7;
    --green: #a7e8bf;
    --paper: #101815;
    --white: #192520;
    --surface-alt: #24362d;
    --border: #394e42;
    --control: #6d8879;
    --focus: #a7e8bf;
    --on-accent: #10291b;
    --red: #ffaaaa;
}

:root[data-theme="contrast"] {
    --ink: #000;
    --muted: #212121;
    --green: #00452b;
    --paper: #fff;
    --white: #fff;
    --surface-alt: #efefef;
    --border: #454545;
    --control: #202020;
    --focus: #003dcb;
    --red: #8b0000;
}

:root:is([data-theme="dark"], [data-theme="contrast"]) :is(
    .sidebar, .topbar, .dashboard-stat, .bank-card, dialog, .modal-head, .modal-foot,
    .btn, .field :is(input, select, textarea), .search input, .invoice-header,
    .crm-card, .crm-toolbar, .sidebar .brand
) {
    background: var(--white);
    color: var(--ink);
    border-color: var(--border);
}

:root:is([data-theme="dark"], [data-theme="contrast"]) :is(
    tbody tr:hover, table .total-row, th, .inline-summary, .ledger-totals, .form-note,
    .form-total, .bar-track, .nav-group summary:hover, .nav-item:hover, .nav-item.active,
    .btn:hover, .icon-btn:hover, .pill, .welcome-icon
) {
    background: var(--surface-alt);
    color: var(--ink);
}

:root:is([data-theme="dark"], [data-theme="contrast"]) :is(
    .nav-item, .nav-group summary, .stat-label, td small, .sidebar-foot small,
    .sidebar-foot .icon-btn, .empty, .ledger-totals strong, .inline-summary > span:first-child,
    .ledger-totals span, .detail-meta, .code, .company-control strong
) { color: var(--ink); }

:root:is([data-theme="dark"], [data-theme="contrast"]) :is(.btn.primary, .btn.primary:hover) {
    background: var(--green);
    border-color: var(--green);
    color: var(--on-accent);
}

:root:is([data-theme="dark"], [data-theme="contrast"]) :is(
    td, .glance > div, .list-toolbar, .modal-head, .modal-foot, .sidebar-foot
) { border-color: var(--border); }

:root:is([data-theme="dark"], [data-theme="contrast"]) :is(
    button, a, summary, input, select, textarea
):focus-visible {
    outline: 3px solid var(--focus);
    outline-offset: 2px;
}

:root[data-theme="dark"] .accounting-chart-bar progress,
:root[data-theme="dark"] .accounting-chart-bar progress::-webkit-progress-bar {
    background: var(--surface-alt);
}

:root[data-theme="dark"] .accounting-chart-bar progress { --accounting-bar: #83cfa6; }
:root[data-theme="dark"] .accounting-chart-bar.expense progress { --accounting-bar: #ffad85; }
:root[data-theme="dark"] .accounting-chart-bar.cash progress { --accounting-bar: #8bc7ff; }
:root[data-theme="dark"] .accounting-chart-bar.negative progress { --accounting-bar: var(--red); }
:root[data-theme="contrast"] .accounting-chart-bar progress { --accounting-bar: #1e1e1e; }

.theme-picker {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 9px;
    color: var(--ink);
    font-size: .875rem;
    font-weight: 600;
}

.theme-picker select {
    min-width: 0;
    min-height: 44px;
    padding: 6px 8px;
    border: 1px solid var(--control);
    border-radius: 8px;
    background: var(--white);
    color: var(--ink);
}

.sidebar-preferences {
    padding: 8px;
    margin-top: auto;
}

.sidebar-preferences + .sidebar-foot { margin-top: 0; }
.access-panel .theme-picker { margin-top: 14px; }
.agent-preferences {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 12px;
    margin-bottom: 12px;
}

.phone-nav {
    display: flex;
    position: fixed;
    inset: auto 0 0;
    z-index: 12;
    justify-content: space-evenly;
    padding: 4px 4px max(6px, env(safe-area-inset-bottom));
    background: var(--white);
    border-top: 1px solid var(--border);
}

.phone-nav button {
    flex: 1;
    display: grid;
    place-items: center;
    align-content: center;
    gap: 2px;
    min-width: 0;
    min-height: 49px;
    padding: 4px;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: var(--muted);
    font-size: .75rem;
    line-height: 1.2;
}

.phone-nav button[aria-current="page"] {
    background: var(--surface-alt);
    color: var(--green);
    font-weight: 700;
}

.with-phone-nav .content { padding-bottom: calc(78px + env(safe-area-inset-bottom)); }
.with-phone-nav .topbar .mobile-menu { display: none; }

@media (max-width: 640px) {
    .phone-nav button, .theme-picker select, .field :is(input, select, textarea) { min-height: 44px; }
    .with-phone-nav .topbar { min-height: 57px; }
    .with-phone-nav .content .page-heading { flex-direction: row; align-items: center; }
    .with-phone-nav .page-heading .heading-actions { width: auto; flex: 0 0 auto; }
    .content .page-heading h1 { overflow-wrap: anywhere; }
    .content .page-heading .btn { padding: 8px 12px; }
    .dashboard-stat { padding: 11px; }
    .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .dashboard-stat small { line-height: 1.35; }
    .panel-heading, .ledger-controls, .journal-search, .padded { padding-inline: 14px; }
    .journal-line, .invoice-line { column-gap: 9px; }
    dialog { width: calc(100% - 16px); max-height: calc(100dvh - 16px); }
}

@media (min-width: 921px) {
    .phone-nav { display: none; }
    .with-phone-nav .content { padding-bottom: 24px; }
}

@media print {
    .phone-nav { display: none; }
    :root { color-scheme: light; }
}

.remember-choice {
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 44px;
    font-size: .875rem;
}
.remember-choice input { width: 18px; height: 18px; }
[data-install-readyestate]:not([hidden]) { margin-top: 8px; }


/* Flexible panel and form layout across narrow windows and wide workspaces. */
@media screen {
    .content { max-inline-size: none; }
    .main, #view, .panel, .dashboard-grid, .panel-heading > *, .page-heading > * {
        min-inline-size: 0;
    }
    .panel-heading, .heading-actions, .list-toolbar {
        flex-wrap: wrap;
    }
    .heading-actions {
        display: flex;
        gap: 8px;
        max-inline-size: 100%;
    }
    .panel-heading h2, .page-heading h1, td, .detail-body {
        overflow-wrap: anywhere;
    }
    .table-wrap {
        max-inline-size: 100%;
    }
    .form-grid {
        grid-template-columns: repeat(auto-fit, minmax(min(100%, 13rem), 1fr));
    }
    .date-controls > .field, .report-toolbar > .field {
        flex: 1 1 10rem;
        min-inline-size: min(100%, 10rem);
    }
    .date-controls input, .report-toolbar input {
        min-inline-size: 0;
    }
    .journal-line, .invoice-line {
        min-inline-size: 0;
    }
}

@media screen and (max-width: 640px) {
    .with-phone-nav .content .page-heading {
        flex-wrap: wrap;
    }
    .with-phone-nav .page-heading .heading-actions {
        flex: 0 1 auto;
        max-inline-size: 100%;
    }
    .panel-heading .field {
        flex: 1 1 9rem;
        max-inline-size: 100%;
    }
    .report-toolbar .btn, .journal-filter-actions .btn {
        flex: 1 1 8rem;
    }
    .table-wrap :is(th, td) {
        padding-inline: 12px;
    }
    .modal-head, .form-body, .detail-body, .modal-foot {
        padding-inline: 14px;
    }
    .modal-foot {
        flex-wrap: wrap;
    }
}

@media screen and (max-width: 359px) {
    .journal-line, .invoice-line {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
    .journal-line .remove-line, .invoice-line .remove-line {
        grid-column: 2;
        justify-self: end;
    }
}

.listing-sort {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-height: 40px;
    padding: 0;
    border: 0;
    background: transparent;
    color: inherit;
    font: inherit;
    text-align: left;
}
.listing-sort:hover { color: var(--green); }
.listing-sort span { font-size: 1rem; }

.listing-form .form-grid {
    grid-template-columns: repeat(auto-fit,minmax(min(100%, 10rem),1fr));
}
.listing-extra {
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 10px 12px;
}
.listing-extra summary {
    cursor: pointer;
    font-size: .875rem;
    font-weight: 600;
    padding-block: 5px;
}
.listing-extra[open] summary { margin-bottom: 13px; }
.listing-extra > .field,
.listing-extra > [data-listing-public-location]:not([hidden]) { margin-top: 13px; }
.listing-mobile-controls { display: none; }

@media screen and (max-width: 720px) {
    .listing-mobile-controls {
        display: flex;
        align-items: end;
        gap: 8px;
        padding: 12px 14px;
    }
    .listing-mobile-controls .field { flex: 1; }
    .property-listing-table .table-wrap { padding: 0 12px 12px; }
    .property-listing-table thead {
        position: absolute;
        block-size: 1px;
        inline-size: 1px;
        overflow: hidden;
        clip-path: inset(50%);
    }
    .property-listing-table :is(table, tbody) { display: block; }
    .property-listing-table tbody tr {
        display: grid;
        grid-template-columns: repeat(2,minmax(0,1fr));
        gap: 8px 12px;
        margin-bottom: 9px;
        padding: 12px;
        border: 1px solid var(--border);
        border-radius: 10px;
        background: var(--white);
    }
    .property-listing-table tbody td {
        display: block;
        padding: 0;
        border: 0;
        text-align: start;
        white-space: normal;
    }
    .property-listing-table td[data-label]::before {
        display: block;
        content: attr(data-label);
        font-size: .75rem;
        color: var(--muted);
    }
    .property-listing-table td[data-listing-cell="title"] {
        order: -2;
        grid-column: 1/-1;
        font-size: 1rem;
        font-weight: 650;
    }
    .property-listing-table td[data-listing-cell="title"]::before { content: none; }
    .property-listing-table td[data-listing-cell="location"] { order: -1; }
    .property-listing-table td[data-listing-cell="reference"] { font-size: .8rem; }
    .property-listing-table td[data-listing-cell="actions"] { align-self: end; }
    .property-listing-table td[data-listing-cell="actions"]:empty { display: none; }
    .property-listing-table td[colspan] { grid-column: 1/-1; }
}

@media screen and (max-width: 640px) {
    .sidebar,
    .sidebar.is-open {
        inset: auto 0 0;
        inline-size: 100%;
        block-size: auto;
        max-block-size: min(78dvh, 42rem);
        padding-bottom: max(12px, env(safe-area-inset-bottom));
        border: 0;
        border-radius: 16px 16px 0 0;
    }
    .sidebar { transform: translateY(110%); }
    .sidebar.is-open { transform: translateY(0); }
    .sidebar nav { min-block-size: 0; }
}
