:root {
  color-scheme: light;
  font-family: system-ui, -apple-system, sans-serif;
  color: var(--ink);
  background: var(--page);
  --muted: #435c50;
  --border: #d0e4d7;
  --accent: #8edeb5;
  --dark: #143829;
  --interactive: #196242;
  --ink: #20392d;
  --page: #f1f8f3;
  --surface: #fff;
  --glow: #d9efe0;
  --brand-muted: #35684e;
  --control-border: #a6c7b4;
  --soft: #e1f2e8;
  --shadow: #102d2120;
  --scrim: #102d2188;
  --danger: #9d2525;
  --primary: #196242;
  --primary-hover: #143829;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --ink: #e7f5eb;
  --page: #101e18;
  --surface: #192d23;
  --glow: #1a3829;
  --muted: #b7cbbf;
  --border: #365545;
  --control-border: #557764;
  --dark: #f0faf3;
  --interactive: #9ee2b8;
  --brand-muted: #aae4bf;
  --soft: #264937;
  --shadow: #06140c60;
  --scrim: #06140caa;
  --danger: #ffaaa8;
  --primary: #22714d;
  --primary-hover: #287b56;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; min-height: 100vh; background: radial-gradient(ellipse at 100% 0%, var(--glow), transparent 62%); }
header { background: var(--page); color: var(--dark); border-bottom: 1px solid var(--border); padding-top: env(safe-area-inset-top, 0px); }
.topbar, main, #account-panel { width: min(1120px, calc(100% - 36px)); margin-inline: auto; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 70px; }
.brand { display: inline-flex; align-items: center; gap: 8px; color: var(--dark); text-decoration: none; font-size: clamp(1.05rem, 4vw, 1.4rem); font-weight: 780; letter-spacing: .035em; text-transform: uppercase; white-space: nowrap; }
.brand-app { color: var(--brand-muted); font-weight: 550; }
#offline-status { font-size: .82rem; color: var(--muted); text-align: right; }
.topbar .actions { justify-content: flex-end; }
#account-menu { position: relative; }
#account-menu > summary { display: flex; align-items: center; gap: 6px; min-height: 44px; padding: 6px 2px; color: var(--interactive); font-size: .88rem; font-weight: 650; cursor: pointer; }
#account-menu > summary::-webkit-details-marker { display: none; }
#account-menu > summary::after { content: ''; width: .42em; height: .42em; border-right: 1.5px solid; border-bottom: 1.5px solid; transform: translateY(-2px) rotate(45deg); }
.account-popover { position: absolute; z-index: 5; top: 100%; right: 0; width: min(285px, calc(100vw - 36px)); padding: 12px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: 0 8px 24px var(--shadow); }
.account-popover p { margin: 0 0 8px; font-size: .84rem; }
.account-popover .actions { justify-content: flex-start; gap: 8px; }
.account-popover button { padding-inline: 10px; font-size: .85rem; }
main { padding-block: 38px; }
.page-heading, .section-heading, .actions { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.page-heading, .section-heading { justify-content: space-between; }
.page-heading { margin-bottom: 32px; }
h1, h2, h3, p, dl, dd { margin: 0; }
h1 { font-size: clamp(1.75rem, 5vw, 2.6rem); letter-spacing: -.055em; }
h2 { font-size: 1.23rem; letter-spacing: -.025em; }
h3 { font-size: 1rem; overflow-wrap: anywhere; }
p { color: var(--muted); margin-top: 6px; line-height: 1.5; overflow-wrap: anywhere; }
button, input, select { font: inherit; }
button, input, select { border: 1px solid var(--control-border); border-radius: 9px; background: var(--surface); color: inherit; padding: 9px 12px; min-height: 42px; }
input[type=checkbox] { min-height: auto; accent-color: var(--dark); }
button { font-weight: 650; cursor: pointer; }
button:hover { background: var(--soft); }
button:disabled { opacity: .6; cursor: wait; }
.primary { color: white; background: var(--primary); border-color: var(--primary); }
.primary:hover { background: var(--primary-hover); }
.with-icon { display: inline-flex; align-items: center; gap: 8px; }
.with-icon svg { width: 18px; height: 18px; flex: none; color: var(--interactive); }
.primary.with-icon svg { color: #baf4d0; }
:focus-visible { outline: 2px solid var(--interactive); outline-offset: 3px; }
.metrics { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; margin-block: 12px 8px; }
#cash-definition { margin-bottom: 26px; font-size: .86rem; }
.metrics > div, .history { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 17px; }
.metrics > div { border-top: 3px solid var(--accent); }
.metrics dt { color: var(--muted); font-size: .86rem; }
.metrics dd { margin-top: 8px; font-weight: 750; font-size: clamp(1rem, 2vw, 1.45rem); overflow-wrap: anywhere; }
.history .section-heading { margin-bottom: 9px; }
#feedback { margin-bottom: 12px; }
#receivables { margin-bottom: 18px; }
#receivables summary { cursor: pointer; }
#receivables-summary { color: var(--muted); font-size: .88rem; }
#receivables[open] #receivables-entries { margin-top: 12px; }
.overdue { color: var(--danger); font-weight: 650; }
.entry { display: grid; grid-template-columns: minmax(0, 1fr) auto auto; gap: 12px; align-items: center; padding-block: 13px; border-top: 1px solid var(--border); }
.entry p { font-size: .86rem; }
.empty { padding-block: 20px 12px; }
label { display: grid; gap: 5px; font-size: .89rem; font-weight: 600; }
label.inline { display: inline-flex; align-items: center; }
input { width: 100%; min-width: 0; }
label.inline input { width: auto; }
dialog { width: min(560px, calc(100% - 24px)); max-height: calc(100% - 24px); padding: 22px; border: 1px solid var(--border); border-radius: 15px; color: inherit; }
dialog::backdrop { background: var(--scrim); }
form { display: grid; gap: 15px; }
form .actions { justify-content: flex-end; }
fieldset { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; border: 1px solid var(--border); border-radius: 10px; margin: 0; padding: 12px; }
#expense-fields, #expense-fields label, #expense-fields select { min-inline-size: 0; }
#expense-fields label { grid-template-columns: minmax(0, 1fr); }
#expense-fields select { inline-size: 100%; }
#expense-fields .expense-wide { grid-column: 1 / -1; }
#expense-fields select[name="tripId"] { text-overflow: ellipsis; }
legend { font-size: .88rem; padding-inline: 5px; color: var(--muted); }
.danger, #form-feedback { color: var(--danger); }
#account-panel { margin-top: 22px; padding: 14px 17px; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; }
#account-panel:not(:has(> :not([hidden]))) { display: none; }
#turnstile-status { font-size: .88rem; }
#import-device { margin-top: 12px; }
#conflicts > article { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--border); }
#conflicts .actions { margin-top: 10px; }
#csv-preview ul { margin: 8px 0 0; padding-inline-start: 22px; }
#csv-preview li { margin-block: 4px; overflow-wrap: anywhere; }
#entry-source dt { margin-top: 8px; font-weight: 650; }
#entry-source dd { overflow-wrap: anywhere; }

#period-controls { margin-bottom: 16px; align-items: end; }
.period-inputs { display: flex; align-items: end; gap: 12px; }
.period-inputs select { min-width: 132px; }
#year { width: 96px; }
#mobile-nav { display: flex; gap: 8px; flex-wrap: wrap; width: min(1120px, calc(100% - 36px)); margin: 16px auto 0; }
#mobile-nav button { display: inline-flex; align-items: center; gap: 8px; }
#mobile-nav button[aria-current="page"] { color: var(--dark); background: var(--soft); }
#mobile-nav svg { width: 18px; height: 18px; flex: none; color: var(--interactive); }

@media (max-width: 760px) {
  .with-mobile-nav { padding-bottom: calc(78px + env(safe-area-inset-bottom, 0px)); }
  #mobile-nav {
    position: fixed;
    z-index: 4;
    inset: auto 0 0;
    width: 100%;
    margin: 0;
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    padding: 6px max(3px, env(safe-area-inset-right, 0px)) calc(6px + env(safe-area-inset-bottom, 0px)) max(3px, env(safe-area-inset-left, 0px));
    border-top: 1px solid var(--border);
    background: var(--surface);
    box-shadow: 0 -4px 18px var(--shadow);
  }
  #mobile-nav button {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    min-height: 54px;
    padding: 4px 1px;
    border: 0;
    color: var(--muted);
    font-size: clamp(.52rem, 2.55vw, .73rem);
    line-height: 1.12;
    overflow-wrap: anywhere;
  }
  #mobile-nav button[aria-current="page"] { color: var(--dark); background: var(--soft); }
  #mobile-nav button:focus-visible { outline-offset: -2px; }
  #mobile-nav svg { width: 21px; height: 21px; flex: none; color: var(--interactive); }
}

@media (max-width: 670px) {
  main { padding-block: 24px; }
  .page-heading { margin-bottom: 20px; }
  .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .entry { grid-template-columns: minmax(0, 1fr) auto; }
  .entry > div { grid-column: 1 / -1; }
  .entry button { justify-self: end; }
}

@media (max-width: 390px) {
  fieldset { grid-template-columns: 1fr; }
}

.theme-picker {
  display: inline-flex;
  gap: 2px;
  padding: 3px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}

.theme-picker button {
  display: grid;
  place-items: center;
  width: 36px;
  min-height: 36px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--muted);
}
.theme-picker button:hover,
.theme-picker button[aria-pressed="true"] { color: var(--interactive); background: var(--soft); }
.theme-picker svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

.login-brand { display: none; }
.login-card { min-width: 0; }
.login-card h1 { margin-bottom: 24px; font-size: clamp(1.5rem, 4vw, 2rem); }
#login-form { display: grid; gap: 15px; }
#login-form input, #login-form button[type="submit"] { min-height: 48px; }
#login-form input { font-size: 16px; }
#login-form button[type="submit"] { width: 100%; }
#login-verification { min-width: 0; }
#login-feedback { margin-top: 14px; color: var(--danger); font-size: .9rem; }

.auth-screen header { background: transparent; border-color: transparent; }
.auth-screen .topbar { justify-content: flex-end; }
.auth-screen .topbar > .brand { display: none; }
.auth-screen #account-panel { width: min(960px, calc(100% - 36px)); min-height: calc(100svh - 106px - env(safe-area-inset-top, 0px)); display: grid; align-content: center; padding: 0 0 32px; margin-top: 0; border: 0; background: transparent; }
.auth-screen #login-content { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); overflow: hidden; border: 1px solid var(--border); border-radius: 22px; box-shadow: 0 24px 70px var(--shadow); }
.auth-screen .login-brand {
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: clamp(28px, 6vw, 64px);
  color: #f2fcf5;
  background: radial-gradient(ellipse at 100% 100%, #28734e, transparent 65%), #173e2b;
  border-bottom: 4px solid #8edeb5;
}
.login-brand img { display: block; }
.login-brand .login-wordmark { color: inherit; font-size: clamp(1.9rem, 5vw, 3rem); font-weight: 780; letter-spacing: -.05em; margin: 12px 0 0; }
.login-wordmark span { color: #baf4d0; font-weight: 580; }
.login-brand p { color: #d4eddc; }
.auth-screen .login-card { padding: clamp(24px, 5vw, 52px); background: var(--surface); }
#account-panel:has(#login-content:not([hidden])) #cloud-feedback { margin-top: 12px; }

@media (max-width: 670px) {
  .auth-screen #account-panel { min-height: auto; }
  .auth-screen #login-content { width: min(440px, 100%); margin-inline: auto; grid-template-columns: minmax(0, 1fr); border-radius: 18px; }
  .auth-screen .login-brand { padding: 22px 24px; display: grid; grid-template-columns: auto 1fr; column-gap: 12px; align-items: center; border-bottom-width: 3px; }
  .login-brand img { width: 50px; height: 50px; grid-row: span 2; }
  .login-brand .login-wordmark { font-size: 1.65rem; line-height: 1.15; margin: 0; }
  .login-brand p { font-size: .85rem; margin-top: 3px; }
  .login-card h1 { margin-bottom: 18px; }
  .topbar .actions { gap: 7px; }
  .topbar .theme-picker button { width: 30px; min-height: 34px; }
}
