:root {
  --bg: #f6f5f2;
  --card: #ffffff;
  --text: #1d1d1f;
  --dim: #6e6e73;
  --line: #e2e0db;
  --accent: #c98a12;
  --ok: #2e9e55;
  --warn: #c0392b;
  /* chart series, in habit creation order (validated categorical palette) */
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #6250d6; --s8: #e34948;
  --other: #8e8d88;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161616;
    --card: #222222;
    --text: #ececec;
    --dim: #9a9a9a;
    --line: #333333;
    --accent: #eaa221;
    --ok: #4cc777;
    --warn: #e2574c;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
    --other: #7a7974;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  padding: 0 16px 48px;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, sans-serif;
}
header, main { max-width: 860px; margin: 0 auto; }
header { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; padding: 16px 0; }
h1 { font-size: 1.3rem; margin: 0; color: var(--accent); }
nav { display: flex; flex-wrap: wrap; gap: 4px; }

button {
  font: inherit;
  padding: 6px 12px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
}
button.secondary, nav button { background: transparent; color: var(--text); border-color: var(--line); }
nav button.active { border-color: var(--accent); color: var(--accent); }
button.link { border: none; background: none; color: var(--dim); }
button.small { padding: 2px 8px; font-size: 0.85rem; }

input, select {
  font: inherit;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  width: 100%;
}
input[type="checkbox"] { width: auto; }
label { display: flex; flex-direction: column; gap: 4px; flex: 1; min-width: 140px; }
label.check { flex-direction: row; align-items: center; gap: 8px; }
fieldset { border: 1px solid var(--line); border-radius: 6px; display: flex; flex-wrap: wrap; gap: 12px; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 16px;
  display: flex; flex-direction: column; gap: 12px; margin-bottom: 16px; }
.row { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; margin-bottom: 12px; }
.card .row { margin-bottom: 0; }
.hint { color: var(--dim); font-size: 0.85rem; margin: 0; }
.error { color: var(--warn); margin: 0; }

table { display: table; width: 100%; border-collapse: collapse; background: var(--card); border-radius: 10px; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); }
th { color: var(--dim); font-weight: 500; font-size: 0.85rem; }
td.num { text-align: right; font-variant-numeric: tabular-nums; }

.habit { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 12px 16px;
  margin-bottom: 8px; display: flex; flex-wrap: wrap; justify-content: space-between; gap: 8px; }
.pill { font-size: 0.85rem; padding: 2px 8px; border-radius: 99px; border: 1px solid var(--line); color: var(--dim); }
.pill.done { color: var(--ok); border-color: var(--ok); }
.pill.due { color: var(--accent); border-color: var(--accent); }
.pill.missed, .pill.skipped { color: var(--warn); border-color: var(--warn); }

.row.top { align-items: flex-start; }
#log-form .row.top > label { flex: 0 1 300px; }
.field { display: flex; flex-direction: column; gap: 4px; }

/* day picker (Log form) */
#day-picker { width: 252px; }
.dp-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 4px; }
.dp-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; text-align: center; }
.dp-grid span { color: var(--dim); font-size: 0.75rem; padding: 2px 0; }
.dp-grid button { padding: 5px 0; font-size: 0.9rem; background: transparent; color: var(--text);
  border: 1px solid transparent; font-variant-numeric: tabular-nums; }
.dp-grid button.today { border-color: var(--line); }
.dp-grid button.picked { background: var(--accent); border-color: var(--accent); color: #fff; }
.dp-grid button:not(:disabled):not(.picked):hover { border-color: var(--accent); }
.dp-grid button:disabled, .dp-head button:disabled { opacity: 0.35; cursor: default; }

/* ---------- stats ---------- */
h2 { font-size: 1rem; font-weight: 600; margin: 0; }
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 16px; }
.tile { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 12px 16px;
  display: flex; flex-direction: column; gap: 2px; }
.tile strong { font-size: 1.6rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.tile span { color: var(--dim); font-size: 0.85rem; }
.tile em { font-style: normal; color: var(--text); }

.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; color: var(--dim); font-size: 0.85rem; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 2px; vertical-align: -1px; margin-right: 4px; }
.legend .swatch { margin-right: 0; }

.grid-row { display: flex; align-items: center; gap: 10px; }
.grid-name { flex: 0 0 130px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 0.9rem; }
.cells { flex: 1; display: grid; grid-template-columns: repeat(var(--n), minmax(0, 28px)); gap: 3px; padding: 2px 0; }
.cells.axis span { color: var(--dim); font-size: 0.75rem; text-align: center; overflow: visible; white-space: nowrap; }
.cell { display: block; aspect-ratio: 1; border-radius: 3px; border: 1.5px solid transparent; }
.legend .cell { width: 14px; }
.cell.done { background: var(--ok); }
.cell.partial { border-color: var(--ok); background: linear-gradient(135deg, var(--ok) 50%, transparent 50%); }
.cell.missed { border-color: var(--warn); }
.cell.skipped { border-color: var(--dim);
  background: linear-gradient(var(--dim), var(--dim)) center / 50% 1.5px no-repeat; }
.cell.pending { border: 1.5px dashed var(--accent); }
.cell.none { background: var(--line); opacity: 0.5; }

#stats-chart { padding-left: 24px; }
.plot { position: relative; height: 180px; margin-bottom: 22px; border-bottom: 1px solid var(--line); }
.gridline { position: absolute; left: 0; right: 0; border-top: 1px solid var(--line); }
.gridline span { position: absolute; left: -24px; top: -0.6em; width: 18px; text-align: right;
  color: var(--dim); font-size: 0.75rem; font-variant-numeric: tabular-nums; }
.cols { position: absolute; inset: 0; display: flex; gap: 2px; }
.col { flex: 1; position: relative; display: flex; flex-direction: column; justify-content: flex-end; }
.col:hover { background: color-mix(in srgb, var(--line) 50%, transparent); }
.stack { display: flex; flex-direction: column-reverse; height: 100%; justify-content: flex-start; align-items: center; }
.seg { width: min(70%, 28px); border-top: 2px solid var(--card); }
.seg:last-child { border-radius: 4px 4px 0 0; }
.col > span { position: absolute; top: 100%; left: 50%; transform: translateX(-50%); padding-top: 4px;
  color: var(--dim); font-size: 0.75rem; white-space: nowrap; }

#tip { position: fixed; z-index: 10; pointer-events: none; max-width: 260px; padding: 6px 10px; border-radius: 6px;
  background: var(--card); color: var(--text); border: 1px solid var(--line); font-size: 0.85rem;
  box-shadow: 0 4px 14px rgb(0 0 0 / 0.15); }

/* ---------- device ---------- */
.themes { display: flex; flex-wrap: wrap; gap: 16px; }
.theme { display: flex; flex-direction: column; align-items: center; gap: 8px; padding: 12px;
  background: transparent; color: var(--text); border: 2px solid var(--line); border-radius: 12px; }
.theme.picked { border-color: var(--accent); }
.theme:not(.picked):hover { border-color: var(--dim); }
/* round like the device's screen, in a dark bezel like the simulator's */
.theme img { display: block; border-radius: 50%; border: 6px solid #2a2a2a; background: #2a2a2a; }

@media (max-width: 600px) {
  .grid-name { flex-basis: 80px; font-size: 0.8rem; }
  .grid-row { gap: 6px; }
  .cells { gap: 1px; }
  .cell { border-radius: 1px; border-width: 1px; }
  table { display: block; overflow-x: auto; white-space: nowrap; font-size: 0.9rem; }
  th, td { padding: 6px; }
}
