:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --danger: #dc2626;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.08), 0 1px 2px rgba(15, 23, 42, 0.04);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.5;
}

/* ---------------- Top bar / tabs ---------------- */
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
  padding: 0.75rem 1rem;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 1rem;
  justify-content: space-between;
}
.topbar__brand { display: flex; align-items: center; gap: 0.6rem; }
.topbar__brand h1 { font-size: 1.15rem; margin: 0; }
.topbar__logo { font-size: 1.4rem; }

.tabs { display: flex; gap: 0.25rem; flex-wrap: wrap; }
.tab {
  border: none;
  background: transparent;
  color: var(--muted);
  padding: 0.5rem 0.9rem;
  border-radius: 8px;
  font-size: 0.95rem;
  cursor: pointer;
  font-weight: 500;
}
.tab:hover { background: var(--bg); color: var(--text); }
.tab.is-active { background: var(--primary); color: #fff; }

/* ---------------- Layout ---------------- */
.content {
  max-width: 980px;
  margin: 0 auto;
  padding: 1.5rem 1rem 4rem;
  display: grid;
  gap: 1.5rem;
}
/* The grid items are the .view sections; without min-width:0 a wide child
   (A4 preview, wide rows) stretches the column and the whole page scrolls.
   Scope to :not([hidden]) so the display:grid doesn't override the hidden
   attribute (which would reveal inactive views). */
.view:not([hidden]) { min-width: 0; display: grid; gap: 1.5rem; align-content: start; }
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem 1.5rem;
  /* Grid items default to min-width:auto (min-content); without this a wide
     child (e.g. the A4 print preview) forces the whole column past the
     viewport, causing horizontal scroll on mobile. */
  min-width: 0;
}
.card h2 { margin-top: 0; font-size: 1.1rem; }
.card h3 { font-size: 0.95rem; margin: 0.5rem 0; }
.muted { color: var(--muted); font-size: 0.9rem; }

/* ---------------- Forms ---------------- */
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(160px, 100%), 1fr));
  gap: 1rem;
  margin: 1rem 0;
}
.field { display: flex; flex-direction: column; gap: 0.3rem; font-size: 0.85rem; font-weight: 600; color: var(--muted); }
.field input, .field select {
  font: inherit;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
}
.field input:focus, .field select:focus { outline: 2px solid var(--primary); border-color: var(--primary); }

.actions { display: flex; flex-wrap: wrap; gap: 0.6rem; margin-top: 1rem; }
.btn {
  font: inherit;
  font-weight: 600;
  padding: 0.55rem 1rem;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  border-radius: 8px;
  cursor: pointer;
}
.btn:hover { background: var(--bg); }
.btn--primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn--primary:hover { background: var(--primary-dark); }
.btn--danger { color: var(--danger); border-color: #fecaca; }
.btn--danger:hover { background: #fef2f2; }
.btn--sm { padding: 0.35rem 0.7rem; font-size: 0.85rem; border-radius: 6px; }

.account { display: flex; align-items: center; gap: 0.6rem; }
.account__user { font-size: 0.9rem; color: var(--text); }
.account__sync { font-size: 0.75rem; color: var(--primary); white-space: nowrap; }
.account__sync[data-state="pending"] { color: var(--muted, #888); }
.account__expired { font-size: 0.85rem; color: #b45309; white-space: nowrap; }
.sep { border: none; border-top: 1px solid var(--border); margin: 1rem 0; }
.status { font-size: 0.85rem; color: var(--primary); min-height: 1.2em; }

/* ---------------- Today / coach view ---------------- */
.coach-hero { background: linear-gradient(135deg, #2563eb, #1e40af); color: #fff; border: none; }
.hero-top { display: flex; justify-content: space-between; align-items: center; }
.hero-streak { font-weight: 700; background: rgba(255,255,255,0.18); padding: 0.25rem 0.7rem; border-radius: 999px; font-size: 0.9rem; }
.hero-streak--cold { background: rgba(255,255,255,0.12); font-weight: 600; }
.hero-pct { font-size: 1.8rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.hero-headline { margin: 0.6rem 0 0.8rem; font-size: 1.2rem; }
.hero-bar { background: rgba(255,255,255,0.22); border-radius: 999px; height: 10px; overflow: hidden; }
.hero-bar__fill { background: #fff; height: 100%; border-radius: 999px; transition: width 0.3s; }

.now-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 0.4rem; }
.now-hour { font-weight: 700; font-size: 1.05rem; }
.now-msg { font-size: 1.05rem; font-weight: 600; margin: 0.3rem 0 0.8rem; }
.now-msg--done { color: #16a34a; }
.now-rest { font-size: 1.05rem; font-weight: 600; }
.now-catchup { background: #fef3c7; border: 1px solid #fde68a; color: #92400e; padding: 0.5rem 0.75rem; border-radius: 8px; font-size: 0.9rem; margin: 0.4rem 0 0.8rem; }
.now-row { border: 1px solid var(--border); border-radius: 10px; padding: 0.6rem 0.75rem; margin: 0.5rem 0; }
.now-row.is-done { border-color: #bbf7d0; background: #f0fdf4; }
.now-row__head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.45rem; }
.now-ex { font-weight: 700; }
.now-count { font-variant-numeric: tabular-nums; color: var(--muted); font-weight: 600; }
.now-actions { display: flex; gap: 0.4rem; flex-wrap: wrap; }
.chip-btn { font: inherit; font-weight: 700; border: 1px solid var(--border); background: #fff; border-radius: 8px; padding: 0.45rem 0.8rem; cursor: pointer; min-width: 3rem; }
.chip-btn:hover { background: var(--bg); }
.chip-btn:active { transform: scale(0.96); }
.chip-btn--done { background: var(--primary); border-color: var(--primary); color: #fff; margin-left: auto; }
.chip-btn--done:hover { background: var(--primary-dark); }

/* ---------------- Sheet exercise picker (with break hours) ---------------- */
.exercise-picker__list { display: grid; gap: 0.6rem; }
.sheet-ex {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  padding: 0.5rem 0.75rem;
}
.sheet-ex__head { display: flex; align-items: center; gap: 0.75rem; flex-wrap: wrap; }
.sheet-ex__name { flex: 1; font-weight: 600; min-width: 6rem; }
.sheet-ex__min { font-size: 0.85rem; color: var(--muted); display: flex; gap: 0.4rem; align-items: center; }
.sheet-ex__min .min-input { width: 4.5rem; }
.btn-hours { padding: 0.35rem 0.7rem; font-size: 0.85rem; }
.hours-count { font-weight: 700; color: var(--primary); }
.hours-grid { margin-top: 0.6rem; }
.hours-hint { margin: 0 0 0.4rem; }
.chips { display: flex; flex-wrap: wrap; gap: 0.3rem; }
.chip {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.2rem 0.5rem;
  font: inherit;
  font-size: 0.8rem;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  background: #fff;
}
.chip.on { background: #eff6ff; border-color: #bfdbfe; color: var(--primary-dark); }
.chip.off { background: #fee2e2; border-color: #fecaca; color: #b91c1c; text-decoration: line-through; }

/* ---------------- Settings exercise list ---------------- */
.settings-exercises { display: grid; gap: 0.6rem; }
.exercise-row {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto auto;
  align-items: center;
  gap: 0.75rem;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
}
.exercise-row input[type="text"] { width: 100%; min-width: 0; }
.exercise-row .min-input { width: 5rem; }
.exercise-row label { font-size: 0.85rem; color: var(--muted); display: flex; gap: 0.4rem; align-items: center; }

/* ---------------- Day list ---------------- */
.day-list { display: grid; gap: 0.5rem; }
.day-list__item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.6rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
}
.day-list__item:hover { border-color: var(--primary); }
.day-list__date { font-weight: 600; }
.day-list__totals { font-size: 0.85rem; color: var(--muted); }

/* ---------------- Stats ---------------- */
.stat-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 1rem; }
.stat-card { background: var(--bg); border-radius: 10px; padding: 1rem; }
.stat-card__value { font-size: 1.6rem; font-weight: 700; }
.stat-card__label { font-size: 0.8rem; color: var(--muted); }
.bar-row { display: grid; grid-template-columns: 130px 1fr 110px; align-items: center; gap: 0.6rem; margin: 0.4rem 0; }
.bar-track { background: var(--bg); border-radius: 6px; height: 18px; overflow: hidden; }
.bar-fill { background: var(--primary); height: 100%; border-radius: 6px; }
.bar-fill--ok { background: #16a34a; }
.bar-value { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
.bar-pct { color: var(--muted); font-weight: 500; font-size: 0.85em; }
.bar-pct.ok { color: #16a34a; }

/* ---------------- Detailed entry table ---------------- */
.entry-table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.entry-table th, .entry-table td { border: 1px solid var(--border); padding: 0.35rem 0.5rem; text-align: center; }
.entry-table th { background: var(--bg); }
.entry-table input { width: 4rem; text-align: center; font: inherit; border: 1px solid var(--border); border-radius: 6px; padding: 0.2rem; }
.entry-totals input { width: 5rem; }
/* break hour: light gray but still enterable (plans aren't always followed) */
.entry-table td.cell-break { background-color: #eef1f5; }
.entry-table td.cell-break input { background: #f6f8fa; color: var(--muted); }

/* ---------------- Printable sheet ---------------- */
.preview-frame {
  overflow: auto;
  background: #cbd5e1;
  padding: 1rem;
  border-radius: 8px;
}
.sheet {
  background: #fff;
  width: 210mm;
  min-height: 297mm;
  padding: 8mm 9mm;
  margin: 0 auto;
  color: #111;
  font-family: "Segoe UI", system-ui, sans-serif;
  box-shadow: 0 2px 8px rgba(0,0,0,0.25);
  /* --sheet-font is set per-sheet by sheet.js to fit one page */
  --sheet-font: 9pt;
}
/* landscape sheet (used when more than 3 exercises) */
.sheet--landscape { width: 297mm; min-height: 210mm; }
.sheet__head {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  margin-bottom: 3mm;
}
.sheet__title { font-size: 13pt; font-weight: 700; margin: 0; }
.sheet__date { font-size: 13pt; font-weight: 700; letter-spacing: 1px; }

.sheet table { width: 100%; border-collapse: collapse; }
.sheet th, .sheet td {
  border: 1px solid #333;
  padding: 1px 5px;
  font-size: var(--sheet-font);
  line-height: 1.2;
}
.sheet thead th { background: #f0f0f0; text-align: center; font-weight: 700; }
.sheet .col-hour { width: 30px; text-align: center; font-weight: 700; background: #fafafa; }
.sheet .col-ex { text-align: left; }
.sheet .col-num { text-align: center; width: 13%; }
.sheet .col-fill { background: repeating-linear-gradient(0deg,#fff,#fff); }
.sheet tbody tr.hour-start td { border-top: 1.5px solid #000; }
/* break hour: light gray so it reads as "optional" but stays writable.
   Uses `td.col-blocked` so it beats the narrow col-min background. */
.sheet td.col-blocked { background-color: #ededed; }
.sheet .sum-block { margin-top: 4mm; }
.sheet .sum-block table { width: 100%; }
.sheet .sigma { font-size: calc(var(--sheet-font) + 6pt); }
.sheet .sum-row td { font-weight: 600; }

/* ---- columns layout (exercises across the top) ---- */
.sheet--cols th, .sheet--cols td { padding: 5px 4px; text-align: center; }
.sheet--cols .ex-group { background: #e9e9e9; font-weight: 700; }
.sheet--cols .col-min { width: 6%; background: #fafafa; white-space: nowrap; }
.sheet--cols .col-actual { width: auto; }
.sheet--cols .col-empty { background: #f4f4f4; }
.sheet--cols thead th { vertical-align: middle; }
.sheet--cols tfoot .sum-row td { border-top: 1.5px solid #000; font-weight: 700; }
.sheet--cols .col-hour { background: #fafafa; }
/* heavier separator at the start of each exercise group */
.sheet--cols .group-start { border-left: 1.5px solid #000; }

/* ---------------- Print rules ---------------- */
.print-only { display: none; }

@media print {
  /* default; #print-orientation (injected by app.js) overrides size when landscape */
  @page { size: A4 portrait; margin: 0; }
  body { background: #fff; }
  .no-print { display: none !important; }
  .app { display: none !important; }
  .print-only { display: block !important; }
  #print-root .sheet {
    box-shadow: none;
    margin: 0;
    width: 210mm;
    min-height: auto;
    page-break-after: avoid;
  }
  #print-root .sheet--landscape { width: 297mm; }
}

@media (max-width: 600px) {
  .topbar { flex-direction: column; align-items: stretch; }
  .tabs { justify-content: space-between; }
}
