:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #eef0f4;
  --text: #11131a;
  --muted: #6b7280;
  --line: #e4e7ec;
  --accent: #2563eb;
  --danger: #e11d48;
  --ok: #16a34a;
  --shadow: 0 6px 22px rgba(17, 19, 26, 0.08);
  --radius: 16px;
  --radius-sm: 12px;
  --tabbar-h: 64px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a21;
    --surface-2: #1f242e;
    --text: #f2f4f8;
    --muted: #9aa3b2;
    --line: #272d38;
    --accent: #3b82f6;
    --shadow: 0 8px 26px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3, p { margin: 0; }

.app {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

/* ---------- App bar ---------- */
.appbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: calc(env(safe-area-inset-top) + 12px) 16px 12px;
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  backdrop-filter: saturate(1.4) blur(12px);
  -webkit-backdrop-filter: saturate(1.4) blur(12px);
  border-bottom: 1px solid var(--line);
}

.brand { font-size: 1.18rem; font-weight: 800; letter-spacing: -0.01em; }
.appbar-actions { display: flex; align-items: center; gap: 8px; }

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  font-size: 1.4rem;
  line-height: 1;
}
.icon-btn:active { transform: scale(0.94); }

.sync-chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 40px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 700;
}
.sync-dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--muted);
  flex: 0 0 auto;
}
.sync-chip[data-state="synced"] .sync-dot { background: var(--ok); }
.sync-chip[data-state="syncing"] .sync-dot { background: var(--accent); animation: pulse 1s infinite; }
.sync-chip[data-state="offline"] .sync-dot,
.sync-chip[data-state="error"] .sync-dot { background: var(--danger); }
.sync-chip[data-state="local"] .sync-dot { background: #d97706; }
@keyframes pulse { 50% { opacity: 0.3; } }

/* ---------- Views ---------- */
.views {
  flex: 1;
  width: min(720px, 100%);
  margin: 0 auto;
  padding: 16px 16px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px);
}
.view { display: none; animation: fade 0.2s ease; }
.view.is-active { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 14px;
}
.eyebrow {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

/* ---------- Hero / live status ---------- */
.hero {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 22px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  margin-bottom: 14px;
  transition: background 0.3s, border-color 0.3s;
}
.hero.is-running {
  background: linear-gradient(135deg, color-mix(in srgb, var(--hero-color, var(--accent)) 16%, var(--surface)), var(--surface));
  border-color: color-mix(in srgb, var(--hero-color, var(--accent)) 45%, var(--line));
}
.hero-label { font-size: 0.95rem; font-weight: 700; color: var(--muted); }
.hero.is-running .hero-label { color: var(--hero-color, var(--accent)); }
.hero-clock {
  font-size: clamp(2.4rem, 12vw, 3.2rem);
  font-weight: 800;
  line-height: 1;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
  margin: 6px 0 4px;
}
.hero-sub { font-size: 0.85rem; color: var(--muted); }
.stop-btn {
  flex: 0 0 auto;
  align-self: stretch;
  min-width: 78px;
  padding: 0 18px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--danger);
  color: #fff;
  font-weight: 800;
  font-size: 1rem;
}
.stop-btn:active { transform: scale(0.96); }

/* ---------- Timer grid ---------- */
.timer-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}
@media (min-width: 560px) {
  .timer-grid { grid-template-columns: repeat(3, 1fr); }
}
.timer-card { position: relative; }
.timer-btn {
  width: 100%;
  min-height: 116px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 16px 14px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  text-align: left;
  box-shadow: var(--shadow);
  transition: transform 0.08s, box-shadow 0.2s;
}
.timer-btn::before {
  content: "";
  width: 34px; height: 5px;
  border-radius: 999px;
  background: var(--c);
  margin-bottom: 6px;
}
.timer-btn:active { transform: scale(0.97); }
.timer-btn.is-active {
  background: color-mix(in srgb, var(--c) 14%, var(--surface));
  border-color: color-mix(in srgb, var(--c) 55%, var(--line));
}
.t-name { font-weight: 800; font-size: 1rem; overflow-wrap: anywhere; }
.t-time {
  margin-top: auto;
  font-size: 1.3rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}
.t-action { font-size: 0.8rem; color: var(--muted); font-weight: 700; }
.timer-btn.is-active .t-action { color: var(--c); }
.t-edit {
  position: absolute;
  top: 8px; right: 8px;
  width: 30px; height: 30px;
  display: grid; place-items: center;
  border: none; border-radius: 9px;
  background: color-mix(in srgb, var(--surface-2) 70%, transparent);
  color: var(--muted);
  opacity: 0.65;
}
.t-edit:active { transform: scale(0.9); }

.today-line {
  text-align: center;
  color: var(--muted);
  font-weight: 700;
  font-size: 0.9rem;
  margin: 14px 0 4px;
}

.ghost-btn {
  width: 100%;
  min-height: 50px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1.5px dashed var(--line);
  border-radius: var(--radius);
  background: transparent;
  color: var(--muted);
  font-weight: 700;
  margin-top: 10px;
}
.ghost-btn:active { transform: scale(0.99); }
.add-timer { margin-top: 14px; }
.plus { font-size: 1.2rem; font-weight: 800; }

/* ---------- Calendar ---------- */
.month-head, .card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 12px;
}
.month-head h2 { font-size: 1.05rem; text-align: center; flex: 1; }
.card-title { text-align: center; flex: 1; }
.card-title h2 { font-size: 1rem; margin-top: 2px; }

.weekday-row, .cal-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 6px;
}
.weekday-row {
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
  text-align: center;
}
.cal-grid { margin-bottom: 16px; }
.cal-day {
  position: relative;
  aspect-ratio: 1 / 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding: 6px 2px 4px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: var(--surface);
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 700;
}
.cal-day:active { transform: scale(0.95); }
.cal-day.is-outside { color: var(--muted); opacity: 0.45; }
.cal-day.is-today { border-color: var(--accent); }
.cal-day.is-selected {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.cal-day .dots {
  display: flex;
  gap: 3px;
  flex-wrap: wrap;
  justify-content: center;
  margin-top: 3px;
  min-height: 7px;
}
.cal-day .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--dc); }
.cal-day.is-selected .dot { box-shadow: 0 0 0 1px rgba(255,255,255,0.7); }
.cal-day .mini {
  position: absolute;
  bottom: 3px;
  font-size: 0.6rem;
  font-weight: 800;
  color: var(--muted);
}
.cal-day.is-selected .mini { color: rgba(255,255,255,0.85); }

/* ---------- Day detail ---------- */
.day-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}
.day-head h3 { font-size: 1.05rem; margin-top: 2px; }
.pill {
  flex: 0 0 auto;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--surface-2);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}
.sessions { display: grid; gap: 8px; margin: 8px 0 4px; }
.session {
  display: grid;
  gap: 4px;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-left: 5px solid var(--c);
  border-radius: var(--radius-sm);
  background: var(--surface);
  text-align: left;
  width: 100%;
}
.session:active { transform: scale(0.99); }
.session-top { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; }
.session-name { font-weight: 800; overflow-wrap: anywhere; }
.session-dur { flex: 0 0 auto; color: var(--muted); font-weight: 800; font-variant-numeric: tabular-nums; }
.session-time { color: var(--muted); font-size: 0.85rem; font-weight: 600; }

/* ---------- Bars (totals) ---------- */
.bars { display: grid; gap: 11px; }
.bar-row { display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; align-items: center; }
.bar-label { font-weight: 700; overflow-wrap: anywhere; }
.bar-value { color: var(--muted); font-weight: 800; font-variant-numeric: tabular-nums; }
.bar-track {
  grid-column: 1 / -1;
  height: 8px;
  border-radius: 999px;
  background: var(--surface-2);
  overflow: hidden;
}
.bar-fill { height: 100%; border-radius: inherit; background: var(--c); width: var(--w); }
.grand {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}
.empty { color: var(--muted); padding: 14px 2px; text-align: center; font-weight: 600; }

/* ---------- Tab bar ---------- */
.tabbar {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  z-index: 20;
  display: flex;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: saturate(1.4) blur(14px);
  -webkit-backdrop-filter: saturate(1.4) blur(14px);
  border-top: 1px solid var(--line);
}
.tab {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  border: none;
  background: none;
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 700;
}
.tab svg { width: 25px; height: 25px; }
.tab.is-active { color: var(--accent); }
.tab:active { transform: scale(0.92); }

/* ---------- Dialogs / sheets ---------- */
.sheet {
  width: min(440px, 100%);
  border: none;
  border-radius: 20px;
  padding: 0;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
.sheet::backdrop { background: rgba(0,0,0,0.45); backdrop-filter: blur(2px); }
.sheet-form { display: grid; gap: 14px; padding: 20px; }
.sheet-form h3 { font-size: 1.15rem; }
@media (max-width: 480px) {
  .sheet {
    width: 100%;
    margin: auto auto 0;
    border-radius: 22px 22px 0 0;
    padding-bottom: env(safe-area-inset-bottom);
  }
}
.field { display: grid; gap: 6px; }
.field > span { font-size: 0.82rem; font-weight: 700; color: var(--muted); }
.field input[type="text"],
.field input[type="date"],
.field input[type="time"],
.field select {
  width: 100%;
  min-height: 46px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
}
.field input:focus, .field select:focus {
  outline: 2px solid color-mix(in srgb, var(--accent) 40%, transparent);
  border-color: var(--accent);
}
.field input[type="color"] {
  width: 100%;
  height: 46px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--bg);
}
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.checkbox { display: flex; align-items: center; gap: 9px; font-weight: 600; }
.checkbox input { width: 20px; height: 20px; }
.hint { font-size: 0.78rem; color: var(--muted); line-height: 1.4; }
.sheet-actions { display: flex; align-items: center; gap: 10px; margin-top: 4px; }
.spacer { flex: 1; }
.primary-btn {
  min-height: 46px;
  padding: 0 20px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: #fff;
  font-weight: 800;
}
.text-btn {
  min-height: 46px;
  padding: 0 12px;
  border: none;
  background: none;
  color: var(--muted);
  font-weight: 700;
}
.danger-btn {
  min-height: 46px;
  padding: 0 16px;
  border: 1px solid color-mix(in srgb, var(--danger) 40%, var(--line));
  border-radius: var(--radius-sm);
  background: none;
  color: var(--danger);
  font-weight: 700;
}
.primary-btn:active, .danger-btn:active { transform: scale(0.97); }

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