/* Setto styles. Mobile first; light and dark follow the system. */

:root {
  --bg: #f5f7f4;
  --surface: #ffffff;
  --surface-2: #eceff0;
  --border: #dde2e0;
  --text: #121614;
  --muted: #5d6763;
  --accent: #4f8a10;
  --accent-strong: #3f7206;
  --accent-ink: #ffffff;
  --accent-soft: #e6f3d3;
  --danger: #c2372b;
  --star: #e0a400;
  --radius: 14px;
  --radius-sm: 10px;
  --tabbar-h: 64px;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101413;
    --surface: #181d1c;
    --surface-2: #222827;
    --border: #2c3331;
    --text: #eef2ef;
    --muted: #97a39e;
    --accent: #b6f24a;
    --accent-strong: #c9f877;
    --accent-ink: #101413;
    --accent-soft: #26331a;
    --danger: #ff7a6b;
    --star: #ffcc3d;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

main {
  max-width: 640px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 8px) 16px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 24px);
  outline: none;
}

h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.6rem; letter-spacing: -0.01em; }
h2 { font-size: 1.05rem; }
h3 { font-size: 1rem; }
p { margin: 0; }
a { color: inherit; text-decoration: none; }

.muted { color: var(--muted); }
.small { font-size: 0.875rem; }

.view { display: flex; flex-direction: column; gap: 16px; }
.stack { display: flex; flex-direction: column; gap: 8px; }

/* ---- header ---- */
.page-header {
  display: flex; align-items: center; gap: 8px;
  min-height: 56px; padding-top: 4px;
}
.page-header .logo { display: inline-flex; width: 32px; height: 32px; flex: none; color: var(--accent); margin-right: 2px; }
.page-header .logo svg { width: 100%; height: 100%; }
.page-title { flex: 1; min-width: 0; }
.page-title h1 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.page-actions { display: flex; gap: 8px; align-items: center; }

/* ---- icons & buttons ---- */
.icon { display: inline-flex; width: 20px; height: 20px; flex: none; }
.icon svg { width: 100%; height: 100%; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 18px;
  border: 1px solid var(--border); border-radius: 999px;
  background: var(--surface); color: var(--text);
  font: inherit; font-weight: 600; cursor: pointer;
  touch-action: manipulation;
}
.btn:active { transform: scale(0.98); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.small { min-height: 36px; padding: 0 12px; font-size: 0.9rem; }
.btn.small .icon { width: 16px; height: 16px; }
.btn.big { min-height: 56px; font-size: 1.1rem; padding: 0 28px; }
.btn.block { width: 100%; }
.danger-text { color: var(--danger); }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 44px; height: 44px; flex: none;
  border: 0; border-radius: 999px; background: transparent; color: var(--text);
  cursor: pointer; padding: 0;
}
.icon-btn .icon { width: 24px; height: 24px; }
.icon-btn.small { width: 36px; height: 36px; color: var(--muted); }
.icon-btn.small .icon { width: 18px; height: 18px; }
.icon-btn:active { background: var(--surface-2); }

.star .icon svg path { fill: none; }
.star { color: var(--muted); }
.star.on { color: var(--star); }
.star.on .icon svg path { fill: currentColor; }

.link-btn {
  border: 0; background: none; padding: 4px 0; cursor: pointer;
  color: var(--accent-strong); font: inherit; font-size: 0.9rem; font-weight: 600;
}

.row-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.row-actions.left { justify-content: flex-start; }

/* ---- cards ---- */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow);
}
.link-card { display: block; }
.link-card:active, .entry-card:active { background: var(--surface-2); }

.hero { display: flex; flex-direction: column; gap: 16px; padding: 24px 0 8px; }

.entry-card { display: flex; align-items: center; gap: 12px; }
.entry-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.chevron { color: var(--muted); width: 18px; height: 18px; }

.workout-row { display: flex; align-items: center; gap: 12px; }
.workout-row > div { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; }

.badge {
  display: inline-block; margin-left: 6px; padding: 1px 8px; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent-strong); font-size: 0.75rem; font-weight: 600; vertical-align: 2px;
}

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  padding: 2px 10px; border-radius: 999px; background: var(--surface-2);
  color: var(--muted); font-size: 0.8rem;
}

/* ---- thumbnails ---- */
.thumb {
  flex: none; display: grid; place-items: center; overflow: hidden;
  background: var(--surface-2); color: var(--accent-strong); border-radius: var(--radius-sm);
}
.thumb svg { width: 70%; height: 70%; }
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.thumb.sm { width: 48px; height: 48px; }
.thumb.md { width: 88px; height: 88px; }
.thumb.lg { width: 100%; aspect-ratio: 4 / 3; border-radius: var(--radius); }
.thumb.lg:not(.photo) { aspect-ratio: 16 / 7; }
.thumb.lg svg { width: 40%; height: 70%; }

/* ---- machine list ---- */
.sticky-tools {
  position: sticky; top: 0; z-index: 2;
  display: flex; flex-direction: column; gap: 10px;
  margin: 0 -16px; padding: 8px 16px 10px; background: var(--bg);
}
input[type="search"] { width: 100%; }
.filter-chips { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; margin: 0 -16px; padding: 0 16px; }
.filter-chips::-webkit-scrollbar { display: none; }
.chip-btn {
  flex: none; min-height: 36px; padding: 0 14px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font: inherit; font-size: 0.9rem; cursor: pointer;
}
.chip-btn.on { background: var(--text); color: var(--bg); border-color: var(--text); }

.machine-list { display: flex; flex-direction: column; gap: 18px; }
.machine-group { display: flex; flex-direction: column; gap: 2px; }
.group-title { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 0 0 6px; }
.machine-row {
  display: flex; align-items: center; gap: 12px; width: 100%;
  padding: 8px; margin: 0 -8px; width: calc(100% + 16px);
  border: 0; border-radius: var(--radius-sm); background: transparent; color: inherit;
  font: inherit; text-align: left; cursor: pointer;
}
.machine-row:active { background: var(--surface-2); }
.machine-row-body { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.machine-row-body .name { font-weight: 600; }
.machine-row-body .small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.empty-inline { text-align: center; padding: 24px 0; }

/* ---- machine detail ---- */
.machine-hero { display: flex; flex-direction: column; gap: 10px; }
.photo-actions { display: flex; gap: 8px; }
.machine-head { display: flex; gap: 14px; align-items: center; }
.last-time { display: flex; flex-direction: column; gap: 2px; min-width: 0; }

.history-list { display: flex; flex-direction: column; }
.history-row { display: flex; gap: 12px; align-items: baseline; padding: 10px 0; border-bottom: 1px solid var(--border); }
.history-row .small { flex: none; width: 76px; }

/* ---- forms ---- */
.section { display: flex; flex-direction: column; gap: 10px; }
.section-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }

.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field label { display: flex; justify-content: space-between; gap: 8px; font-size: 0.85rem; color: var(--muted); font-weight: 500; }
.field-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.field-head .link-btn { font-size: 0.8rem; padding: 0; color: var(--muted); font-weight: 400; text-decoration: underline; }
.form .field label { flex-direction: column; }

.grid-fields { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 10px; }

input[type="text"], input[type="search"], input[type="date"], input[type="time"], select, textarea {
  width: 100%; min-height: 44px; padding: 10px 12px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--text);
  font: inherit; font-size: 16px; /* 16px stops iOS from zooming in */
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }
.btn:focus-visible, .icon-btn:focus-visible, .chip-btn:focus-visible, .machine-row:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}

.input-wrap { position: relative; display: flex; align-items: center; }
.input-wrap .unit { position: absolute; right: 12px; color: var(--muted); font-size: 0.85rem; pointer-events: none; }
.input-wrap input { padding-right: 52px; }

/* sets */
.sets { display: flex; flex-direction: column; gap: 8px; }
.set-row { display: grid; grid-template-columns: 18px 1fr 1fr 36px; gap: 6px; align-items: center; }
.set-no { color: var(--muted); font-weight: 600; font-size: 0.9rem; text-align: center; }
.stepper {
  display: grid; grid-template-columns: 36px 1fr 36px; align-items: stretch;
  border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface); overflow: hidden;
  min-height: 48px;
}
.step-btn { border: 0; background: var(--surface-2); color: var(--text); cursor: pointer; display: grid; place-items: center; padding: 0; touch-action: manipulation; }
.step-btn .icon { width: 16px; height: 16px; }
.step-btn:active { background: var(--accent-soft); }
.step-input { display: flex; flex-direction: column; align-items: center; justify-content: center; min-width: 0; }
.step-input input {
  width: 100%; min-height: 0; padding: 2px 0 0; border: 0; border-radius: 0; background: transparent;
  text-align: center; font-weight: 700; font-size: 18px;
}
.step-input input:focus { outline: none; }
.stepper:focus-within { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.step-input .unit { font-size: 0.7rem; color: var(--muted); line-height: 1; padding-bottom: 4px; }

/* custom machine form */
.form { display: flex; flex-direction: column; gap: 14px; }
.settings-editor { display: flex; flex-direction: column; gap: 8px; }
.setting-edit-row { display: grid; grid-template-columns: 1fr 76px 36px; gap: 6px; align-items: center; }
fieldset.muscles { border: 0; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: 6px; }
fieldset.muscles legend { font-size: 0.85rem; color: var(--muted); font-weight: 500; margin-bottom: 6px; padding: 0; }
.check-chip { position: relative; }
.check-chip input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.check-chip span {
  display: inline-block; padding: 6px 12px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface); font-size: 0.875rem;
}
.check-chip input:checked + span { background: var(--accent-soft); border-color: var(--accent); color: var(--text); }
.check-chip input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

/* settings page */
.setting-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.setting-row select { width: auto; }
.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 999px; padding: 3px; background: var(--surface-2); }
.segmented button {
  min-width: 56px; min-height: 36px; border: 0; border-radius: 999px; background: transparent;
  color: var(--text); font: inherit; font-weight: 600; cursor: pointer;
}
.segmented button.on { background: var(--surface); box-shadow: 0 1px 3px rgb(0 0 0 / 20%); }
.check-row { display: flex; gap: 10px; align-items: center; min-height: 44px; }
.check-row input { width: 20px; height: 20px; accent-color: var(--accent); }

/* ---- progress ---- */
.stack-lg { display: flex; flex-direction: column; gap: 16px; }
.grow { flex: 1; min-width: 0; }
.stat-tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat-tile {
  display: flex; flex-direction: column; gap: 2px; min-width: 0;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px;
}
.stat-tile strong { font-size: 1.25rem; line-height: 1.2; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-variant-numeric: tabular-nums; }
.stat-tile .small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.headline { display: flex; flex-direction: column; gap: 2px; }
.headline-row { display: flex; align-items: center; gap: 4px; }
.big-num { font-size: 1.9rem; line-height: 1.15; letter-spacing: -0.01em; font-variant-numeric: tabular-nums; }
.trend { font-weight: 600; font-size: 0.95rem; }
.trend.up { color: var(--accent-strong); }
.machine-row-body .trend { font-weight: 600; }
.spark-wrap { flex: none; display: inline-flex; }
.spark .line, .chart-svg .line { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.spark .dot { fill: var(--accent); }
.progress-teaser { display: flex; align-items: center; gap: 12px; }
.progress-teaser .grow { display: flex; flex-direction: column; gap: 2px; }
.progress-teaser strong { font-size: 1.2rem; }
.history-row .badge { margin-left: auto; }

.chart { position: relative; outline: none; touch-action: pan-y; user-select: none; -webkit-user-select: none; cursor: crosshair; }
.chart:focus-visible { outline: 2px solid var(--accent); outline-offset: 4px; border-radius: 4px; }
.chart-plot { min-height: 120px; }
.chart-svg { display: block; overflow: visible; }
.chart-svg .grid { stroke: var(--border); stroke-width: 1; }
.chart-svg .grid.base { stroke: var(--muted); }
.chart-svg .axis { fill: var(--muted); font-size: 11px; font-family: inherit; font-variant-numeric: tabular-nums; }
.chart-svg .area { fill: var(--accent); opacity: 0.1; }
.chart-svg .dot { fill: var(--accent); stroke: var(--surface); stroke-width: 2; }
.chart-svg .bar { fill: var(--accent); }
.chart-cross {
  position: absolute; left: 0; top: 12px; bottom: 26px; width: 1px; margin-left: -0.5px;
  background: var(--muted); opacity: 0.6; pointer-events: none;
}
.chart-hl {
  position: absolute; left: 0; top: 0; width: 12px; height: 12px; margin: -6px 0 0 -6px; border-radius: 50%;
  background: var(--accent); box-shadow: 0 0 0 2px var(--surface); pointer-events: none;
}
.chart-tip {
  position: absolute; left: 0; top: 0; z-index: 1;
  display: flex; flex-direction: column; padding: 6px 10px; border-radius: 10px; white-space: nowrap;
  background: var(--text); color: var(--bg); font-size: 0.8rem; line-height: 1.3; pointer-events: none;
  box-shadow: 0 4px 14px rgb(0 0 0 / 20%);
}
.chart-tip strong { font-size: 0.95rem; font-variant-numeric: tabular-nums; }
.chart-tip .tip-title, .chart-tip .tip-note { opacity: 0.75; }

/* ---- empty ---- */
.empty { text-align: center; padding: 32px 8px; display: flex; flex-direction: column; gap: 10px; align-items: center; }

/* ---- tab bar ---- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(5, 1fr);
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
}
.tabbar a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); font-size: 0.72rem; font-weight: 600;
}
.tabbar svg { width: 24px; height: 24px; }
.tabbar a.on { color: var(--text); }
.tabbar a.on svg { color: var(--accent-strong); }

/* ---- toast & dialog ---- */
#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 12px); z-index: 20;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  display: flex; align-items: center; gap: 12px;
  max-width: calc(100% - 32px); padding: 10px 12px 10px 16px; border-radius: 12px;
  background: var(--text); color: var(--bg); font-size: 0.9rem; box-shadow: 0 6px 24px rgb(0 0 0 / 25%);
  transition: opacity 0.2s, transform 0.2s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
#toast .btn { min-height: 32px; background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

.dialog {
  width: min(420px, calc(100% - 32px)); border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text); padding: 20px;
}
.dialog::backdrop { background: rgb(0 0 0 / 45%); }
.dialog form { display: flex; flex-direction: column; gap: 12px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; margin-top: 4px; }

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