:root {
    color-scheme: light;
    --bg: #f5f5f7;
    --surface: rgba(255, 255, 255, 0.82);
    --surface-solid: #ffffff;
    --sidebar: rgba(255, 255, 255, 0.72);
    --control: #ebebef;
    --panel-border: rgba(216, 216, 223, 0.75);
    --soft: #f7f7fa;
    --notice-bg: #fff4ed;
    --text: #1d1d1f;
    --muted: #6e6e73;
    --line: #d8d8df;
    --accent: #184e38;
    --on-accent: #ffffff;
    --danger: #b42318;
    --success: #16833a;
    --shadow: rgba(0, 0, 0, 0.06);
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", sans-serif;
}

:root[data-theme="dark"] {
    color-scheme: dark;
    --bg: #111412;
    --surface: rgba(30, 34, 31, 0.82);
    --surface-solid: #1b201d;
    --sidebar: rgba(20, 24, 22, 0.76);
    --control: #2b302d;
    --panel-border: rgba(72, 82, 75, 0.72);
    --soft: #202621;
    --notice-bg: rgba(180, 35, 24, 0.16);
    --text: #f2f0eb;
    --muted: #a7aea8;
    --line: #3c453f;
    --accent: #7bc6a0;
    --on-accent: #112017;
    --danger: #ff8b7d;
    --success: #78d991;
    --shadow: rgba(0, 0, 0, 0.28);
}

* { box-sizing: border-box; }
body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
}
a { color: inherit; text-decoration: none; }
.sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: 240px;
    padding: 28px 18px;
    border-right: 1px solid var(--line);
    background: var(--sidebar);
    backdrop-filter: blur(18px);
    display: flex;
    flex-direction: column;
    gap: 28px;
}
.brand {
    display: flex;
    align-items: center;
    min-height: 58px;
    color: var(--text);
    width: 100%;
}
.brand img {
    width: 100%;
    height: auto;
    object-fit: contain;
    object-position: left center;
    border-radius: 6px;
    display: block;
}
.brand .logo-dark { display: none; }
:root[data-theme="dark"] .brand .logo-light { display: none; }
:root[data-theme="dark"] .brand .logo-dark { display: block; }
nav { display: grid; gap: 8px; }
nav a, .logout button, .button, button {
    min-height: 38px;
    border-radius: 8px;
    border: 1px solid transparent;
    padding: 9px 13px;
    font: inherit;
    background: transparent;
    color: var(--text);
    cursor: pointer;
}
nav a:hover, .button, button { background: var(--control); }
.primary {
    background: var(--accent) !important;
    border-color: var(--accent) !important;
    color: var(--on-accent) !important;
}
.logout {
    margin-top: auto;
    display: grid;
    gap: 8px;
    padding-top: 18px;
    border-top: 1px solid var(--line);
    color: var(--muted);
}
.sidebar-user {
    display: block;
    margin-bottom: 4px;
    padding: 0 4px;
    color: var(--muted);
    font-size: 13px;
    font-weight: 600;
}
.logout button {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 9px;
    min-height: 36px;
    padding: 8px 10px;
    color: var(--text);
    text-align: left;
}
.logout button:not(.theme-toggle) {
    color: var(--muted);
}
.logout button:hover {
    color: var(--text);
}
.main {
    margin-left: 240px;
    padding: 38px;
    max-width: 1280px;
}
.page-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 18px;
    margin-bottom: 26px;
}
h1, h2, p { margin-top: 0; }
h1 { font-size: 34px; letter-spacing: 0; margin-bottom: 0; }
h2 { font-size: 20px; margin-bottom: 16px; }
.eyebrow {
    color: var(--muted);
    font-size: 13px;
    font-weight: 700;
    margin-bottom: 8px;
    text-transform: uppercase;
}
.panel, .metric, .login-card {
    background: var(--surface);
    border: 1px solid var(--panel-border);
    border-radius: 8px;
    box-shadow: 0 24px 60px var(--shadow);
}
.panel { padding: 22px; margin-bottom: 22px; overflow: auto; }
.metric-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 16px;
    margin-bottom: 22px;
}
.metric { padding: 18px; }
.metric span { display: block; color: var(--muted); font-size: 14px; margin-bottom: 8px; }
.metric strong { font-size: 28px; letter-spacing: 0; }
table { width: 100%; border-collapse: collapse; }
th {
    text-align: left;
    color: var(--muted);
    font-size: 13px;
    font-weight: 600;
}
th, td { padding: 12px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
tr:last-child td { border-bottom: 0; }
.table-subline {
    color: var(--muted);
    display: block;
    font-size: 13px;
    font-weight: 500;
    margin-top: 3px;
}
.num { text-align: right; white-space: nowrap; }
.actions { text-align: right; }
.actions form { display: inline; }
.link-button {
    min-height: auto;
    padding: 0;
    background: transparent !important;
    color: var(--danger);
}
.overdue td { color: var(--danger); }
.status {
    display: inline-block;
    padding: 4px 9px;
    border-radius: 8px;
    background: var(--control);
}
.status.paid { color: var(--success); }
.status.cancelled { color: var(--danger); }
input, textarea, select {
    width: 100%;
    min-height: 40px;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 9px 11px;
    background: var(--surface-solid);
    color: var(--text);
    font: inherit;
}
textarea { min-height: 88px; resize: vertical; }
label {
    display: grid;
    gap: 7px;
    color: var(--muted);
    font-size: 13px;
    font-weight: 600;
}
.form-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 16px;
}
.settings-form {
    display: grid;
    gap: 26px;
}
.settings-section {
    border-bottom: 1px solid var(--line);
    padding-bottom: 24px;
}
.settings-section:last-of-type { border-bottom: 0; padding-bottom: 0; }
.settings-section h2 {
    color: var(--text);
    font-size: 16px;
    margin-bottom: 14px;
}
.wide { grid-column: 1 / -1; }
.check {
    display: flex;
    align-items: center;
    gap: 10px;
}
.check input { width: auto; min-height: auto; }
.form-actions, .head-actions, .panel-head, .inline-form, .line-items-head {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 10px;
}
.panel-head { justify-content: space-between; margin-bottom: 14px; }
.short { width: 92px; }
.invoice-form {
    display: grid;
    gap: 24px;
}
.line-items-head {
    justify-content: space-between;
    margin-top: 4px;
}
.line-items-head h2 { margin-bottom: 0; }
.line-items { display: grid; gap: 14px; }
.line-item {
    display: grid;
    grid-template-columns: minmax(160px, 1fr) minmax(120px, 0.8fr) minmax(170px, 1fr) minmax(90px, 0.55fr) minmax(82px, 0.45fr) minmax(150px, 0.9fr) 42px;
    grid-template-areas:
        "template title title date date remove remove"
        "desc desc desc quantity unit price price";
    gap: 14px;
    align-items: end;
    padding: 18px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: color-mix(in srgb, var(--surface-solid) 68%, transparent);
}
.line-item .template-field { grid-area: template; }
.line-item .title-field { grid-area: title; }
.line-item .date-field { grid-area: date; }
.line-item .desc { grid-area: desc; }
.line-item .quantity-field { grid-area: quantity; }
.line-item .unit-field { grid-area: unit; }
.line-item .price-field { grid-area: price; }
.line-item .desc textarea {
    height: 112px;
    min-height: 112px;
}
.icon-button {
    width: 40px;
    height: 40px;
    padding: 0;
    font-size: 22px;
    line-height: 1;
}
.remove-line {
    grid-area: remove;
    color: var(--danger);
}
.details {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 14px;
    margin: 0 0 18px;
}
.details div { padding: 12px; background: var(--soft); border-radius: 8px; }
dt { color: var(--muted); font-size: 13px; }
dd { margin: 4px 0 0; font-weight: 650; }
.inline-form { justify-content: start; margin-bottom: 16px; }
.inline-form input { max-width: 180px; }
.view-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    margin-top: 6px;
}
.danger-zone {
    display: flex;
    justify-content: flex-end;
    flex: 0 0 auto;
}
.danger-zone button { color: var(--danger); }
.audit-trail {
    color: var(--muted);
    font-size: 12px;
    margin: 0;
    text-align: left;
}
.guest {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 24px;
}
.login-card {
    width: min(420px, 100%);
    display: grid;
    gap: 16px;
    padding: 28px;
}
.login-logo {
    width: 100%;
    max-height: 132px;
    border-radius: 8px;
}
.login-logo img {
    width: 100%;
    max-height: 132px;
    object-fit: contain;
    border-radius: 8px;
    display: block;
}
.login-logo .logo-dark { display: none; }
:root[data-theme="dark"] .login-logo .logo-light { display: none; }
:root[data-theme="dark"] .login-logo .logo-dark { display: block; }
.notice {
    padding: 12px;
    border-radius: 8px;
    background: var(--notice-bg);
    color: var(--danger);
}
.theme-toggle {
    display: flex;
    align-items: center;
    gap: 8px;
}
.theme-toggle-icon {
    width: 16px;
    height: 16px;
    border: 2px solid currentColor;
    border-radius: 50%;
    box-shadow: inset -5px -5px 0 currentColor;
    flex: 0 0 auto;
}
@media (max-width: 860px) {
    .sidebar {
        position: static;
        width: auto;
        border-right: 0;
        border-bottom: 1px solid var(--line);
    }
    .main { margin-left: 0; padding: 22px; }
    .page-head, .panel-head, .inline-form, .line-items-head { align-items: stretch; flex-direction: column; }
    .metric-grid, .form-grid, .details { grid-template-columns: 1fr; }
    .line-item {
        grid-template-columns: 1fr;
        grid-template-areas:
            "template"
            "title"
            "date"
            "desc"
            "quantity"
            "unit"
            "price"
            "remove";
    }
    .head-actions { justify-content: start; }
    .view-footer { align-items: stretch; flex-direction: column-reverse; }
    .danger-zone { justify-content: start; }
}
