:root {
  --bg: #f3f5f8;
  --surface: #ffffff;
  --text: #17202a;
  --muted: #5d6b7a;
  --border: #dde3ea;
  --accent: #1f5f8b;
  --accent-strong: #174a6d;
  --accent-soft: #e3eef6;
  --on-accent: #ffffff;
  --success: #1d7a46;
  --success-soft: #e3f4ea;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141a;
    --surface: #18202a;
    --text: #e8edf2;
    --muted: #9aa8b6;
    --border: #2b3643;
    --accent: #5aa6dc;
    --accent-strong: #7dbbe6;
    --accent-soft: #1c3346;
    --on-accent: #0b1620;
    --success: #5cc98a;
    --success-soft: #16301f;
    --danger: #f2877d;
    --danger-soft: #3a1c1a;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

#app { min-height: 100vh; display: flex; flex-direction: column; }

main {
  width: 100%;
  max-width: 620px;
  margin: 0 auto;
  padding: 16px 16px calc(96px + env(safe-area-inset-bottom));
  display: grid;
  gap: 16px;
}

main.center { flex: 1; align-content: center; padding-bottom: 48px; }

h1 { font-size: 1.35rem; margin: 0 0 12px; }
h2 { font-size: 1.1rem; margin: 0 0 12px; }
p { margin: 0 0 12px; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  max-width: 620px;
  width: 100%;
  margin: 0 auto;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--text);
  text-decoration: none;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 9px;
  background: var(--accent);
  color: var(--on-accent);
  font-weight: 800;
}

.lang-switch {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 999px;
  overflow: hidden;
  background: var(--surface);
}

.lang-switch button {
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  padding: 4px 12px;
  cursor: pointer;
}

.lang-switch button[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); }

/* ---------- Cards & forms ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  min-width: 0;
}

.card.login { max-width: 420px; width: 100%; margin: 0 auto; }

label { display: block; font-weight: 600; font-size: 0.9rem; margin: 0 0 6px; }

input {
  width: 100%;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  margin: 0 0 14px;
}

input:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }

.code-input { font-size: 1.5rem; letter-spacing: 0.3em; text-align: center; font-variant-numeric: tabular-nums; }

/* manipulation: quick taps on −/+ shouldn't trigger iOS double-tap zoom. */
button { font: inherit; cursor: pointer; touch-action: manipulation; }
button:disabled { opacity: 0.6; cursor: default; }

.primary, .secondary {
  width: 100%;
  border-radius: 10px;
  padding: 12px 16px;
  font-weight: 600;
}

.primary { background: var(--accent); color: var(--on-accent); border: 0; }
.primary:hover:not(:disabled) { background: var(--accent-strong); }
.secondary { background: transparent; color: var(--text); border: 1px solid var(--border); }

.link {
  background: none;
  border: 0;
  padding: 4px 0;
  color: var(--accent);
  font-weight: 600;
}

.link.danger { color: var(--danger); }

.row-links { display: flex; justify-content: space-between; gap: 12px; margin-top: 12px; flex-wrap: wrap; }

.muted { color: var(--muted); }
.small { font-size: 0.85rem; margin: 12px 0 0; }

.info, .error, .success { border-radius: 10px; padding: 10px 12px; font-size: 0.92rem; }
.info { background: var(--accent-soft); }
.error { background: var(--danger-soft); color: var(--danger); }
.success { background: var(--success-soft); color: var(--success); font-weight: 600; }
.banner { margin: 0; }

/* ---------- Score form ---------- */
.score-row { display: grid; grid-template-columns: 1fr 110px; gap: 12px; }

.stepper { display: grid; grid-template-columns: 48px 1fr 48px; gap: 6px; margin-bottom: 14px; }
.stepper input { margin: 0; text-align: center; font-size: 1.4rem; font-weight: 700; padding: 6px; }
.stepper button {
  border: 1px solid var(--border);
  background: var(--accent-soft);
  color: var(--accent-strong);
  border-radius: 10px;
  font-size: 1.4rem;
  font-weight: 700;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.max-field input { font-size: 1.4rem; text-align: center; padding: 6px; height: calc(100% - 14px - 27px); min-height: 48px; }

/* iOS Safari gives date inputs a fixed native width that ignores width: 100%. */
input[type="date"] {
  -webkit-appearance: none;
  appearance: none;
  display: block;
  min-width: 0;
  max-width: 100%;
  min-height: 46px;
}
input[type="date"]::-webkit-date-and-time-value { text-align: left; }

input[type="number"] { -moz-appearance: textfield; }
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }

/* ---------- Recent days ---------- */
.recent { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }

.recent li { display: grid; grid-template-columns: 6.5em 1fr auto; gap: 12px; align-items: center; }
.recent .date { color: var(--muted); font-size: 0.92rem; }
.recent .score { font-weight: 700; font-variant-numeric: tabular-nums; min-width: 4.5em; text-align: right; }
.meter { height: 10px; border-radius: 999px; background: var(--bg); overflow: hidden; }
.meter span { display: block; height: 100%; width: var(--w); background: var(--accent); border-radius: inherit; }

/* ---------- Offices ---------- */
.brand-office {
  display: block;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--muted);
  line-height: 1.1;
}

.subtitle { margin-top: -8px; }
.hint { margin: -8px 0 14px; }

.office-list { display: grid; gap: 8px; margin-bottom: 16px; }

.add-office summary { color: var(--accent); font-weight: 600; cursor: pointer; padding: 4px 0; }
.add-office[open] summary { margin-bottom: 10px; }
.add-office .error { margin-top: 8px; }

.inline-form { display: flex; gap: 8px; }
.inline-form input { margin: 0; flex: 1; min-width: 0; }
.inline-form .secondary { width: auto; }

select {
  width: 100%;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  margin: 0 0 14px;
}

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

/* ---------- Stats & chart ---------- */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 16px; }

.stat {
  background: var(--bg);
  border-radius: 10px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  min-width: 0;
}

.stat .label { font-size: 0.78rem; color: var(--muted); }
.stat .value { font-size: 1.3rem; font-weight: 700; font-variant-numeric: tabular-nums; }

.chart { margin: 0; }
.chart figcaption { font-size: 0.85rem; color: var(--muted); margin-bottom: 8px; }
.chart svg { width: 100%; height: 140px; display: block; overflow: visible; }
.chart .bars rect { fill: var(--accent); }
.chart .bars rect:hover { fill: var(--accent-strong); }
.chart .grid line { stroke: var(--border); stroke-dasharray: 4 4; vector-effect: non-scaling-stroke; }
.chart-axis { display: flex; justify-content: space-between; font-size: 0.78rem; color: var(--muted); margin-top: 4px; }

.history { list-style: none; margin: 0; padding: 0; }

.history li {
  display: grid;
  grid-template-columns: 1fr auto auto;
  grid-template-areas: "date score pct" "actions actions actions";
  column-gap: 12px;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}

.history li:last-child { border-bottom: 0; }
.history .date { grid-area: date; }
.history .score { grid-area: score; font-weight: 700; font-variant-numeric: tabular-nums; }
.history .pct { grid-area: pct; color: var(--muted); font-variant-numeric: tabular-nums; min-width: 3.5em; text-align: right; }
.history .actions { grid-area: actions; display: flex; align-items: center; gap: 16px; font-size: 0.85rem; }
.history .actions .muted { margin-right: auto; }

/* ---------- Bottom tab bar ---------- */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: center;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 10;
}

.tabbar a {
  flex: 1;
  max-width: 155px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 4px 10px;
  color: var(--muted);
  text-decoration: none;
  font-size: 0.75rem;
  font-weight: 600;
}

.tabbar svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.tabbar a.active { color: var(--accent); }

@media (max-width: 420px) {
  .stats { grid-template-columns: repeat(2, 1fr); }
  .card { padding: 16px; }
}
