:root {
  --bg: #F7F6F2;
  --surface: #FFFFFF;
  --ink: #17171A;
  --muted: #6A6860;
  --faint: #8A877E;
  --empty: #E6E3DB;
  --line: #E6E3DB;
  --edge: #CFCBC0;
  --danger: #B3261E;
  color-scheme: light;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.35 'IBM Plex Sans', system-ui, -apple-system, 'Segoe UI', sans-serif;
}
[hidden] { display: none !important; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
.mono { font-family: 'IBM Plex Mono', ui-monospace, monospace; }

/* ---------- sign in ---------- */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 8px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 24px;
}
.login-card img { align-self: center; }
.login-card h1 { margin: 4px 0 12px; text-align: center; font-size: 1.4rem; }
.login-msg { min-height: 1.4em; margin: 4px 0 0; color: var(--danger); font-size: .9rem; }

/* ---------- controls ---------- */
input, select {
  width: 100%; padding: 12px 14px; border-radius: 10px;
  border: 1px solid var(--edge); background: var(--surface);
}
input:focus-visible, select:focus-visible, button:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }
button { min-height: 44px; padding: 10px 16px; border-radius: 10px; border: 1px solid var(--edge); background: var(--surface); }
button.primary { background: var(--ink); color: var(--bg); border-color: var(--ink); font-weight: 600; }
button.danger { color: var(--danger); }
button.quiet { color: var(--muted); }
button.link { border: 0; background: none; color: var(--muted); text-decoration: underline; min-height: 44px; padding: 8px 0; }
button.wide { width: 100%; }
label, legend { font-size: .85rem; color: var(--muted); margin-top: 6px; font-weight: 500; }

/* ---------- layout ---------- */
.app { min-height: 100dvh; display: flex; flex-direction: column; }
.screen { flex: 1; width: 100%; max-width: 520px; margin: 0 auto; padding: calc(20px + env(safe-area-inset-top)) 20px 16px; }
.tabs {
  position: sticky; bottom: 0; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr));
  background: var(--surface); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
}
.tab { border: 0; border-top: 3px solid transparent; border-radius: 0; background: none; min-height: 58px; font-weight: 500; color: #5E5C55; }
.tab[aria-current="page"] { border-top-color: var(--ink); color: var(--ink); font-weight: 700; }

/* ---------- headers ---------- */
.head { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; margin-bottom: 16px; }
.eyebrow { font-size: 13px; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); font-weight: 600; }
.head h1 { margin: 2px 0 0; font-size: 32px; line-height: 1.05; font-weight: 700; }
.head-right { display: flex; align-items: center; gap: 8px; }
.badge { font-family: 'IBM Plex Mono', monospace; font-size: 15px; font-weight: 600; padding: 6px 10px; border-radius: 8px; background: var(--ink); color: var(--bg); }
.icon-btn { width: 44px; height: 44px; padding: 0; display: grid; place-items: center; border-radius: 10px; }

/* ---------- this week grid ---------- */
.grid { display: grid; grid-template-columns: 112px repeat(7, minmax(0, 1fr)); column-gap: 6px; row-gap: 8px; align-items: center; }
.dayhead { display: flex; flex-direction: column; align-items: center; gap: 1px; font-family: 'IBM Plex Mono', monospace; }
.dayhead .l { font-size: 12px; font-weight: 600; color: var(--muted); }
.dayhead .n { font-size: 11px; color: var(--faint); }
.dayhead.today .l, .dayhead.today .n { color: var(--ink); }
.section { grid-column: 1 / -1; margin: 6px 0 0; font-size: 12px; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); font-weight: 600; }
.section.second { margin-top: 10px; }
.rowlabel { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.rowlabel .name { font-size: 16px; font-weight: 600; overflow-wrap: anywhere; }
.rowlabel .sub { font-family: 'IBM Plex Mono', monospace; font-size: 11px; color: var(--muted); }
.pips { display: flex; gap: 3px; flex-wrap: wrap; }
.pips span { width: 14px; height: 5px; border-radius: 3px; background: #DDD9CF; }
.cell { width: 100%; aspect-ratio: 1; min-height: 34px; padding: 0; border-radius: 8px; border: 2px solid transparent; }
.cell.future { border: 1.5px dashed #D5D1C6; background: transparent; }
.week-foot { margin-top: 18px; display: flex; flex-direction: column; gap: 8px; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); flex-wrap: wrap; }
.legend span { display: flex; align-items: center; gap: 6px; }
.legend i { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }
.sofar { display: flex; justify-content: space-between; font-size: 13px; color: var(--muted); }
.sofar b { font-family: 'IBM Plex Mono', monospace; color: var(--ink); }
.bar { height: 8px; border-radius: 4px; background: var(--empty); overflow: hidden; }
.bar div { height: 100%; border-radius: 4px; background: var(--ink); }
.status { align-self: flex-start; border: 0; background: none; min-height: 32px; padding: 4px 0; font-size: 12px; color: var(--muted); font-family: 'IBM Plex Mono', monospace; }

/* ---------- history ---------- */
.segs { display: grid; grid-template-columns: repeat(var(--n, 8), minmax(0, 1fr)); gap: 4px; margin: 2px 0 8px; }
.segs div { height: 8px; border-radius: 4px; background: var(--empty); }
.subline { font-family: 'IBM Plex Mono', monospace; font-size: 13px; color: var(--muted); margin-bottom: 14px; }
.tiles { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.tile {
  display: flex; flex-direction: column; justify-content: space-between; gap: 10px; text-align: left;
  padding: 12px; border-radius: 14px; border: 2px solid var(--line); background: var(--surface); min-width: 0;
}
.tile .top { display: flex; justify-content: space-between; align-items: flex-start; gap: 6px; }
.tile .tname { font-size: 19px; font-weight: 700; line-height: 1.1; display: block; overflow-wrap: anywhere; }
.tile .tstats { font-family: 'IBM Plex Mono', monospace; font-size: 12px; font-weight: 600; display: block; margin-top: 2px; }
.heat { display: grid; grid-template-rows: repeat(7, 8px) 4px; grid-auto-flow: column; grid-auto-columns: 8px; gap: 2px; }
.heat span { border-radius: 2px; }

/* ---------- dialogs ---------- */
dialog {
  width: min(440px, calc(100vw - 24px)); max-height: calc(100dvh - 32px); border: 1px solid var(--line); border-radius: 16px;
  background: var(--surface); color: var(--ink); padding: 0;
}
dialog::backdrop { background: rgba(0,0,0,.45); }
.dlg { display: flex; flex-direction: column; gap: 8px; padding: 20px; }
.dlg h2 { margin: 0; font-size: 1.2rem; }
.dlg h3 { margin: 14px 0 4px; font-size: .85rem; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.dlg-head { display: flex; justify-content: space-between; align-items: center; }
.close { width: 44px; height: 44px; padding: 0; border: 0; background: none; font-size: 1.6rem; color: var(--muted); }
.dlg-actions { display: flex; gap: 8px; margin-top: 14px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.habit-list { list-style: none; margin: 4px 0 8px; padding: 0; display: flex; flex-direction: column; }
.habit-list li { display: flex; align-items: center; gap: 8px; padding: 4px 0; border-bottom: 1px solid var(--line); }
.habit-list .dot { width: 14px; height: 14px; border-radius: 4px; flex: none; }
.habit-list .hname { flex: 1; font-weight: 600; min-width: 0; overflow-wrap: anywhere; }
.habit-list .freq { font-family: 'IBM Plex Mono', monospace; font-size: 12px; color: var(--muted); }
.habit-list button { min-width: 44px; padding: 8px; border: 0; background: none; color: var(--muted); }
.swatches { border: 0; padding: 0; margin: 6px 0 0; }
.swatch-grid { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 8px; margin-top: 6px; }
.swatch { aspect-ratio: 1; min-height: 40px; padding: 0; border-radius: 10px; border: 3px solid transparent; }
.swatch[aria-pressed="true"] { border-color: var(--ink); box-shadow: inset 0 0 0 2px #fff; }
