:root {
  --bg: #17191c;
  --bg-lift: #22262a;
  --surface: #202428;
  --surface-2: #2a2f34;
  --line: #31363b;
  --text: #e9eceb;
  --muted: #8b938f;
  --green: #3ddc84;
  --green-deep: #1faa5c;
  --green-ink: #06240f;
  --accent: var(--green);
  --dial: min(80vw, 44vh, 340px);
  --safe-top: var(--tg-safe-area-inset-top, env(safe-area-inset-top, 0px));
  --safe-bottom: var(--tg-safe-area-inset-bottom, env(safe-area-inset-bottom, 0px));
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

button { font: inherit; color: inherit; border: 0; background: none; padding: 0; cursor: pointer; touch-action: manipulation; }

#app {
  position: relative;
  height: var(--tg-viewport-stable-height, 100dvh);
  max-width: 520px;
  margin: 0 auto;
  overflow: hidden;
  background: radial-gradient(120% 70% at 50% 32%, var(--bg-lift) 0%, var(--bg) 62%);
}

/* ---------- Screens ---------- */

.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  padding: calc(var(--safe-top) + 8px) 16px calc(var(--safe-bottom) + 16px);
  transition: transform .38s cubic-bezier(.22, .8, .25, 1), opacity .3s ease;
}
.screen-presets { transform: translateX(100%); visibility: hidden; transition: transform .38s cubic-bezier(.22, .8, .25, 1), visibility 0s .38s; background: var(--bg); }
.presets-open .screen-main { transform: translateX(-22%); opacity: 0; pointer-events: none; }
.presets-open .screen-presets { transform: none; visibility: visible; transition-delay: 0s; }

.topbar { display: flex; align-items: center; gap: 8px; height: 48px; flex: none; }
.title {
  flex: 1; margin: 0; text-align: center;
  font-size: 17px; font-weight: 600; letter-spacing: .02em;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.icon-btn {
  width: 44px; height: 44px; flex: none;
  display: grid; place-items: center;
  border-radius: 14px; color: var(--muted);
  transition: background .15s, color .15s, opacity .25s, transform .1s;
}
.icon-btn:active { background: var(--surface-2); color: var(--text); transform: scale(.94); }
.icon-btn.spacer { pointer-events: none; }
.ico { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.ico-sound-off { display: none; }
.muted .ico-sound-on { display: none; }
.muted .ico-sound-off { display: block; }
.tg-back #backBtn { visibility: hidden; }

/* ---------- Dial ---------- */

.stage { flex: 1; min-height: 0; display: grid; place-items: center; }

.dial {
  position: relative;
  width: var(--dial); height: var(--dial);
  display: grid; place-items: center;
  transition: transform .5s cubic-bezier(.22, .8, .25, 1);
}
.dial > * { grid-area: 1 / 1; }
[data-state="running"] .dial, [data-state="done"] .dial { transform: scale(1.06); }

.aura {
  width: 100%; height: 100%; border-radius: 50%;
  background: radial-gradient(circle, color-mix(in srgb, var(--accent) 26%, transparent) 0%, transparent 66%);
  filter: blur(18px);
  animation: breathe 4s ease-in-out infinite;
}
@keyframes breathe { 0%, 100% { opacity: .45; transform: scale(.94); } 50% { opacity: .9; transform: scale(1.06); } }

.rings { width: 100%; height: 100%; transform: rotate(-90deg); overflow: visible; }
.rings circle { fill: none; }
.ticks { stroke: var(--line); stroke-width: 3; stroke-dasharray: .12 .88; }
.track { stroke: var(--surface-2); stroke-width: 6; }
.orbit {
  stroke: var(--accent); stroke-width: 6; stroke-linecap: round;
  stroke-dasharray: .18 .82; opacity: .55;
  transform-origin: 100px 100px;
  animation: spin 5s linear infinite;
  transition: opacity .3s;
}
@keyframes spin { to { transform: rotate(360deg); } }
.progress {
  stroke: var(--accent); stroke-width: 6; stroke-linecap: round;
  stroke-dasharray: 1; stroke-dashoffset: 0;
  filter: drop-shadow(0 0 5px color-mix(in srgb, var(--accent) 80%, transparent));
  opacity: 0; transition: opacity .3s, stroke .4s;
}

.head { width: 100%; height: 100%; position: relative; opacity: 0; transition: opacity .3s; pointer-events: none; }
.head span {
  position: absolute; top: 6%; left: 50%;
  width: 14px; height: 14px; margin: -7px 0 0 -7px; border-radius: 50%;
  background: #fff;
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 55%, transparent), 0 0 18px 4px var(--accent);
}

.flash { width: 88%; height: 88%; border-radius: 50%; border: 2px solid var(--accent); opacity: 0; pointer-events: none; }
.flash.on { animation: flash .8s ease-out; }
@keyframes flash { 0% { opacity: .8; transform: scale(1); } 100% { opacity: 0; transform: scale(1.28); } }

/* GO button + idle pulses */

.go, .pulse { width: 46%; height: 46%; border-radius: 50%; }
.pulse {
  border: 2px solid var(--green);
  opacity: 0; pointer-events: none;
  animation: pulse 3s cubic-bezier(.2, .6, .35, 1) infinite;
}
.pulse:nth-of-type(2) { animation-delay: 1s; }
.pulse:nth-of-type(3) { animation-delay: 2s; }
@keyframes pulse { 0% { opacity: .55; transform: scale(1); } 100% { opacity: 0; transform: scale(1.85); } }

.go {
  position: relative; z-index: 2;
  background: radial-gradient(circle at 34% 28%, #7df0ae 0%, var(--green) 42%, var(--green-deep) 100%);
  color: var(--green-ink);
  font-size: calc(var(--dial) * .15); font-weight: 800; letter-spacing: .06em;
  box-shadow: 0 10px 34px -6px rgba(61, 220, 132, .55), inset 0 2px 0 rgba(255, 255, 255, .35), inset 0 -6px 14px rgba(0, 0, 0, .18);
  transition: transform .35s cubic-bezier(.22, .8, .25, 1), opacity .25s;
  animation: bob 3s ease-in-out infinite;
}
@keyframes bob { 0%, 100% { box-shadow: 0 10px 34px -6px rgba(61, 220, 132, .45), inset 0 2px 0 rgba(255, 255, 255, .35), inset 0 -6px 14px rgba(0, 0, 0, .18); } 50% { box-shadow: 0 10px 46px 0 rgba(61, 220, 132, .7), inset 0 2px 0 rgba(255, 255, 255, .35), inset 0 -6px 14px rgba(0, 0, 0, .18); } }
.go:active { transform: scale(.94); }

/* Readout */

.readout {
  z-index: 1; text-align: center;
  opacity: 0; transform: scale(.9); pointer-events: none;
  transition: opacity .3s, transform .4s cubic-bezier(.22, .8, .25, 1);
}
.readout.swap { animation: swap .45s cubic-bezier(.22, .8, .25, 1); }
@keyframes swap { 0% { opacity: 0; transform: scale(.82); } 100% { opacity: 1; transform: scale(1); } }
.phase { font-size: calc(var(--dial) * .052); font-weight: 700; letter-spacing: .22em; color: var(--accent); transition: color .4s; }
.time {
  font-size: calc(var(--dial) * .36); line-height: 1.05; font-weight: 200;
  font-variant-numeric: tabular-nums; letter-spacing: -.02em;
  text-shadow: 0 0 28px color-mix(in srgb, var(--accent) 45%, transparent);
}
.time.long { font-size: calc(var(--dial) * .25); }
.time.tick { animation: tick .32s ease-out; }
.time.tick.urgent { animation: tick-urgent .42s ease-out; }
@keyframes tick { 0% { transform: scale(1.05); } 100% { transform: scale(1); } }
@keyframes tick-urgent { 0% { transform: scale(1.18); color: var(--accent); } 100% { transform: scale(1); } }
.setinfo { font-size: calc(var(--dial) * .05); color: var(--muted); letter-spacing: .04em; min-height: 1.3em; }

/* State switches */

[data-phase="rest"] { --accent: #74b592; }

[data-state="running"] .go, [data-state="done"] .go { opacity: 0; transform: scale(.5); pointer-events: none; animation: none; }
[data-state="running"] .pulse, [data-state="done"] .pulse { animation: none; opacity: 0; }
[data-state="running"] .orbit, [data-state="done"] .orbit { opacity: 0; }
[data-state="running"] .progress, [data-state="done"] .progress { opacity: 1; }
[data-state="running"] .head { opacity: 1; }
[data-state="running"] .readout, [data-state="done"] .readout { opacity: 1; transform: none; }
[data-state="done"] .dial { cursor: pointer; }
[data-state="running"] #presetsBtn { opacity: 0; pointer-events: none; }

/* ---------- Bottom panel ---------- */

.bottom { flex: none; display: grid; align-items: end; }
.bottom > * { grid-area: 1 / 1; }

.controls { transition: opacity .3s, transform .4s cubic-bezier(.22, .8, .25, 1); }
[data-state="running"] .controls { opacity: 0; transform: translateY(16px); pointer-events: none; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: 20px; }
.row { display: flex; align-items: center; justify-content: space-between; padding: 8px 8px 8px 18px; }
.row + .row { border-top: 1px solid var(--line); }
.row-label { font-size: 15px; color: var(--muted); letter-spacing: .02em; }
.stepper { display: flex; align-items: center; gap: 4px; }
.step {
  width: 44px; height: 44px; border-radius: 14px;
  background: var(--surface-2); color: var(--green);
  font-size: 24px; font-weight: 400; line-height: 1;
  transition: transform .1s, background .15s;
}
.step:active { transform: scale(.9); background: #343a40; }
.value { min-width: 72px; text-align: center; font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; }
.total { margin-top: 10px; text-align: center; font-size: 13px; color: var(--muted); letter-spacing: .04em; }
.total span { color: var(--text); font-variant-numeric: tabular-nums; }

.stop {
  justify-self: center; align-self: center;
  padding: 14px 44px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface);
  font-size: 16px; font-weight: 600; letter-spacing: .08em; text-transform: uppercase; color: var(--text);
  opacity: 0; transform: translateY(16px); pointer-events: none;
  transition: opacity .3s, transform .4s cubic-bezier(.22, .8, .25, 1), background .15s;
}
.stop:active { background: var(--surface-2); }
[data-state="running"] .stop { opacity: 1; transform: none; pointer-events: auto; }

/* ---------- Presets ---------- */

.presets-body { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; padding-top: 8px; }
.save-form { padding: 14px; }
.save-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 10px; padding: 0 4px; }
.save-summary { font-size: 14px; font-variant-numeric: tabular-nums; }
.save-line { display: flex; gap: 8px; }
.save-line input {
  flex: 1; min-width: 0; height: 46px; padding: 0 14px;
  border-radius: 14px; border: 1px solid var(--line); background: var(--bg);
  color: var(--text); font: inherit; font-size: 16px; outline: none;
  -webkit-user-select: text; user-select: text;
}
.save-line input::placeholder { color: var(--muted); }
.save-line input:focus { border-color: var(--green); }
.save-btn { height: 46px; padding: 0 20px; border-radius: 14px; background: var(--green); color: var(--green-ink); font-weight: 700; transition: transform .1s; }
.save-btn:active { transform: scale(.95); }

.preset-list { list-style: none; margin: 16px 0 0; padding: 0; display: grid; gap: 10px; }
.preset { display: flex; align-items: stretch; background: var(--surface); border: 1px solid var(--line); border-radius: 18px; overflow: hidden; animation: swap .3s ease-out; }
.preset.active { border-color: color-mix(in srgb, var(--green) 60%, var(--line)); }
.preset-load { flex: 1; min-width: 0; text-align: left; padding: 14px 4px 14px 18px; }
.preset-load:active { background: var(--surface-2); }
.preset-name { display: block; font-size: 17px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.preset-meta { display: block; margin-top: 3px; font-size: 14px; color: var(--muted); font-variant-numeric: tabular-nums; }
.preset.active .preset-name { color: var(--green); }
.preset-del { width: 52px; flex: none; display: grid; place-items: center; color: var(--muted); }
.preset-del:active { background: var(--surface-2); color: #ff6b6b; }
.empty { margin: 36px 24px; text-align: center; color: var(--muted); font-size: 15px; line-height: 1.5; }

@media (max-height: 620px) {
  :root { --dial: min(72vw, 40vh, 300px); }
  .row { padding-top: 5px; padding-bottom: 5px; }
  .step { width: 40px; height: 40px; }
}

@media (prefers-reduced-motion: reduce) {
  .pulse, .aura, .orbit, .go { animation: none; }
}
