:root {
  color-scheme: light;
  --bg: #f4f3ef;
  --fg: #141414;
  --muted: #6b6b6b;
  --ok: #2f7d4f;
  --panel: #ffffff;
  --line: #d6d3cc;
  --btn: #e8e6e0;
  --btn-hover: #dcd9d1;
  --danger-bg: #c62828;
  --danger-fg: #ffffff;
  --stale-bg: #f2c037;
  --stale-fg: #1a1a1a;
  --warn-text: #c62828;
  --alert: #d32f2f;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0b0c0e;
    --fg: #e9e9e6;
    --muted: #8a8d92;
    --ok: #4cc38a;
    --panel: #17191c;
    --line: #2c2f34;
    --btn: #1f2226;
    --btn-hover: #2a2e33;
    --danger-bg: #d32f2f;
    --danger-fg: #ffffff;
    --stale-bg: #e0b12e;
    --stale-fg: #111111;
    --warn-text: #ff6b63;
    --alert: #ff5a52;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0b0c0e;
  --fg: #e9e9e6;
  --muted: #8a8d92;
  --ok: #4cc38a;
  --panel: #17191c;
  --line: #2c2f34;
  --btn: #1f2226;
  --btn-hover: #2a2e33;
  --danger-bg: #d32f2f;
  --danger-fg: #ffffff;
  --stale-bg: #e0b12e;
  --stale-fg: #111111;
  --warn-text: #ff6b63;
  --alert: #ff5a52;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--fg);
  font-family: "SF Pro Display", "Segoe UI", Roboto, "Helvetica Neue", "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", sans-serif;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}
body.idle { cursor: none; }

/* ---------- banner ---------- */
.banner {
  position: fixed; inset: 0 0 auto 0; z-index: 20;
  padding: 0.6em 16px;
  font-size: clamp(16px, 2.6vw, 34px);
  font-weight: 700;
  text-align: center;
  line-height: 1.3;
}
.banner.danger { background: var(--danger-bg); color: var(--danger-fg); animation: pulse 2s ease-in-out infinite; }
.banner.stale { background: var(--stale-bg); color: var(--stale-fg); }
@keyframes pulse { 50% { filter: brightness(1.25); } }

/* ---------- stage ---------- */
.stage {
  height: 100%;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 1.5vh;
  padding: 0 16px;
  text-align: center;
}
.date { font-size: clamp(18px, 3.4vw, 48px); color: var(--muted); font-weight: 500; letter-spacing: 0.04em; }
.clock, .timer-value {
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1;
  font-weight: 600;
  line-height: 1;
  white-space: nowrap;
  letter-spacing: -0.01em;
}
.clock { font-size: min(15vw, 36vh); }
/* 数字セル: 最も幅の広い数字に合わせた固定幅(--dwはJSで計測) */
.clock .d, .timer-value .d { display: inline-block; width: var(--dw, 0.62em); text-align: center; }
.clock .p, .timer-value .p { display: inline-block; }
.clock .ms { font-size: 0.42em; color: var(--muted); margin-left: 0.04em; }
body.warn .clock, body.warn .clock .ms { color: var(--warn-text); }

.timer {
  all: unset;
  cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 0.08em;
  font-size: min(8vw, 17vh);
  border-radius: 12px;
  padding: 0 0.2em;
}
.timer:focus-visible { outline: 3px solid var(--ok); }
.timer-label { font-size: max(14px, 0.18em); color: var(--muted); font-weight: 600; }
.timer.over .timer-value, .timer.over .timer-label { color: var(--alert); }
.timer.paused .timer-value { opacity: 0.45; }

body.timer-main .clock { font-size: min(8vw, 17vh); }
body.timer-main .timer { font-size: min(15vw, 36vh); }

.sync {
  font-size: clamp(12px, 1.5vw, 20px);
  color: var(--muted);
  display: flex; align-items: center; gap: 0.5em; flex-wrap: wrap; justify-content: center;
}
.sync .dot { width: 0.7em; height: 0.7em; border-radius: 50%; background: var(--muted); flex: none; }
.sync.ok .dot { background: var(--ok); }
.sync.stale .dot { background: var(--stale-bg); }
.sync.bad .dot { background: var(--danger-bg); }

/* ---------- countdown end flash ---------- */
.flash {
  position: fixed; inset: 0; z-index: 5; pointer-events: none;
  background: var(--alert); opacity: 0;
}
body.alerting .flash { animation: flash 1s steps(1, end) infinite; }
@keyframes flash { 0% { opacity: 0.35; } 50% { opacity: 0; } }

/* ---------- controls ---------- */
.controls {
  position: fixed; left: 50%; bottom: max(16px, env(safe-area-inset-bottom)); transform: translateX(-50%);
  z-index: 10;
  display: flex; flex-wrap: wrap; justify-content: center; gap: 8px;
  width: max-content; max-width: calc(100vw - 32px);
  padding: 8px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  transition: opacity .3s;
}
body.idle .controls, body.idle .chip { opacity: 0; pointer-events: none; }

button {
  font: inherit; font-size: 15px;
  color: var(--fg);
  background: var(--btn);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 14px;
  cursor: pointer;
  min-height: 40px;
}
button:hover { background: var(--btn-hover); }
button.primary { background: var(--ok); color: #fff; border-color: transparent; }
button[hidden] { display: none; }

.chip {
  position: fixed; top: 16px; right: 16px; z-index: 15;
  transition: opacity .3s;
}
body.has-banner .chip { top: auto; bottom: 96px; }

/* ---------- dialogs ---------- */
dialog {
  color: var(--fg); background: var(--panel);
  border: 1px solid var(--line); border-radius: 16px;
  padding: 20px; width: min(440px, calc(100vw - 32px));
}
dialog::backdrop { background: rgb(0 0 0 / 0.5); }
dialog h2 { margin: 0 0 12px; font-size: 20px; }
dialog h3 { margin: 16px 0 8px; font-size: 15px; color: var(--muted); font-weight: 600; }
dialog section + section { border-top: 1px solid var(--line); margin-top: 12px; }
.presets { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.presets button { padding: 8px 0; }
.row { display: flex; align-items: center; gap: 8px; margin-top: 10px; }
.row input[type="number"], .row input[type="time"] {
  font: inherit; font-size: 16px; color: var(--fg); background: var(--bg);
  border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; min-height: 40px;
  flex: 1; min-width: 0;
}
.row input[type="range"] { flex: 1; }
.row label[for] { min-width: 3em; }
.switch { display: flex; align-items: center; gap: 10px; flex: 1; cursor: pointer; font-size: 16px; }
.switch input { width: 20px; height: 20px; accent-color: var(--ok); }
.note { color: var(--muted); font-size: 13px; line-height: 1.6; margin: 14px 0 0; }
.foot { display: flex; justify-content: flex-end; margin-top: 18px; }

@media (max-width: 600px) {
  .clock { font-size: 13.5vw; }
  body.timer-main .timer { font-size: 13.5vw; }
  button { font-size: 14px; padding: 6px 10px; }
}
