/* Daily Tracker — mobile-first styles.
   Light values live on :root; dark values are redefined for both the OS
   setting and the in-app toggle, so the toggle wins in either direction. */

:root {
  color-scheme: light;

  --page:            #f5f4f1;
  --surface-1:       #fcfcfb;
  --surface-2:       #f0efec;
  --border:          #e2e0d9;
  --border-strong:   #cfccc2;

  --text-primary:    #0b0b0b;
  --text-secondary:  #52514e;
  --text-muted:      #78766f;

  --accent:          #2a78d6;
  --accent-contrast: #ffffff;
  --success:         #0ca30c;
  --danger:          #d03b3b;

  --grid:            #e6e4de;

  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-7: #4a3aa7;
  --series-8: #e34948;

  --radius:      12px;
  --radius-lg:   16px;
  --shadow-card: 0 1px 2px rgb(0 0 0 / 6%), 0 1px 12px rgb(0 0 0 / 4%);
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;

    --page:            #141413;
    --surface-1:       #1a1a19;
    --surface-2:       #232322;
    --border:          #35342f;
    --border-strong:   #4a4842;

    --text-primary:    #ffffff;
    --text-secondary:  #c3c2b7;
    --text-muted:      #92908a;

    --accent:          #3987e5;
    --accent-contrast: #0b0b0b;
    --success:         #0ca30c;
    --danger:          #d03b3b;

    --grid:            #2e2e2b;

    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;
    --series-7: #9085e9;
    --series-8: #e66767;

    --shadow-card: 0 1px 2px rgb(0 0 0 / 40%), 0 1px 12px rgb(0 0 0 / 24%);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;

  --page:            #141413;
  --surface-1:       #1a1a19;
  --surface-2:       #232322;
  --border:          #35342f;
  --border-strong:   #4a4842;

  --text-primary:    #ffffff;
  --text-secondary:  #c3c2b7;
  --text-muted:      #92908a;

  --accent:          #3987e5;
  --accent-contrast: #0b0b0b;
  --success:         #0ca30c;
  --danger:          #d03b3b;

  --grid:            #2e2e2b;

  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
  --series-6: #008300;
  --series-7: #9085e9;
  --series-8: #e66767;

  --shadow-card: 0 1px 2px rgb(0 0 0 / 40%), 0 1px 12px rgb(0 0 0 / 24%);
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  background: var(--page);
  color: var(--text-primary);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
  overflow-x: hidden;
}

h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }

/* ---------- Header & nav ---------- */

.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: color-mix(in srgb, var(--page) 88%, transparent);
  backdrop-filter: saturate(140%) blur(8px);
  border-bottom: 1px solid var(--border);
  padding: max(env(safe-area-inset-top), 8px) 16px 0;
}

.app-header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 44px;
}

.app-title { font-size: 1.125rem; }

.app-header-actions { display: flex; gap: 4px; }

.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
}
.icon-button:hover { background: var(--surface-2); color: var(--text-primary); }

.app-nav { display: flex; gap: 4px; margin-top: 4px; }

.nav-link {
  flex: 1 1 auto;
  padding: 10px 12px 12px;
  border-bottom: 2px solid transparent;
  color: var(--text-secondary);
  font-size: 0.9375rem;
  font-weight: 500;
  text-align: center;
  text-decoration: none;
}
.nav-link.is-active { color: var(--text-primary); border-bottom-color: var(--accent); }
.nav-link.is-disabled { opacity: 0.4; pointer-events: none; }

@media (min-width: 600px) {
  .app-nav { justify-content: flex-start; margin-top: 0; }
  .nav-link { flex: 0 0 auto; min-width: 110px; }
  .app-header { padding-inline: 24px; }
}

/* ---------- Layout ---------- */

#view-host, #status-host {
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  padding: 0 16px;
}
#status-host { padding-top: 12px; }

@media (min-width: 600px) {
  #view-host, #status-host { padding-inline: 24px; }
}

.view { padding: 20px 0 32px; display: flex; flex-direction: column; gap: 16px; }

/* ---------- Banners & buttons ---------- */

.banner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-1);
  font-size: 0.9375rem;
}
.banner.is-error { border-color: color-mix(in srgb, var(--danger) 45%, var(--border)); }
.banner-button, .link-button {
  border: 0;
  background: none;
  padding: 0;
  color: var(--accent);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.primary-button {
  min-height: 52px;
  padding: 14px 20px;
  border: 0;
  border-radius: var(--radius);
  background: var(--accent);
  color: var(--accent-contrast);
  font: inherit;
  font-size: 1.0625rem;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
}
.primary-button:disabled { opacity: 0.6; cursor: progress; }

.secondary-button {
  min-height: 48px;
  padding: 12px 18px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface-1);
  color: var(--text-primary);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
}

.text-input, .rating-notes {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface-1);
  color: var(--text-primary);
  font: inherit;
  font-size: 1rem; /* keeps iOS from zooming on focus */
}
.text-input:focus-visible, .rating-notes:focus-visible,
.primary-button:focus-visible, .secondary-button:focus-visible,
.icon-button:focus-visible, .segmented-button:focus-visible,
.legend-item:focus-visible, .rating-slider:focus-visible, .rating-checkbox:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.field-label { font-size: 0.8125rem; font-weight: 600; color: var(--text-secondary); text-transform: uppercase; letter-spacing: 0.04em; }
.field-help { margin: 0; font-size: 0.875rem; color: var(--text-muted); }
.field-help code { font-size: 0.8125rem; word-break: break-all; }

/* ---------- Gate & loading screens ---------- */

.gate-view, .loading-view { align-items: stretch; text-align: left; }
.gate-lead { margin: 0; color: var(--text-secondary); }
.setup-form { display: flex; flex-direction: column; gap: 10px; }

.loading-view { align-items: center; padding-top: 64px; color: var(--text-secondary); }
.spinner {
  width: 28px;
  height: 28px;
  border: 3px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Animated checkmark ---------- */

.checkmark {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 8px 0 4px;
}
.checkmark-art { width: 116px; height: 116px; }
.checkmark-svg { width: 100%; height: 100%; overflow: visible; }
.checkmark-circle, .checkmark-tick {
  stroke: var(--success);
  stroke-linecap: round;
  stroke-linejoin: round;
}
.checkmark-circle { stroke-width: 2.5; }
.checkmark-tick { stroke-width: 3.5; }
.checkmark-caption { margin: 0; color: var(--text-secondary); font-size: 0.9375rem; }

.checkmark-art.is-animating .checkmark-circle {
  stroke-dasharray: 152;
  stroke-dashoffset: 152;
  animation: draw-stroke 0.55s cubic-bezier(0.65, 0, 0.45, 1) forwards;
}
.checkmark-art.is-animating .checkmark-tick {
  stroke-dasharray: 46;
  stroke-dashoffset: 46;
  animation: draw-stroke 0.32s 0.42s cubic-bezier(0.65, 0, 0.45, 1) forwards;
}
.checkmark-art.is-animating .checkmark-svg {
  animation: check-pop 0.4s 0.6s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes draw-stroke { to { stroke-dashoffset: 0; } }
@keyframes check-pop { 50% { transform: scale(1.08); } }

@media (prefers-reduced-motion: reduce) {
  .checkmark-art.is-animating .checkmark-circle,
  .checkmark-art.is-animating .checkmark-tick,
  .checkmark-art.is-animating .checkmark-svg { animation: none; stroke-dashoffset: 0; }
  .spinner { animation-duration: 2s; }
}

/* ---------- Entry form ---------- */

.entry-status { margin: 0; color: var(--text-secondary); font-size: 0.9375rem; }
.entry-form { display: flex; flex-direction: column; gap: 14px; }

.rating-card {
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface-1);
  box-shadow: var(--shadow-card);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.rating-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.rating-name { font-size: 1rem; font-weight: 600; }
.rating-value {
  font-size: 1.75rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}
.rating-value.is-empty { color: var(--text-muted); font-size: 1.25rem; }

.rating-slider {
  --fill-percent: 50%;
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 32px;
  margin: 0;
  background: transparent;
  cursor: pointer;
  touch-action: manipulation;
}
.rating-slider::-webkit-slider-runnable-track {
  height: 10px;
  border-radius: 999px;
  background: linear-gradient(
    to right,
    var(--accent) 0 var(--fill-percent),
    var(--surface-2) var(--fill-percent) 100%
  );
  border: 1px solid var(--border);
}
.rating-slider::-moz-range-track {
  height: 10px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
}
.rating-slider::-moz-range-progress { height: 10px; border-radius: 999px; background: var(--accent); }
.rating-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 28px;
  height: 28px;
  margin-top: -10px;
  border: 2px solid var(--surface-1);
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 1px 4px rgb(0 0 0 / 25%);
}
.rating-slider::-moz-range-thumb {
  width: 26px;
  height: 26px;
  border: 2px solid var(--surface-1);
  border-radius: 50%;
  background: var(--accent);
}
.rating-slider.is-empty::-webkit-slider-thumb { background: var(--text-muted); }
.rating-slider.is-empty::-moz-range-thumb { background: var(--text-muted); }
.rating-slider.is-empty::-webkit-slider-runnable-track { background: var(--surface-2); }

.rating-scale {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  font-size: 0.8125rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}
.rating-clear {
  border: 0;
  background: none;
  padding: 4px 8px;
  color: var(--text-muted);
  font: inherit;
  font-size: 0.8125rem;
  text-decoration: underline;
  text-underline-offset: 2px;
  cursor: pointer;
}
/* Checkbox fields (`Name[Check]`) reuse the rating card, swapping the slider
   and its scale row for a single tap target. */
.rating-check-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 32px;
}
.rating-check-label {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  font-size: 0.9375rem;
  color: var(--text-secondary);
  cursor: pointer;
  /* Widen the tap target without moving the box. */
  padding: 6px 8px;
  margin: -6px -8px;
}
.rating-checkbox {
  width: 26px;
  height: 26px;
  margin: 0;
  flex: none;
  accent-color: var(--accent);
  cursor: pointer;
  touch-action: manipulation;
}
.rating-checkbox.is-empty { accent-color: var(--text-muted); opacity: 0.75; }

.rating-notes { resize: vertical; min-height: 44px; }

.entry-actions {
  position: sticky;
  bottom: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px 0 max(env(safe-area-inset-bottom), 12px);
  background: linear-gradient(to bottom, transparent, var(--page) 40%);
}
.entry-actions .primary-button { width: 100%; }
.entry-message { margin: 0; font-size: 0.875rem; text-align: center; }
.entry-message.is-success { color: var(--success); }
.entry-message.is-error { color: var(--danger); }

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

.history-head { display: flex; flex-direction: column; gap: 12px; }
.history-controls { display: flex; flex-wrap: wrap; gap: 8px; }

.segmented {
  display: flex;
  padding: 3px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  overflow-x: auto;
  scrollbar-width: none;
}
.segmented::-webkit-scrollbar { display: none; }
.segmented-button {
  flex: 1 0 auto;
  min-height: 36px;
  padding: 6px 14px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--text-secondary);
  font: inherit;
  font-size: 0.875rem;
  font-weight: 500;
  white-space: nowrap;
  cursor: pointer;
}
.segmented-button[aria-pressed="true"] {
  background: var(--surface-1);
  color: var(--text-primary);
  box-shadow: var(--shadow-card);
}

.legend { display: flex; flex-wrap: wrap; gap: 6px 10px; }
.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 36px;
  padding: 4px 10px 4px 6px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-1);
  color: var(--text-secondary);
  font: inherit;
  font-size: 0.875rem;
  cursor: pointer;
}
.legend-item.is-off { opacity: 0.45; }
.legend-swatch { width: 12px; height: 12px; border-radius: 3px; flex: 0 0 auto; }
.legend-label { color: var(--text-primary); }
.legend-item.is-off .legend-label { color: var(--text-muted); }

/* ---------- Charts ---------- */

.charts-host { display: flex; flex-direction: column; gap: 12px; }

.chart-card {
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface-1);
  box-shadow: var(--shadow-card);
}
.chart-card-head { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.chart-card-title { font-size: 0.9375rem; font-weight: 600; }
.chart-card-range { margin-left: auto; font-size: 0.8125rem; color: var(--text-muted); font-variant-numeric: tabular-nums; }

.chart-host { position: relative; width: 100%; }
.chart { position: relative; }
.chart-svg { display: block; width: 100%; touch-action: pan-y; }

.chart-grid line { stroke: var(--grid); stroke-width: 1; }
.chart-axis-text { fill: var(--text-muted); font-size: 11px; }
.chart-axis-label { fill: var(--text-secondary); font-size: 11px; }
.chart-marker, .chart-highlight { stroke: var(--surface-1); }
.chart-crosshair { stroke: var(--border-strong); stroke-width: 1; }

.chart-tooltip {
  position: absolute;
  z-index: 5;
  min-width: 150px;
  max-width: 240px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-1);
  box-shadow: 0 4px 16px rgb(0 0 0 / 14%);
  font-size: 0.8125rem;
  pointer-events: none;
}
.chart-tooltip-title { margin-bottom: 6px; font-weight: 600; color: var(--text-primary); }
.chart-tooltip-row { display: flex; align-items: center; gap: 7px; padding: 1px 0; }
.chart-tooltip-name { color: var(--text-secondary); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chart-tooltip-value { margin-left: auto; font-variant-numeric: tabular-nums; font-weight: 600; }
.chart-tooltip-note { padding: 2px 0 4px 19px; color: var(--text-muted); font-style: italic; }
.chart-swatch { width: 10px; height: 10px; border-radius: 3px; flex: 0 0 auto; }

.chart-note { margin: 0; font-size: 0.8125rem; color: var(--text-muted); }
.empty-state {
  margin: 0;
  padding: 32px 16px;
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-lg);
  color: var(--text-muted);
  text-align: center;
}

/* ---------- Data table ---------- */

.table-actions { display: flex; }
.table-scroll { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface-1); }
.data-table { width: 100%; border-collapse: collapse; font-size: 0.875rem; }
.data-table th, .data-table td {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  white-space: nowrap;
}
.data-table thead th { background: var(--surface-2); color: var(--text-secondary); font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.04em; position: sticky; top: 0; }
.data-table tbody th { font-weight: 500; color: var(--text-primary); }
.data-table td.numeric { font-variant-numeric: tabular-nums; }
.data-table tbody tr:last-child th, .data-table tbody tr:last-child td { border-bottom: 0; }

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

.settings-dialog {
  width: min(480px, calc(100vw - 32px));
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface-1);
  color: var(--text-primary);
}
.settings-dialog::backdrop { background: rgb(0 0 0 / 45%); }
.settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 12px 14px 18px;
  border-bottom: 1px solid var(--border);
}
.settings-header h2 { font-size: 1.0625rem; }
.settings-body { display: flex; flex-direction: column; gap: 10px; padding: 18px; }
.settings-block { display: flex; flex-direction: column; gap: 8px; padding-top: 16px; border-top: 1px solid var(--border); }
.external-link { color: var(--accent); font-weight: 500; }
