/* Focus. Plain product UI: solid surfaces, 1px borders, small radii, no decoration.
   Accent is reserved for the primary action, focus rings and the current tab.
   Subject colours only mark which subject a value belongs to. */

:root {
  color-scheme: light;
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #f0eee8;
  --line: #e3e0d8;
  --line-strong: #cbc6ba;
  --text: #1d2125;
  --muted: #545c61;
  --accent: #2f6871;
  --accent-hover: #24525a;
  --on-accent: #ffffff;
  --break: #a85d42;
  --danger: #a63d2a;
  --heat-0: #e9e6df;
  --heat-1: #c6dfe1;
  --heat-2: #8fc0c5;
  --heat-3: #4f949c;
  --heat-4: #2f6871;
  --radius: 8px;
  --radius-sm: 6px;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  --font: system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  --header-h: 52px;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121517;
  --surface: #181c1f;
  --surface-2: #20262a;
  --line: #2b3236;
  --line-strong: #3d464c;
  --text: #e6e9eb;
  --muted: #a2abb1;
  --accent: #6fb3bb;
  --accent-hover: #8cc5cc;
  --on-accent: #0e1a1c;
  --break: #dd987b;
  --danger: #ec8a74;
  --heat-0: #232a2e;
  --heat-1: #22423f;
  --heat-2: #2f646a;
  --heat-3: #4c9098;
  --heat-4: #7cc3cb;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]):not([data-theme="dark"]) {
    color-scheme: dark;
    --bg: #121517;
    --surface: #181c1f;
    --surface-2: #20262a;
    --line: #2b3236;
    --line-strong: #3d464c;
    --text: #e6e9eb;
    --muted: #a2abb1;
    --accent: #6fb3bb;
    --accent-hover: #8cc5cc;
    --on-accent: #0e1a1c;
    --break: #dd987b;
    --danger: #ec8a74;
    --heat-0: #232a2e;
    --heat-1: #22423f;
    --heat-2: #2f646a;
    --heat-3: #4c9098;
    --heat-4: #7cc3cb;
    --shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
  }
}

/* Base ------------------------------------------------------------------- */

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
}

h1, h2, p, dl, dd { margin: 0; }
h1 { font-size: 20px; font-weight: 600; }
h2 { font-size: 15px; font-weight: 600; }

a { color: var(--accent); text-underline-offset: 2px; }
a:hover { color: var(--accent-hover); }

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.visually-hidden {
  position: absolute !important;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.skip-link {
  position: absolute;
  top: -100px;
  left: 8px;
  z-index: 100;
  padding: 8px 12px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
}
.skip-link:focus { top: 8px; }

.muted { color: var(--muted); }

/* Header and navigation ---------------------------------------------------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.topbar-inner {
  display: flex;
  align-items: stretch;
  gap: 24px;
  max-width: 1200px;
  height: var(--header-h);
  margin: 0 auto;
  padding: 0 24px;
}

.brand {
  display: flex;
  align-items: center;
  color: var(--text);
  font-weight: 600;
  text-decoration: none;
}
.brand:hover { color: var(--text); }

.nav { display: flex; align-items: stretch; gap: 4px; }

.nav a {
  display: flex;
  align-items: center;
  padding: 0 10px;
  border-bottom: 2px solid transparent;
  color: var(--muted);
  font-weight: 500;
  text-decoration: none;
  transition: color 150ms ease, border-color 150ms ease;
}
.nav a:hover { color: var(--text); }
.nav a[aria-current="page"] { border-bottom-color: var(--accent); color: var(--text); }

.sync {
  display: flex;
  align-items: center;
  margin-left: auto;
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
}
.sync[data-state="offline"], .sync[data-state="login"] { color: var(--danger); }

.bottom-nav { display: none; }

/* Page --------------------------------------------------------------------- */

.page {
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px;
  outline: none;
}

.view { display: grid; gap: 16px; }

.view-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 36px;
}

.actions { display: flex; flex-wrap: wrap; gap: 8px; }

.panel {
  min-width: 0;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

.panel-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 4px 12px;
  margin-bottom: 12px;
}
.panel-head span, .panel-head a { font-size: 13px; }

.empty { padding: 12px 0; color: var(--muted); }
.empty .btn { margin-top: 8px; }

/* Controls ------------------------------------------------------------------ */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 36px;
  padding: 0 12px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  transition: background-color 150ms ease, border-color 150ms ease;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary { border-color: var(--accent); background: var(--accent); color: var(--on-accent); }
.btn-primary:hover { border-color: var(--accent-hover); background: var(--accent-hover); }
.btn-lg { min-height: 44px; padding: 0 18px; font-size: 15px; }
.btn-text {
  min-height: 32px;
  padding: 0 8px;
  border-color: transparent;
  background: transparent;
  color: var(--muted);
}
.btn-text:hover { background: var(--surface-2); color: var(--text); }
.btn-text.danger:hover { color: var(--danger); }
.btn-icon { width: 36px; padding: 0; }
.btn-icon svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.btn-group {
  display: inline-flex;
  flex-wrap: wrap;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  overflow: hidden;
}
.btn-group button {
  min-height: 34px;
  padding: 0 12px;
  border: 0;
  border-right: 1px solid var(--line-strong);
  background: transparent;
  color: var(--muted);
  font-size: 14px;
  font-weight: 500;
  transition: background-color 150ms ease, color 150ms ease;
}
.btn-group button:last-child { border-right: 0; }
.btn-group button:hover { background: var(--surface-2); color: var(--text); }
.btn-group button[aria-pressed="true"] { background: var(--surface-2); color: var(--text); font-weight: 600; }
.btn-group button:focus-visible { outline-offset: -2px; }

label, .label { font-size: 13px; font-weight: 500; }

select, input[type="text"], input[type="number"], input[type="date"], input[type="time"], input[type="password"] {
  width: 100%;
  min-height: 36px;
  padding: 6px 10px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-size: 14px;
}
input[type="checkbox"], input[type="radio"] { width: 16px; height: 16px; margin: 0; accent-color: var(--accent); }

.field { display: grid; gap: 4px; min-width: 0; margin: 0; padding: 0; border: 0; }
.field-row { display: flex; gap: 8px; }
.field-row select { flex: 1; min-width: 0; }
.field-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 16px; }
.hint { color: var(--muted); font-size: 13px; font-weight: 400; }
.form-error { min-height: 20px; color: var(--danger); font-size: 13px; }

.input-unit { display: flex; align-items: center; gap: 8px; }
.input-unit input { max-width: 120px; }
.input-unit span { color: var(--muted); font-size: 13px; }

.check { display: flex; align-items: flex-start; gap: 10px; font-size: 14px; font-weight: 400; cursor: pointer; }
.check input { margin-top: 3px; }
.check .hint { display: block; }

.swatch-sm {
  display: inline-block;
  flex-shrink: 0;
  width: 10px;
  height: 10px;
  border-radius: 2px;
  background: var(--c);
}

/* Tables --------------------------------------------------------------------- */

.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th {
  padding: 8px 12px 8px 0;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  font-size: 13px;
  font-weight: 500;
  text-align: left;
}
.table td { padding: 8px 12px 8px 0; border-bottom: 1px solid var(--line); vertical-align: middle; }
.table tr:last-child td { border-bottom: 0; }
.table tbody tr:hover td { background: var(--surface-2); }
.table th.right, .table td.right { text-align: right; font-variant-numeric: tabular-nums; }
.table td.cell-actions { width: 1%; padding-right: 0; white-space: nowrap; text-align: right; }
.subject-cell { display: flex; align-items: center; gap: 8px; min-width: 0; }
.subject-cell span:last-child { overflow-wrap: anywhere; }
.subject-note { display: block; color: var(--muted); font-size: 13px; font-weight: 400; }

/* Timer view ---------------------------------------------------------------- */

.timer-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}

.timer-panel { display: grid; gap: 20px; padding: 20px; }
.timer-panel .btn-group { justify-self: start; }

.readout { display: grid; gap: 6px; }
.time {
  font-size: clamp(56px, 7vw, 80px);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
  line-height: 1;
}
.readout-meta { color: var(--muted); }

.progress {
  height: 6px;
  border-radius: 3px;
  background: var(--surface-2);
  overflow: hidden;
}
.progress span { display: block; height: 100%; width: 0; background: var(--c, var(--accent)); }

.timer-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.timer-actions [data-action="toggle"] { min-width: 120px; }

.status { min-height: 22px; color: var(--muted); }

.today-summary { margin-bottom: 8px; color: var(--muted); }
.today-summary strong { color: var(--text); font-weight: 600; }

/* Log view ------------------------------------------------------------------- */

.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 16px; }
.period-nav { display: flex; align-items: center; gap: 8px; }
.period-label { min-width: 190px; font-weight: 600; text-align: center; }
.toolbar-select { width: 220px; margin-left: auto; }

.summary {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 32px;
  padding: 4px 0;
}
.summary div { display: grid; }
.summary dt { color: var(--muted); font-size: 13px; }
.summary dd { font-size: 17px; font-weight: 600; font-variant-numeric: tabular-nums; }
.summary dd small { color: var(--muted); font-size: 13px; font-weight: 400; }

.log-grid {
  display: grid;
  grid-template-columns: minmax(0, 3fr) minmax(0, 2fr);
  gap: 16px;
  align-items: start;
}

.chart svg { display: block; width: 100%; height: auto; }
.chart .grid-line { stroke: var(--line); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 11px; font-family: var(--font); }
.chart .axis.today { fill: var(--text); font-weight: 600; }
.chart .goal-line { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 4 3; }
.chart .hit { fill: transparent; cursor: pointer; }
.chart .hit:hover, .chart .hit:focus-visible { fill: color-mix(in srgb, var(--text) 6%, transparent); outline: none; }

.legend { display: flex; flex-wrap: wrap; gap: 4px 16px; margin: 12px 0 0; padding: 0; list-style: none; font-size: 13px; }
.legend li { display: flex; align-items: center; gap: 6px; }
.chart-note { margin-top: 8px; color: var(--muted); font-size: 13px; }

.heatmap { overflow-x: auto; }
.heatmap svg { display: block; }
.heatmap .day { cursor: pointer; }
.heatmap .day:hover { stroke: var(--text); stroke-width: 1; }
.heatmap .label { fill: var(--muted); font-size: 10px; font-family: var(--font); }
.heatmap .h0 { fill: var(--heat-0); }
.heatmap .h1 { fill: var(--heat-1); }
.heatmap .h2 { fill: var(--heat-2); }
.heatmap .h3 { fill: var(--heat-3); }
.heatmap .h4 { fill: var(--heat-4); }
.heat-legend { display: flex; align-items: center; gap: 4px; margin-top: 8px; color: var(--muted); font-size: 12px; }
.heat-legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; }
.heat-legend .h0 { background: var(--heat-0); }
.heat-legend .h1 { background: var(--heat-1); }
.heat-legend .h2 { background: var(--heat-2); }
.heat-legend .h3 { background: var(--heat-3); }
.heat-legend .h4 { background: var(--heat-4); }

.more { padding-top: 12px; }

/* Settings ------------------------------------------------------------------ */

.settings { display: grid; max-width: 680px; }
.settings fieldset {
  display: grid;
  gap: 12px;
  margin: 0;
  padding: 20px 0;
  border: 0;
  border-top: 1px solid var(--line);
}
.settings fieldset:first-child { padding-top: 4px; border-top: 0; }
.settings legend { float: left; width: 100%; margin-bottom: 4px; padding: 0; font-size: 15px; font-weight: 600; }
.settings .radios { display: flex; flex-wrap: wrap; gap: 8px 20px; }
.shortcuts { width: auto; }
.shortcuts td:first-child { padding-right: 24px; }
kbd {
  display: inline-block;
  min-width: 22px;
  padding: 1px 6px;
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  background: var(--surface-2);
  font-family: var(--font);
  font-size: 12px;
  text-align: center;
}

/* Dialogs, login, toasts ----------------------------------------------------- */

.dialog {
  width: min(480px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  padding: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}
.dialog::backdrop { background: rgba(0, 0, 0, 0.4); }
.dialog-form { display: grid; gap: 16px; padding: 20px; }
.dialog-form h2 { font-size: 17px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }

.swatches { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.swatch { position: relative; display: inline-flex; }
.swatch input { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; margin: 0; cursor: pointer; }
.swatch span {
  width: 28px;
  height: 28px;
  border: 2px solid transparent;
  border-radius: var(--radius-sm);
  background: var(--c);
  box-shadow: inset 0 0 0 2px var(--surface);
}
.swatch input:checked + span { border-color: var(--text); }
.swatch input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.swatches input[type="color"] {
  width: 28px;
  height: 28px;
  padding: 0;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: none;
  cursor: pointer;
}

.auth {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 16px;
  background: var(--bg);
}
.auth-form { display: grid; gap: 14px; width: min(360px, 100%); padding: 24px; }
.auth-form h1 { font-size: 18px; }

.toasts {
  position: fixed;
  right: 0;
  bottom: 16px;
  left: 0;
  z-index: 60;
  display: grid;
  gap: 8px;
  width: min(420px, calc(100vw - 32px));
  margin: 0 auto;
}
.toast {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px 10px 14px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  font-size: 14px;
}
.toast p { flex: 1; }

/* Responsive ------------------------------------------------------------------ */

/* Medium widths: one content column, desktop navigation kept. */
@media (max-width: 1023px) {
  .timer-layout, .log-grid { grid-template-columns: minmax(0, 1fr); }
  .toolbar-select { margin-left: 0; }
}

/* Phone widths: compact header, bottom navigation, tables become row lists. */
@media (max-width: 640px) {
  .topbar-inner { height: 48px; padding: 0 16px; }
  .topbar .nav { display: none; }

  .bottom-nav {
    position: fixed;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 20;
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    padding-bottom: env(safe-area-inset-bottom);
    border-top: 1px solid var(--line);
    background: var(--surface);
  }
  .bottom-nav a {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 52px;
    padding: 0 4px;
    border-top: 2px solid transparent;
    color: var(--muted);
    font-size: 13px;
    font-weight: 500;
    text-align: center;
    text-decoration: none;
  }
  .bottom-nav a[aria-current="page"] { border-top-color: var(--accent); color: var(--text); font-weight: 600; }

  .page { padding: 16px 16px calc(72px + env(safe-area-inset-bottom)); }
  .view { gap: 12px; }
  .panel { padding: 14px; }
  .timer-panel { gap: 16px; padding: 16px; }

  .btn, .btn-group button, select, input[type="text"], input[type="number"], input[type="date"], input[type="time"], input[type="password"] { min-height: 44px; }
  .btn-icon { width: 44px; }
  .btn-text { min-height: 40px; }

  .btn-group { display: grid; grid-auto-columns: minmax(0, 1fr); grid-auto-flow: column; width: 100%; }
  .btn-group button { padding: 0 6px; }
  .timer-actions { display: grid; grid-template-columns: 2fr 1fr 1fr; }
  .timer-actions [data-action="toggle"] { min-width: 0; }

  .toolbar { gap: 8px; }
  .period-nav { width: 100%; }
  .period-label { flex: 1; min-width: 0; }
  .toolbar-select { width: 100%; }
  .view-head .actions { width: 100%; }
  .view-head .actions .btn { flex: 1; }
  .hide-mobile { display: none; }

  .summary { gap: 8px 24px; }
  .field-grid { grid-template-columns: minmax(0, 1fr); }

  /* Tables reflow into labelled rows. */
  .table thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  .table, .table tbody, .table tr, .table td { display: block; }
  .table tr { padding: 10px 0; border-bottom: 1px solid var(--line); }
  .table tr:last-child { border-bottom: 0; }
  .table td { display: flex; justify-content: space-between; gap: 12px; padding: 2px 0; border: 0; }
  .table td.right { text-align: right; }
  .table td[data-label]::before { content: attr(data-label); color: var(--muted); font-weight: 400; }
  .table td.cell-main { font-weight: 600; }
  .table td.cell-actions { width: auto; justify-content: flex-start; gap: 8px; padding-top: 6px; }
  .table tbody tr:hover td { background: none; }

  /* Session rows: subject and duration on the first line, the rest below. */
  .table.sessions tr { display: flex; flex-wrap: wrap; align-items: center; gap: 2px 10px; }
  .table.sessions td { display: block; padding: 0; }
  .table.sessions .c-subject { flex: 1 1 calc(100% - 100px); order: 1; }
  .table.sessions .c-dur { flex: 0 0 90px; order: 2; font-weight: 600; text-align: right; }
  .table.sessions .c-date, .table.sessions .c-time, .table.sessions .c-kind { order: 3; color: var(--muted); font-size: 13px; }
  .table.sessions .c-actions { order: 4; margin-left: auto; padding: 0; }
  .table.sessions .c-actions .btn-text { min-height: 36px; }
  .timer-panel .btn-group { justify-self: stretch; }

  .toasts { bottom: calc(64px + env(safe-area-inset-bottom)); }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
