:root {
  --bg: #0b0d10; --surface: #161a20; --surface2: #20262e; --line: #2c333d;
  --text: #f3f5f7; --muted: #a3acb8; --accent: #22c55e; --danger: #f87171;
  /* State colours: chosen for contrast with their text colour (WCAG AA for large text or better). */
  --prep: #facc15; --prep-fg: #111; --work: #15803d; --work-fg: #fff; --rest: #b91c1c; --rest-fg: #fff;
  --done: #1d4ed8; --done-fg: #fff;
  --sat: env(safe-area-inset-top); --sab: env(safe-area-inset-bottom); --sal: env(safe-area-inset-left); --sar: env(safe-area-inset-right);
  color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%; overscroll-behavior: none; touch-action: manipulation;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; touch-action: manipulation; }
button:disabled { opacity: .35; cursor: default; }
:focus-visible { outline: 3px solid #60a5fa; outline-offset: 2px; }

.page { max-width: 720px; margin: 0 auto; padding: calc(var(--sat) + 8px) calc(var(--sar) + 16px) calc(var(--sab) + 24px) calc(var(--sal) + 16px); }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-height: 52px; }
.topbar h1 { font-size: 22px; letter-spacing: .08em; margin: 0; font-weight: 800; }
.topbar h2 { font-size: 18px; margin: 0; }
h3 { font-size: 14px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; margin: 28px 0 8px; }

.btn { background: var(--surface2); border: 1px solid var(--line); border-radius: 12px; padding: 12px 16px; min-height: 48px; display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
.btn.primary { background: var(--accent); color: #04130a; border-color: transparent; font-weight: 700; }
.btn.danger { background: var(--danger); color: #1a0505; border-color: transparent; font-weight: 700; }
.btn.small { min-height: 40px; padding: 6px 12px; }
.btn.wide { width: 100%; margin-top: 12px; }
.danger-text { color: var(--danger) !important; }
.icon-btn { background: transparent; border: 0; width: 48px; height: 48px; border-radius: 12px; display: inline-flex; align-items: center; justify-content: center; font-size: 22px; }
.icon-btn svg { width: 26px; height: 26px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; margin: 12px 0; }
.hint, .note { color: var(--muted); font-size: 14px; }
.spacer { height: 40px; }

.tabs { display: flex; gap: 8px; margin: 8px 0 12px; }
.tabs button { flex: 1; background: var(--surface); border: 1px solid var(--line); border-radius: 999px; min-height: 44px; }
.tabs button[aria-selected="true"] { background: var(--text); color: var(--bg); font-weight: 700; }

.cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.card { display: grid; grid-template-columns: 44px 1fr 72px; align-items: stretch; background: var(--surface); border: 1px solid var(--line); border-radius: 16px; overflow: hidden; }
.card .fav { background: none; border: 0; font-size: 22px; color: var(--muted); }
.card .fav.on { color: var(--prep); }
.card-body { background: none; border: 0; text-align: left; padding: 14px 4px; display: grid; gap: 2px; min-width: 0; }
.card-name { font-weight: 700; font-size: 18px; }
.card-sub { color: var(--muted); font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.play { background: var(--accent); color: #04130a; border: 0; display: flex; align-items: center; justify-content: center; }
.play svg { width: 34px; height: 34px; }
.empty { color: var(--muted); padding: 24px; text-align: center; }

.field { display: grid; gap: 6px; margin: 10px 0; }
.field > span { color: var(--muted); font-size: 14px; }
.field input:not([type=checkbox]):not([type=radio]):not([type=range]), .field select, .block-name {
  background: var(--surface2); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; min-height: 46px; width: 100%;
}
.field input[type=range] { width: 100%; accent-color: var(--accent); min-height: 40px; }
.field.check { grid-template-columns: 28px 1fr; align-items: center; min-height: 44px; }
.field.check input { width: 22px; height: 22px; accent-color: var(--accent); }
.field.check span { color: var(--text); font-size: 16px; }
.sound-lib .field { grid-template-columns: 1fr 64px; align-items: center; margin: 4px 0; min-height: 44px; }
.sound-lib .field > span { color: var(--text); font-size: 15px; }
.dur-row { display: flex; align-items: center; gap: 6px; }
.dur-row input { width: 4.2em !important; text-align: center; font-variant-numeric: tabular-nums; font-size: 20px; }
.dur-row .step { width: 52px; height: 46px; border-radius: 10px; background: var(--surface2); border: 1px solid var(--line); font-size: 22px; }
.inline { display: flex; gap: 8px; align-items: center; }
.inline select { flex: 1; }
.inline input { width: 5em !important; }
.block { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 8px 12px; margin: 10px 0; }
.block-head { display: flex; align-items: center; gap: 4px; }
.block-head .block-name { flex: 1; min-width: 0; }
.total-line { color: var(--muted); }
.seg { display: grid; gap: 8px; }
.seg label { display: flex; gap: 10px; align-items: center; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 12px; min-height: 48px; }
.seg input { width: 20px; height: 20px; accent-color: var(--accent); }
.sound-ed { margin-top: 8px; }

.overlay { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: flex; align-items: center; justify-content: center; z-index: 20; padding: 16px; }
.dialog { background: var(--surface); border: 1px solid var(--line); border-radius: 18px; padding: 20px; width: min(420px, 100%); }
.dialog p { font-size: 18px; margin: 0 0 16px; }
.dialog-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 12px; }
.sheet-ov { align-items: flex-end; padding: 0; }
.sheet { background: var(--surface); border-radius: 18px 18px 0 0; width: min(560px, 100%); padding: 12px 16px calc(var(--sab) + 12px); max-height: 85vh; overflow: auto; }
.sheet h3 { margin: 4px 0 8px; text-transform: none; font-size: 16px; color: var(--text); }
.sheet-item { display: block; width: 100%; text-align: left; background: var(--surface2); border: 0; border-radius: 12px; padding: 14px 16px; margin: 6px 0; min-height: 50px; font-size: 17px; }
.sheet-item.cancel { text-align: center; background: transparent; border: 1px solid var(--line); }
.toast { position: fixed; left: 50%; bottom: calc(var(--sab) + 16px); transform: translateX(-50%); background: #e5e7eb; color: #111; padding: 12px 16px; border-radius: 12px; z-index: 30; display: flex; gap: 12px; align-items: center; width: max-content; max-width: calc(100% - 32px); box-shadow: 0 6px 24px rgba(0,0,0,.4); }
.toast.top { bottom: auto; top: calc(var(--sat) + 64px); }
.toast button { background: #111; color: #fff; border: 0; border-radius: 8px; padding: 8px 12px; }

/* ---------------- Run screen ---------------- */
.run {
  --c: var(--prep); --fg: var(--prep-fg);
  position: fixed; inset: 0; background: var(--c); color: var(--fg); display: grid;
  grid-template-rows: auto 1fr auto; overflow: hidden; user-select: none; -webkit-user-select: none;
  padding: var(--sat) var(--sar) var(--sab) var(--sal);
}
.run.phase-work { --c: var(--work); --fg: var(--work-fg); }
.run.phase-rest { --c: var(--rest); --fg: var(--rest-fg); }
/* Non-colour cue: rest screens get a diagonal stripe texture. */
.run.phase-rest::before { content: ""; position: absolute; inset: 0; pointer-events: none;
  background: repeating-linear-gradient(135deg, rgba(0,0,0,.10) 0 14px, transparent 14px 28px); }
.run.phase-done { --c: var(--done); --fg: var(--done-fg); }
.run > * { position: relative; }
.run-top { display: flex; align-items: center; justify-content: space-between; padding: 4px 8px; }
.run-top .icon-btn { width: 52px; height: 52px; background: rgba(0,0,0,.16); color: var(--fg); font-size: 24px; }
.run-title { font-weight: 700; text-align: center; font-size: 15px; opacity: .9; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 0 8px; }
.run-main { display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 0; padding: 0 12px; }
.phase-label { font-size: clamp(26px, 8vw, 56px); font-weight: 900; letter-spacing: .04em; display: flex; gap: .35em; align-items: center; text-transform: uppercase; }
.phase-icon { font-size: .8em; }
.time { font-weight: 800; font-variant-numeric: tabular-nums; line-height: .95; letter-spacing: -.02em; white-space: nowrap;
  font-size: min(38vw, 36vh); font-family: -apple-system, "SF Pro Display", "Helvetica Neue", Roboto, sans-serif; }
.time[data-len="5"] { font-size: min(30vw, 34vh); }
.time[data-len="7"] { font-size: min(20vw, 26vh); }
.setinfo { font-size: clamp(22px, 7vw, 44px); font-weight: 800; min-height: 1.2em; font-variant-numeric: tabular-nums; }
.bar { width: min(92%, 640px); height: 12px; background: rgba(0,0,0,.22); border-radius: 6px; overflow: hidden; margin-top: 12px; }
.bar-fill, .totalbar > div { height: 100%; background: var(--fg); transform-origin: left; transform: scaleX(0); }
.run-side { padding: 8px 16px 16px; }
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.stats > div { background: rgba(0,0,0,.18); border-radius: 12px; padding: 8px 12px; min-width: 0; }
.stats small { display: block; font-size: 13px; opacity: .85; font-weight: 700; }
.stats b { font-size: clamp(16px, 5vw, 28px); line-height: 1.15; font-variant-numeric: tabular-nums; display: block; overflow-wrap: anywhere; }
.totalbar { height: 6px; background: rgba(0,0,0,.22); border-radius: 3px; overflow: hidden; margin: 10px 0; }
.controls { display: grid; grid-template-columns: 1fr 1.6fr 1fr; gap: 12px; align-items: center; }
.ctl { height: 76px; border: 0; border-radius: 20px; background: rgba(0,0,0,.22); color: var(--fg); display: flex; align-items: center; justify-content: center; }
.ctl svg { width: 34px; height: 34px; }
.ctl.main { height: 92px; background: rgba(0,0,0,.38); }
.ctl.main svg { width: 46px; height: 46px; }
.run.paused .ctl.main { outline: 4px solid var(--fg); }
.run.paused .time { opacity: .55; }
.paused-tag { display: none; font-weight: 800; font-size: clamp(16px, 4.5vw, 24px); padding: 4px 14px; border-radius: 999px; background: var(--fg); color: var(--c); margin-bottom: 6px; }
.run.paused .paused-tag { display: inline-block; }
.done-actions .btn { color: #fff; }
.phase-prep .done-actions .btn, .phase-prep .stats { color: var(--prep-fg); }

@media (orientation: landscape) and (max-height: 600px) {
  .run { grid-template-columns: 1fr minmax(260px, 36%); grid-template-rows: auto 1fr; }
  .run-top { grid-column: 1 / -1; padding: 0 8px; }
  .run-top .icon-btn { width: 44px; height: 44px; }
  .run-main { grid-row: 2; }
  .run-side { grid-row: 2; display: flex; flex-direction: column; justify-content: center; }
  .time { font-size: min(22vw, 58vh); }
  .time[data-len="5"] { font-size: min(18vw, 52vh); }
  .time[data-len="7"] { font-size: min(12vw, 40vh); }
  .phase-label { font-size: clamp(20px, 7vh, 40px); }
  .setinfo { font-size: clamp(18px, 7vh, 34px); }
  .stats { grid-template-columns: 1fr; }
  .ctl { height: 64px; } .ctl.main { height: 76px; }
}
