:root {
  --bg: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #232321;
  --border: #2c2c2a;
  --text: #ffffff;
  --text-secondary: #c3c2b7;
  --muted: #898781;
  --series-sys: #e24a4a;
  --series-dia: #3987e5;
  --series-pulse: #d9a926;
  --good: #0ca30c;
  --elevated: #c9a227;
  --high: #d97a26;
  --critical: #d03b3b;
  --accent: #e24a4a;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f9f9f7;
    --surface: #fcfcfb;
    --surface-2: #f1f0ec;
    --border: #e1e0d9;
    --text: #0b0b0b;
    --text-secondary: #52514e;
    --muted: #898781;
    --series-sys: #c6342f;
    --series-dia: #2a6fc9;
    --series-pulse: #96791a;
    --good: #0ca30c;
    --elevated: #96791a;
    --high: #c06318;
    --critical: #c6342f;
    --accent: #c6342f;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  padding: 1.25rem 1rem 3rem;
  -webkit-text-size-adjust: 100%;
}
.wrap { max-width: 720px; margin: 0 auto; }
a { color: inherit; }

header {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 1rem; gap: .75rem; flex-wrap: wrap;
}
h1 { font-size: 1.3rem; margin: 0; }
h2 { font-size: 1rem; margin: 0 0 .7rem; }
.user-box { display: flex; align-items: center; gap: .7rem; font-size: .85rem; }
.user-name { color: var(--text-secondary); }
.link-btn {
  background: none; border: none; color: var(--muted); font: inherit; font-size: .82rem;
  cursor: pointer; text-decoration: underline; padding: 0;
}
.link-btn:hover { color: var(--text); }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 1.1rem; margin-bottom: 1rem;
}
.lede { color: var(--text-secondary); line-height: 1.5; margin-top: 0; }
.meta { color: var(--muted); font-size: .82rem; }
.meta.error { color: var(--critical); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  font: inherit; font-size: .95rem; font-weight: 600; cursor: pointer;
  padding: .75rem 1.3rem; border-radius: 10px; border: 1px solid transparent;
  text-decoration: none; line-height: 1;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.08); }
.btn-secondary { background: var(--surface-2); color: var(--text); border-color: var(--border); width: 100%; }
.btn-secondary:hover { border-color: var(--muted); }
.btn-ghost { background: none; color: var(--muted); border-color: var(--border); }

/* --- Quick add card --- */
.add-card-head { display: flex; align-items: flex-start; gap: .5rem; margin-bottom: 1rem; }
.add-card-head .add-tabs { flex: 1; }
.icon-btn {
  flex-shrink: 0; font-size: 1.1rem; line-height: 1; cursor: pointer;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px;
  padding: .5rem .6rem;
}
.icon-btn:hover { border-color: var(--muted); }

.add-tabs { display: flex; gap: .4rem; }
.add-tab {
  flex: 1; font: inherit; font-size: .88rem; font-weight: 600; cursor: pointer;
  padding: .55rem .5rem; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text-secondary);
}
.add-tab.active { background: var(--text); color: var(--bg); border-color: var(--text); }

.photo-panel { margin-bottom: 1rem; }
.photo-panel .meta { margin: 0 0 .7rem; }
.photo-preview-wrap { margin-top: .8rem; border-radius: 10px; overflow: hidden; border: 1px solid var(--border); max-height: 220px; }
.photo-preview-wrap img { width: 100%; display: block; object-fit: cover; max-height: 220px; }
#photo-status { margin-top: .7rem; }
#photo-status.loading::before {
  content: ''; display: inline-block; width: 10px; height: 10px; margin-right: .4rem;
  border: 2px solid var(--muted); border-top-color: transparent; border-radius: 50%;
  animation: spin .7s linear infinite; vertical-align: middle;
}
@keyframes spin { to { transform: rotate(360deg); } }

.num-row { display: flex; align-items: flex-end; gap: .5rem; margin-bottom: .9rem; }
.num-field { display: flex; flex-direction: column; gap: .25rem; flex: 1; min-width: 0; }
.num-field-pulse { flex: .8; }
.num-label { font-size: .72rem; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.num-field input {
  font: inherit; font-size: 1.6rem; font-weight: 700; font-variant-numeric: tabular-nums;
  text-align: center; width: 100%; padding: .55rem .2rem; border-radius: 10px;
  border: 1.5px solid var(--border); background: var(--surface-2); color: var(--text);
  -moz-appearance: textfield;
}
.num-field input::-webkit-outer-spin-button, .num-field input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.num-field input:focus { outline: none; border-color: var(--accent); }
.num-unit { font-size: .68rem; color: var(--muted); text-align: center; }
.num-sep { font-size: 1.6rem; color: var(--muted); padding-bottom: 1.3rem; font-weight: 300; }

/* --- Sliders: one per value, directly below the number row, always
   visible rather than tucked behind a toggle. The min/max shown at each
   end come from the slider-range settings (localStorage), so they always
   match the slider's actual current span. --- */
.slider-row { display: flex; flex-direction: column; gap: .5rem; margin-bottom: .9rem; }
.slider-track-row { display: flex; align-items: center; gap: .5rem; }
.slider-end { font-size: .68rem; color: var(--muted); font-variant-numeric: tabular-nums; width: 2.4em; flex-shrink: 0; }
.slider-end:last-child { text-align: right; }
.slider {
  flex: 1; -webkit-appearance: none; appearance: none; height: 28px;
  background: transparent; cursor: pointer; margin: 0;
}
.slider::-webkit-slider-runnable-track { height: 6px; border-radius: 3px; background: var(--surface-2); border: 1px solid var(--border); }
.slider::-moz-range-track { height: 6px; border-radius: 3px; background: var(--surface-2); border: 1px solid var(--border); }
.slider::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none; width: 22px; height: 22px; border-radius: 50%;
  margin-top: -9px; border: 3px solid var(--bg); cursor: grab;
}
.slider::-moz-range-thumb { width: 22px; height: 22px; border-radius: 50%; border: 3px solid var(--bg); cursor: grab; }
.slider:active::-webkit-slider-thumb { cursor: grabbing; }
.slider-sys::-webkit-slider-thumb, .slider-sys::-moz-range-thumb { background: var(--series-sys); }
.slider-dia::-webkit-slider-thumb, .slider-dia::-moz-range-thumb { background: var(--series-dia); }
.slider-pulse::-webkit-slider-thumb, .slider-pulse::-moz-range-thumb { background: var(--series-pulse); }

.category-preview {
  font-size: .85rem; font-weight: 600; padding: .5rem .7rem; border-radius: 8px;
  margin-bottom: .9rem; text-align: center;
}

.more-details { margin-bottom: 1rem; }
.more-details summary {
  cursor: pointer; font-size: .85rem; color: var(--text-secondary); font-weight: 600;
  padding: .3rem 0;
}
.field-row { display: flex; gap: .6rem; margin: .7rem 0; }
.field { display: flex; flex-direction: column; gap: .3rem; flex: 1; }
.field-label { font-size: .72rem; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.field input {
  font: inherit; font-size: .95rem; padding: .55rem .6rem; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text); width: 100%;
}
.field input:focus { outline: none; border-color: var(--accent); }

.form-error {
  color: var(--critical); font-size: .85rem; background: color-mix(in srgb, var(--critical) 12%, transparent);
  border-radius: 8px; padding: .55rem .7rem; margin-bottom: .8rem;
}
.form-actions { display: flex; gap: .6rem; }
.form-actions .btn { flex: 1; }

/* --- Summary cards --- */
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: .7rem; margin-bottom: 1rem; }
.sum-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: .85rem .9rem;
}
.sum-label { color: var(--muted); font-size: .7rem; text-transform: uppercase; letter-spacing: .04em; }
.sum-value { font-size: 1.4rem; font-weight: 700; margin-top: .25rem; font-variant-numeric: tabular-nums; }
.sum-sub { font-size: .76rem; color: var(--muted); margin-top: .15rem; }
.badge {
  display: inline-block; font-size: .68rem; font-weight: 700; padding: .1rem .5rem;
  border-radius: 999px; margin-top: .3rem;
}

/* --- Chart --- */
.chart-head { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: .5rem; margin-bottom: .6rem; }
.chart-head h2 { margin: 0; }
.chart-head-actions { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
.btn-small { padding: .4rem .7rem; font-size: .8rem; }
.range-toggle { display: inline-flex; gap: .3rem; flex-wrap: wrap; }
.range-btn {
  font: inherit; font-size: .74rem; font-weight: 600; cursor: pointer;
  padding: .28rem .6rem; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text-secondary);
}
.range-btn.active { background: var(--text); color: var(--bg); border-color: var(--text); }
.legend { display: flex; gap: 1rem; margin-bottom: .7rem; font-size: .8rem; color: var(--text-secondary); flex-wrap: wrap; }
.legend-item { display: flex; align-items: center; gap: .35rem; }
.legend-swatch { width: 16px; height: 2px; border-radius: 1px; }
.legend-dot { width: 7px; height: 7px; border-radius: 50%; }
.legend-swatch-sys { background: var(--series-sys); }
.legend-swatch-dia { background: var(--series-dia); }
.legend-swatch-pulse { background: var(--series-pulse); }
.chart-wrap { position: relative; }
.chart-wrap svg { width: 100%; aspect-ratio: 680 / 220; height: auto; display: block; overflow: visible; touch-action: none; }
.chart-wrap svg text { fill: var(--muted); font-size: 10px; }
.grid-line { stroke: var(--border); stroke-width: 1; }
.band-good { fill: color-mix(in srgb, var(--good) 7%, transparent); }
.chart-empty { fill: var(--muted); font-size: 12px; }
.chart-tooltip {
  position: absolute; transform: translate(-50%, -115%); pointer-events: none;
  background: var(--surface); border: 1px solid var(--border); border-radius: 6px;
  padding: .45rem .65rem; box-shadow: 0 4px 14px rgba(0,0,0,.3);
  opacity: 0; transition: opacity .1s ease; white-space: nowrap; z-index: 2; font-size: .78rem;
}
.chart-tooltip.visible { opacity: 1; }
.chart-tooltip .tt-time { color: var(--muted); margin-bottom: .2rem; }
.chart-tooltip .tt-row { display: flex; align-items: center; gap: .35rem; }
.chart-tooltip .tt-key { width: 9px; height: 2px; display: inline-block; }
.chart-tooltip .tt-val { font-weight: 700; margin-left: auto; padding-left: .6rem; font-variant-numeric: tabular-nums; }

/* --- History table --- */
#history-table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: .85rem; min-width: 460px; }
th, td { text-align: left; padding: .5rem .5rem; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { color: var(--muted); font-weight: 500; font-size: .72rem; text-transform: uppercase; }
td.bp-cell { font-weight: 700; font-variant-numeric: tabular-nums; }
td.note-cell { white-space: normal; color: var(--text-secondary); font-weight: 400; max-width: 180px; }
.row-actions { display: flex; gap: .4rem; }
.row-actions button {
  font: inherit; font-size: .75rem; cursor: pointer; background: none; border: none;
  color: var(--muted); padding: .2rem .35rem; text-decoration: underline;
}
.row-actions button:hover { color: var(--text); }
.row-actions button.danger:hover { color: var(--critical); }

/* --- Admin --- */
.admin-row {
  display: flex; align-items: center; justify-content: space-between; gap: .6rem;
  padding: .6rem 0; border-bottom: 1px solid var(--border); font-size: .88rem;
}
.admin-row:last-child { border-bottom: none; }
.admin-row .btn { padding: .4rem .8rem; font-size: .82rem; }

footer { color: var(--muted); font-size: .72rem; text-align: center; margin-top: 1.5rem; line-height: 1.5; }

/* --- Dialogs (native <dialog>) --- */
dialog {
  width: min(420px, 92vw); max-height: 85vh; overflow-y: auto;
  border: 1px solid var(--border); border-radius: 12px; padding: 0;
  background: var(--surface); color: var(--text);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.55); }
dialog.dialog-wide { width: min(780px, 95vw); }
.modal-inner { padding: 1.1rem; }
.modal-head { display: flex; align-items: flex-start; justify-content: space-between; gap: .6rem; margin-bottom: .3rem; }
.modal-head h2 { margin: 0; }
.modal-close-x {
  background: none; border: none; color: var(--muted); font-size: 1.3rem; line-height: 1;
  cursor: pointer; padding: .1rem .3rem; flex-shrink: 0;
}
.modal-close-x:hover { color: var(--text); }
.range-setting-group { margin-top: .9rem; }
.range-setting-label { font-size: .82rem; font-weight: 600; color: var(--text-secondary); margin: 0 0 .4rem; }
.range-setting-row { display: flex; gap: .6rem; }

/* --- Full analysis dialog --- */
#analysis-period-label { margin: .5rem 0 0; }
.analysis-stat-grid { margin: .9rem 0; }
.analysis-section { margin-top: 1.3rem; }
.analysis-section-label { font-size: .82rem; font-weight: 600; color: var(--text-secondary); margin: 0 0 .5rem; }
.analysis-footnote { margin-top: 1.3rem; padding-top: .9rem; border-top: 1px solid var(--border); line-height: 1.5; }
.table-scroll { overflow-x: auto; }

.analysis-trend {
  background: var(--surface-2); border-radius: 10px; padding: .8rem .9rem;
  margin: 0 0 .9rem; font-size: .88rem; line-height: 1.5;
}
.analysis-trend .trend-arrow { font-weight: 700; font-size: 1.15em; margin-right: .25rem; }
.analysis-trend .trend-up { color: var(--high); }
.analysis-trend .trend-down { color: var(--good); }
.analysis-trend .trend-flat { color: var(--text-secondary); }

.cat-row { display: flex; align-items: center; gap: .6rem; margin-bottom: .45rem; font-size: .82rem; }
.cat-row:last-child { margin-bottom: 0; }
.cat-label { width: 112px; flex-shrink: 0; color: var(--text-secondary); }
.cat-track { flex: 1; height: 10px; border-radius: 5px; background: var(--surface-2); overflow: hidden; }
.cat-fill { height: 100%; border-radius: 5px; min-width: 2px; }
.cat-count { width: 56px; text-align: right; flex-shrink: 0; font-variant-numeric: tabular-nums; color: var(--muted); }

@media (max-width: 420px) {
  .num-field input { font-size: 1.35rem; }
  .num-sep { font-size: 1.35rem; padding-bottom: 1.15rem; }
}
