:root {
  --teal: #0F766E;
  --teal-dark: #115E59;
  --teal-mid: #99F6E4;
  --teal-light: #CCFBF1;
  --bg: #F8FAFC;
  --surface: #FFFFFF;
  --text: #0F172A;
  --text-2: #475569;
  --text-3: #64748B;
  --border: #E2E8F0;
  --border-soft: #F1F5F9;
  --border-strong: #CBD5E1;
  --danger: #B91C1C;
  --radius: 12px;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 16px;
  line-height: 1.4;
  color: var(--text);
  background: var(--bg);
}
h1, h2, h3, p, ul, dl, dd { margin: 0; }
h1 { font-size: 28px; line-height: 1.25; }
h2 { font-size: 18px; }
ul { padding: 0; list-style: none; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { opacity: .5; cursor: default; }
a { color: var(--teal); }
[hidden] { display: none !important; }
#main:focus { outline: none; }
:focus-visible { outline: 2px solid var(--teal); outline-offset: 2px; }

.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.muted { color: var(--text-3); }
.small { font-size: 13px; }
.num { font-variant-numeric: tabular-nums; }
.strong { font-weight: 600; }
.stack { display: flex; flex-direction: column; gap: 16px; }
.narrow { width: 100%; max-width: 560px; margin-inline: auto; }
.self-start { align-self: flex-start; }
.label-caps { font-size: 14px; font-weight: 700; color: var(--text-3); text-transform: uppercase; }
.brand { font-weight: 700; font-size: 18px; color: var(--teal); text-decoration: none; }
.skip { position: absolute; left: 8px; top: -60px; z-index: 30; padding: 12px 16px; background: var(--surface); border-radius: 8px; }
.skip:focus { top: 8px; }

/* ------------------------------------------------------------ layout */

.sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  width: 220px;
  padding: 24px;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-right: 1px solid var(--border);
  font-size: 15px;
  overflow-y: auto;
}
.sidebar .brand { margin-bottom: 26px; }
.sidebar nav { display: flex; flex-direction: column; gap: 10px; }
.sidebar .nav-link {
  display: flex;
  align-items: center;
  min-height: 44px;
  padding: 10px 12px;
  border: 0;
  border-radius: 8px;
  background: none;
  color: var(--text-2);
  text-align: left;
  text-decoration: none;
}
.sidebar .nav-link svg { display: none; }
.sidebar .nav-link:hover { background: var(--border-soft); }
.sidebar .nav-link[aria-current] { background: var(--teal-light); color: var(--teal); font-weight: 600; }
.sidebar-foot { margin-top: auto; padding-top: 24px; display: flex; flex-direction: column; gap: 4px; }
.sidebar-foot .muted { padding: 0 12px; font-size: 13px; overflow-wrap: anywhere; }
.topbar, .bottom-nav { display: none; }

#main {
  margin-left: 220px;
  max-width: 1220px;
  min-height: 100vh;
  padding: 32px 40px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}
#main[aria-busy=true] { opacity: .6; }
body[data-page="setup"] #main { margin: 0 auto; }

.page-head { display: flex; justify-content: space-between; align-items: center; gap: 16px; }
.kicker { font-size: 14px; color: var(--text-3); }
.page-head h1 { margin-top: 4px; }
.head-side { align-self: flex-end; display: flex; align-items: center; gap: 12px; font-size: 15px; }
.grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.card-head-side { display: flex; align-items: center; gap: 12px; font-size: 15px; }

.stat-label { font-size: 13px; color: var(--text-3); }
.stat-value { margin-top: 6px; font-size: 26px; line-height: 1.3; font-weight: 700; overflow-wrap: anywhere; }
.stat-note { margin-top: 4px; font-size: 13px; color: var(--text-3); }

/* ----------------------------------------------------------- controls */

.btn {
  min-height: 44px;
  padding: 0 20px;
  border: 1px solid var(--teal);
  border-radius: 10px;
  background: var(--teal);
  color: #FFFFFF;
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
}
.btn:hover:not(:disabled) { background: var(--teal-dark); border-color: var(--teal-dark); }
.btn.secondary { background: var(--surface); border-color: var(--border-strong); color: var(--text); }
.btn.secondary:hover:not(:disabled) { background: var(--border-soft); border-color: var(--border-strong); }
.btn.danger, .link-btn.danger { color: var(--danger); }
.icon-btn {
  flex: none;
  width: 44px;
  height: 44px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text-2);
  font-size: 18px;
  line-height: 1;
}
.icon-btn:hover:not(:disabled) { background: var(--border-soft); }
.icon-btn.add { color: var(--teal); font-size: 22px; }
.link-btn { min-height: 44px; min-width: 44px; padding: 0 8px; border: 0; background: none; color: var(--teal); font-size: 14px; font-weight: 600; }
.link-btn:hover { text-decoration: underline; }
.pager { display: flex; gap: 8px; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 14px; font-weight: 600; }
input[type=text], input[type=password], select {
  width: 100%;
  min-width: 0;
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  background: var(--surface);
  font-size: 16px; /* below 16px, iOS zooms the page on focus */
}
input[aria-invalid=true], select[aria-invalid=true] { border-color: var(--danger); }
.hint { font-size: 13px; color: var(--text-3); }
.field-error { font-size: 13px; color: var(--danger); }
.field-error:empty { display: none; }
.field:has([aria-invalid=true]) .hint { display: none; }
.form-error { padding: 12px; border: 1px solid #FECACA; border-radius: 10px; background: #FEF2F2; color: var(--danger); font-size: 14px; }
.form { display: flex; flex-direction: column; gap: 16px; padding: 24px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; align-items: start; }
.form-actions { display: flex; flex-wrap: wrap; gap: 12px; }

/* -------------------------------------------------- log in and sign up */

.auth {
  min-height: 100vh;
  padding: 40px 48px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 420px);
  gap: 64px;
  align-items: center;
  max-width: 1120px;
  margin: 0 auto;
}
.auth .brand { font-size: 22px; }
.auth-intro { display: flex; flex-direction: column; gap: 16px; }
.auth-intro h1 { font-size: 40px; line-height: 1.15; letter-spacing: -.02em; text-wrap: balance; }
.auth-intro p { font-size: 17px; color: var(--text-2); max-width: 44ch; }
.auth .form h2 { font-size: 22px; }
.auth .btn { width: 100%; }
.auth-switch { font-size: 14px; color: var(--text-2); text-align: center; }
.auth-switch a { display: inline-block; padding: 12px 4px; font-weight: 600; }

/* -------------------------------------------------------------- Today */

.checklist { padding: 0; overflow: hidden; }
.check-row { display: flex; align-items: center; padding-right: 12px; }
.check-row + .check-row { border-top: 1px solid var(--border-soft); }
.check { flex: 1; min-width: 0; display: flex; align-items: center; gap: 16px; min-height: 62px; padding: 12px 12px 12px 20px; cursor: pointer; }
.check input { position: absolute; opacity: 0; width: 26px; height: 26px; margin: 0; }
.box {
  flex: none;
  width: 26px;
  height: 26px;
  display: grid;
  place-items: center;
  border: 2px solid var(--border-strong);
  border-radius: 7px;
  color: transparent;
  font-size: 14px;
  line-height: 1;
}
.check:hover .box { border-color: var(--teal); }
.check input:checked + .box { background: var(--teal); border-color: var(--teal); color: #FFFFFF; }
.check input:focus-visible + .box { outline: 2px solid var(--teal); outline-offset: 2px; }
.check-name { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.done .check-name { text-decoration: line-through; color: var(--text-3); }
.check-amount { font-size: 13px; color: var(--text-3); text-align: right; font-variant-numeric: tabular-nums; }
.over { color: var(--danger) !important; }

.need-value { margin-top: 6px; font-size: 20px; font-weight: 700; }
.need-explain { margin-top: 6px; font-size: 14px; color: var(--text-2); }
.formula { margin-top: 8px; padding: 8px 10px; border-radius: 8px; background: var(--bg); font-size: 13px; color: var(--text-2); }

/* --------------------------------------------------------------- Week */

.week-layout { flex: 1; display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 24px; align-items: start; }
.week-card { display: flex; flex-direction: column; gap: 20px; padding: 28px; min-height: 620px; height: calc(100vh - 64px); max-height: 760px; }
.week-card h1 { font-size: 24px; }
.week-card .scroll-x { flex: 1; display: flex; }
.week-side { display: flex; flex-direction: column; gap: 16px; }
.week-side .card { padding: 22px; }
.big-pct { margin-top: 6px; font-size: 36px; line-height: 1.3; font-weight: 700; color: var(--teal); }
.breakdown { display: flex; flex-direction: column; gap: 12px; }
.break-row { display: flex; justify-content: space-between; gap: 12px; font-size: 15px; }
.break-row span:first-child { overflow-wrap: anywhere; }
.scroll-x { overflow-x: auto; }
.bars { flex: 1; min-width: 320px; display: flex; gap: 24px; }
.bar-col { flex: 1; display: flex; flex-direction: column; text-align: center; }
.bar-slot { flex: 1; min-height: 200px; padding-top: 24px; display: flex; flex-direction: column; justify-content: flex-end; border-bottom: 1px solid var(--border); }
.bar { min-height: 0; flex: none; background: var(--teal-light); border-radius: 8px 8px 0 0; }
.bar.strong { background: var(--teal); }
.bar-slot:hover .bar { filter: brightness(.95); }
.bar-value { font-size: 13px; color: var(--text-2); margin-bottom: 6px; font-variant-numeric: tabular-nums; }
.bar-day { padding-top: 8px; font-size: 14px; color: var(--text-3); }
.bar-day.now { color: var(--text); font-weight: 600; }

.legend { display: flex; flex-wrap: wrap; gap: 8px 20px; font-size: 13px; color: var(--text-2); }
.legend span { display: inline-flex; align-items: center; gap: 8px; }
.sw { width: 14px; height: 14px; border-radius: 4px; background: var(--border); }
.sw-low { background: var(--teal-light); }
.sw-mid { background: var(--teal-mid); }
.sw-done { background: var(--teal); }

/* -------------------------------------------------------------- Month */

.page-head:has(.head-side) h1 { font-size: 32px; }
.heat-card { display: flex; flex-direction: column; gap: 20px; padding: 20px; }
.heat { width: 100%; border-collapse: separate; border-spacing: 0; table-layout: fixed; }
.heat th { font-weight: 400; text-align: left; }
.heat thead th { font-size: 11px; color: var(--text-3); padding: 0 0 10px 1px; }
.heat thead th.now { color: var(--text); font-weight: 700; }
.heat thead th:first-child { width: 120px; font-size: 14px; font-weight: 700; color: var(--text-3); }
.heat tbody th { font-size: 16px; padding-right: 12px; border-bottom: 1px solid var(--border); overflow-wrap: anywhere; }
.heat thead th:first-child, .heat tbody th { position: sticky; left: 0; z-index: 1; background: var(--surface); }
.heat td { position: relative; padding: 3px 0 3px 5px; }
.heat td i { display: block; height: 44px; border-radius: 6px; background: var(--border); }
.heat td.l1 i { background: var(--teal-light); }
.heat td.l2 i { background: var(--teal-mid); }
.heat td.l3 i { background: var(--teal); }
.heat td.off i { background: none; border: 1px solid var(--border-soft); }
.heat td[data-tip]:hover i { outline: 2px solid var(--text); outline-offset: 1px; }

/* ------------------------------------------------- Trackers and Stats */

.tracker { display: flex; flex-direction: column; }
.tracker dl { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin: 16px 0 8px; }
.tracker dt { font-size: 13px; color: var(--text-3); }
.tracker dd { font-size: 16px; font-weight: 600; overflow-wrap: anywhere; }
.tracker-foot { margin-top: auto; padding-top: 12px; display: flex; flex-direction: column; align-items: flex-start; gap: 4px; }
.tracker-foot .link-btn { padding-left: 0; }
.quick { width: 100%; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.quick .btn { padding: 0 4px; }
.meter { height: 8px; margin-top: 12px; border-radius: 4px; background: var(--teal-light); overflow: hidden; }
.meter i { display: block; height: 100%; border-radius: 4px; background: var(--teal); }
.meter.over i { background: var(--danger); }
.stats-list { padding: 24px; }
.stat-row { padding-top: 18px; }
.stat-row .meter { margin-top: 8px; }
.stat-row .stat-note { margin-top: 8px; }
.stat-row-top { display: flex; justify-content: space-between; gap: 12px; }

/* ----------------------------------------- dialogs, toast and tooltip */

.dialog { width: min(440px, calc(100vw - 32px)); max-height: calc(100dvh - 32px); padding: 20px; border: 1px solid var(--border); border-radius: var(--radius); color: var(--text); overflow-y: auto; }
.dialog::backdrop { background: rgb(15 23 42 / .45); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.dialog-head h2 { font-size: 20px; overflow-wrap: anywhere; }
.dialog-body { display: flex; flex-direction: column; gap: 14px; margin-top: 8px; }
.amount-form { width: 100%; display: flex; gap: 8px; }
.input-unit { position: relative; flex: 1; min-width: 0; }
.input-unit input { padding-right: 74px; }
.input-unit span { position: absolute; right: 12px; top: 50%; translate: 0 -50%; max-width: 62px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; color: var(--text-3); pointer-events: none; }
.entry-list li { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; min-height: 52px; border-top: 1px solid var(--border-soft); font-size: 15px; }
.entry-list li .muted { flex: 1; font-size: 13px; }
.entry-list li.empty { font-size: 14px; }
.entry-amount { font-weight: 600; }
.entry-list .amount-form { padding: 6px 0; }
.entry-list .amount-form .btn { padding: 0 12px; }

.toast { position: fixed; left: 50%; bottom: 24px; z-index: 40; translate: -50% 20px; opacity: 0; pointer-events: none; max-width: calc(100vw - 32px); width: max-content; display: flex; align-items: center; gap: 12px; min-height: 48px; padding: 4px 18px; border-radius: 10px; background: var(--text); color: #FFFFFF; font-size: 14px; transition: opacity .15s, translate .15s; }
.toast.show { opacity: 1; translate: -50% 0; pointer-events: auto; }
.toast button { min-height: 44px; min-width: 44px; border: 0; background: none; color: var(--teal-mid); font-weight: 600; }
.tip { position: fixed; z-index: 35; padding: 8px 10px; border-radius: 8px; background: var(--text); color: #FFFFFF; font-size: 13px; opacity: 0; pointer-events: none; display: flex; flex-direction: column; max-width: 220px; }
.tip span { color: var(--border-strong); }
.tip.show { opacity: 1; }

/* ------------------------------------------------------------- tablet */

@media (max-width: 1023px) {
  #main { padding: 28px 24px; }
  .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .week-layout { grid-template-columns: minmax(0, 1fr); }
  .week-card { height: auto; min-height: 460px; }
  .heat { min-width: 820px; }
}

/* -------------------------------------------------------------- phone */

@media (max-width: 767px) {
  .sidebar { display: none; }
  .topbar {
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    justify-content: space-between;
    align-items: center;
    min-height: 56px;
    padding: 0 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
  }
  .topbar .brand { display: flex; align-items: center; min-height: 44px; }
  .topbar .logout { min-height: 44px; padding: 0 4px; border: 0; background: none; color: var(--text-2); font-size: 14px; font-weight: 600; }
  .bottom-nav {
    position: fixed;
    inset: auto 0 0 0;
    z-index: 20;
    display: grid;
    grid-template-columns: repeat(6, minmax(0, 1fr));
    padding-bottom: env(safe-area-inset-bottom);
    background: var(--surface);
    border-top: 1px solid var(--border);
  }
  .bottom-nav .nav-link { min-height: 60px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: 11px; color: var(--text-2); text-decoration: none; }
  .bottom-nav .nav-link[aria-current] { color: var(--teal); font-weight: 600; }

  #main { margin-left: 0; min-height: 0; padding: 20px 16px calc(84px + env(safe-area-inset-bottom)); gap: 20px; }
  body[data-page="setup"] #main { padding-bottom: 24px; }
  h1, .page-head:has(.head-side) h1 { font-size: 24px; }
  .grid, .form-grid { grid-template-columns: minmax(0, 1fr); }
  .page-head { flex-wrap: wrap; }
  .head-side { width: 100%; justify-content: space-between; }
  .check { gap: 12px; padding-left: 16px; }
  .week-card { padding: 20px; min-height: 420px; }
  .week-card .card-head { flex-wrap: wrap; }
  .card-head-side { margin-left: auto; }
  .bars { gap: 10px; min-width: 300px; }
  .heat-card { padding: 16px; }
  .heat { min-width: 1010px; }
  .heat thead th:first-child { width: 104px; }
  .heat tbody th { font-size: 14px; }
  .form { padding: 18px; }
  .toast { bottom: calc(76px + env(safe-area-inset-bottom)); }
  .dialog .toast { bottom: 24px; }

  .auth { grid-template-columns: minmax(0, 1fr); gap: 28px; padding: 32px 16px; align-content: start; }
  .auth-intro h1 { font-size: 30px; }
}

@media (prefers-reduced-motion: reduce) {
  .toast { transition: none; }
}
