:root {
  --bg: #f4f1ea;
  --card: #fffdf8;
  --ink: #1d1c1a;
  --ink-2: #6b665d;
  --ink-3: #a39d92;
  --line: #e4ded2;
  --accent: #e0572b;
  --accent-ink: #ffffff;
  --rest: #2f8f7a;
  --rest-soft: #e2f1ec;
  --over: #c2371a;
  --over-soft: #fbe6df;
  --shadow: 0 1px 2px rgba(40, 30, 10, .06), 0 8px 24px rgba(40, 30, 10, .06);
  --radius: 20px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16171b;
    --card: #212228;
    --ink: #f1efe9;
    --ink-2: #a8a49b;
    --ink-3: #6d6a64;
    --line: #2f3037;
    --accent: #ff6a3d;
    --accent-ink: #1a0d07;
    --rest: #4cc3a5;
    --rest-soft: #1c3530;
    --over: #ff7a55;
    --over-soft: #3a2119;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .25);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}
button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; touch-action: manipulation; }

.app {
  max-width: 520px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 8px) 16px calc(env(safe-area-inset-bottom) + 32px);
}

.topbar { display: flex; align-items: center; justify-content: space-between; height: 44px; }
.brand { font-weight: 700; letter-spacing: .02em; font-size: 15px; color: var(--ink-2); }
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px;
  display: grid; place-items: center;
  color: var(--ink-2); font-size: 26px; line-height: 1;
}
.icon-btn:active { background: var(--line); }
.icon-btn:disabled { opacity: .25; }

/* ── 現在這一圈 ── */
.now {
  margin-top: 8px;
  padding: 22px 20px 18px;
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  text-align: center;
  transition: background .3s;
}
.now-label { font-size: 13px; color: var(--ink-3); letter-spacing: .08em; }
.now-name {
  display: block; width: 100%;
  margin-top: 4px;
  font-size: 22px; font-weight: 700;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.now-name.is-empty { color: var(--ink-3); font-weight: 500; }
.now-timer {
  margin-top: 6px;
  font-size: 64px; font-weight: 300; letter-spacing: -.02em;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}
.now-sub { margin-top: 6px; font-size: 14px; color: var(--ink-2); font-variant-numeric: tabular-nums; }

.now.is-rest { background: var(--rest-soft); }
.now.is-rest .now-timer { color: var(--rest); }
.now.is-over { background: var(--over-soft); }
.now.is-over .now-timer { color: var(--over); }

/* ── 按鈕 ── */
.actions { display: grid; grid-template-columns: 1fr auto; gap: 12px; margin-top: 14px; }
.btn-lap {
  height: 84px; border-radius: var(--radius);
  background: var(--accent); color: var(--accent-ink);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  box-shadow: 0 6px 18px color-mix(in srgb, var(--accent) 35%, transparent);
  transition: transform .08s ease;
}
.btn-lap:active, .btn-rest:active { transform: scale(.97); }
.btn-lap-main { font-size: 24px; font-weight: 800; letter-spacing: .04em; }
.btn-lap-hint { font-size: 12px; opacity: .8; margin-top: 2px; }
.btn-rest {
  width: 104px; height: 84px; border-radius: var(--radius);
  background: var(--card); box-shadow: var(--shadow);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: 14px; font-weight: 600; color: var(--rest);
  transition: transform .08s ease;
}
.btn-rest-emoji { font-size: 24px; }

.banner {
  margin-top: 14px; padding: 12px 14px;
  border-radius: 14px;
  background: var(--over-soft); color: var(--over);
  font-size: 14px; font-weight: 600;
}

/* ── 當天 ── */
.day { margin-top: 28px; }
.day-nav { display: flex; align-items: center; justify-content: space-between; }
.day-title { font-weight: 700; font-size: 17px; }

.seg {
  display: grid; grid-template-columns: 1fr 1fr;
  margin: 10px 0 14px; padding: 3px;
  background: var(--line); border-radius: 12px;
}
.seg-btn { height: 34px; border-radius: 9px; font-size: 14px; font-weight: 600; color: var(--ink-2); }
.seg-btn.is-on { background: var(--card); color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.08); }

.period { margin-bottom: 18px; }
.period-head {
  display: flex; justify-content: space-between; align-items: baseline;
  padding: 0 4px 6px; font-size: 13px; color: var(--ink-3); letter-spacing: .06em;
}
.period-list { background: var(--card); border-radius: 16px; box-shadow: var(--shadow); overflow: hidden; }

.lap {
  display: grid; grid-template-columns: 52px 1fr auto; align-items: center; gap: 10px;
  width: 100%; padding: 12px 14px; text-align: left;
  border-top: 1px solid var(--line);
}
.lap:first-child { border-top: 0; }
.lap:active { background: var(--line); }
.lap-time { font-size: 13px; color: var(--ink-3); font-variant-numeric: tabular-nums; }
.lap-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lap-name.is-empty { color: var(--ink-3); font-weight: 400; font-style: italic; }
.lap-dur { font-variant-numeric: tabular-nums; font-size: 15px; color: var(--ink-2); }
.lap.is-running .lap-dur { color: var(--accent); font-weight: 700; }
.lap-tag { font-size: 12px; color: var(--rest); margin-left: 6px; font-weight: 500; }
.lap-tag.is-over { color: var(--over); }

.empty { text-align: center; color: var(--ink-3); padding: 32px 0; font-size: 14px; }

/* 統計 */
.stat-total { text-align: center; color: var(--ink-2); font-size: 14px; margin-bottom: 12px; }
.stat-total b { color: var(--ink); font-size: 20px; font-variant-numeric: tabular-nums; }
.stat { padding: 10px 14px; border-top: 1px solid var(--line); }
.stat:first-child { border-top: 0; }
.stat-row { display: flex; justify-content: space-between; gap: 10px; }
.stat-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stat-val { color: var(--ink-2); font-variant-numeric: tabular-nums; white-space: nowrap; }
.stat-bar { margin-top: 6px; height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; }
.stat-bar > i { display: block; height: 100%; border-radius: 3px; background: var(--accent); }
.stat.is-rest .stat-bar > i { background: var(--rest); }

/* ── 底部面板 ── */
.sheet {
  width: 100%; max-width: 520px;
  margin: auto auto 0; padding: 0;
  border: 0; border-radius: 22px 22px 0 0;
  background: var(--card); color: var(--ink);
  max-height: 88vh;
}
.sheet::backdrop { background: rgba(0, 0, 0, .35); }
.sheet[open] { animation: up .22s ease-out; }
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
.sheet-body { padding: 10px 18px calc(env(safe-area-inset-bottom) + 18px); }
.sheet-grip { width: 38px; height: 5px; border-radius: 3px; background: var(--line); margin: 0 auto 12px; }
.sheet-title { margin: 0 0 14px; font-size: 19px; }

.field {
  display: block; width: 100%; margin-top: 6px;
  padding: 13px 14px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--bg); color: var(--ink);
  font: inherit; font-size: 17px; /* ≥16px iOS 才不會自動放大 */
  outline: none; -webkit-appearance: none; appearance: none;
}
.field:focus { border-color: var(--accent); }
.lbl { display: block; font-size: 13px; color: var(--ink-2); margin-bottom: 12px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 12px; }
.row2 .lbl { margin-bottom: 0; }
.hint { font-size: 12px; color: var(--ink-3); margin: 0 0 12px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0 4px; }
.chip {
  padding: 8px 14px; border-radius: 999px;
  background: var(--bg); border: 1px solid var(--line);
  font-size: 15px;
}
.chip:active { background: var(--line); }
.chip.is-back { border-color: var(--accent); color: var(--accent); }

.sheet-actions { display: flex; gap: 10px; margin-top: 16px; align-items: center; }
.sheet-actions > .btn-primary, .sheet-actions > .btn-ghost { flex: 1; }
.sheet-actions .spacer { flex: 0; }
.btn-primary, .btn-ghost, .btn-danger { height: 48px; border-radius: 14px; font-weight: 700; padding: 0 16px; }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-ghost { background: var(--bg); border: 1px solid var(--line); }
.btn-danger { color: var(--over); }
.wide { width: 100%; margin-bottom: 12px; }
