:root {
  color-scheme: light dark;
  --bg: #f2f2f5;
  --surface: #ffffff;
  --surface-2: #e8e8ed;
  --text: #121214;
  --text-2: #6b6b74;
  --text-3: #a3a3ab;
  --sep: rgba(0, 0, 0, 0.08);
  --accent: #ea6a12;
  --accent-soft: rgba(234, 106, 18, 0.13);
  --go: #16a34a;
  --go-soft: rgba(22, 163, 74, 0.14);
  --danger: #dc2626;
  --danger-soft: rgba(220, 38, 38, 0.12);
  --warn: #c2410c;
  --warn-soft: rgba(234, 106, 18, 0.14);
  --neutral-soft: rgba(120, 120, 128, 0.16);
  --day: #e8a10c;
  --night: #6b7bd6;
  --radius: 18px;
  --tabbar-h: 62px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", "PingFang SC", "Hiragino Sans",
    "Microsoft YaHei", "Noto Sans Devanagari", "Noto Sans Arabic", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0b0d;
    --surface: #18181b;
    --surface-2: #26262b;
    --text: #f4f4f5;
    --text-2: #a1a1aa;
    --text-3: #5f5f68;
    --sep: rgba(255, 255, 255, 0.08);
    --accent: #fb923c;
    --accent-soft: rgba(251, 146, 60, 0.16);
    --go: #4ade80;
    --go-soft: rgba(74, 222, 128, 0.14);
    --danger: #f87171;
    --danger-soft: rgba(248, 113, 113, 0.14);
    --warn: #fb923c;
    --warn-soft: rgba(251, 146, 60, 0.16);
    --neutral-soft: rgba(160, 160, 170, 0.16);
    --day: #fbbf24;
    --night: #a5b4fc;
  }
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { cursor: default; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

svg.i {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: none;
}
.ico { display: inline-flex; }
.ico svg.i { width: 100%; height: 100%; }

/* Digits that tick must not jiggle, and stay left-to-right in RTL locales. */
.digits, .t-main, .wheel {
  font-variant-numeric: tabular-nums;
  direction: ltr;
  unicode-bidi: isolate;
}

/* ---------- layout ---------- */

.app {
  max-width: 560px;
  margin: 0 auto;
  min-height: 100dvh;
  padding: 0 16px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 24px);
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(14px + env(safe-area-inset-top)) 0 12px;
  background: var(--bg);
}
.topbar h1 {
  margin: 0;
  font-size: clamp(26px, 7vw, 34px);
  font-weight: 700;
  letter-spacing: -0.02em;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.actions { display: flex; align-items: center; gap: 2px; flex: none; }

.text-btn {
  border: 0;
  background: none;
  color: var(--accent);
  font-size: 17px;
  padding: 8px 10px;
  border-radius: 10px;
}
.text-btn.strong { font-weight: 600; }
.text-btn:disabled { color: var(--text-3); }

.icon-btn {
  position: relative;
  display: inline-grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 50%;
  background: none;
  color: var(--accent);
}
@media (hover: hover) {
  .icon-btn:hover, .text-btn:not(:disabled):hover { background: var(--neutral-soft); }
}
.lang-btn { cursor: pointer; }
.lang-btn select {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
  font-size: 16px; /* stops iOS from zooming */
}
.lang-btn:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }

.banner {
  display: block;
  width: 100%;
  margin: 0 0 12px;
  padding: 10px 14px;
  border: 0;
  border-radius: 12px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 14px;
  text-align: center;
}

.empty, .note, .kb-hint {
  text-align: center;
  color: var(--text-3);
  font-size: 14px;
}
.empty { padding: 32px 0 8px; font-size: 16px; }
.note { margin: 20px 0 0; }
.kb-hint { display: none; margin: 28px 0 0; font-size: 13px; }
@media (hover: hover) and (pointer: fine) {
  .kb-hint { display: block; }
}

/* ---------- cards / lists ---------- */

.list {
  list-style: none;
  margin: 0;
  padding: 0;
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
}
.list:empty { display: none; }
.list > li + li { border-top: 1px solid var(--sep); }

.del-btn {
  flex: none;
  display: grid;
  place-items: center;
  width: 26px;
  height: 26px;
  padding: 4px;
  border: 0;
  border-radius: 50%;
  background: var(--danger);
  color: #fff;
}
.list:not(.editing) .del-btn { display: none; }

.time { display: inline-flex; align-items: baseline; gap: 0.2em; white-space: nowrap; }
.t-period { font-size: 0.42em; font-weight: 500; letter-spacing: 0; }

/* ---------- world clock ---------- */

.local {
  display: grid;
  justify-items: center;
  gap: 4px;
  padding: 4px 0 22px;
  text-align: center;
}
.analog { width: min(52vw, 190px); height: auto; margin-bottom: 10px; }
.analog .face { fill: var(--surface); stroke: var(--sep); stroke-width: 1; }
.analog .ticks line { stroke-linecap: round; }
.analog .ticks .minor { stroke: var(--text-3); stroke-width: 1.2; }
.analog .ticks .major { stroke: var(--text); stroke-width: 3; }
.analog .hand { stroke-linecap: round; }
.analog .hour { stroke: var(--text); stroke-width: 6; }
.analog .minute { stroke: var(--text); stroke-width: 4; }
.analog .second line { stroke: var(--accent); stroke-width: 1.6; }
.analog .second circle { fill: var(--accent); }
.analog .pin { fill: var(--surface); }

.local-time { font-size: clamp(40px, 12vw, 52px); font-weight: 250; letter-spacing: -0.01em; line-height: 1.1; }
.local-date { font-size: 16px; color: var(--text); }
.local-zone { font-size: 13px; color: var(--text-2); }

.city-row {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 74px;
  padding: 12px 16px;
}
.city-info { flex: 1; min-width: 0; }
.city-name {
  font-size: 19px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.city-sub {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 2px;
  font-size: 13px;
  color: var(--text-2);
}
.city-ico { width: 15px; height: 15px; }
.city-ico.is-day { color: var(--day); }
.city-ico.is-night { color: var(--night); }
.city-time { font-size: clamp(30px, 9vw, 38px); font-weight: 250; flex: none; }

/* ---------- alarm ---------- */

.alarm-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
}
.alarm-main {
  flex: 1;
  min-width: 0;
  padding: 2px 0;
  border: 0;
  background: none;
  text-align: start;
  border-radius: 8px;
}
.alarm-time { font-size: clamp(38px, 11vw, 46px); font-weight: 250; line-height: 1.15; }
.alarm-sub {
  font-size: 14px;
  color: var(--text-2);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.alarm-row.off .alarm-time, .alarm-row.off .alarm-sub { color: var(--text-3); }

.switch { position: relative; flex: none; width: 52px; height: 32px; cursor: pointer; }
.switch input { position: absolute; inset: 0; margin: 0; opacity: 0; cursor: pointer; }
.switch .track {
  position: absolute;
  inset: 0;
  border-radius: 999px;
  background: var(--neutral-soft);
  transition: background 0.2s;
  pointer-events: none;
}
.switch .track::after {
  content: "";
  position: absolute;
  top: 3px;
  inset-inline-start: 3px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
  transition: transform 0.2s;
}
.switch input:checked + .track { background: var(--go); }
.switch input:checked + .track::after { transform: translateX(20px); }
[dir="rtl"] .switch input:checked + .track::after { transform: translateX(-20px); }
.switch input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- stopwatch / timer shared ---------- */

.big-digits {
  text-align: center;
  font-size: clamp(56px, 19vw, 92px);
  font-weight: 200;
  letter-spacing: -0.02em;
  padding: 40px 0 36px;
  line-height: 1;
}

.controls {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  margin: 0 0 24px;
}
.pill {
  flex: 1;
  max-width: 190px;
  min-height: 56px;
  padding: 8px 18px;
  border: 0;
  border-radius: 999px;
  background: var(--neutral-soft);
  color: var(--text);
  font-size: 17px;
  font-weight: 600;
  line-height: 1.2;
  transition: transform 0.08s, background 0.2s;
}
.pill:not(:disabled):active { transform: scale(0.97); }
.pill:disabled { opacity: 0.4; }
.pill.go { background: var(--go-soft); color: var(--go); }
.pill.danger { background: var(--danger-soft); color: var(--danger); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.primary { background: var(--accent); color: #fff; }

.laps { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--sep); }
.laps li {
  display: flex;
  justify-content: space-between;
  padding: 13px 4px;
  border-bottom: 1px solid var(--sep);
  font-size: 17px;
}
.laps li.best { color: var(--go); }
.laps li.worst { color: var(--danger); }

/* ---------- wheels ---------- */

.wheels {
  position: relative;
  isolation: isolate;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 6px;
  padding: 12px 0 20px;
  direction: ltr;
}
.wheels::before {
  content: "";
  position: absolute;
  inset-inline: 0;
  top: calc(50% - 4px);
  height: 44px;
  transform: translateY(-50%);
  border-radius: 12px;
  background: var(--surface);
  z-index: -1;
}
.wheel-wrap { display: flex; align-items: center; }
.wheel {
  height: 220px;
  width: 64px;
  overflow-y: scroll;
  scroll-snap-type: y mandatory;
  overscroll-behavior: contain;
  scrollbar-width: none;
  border-radius: 12px;
  -webkit-mask-image: linear-gradient(to bottom, transparent, #000 32%, #000 68%, transparent);
  mask-image: linear-gradient(to bottom, transparent, #000 32%, #000 68%, transparent);
}
.wheel::-webkit-scrollbar { display: none; }
.wheel:focus-visible { outline-offset: -2px; -webkit-mask-image: none; mask-image: none; }
.wheel-spacer { height: 88px; }
.wheel-item {
  height: 44px;
  line-height: 44px;
  text-align: center;
  scroll-snap-align: center;
  font-size: 26px;
  color: var(--text-3);
  cursor: pointer;
  user-select: none;
}
.wheel-item.sel { color: var(--text); }
.wheel-unit { min-width: 46px; padding-inline-start: 2px; font-size: 15px; color: var(--text-2); }
.wheel-sep { font-size: 26px; padding-bottom: 4px; }

/* ---------- timer ---------- */

.presets {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  margin: 0 0 28px;
}
.chip {
  padding: 8px 16px;
  border: 1px solid var(--sep);
  border-radius: 999px;
  background: var(--surface);
  font-size: 15px;
}
@media (hover: hover) { .chip:hover { border-color: var(--accent); color: var(--accent); } }

.timer-run {
  position: relative;
  width: min(76vw, 300px);
  aspect-ratio: 1;
  margin: 12px auto 32px;
}
.ring { width: 100%; height: 100%; display: block; }
.ring-track { fill: none; stroke: var(--neutral-soft); stroke-width: 8; }
.ring-bar {
  fill: none;
  stroke: var(--accent);
  stroke-width: 8;
  stroke-linecap: round;
  transform: rotate(-90deg);
  transform-box: fill-box;
  transform-origin: center;
}
.ring-center {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 6px;
}
.timer-remaining { font-size: clamp(44px, 14vw, 64px); font-weight: 200; line-height: 1; }
.timer-end {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--text-2);
  font-size: 15px;
  transition: opacity 0.2s;
}
.timer-end svg.i { width: 16px; height: 16px; }
.timer-end.dim { opacity: 0.4; }

/* ---------- dialogs ---------- */

dialog {
  border: 0;
  padding: 0;
  color: var(--text);
  background: var(--surface);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.4); }

.sheet {
  width: min(100% - 32px, 480px);
  max-width: none;
  max-height: min(86dvh, 720px);
  border-radius: 20px;
  overflow: hidden;
}
.sheet[open] { display: flex; flex-direction: column; }
/* Fixed height so the sheet doesn't jump while search results change. */
#city-dialog { height: min(86dvh, 720px); }
@media (max-width: 600px) {
  .sheet {
    width: 100%;
    margin: auto 0 0;
    border-radius: 20px 20px 0 0;
    padding-bottom: env(safe-area-inset-bottom);
  }
}
.sheet-head {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  padding: 10px 8px;
  border-bottom: 1px solid var(--sep);
}
.sheet-head > :first-child { justify-self: start; }
.sheet-head > :last-child { justify-self: end; }
.sheet-head h2 { margin: 0; font-size: 17px; font-weight: 600; text-align: center; }
.sheet-body { overflow-y: auto; padding-bottom: 8px; }
.sheet .wheels::before { background: var(--surface-2); }

.sheet-search { padding: 12px 16px 8px; }
input[type="search"], input[type="text"] {
  width: 100%;
  padding: 11px 14px;
  border: 0;
  border-radius: 12px;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: 16px;
}
input::placeholder { color: var(--text-3); }

.search-results { flex: 1; overflow-y: auto; list-style: none; margin: 0; padding: 0 0 12px; }
.result {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  padding: 12px 16px;
  border: 0;
  border-bottom: 1px solid var(--sep);
  background: none;
  text-align: start;
}
.result:disabled { color: var(--text-2); }
@media (hover: hover) { .result:not(:disabled):hover { background: var(--neutral-soft); } }
.r-text { min-width: 0; }
.r-name { font-size: 16px; }
.r-sub { font-size: 13px; color: var(--text-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.r-time { flex: none; color: var(--text-2); font-size: 15px; }
.r-time .ico { width: 20px; height: 20px; color: var(--accent); }
.no-results { padding: 24px; text-align: center; color: var(--text-3); }

.field { display: flex; flex-direction: column; gap: 10px; padding: 12px 16px; }
.field-label { font-size: 14px; color: var(--text-2); }
.days { display: flex; gap: 6px; justify-content: space-between; }
.day {
  flex: 1;
  max-width: 44px;
  aspect-ratio: 1;
  border: 0;
  border-radius: 50%;
  background: var(--surface-2);
  font-size: 14px;
}
.day[aria-pressed="true"] { background: var(--accent); color: #fff; }

.danger-btn {
  display: block;
  width: calc(100% - 32px);
  margin: 8px 16px 12px;
  padding: 12px;
  border: 0;
  border-radius: 12px;
  background: var(--danger-soft);
  color: var(--danger);
  font-size: 16px;
}

.ring-dialog {
  width: min(100% - 32px, 380px);
  padding: 32px 24px 24px;
  border-radius: 28px;
  text-align: center;
}
.ring-dialog[open] { display: grid; justify-items: center; gap: 10px; }
.ring-dialog::backdrop { background: rgba(0, 0, 0, 0.6); backdrop-filter: blur(6px); }
.ring-icon {
  display: grid;
  place-items: center;
  width: 72px;
  height: 72px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  animation: pulse 1.2s ease-in-out infinite;
}
.ring-icon .ico { width: 34px; height: 34px; }
.ring-heading { font-size: 40px; font-weight: 300; line-height: 1.15; margin-top: 6px; }
.ring-label { color: var(--text-2); font-size: 16px; }
.ring-actions { display: flex; gap: 12px; width: 100%; margin-top: 14px; }
.ring-actions .pill { max-width: none; }

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.08) rotate(-6deg); }
}

/* ---------- tab bar ---------- */

.tabbar {
  position: fixed;
  inset-inline: 0;
  bottom: 0;
  z-index: 10;
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-top: 1px solid var(--sep);
}
.tabbar-inner { display: flex; max-width: 560px; margin: 0 auto; }
.tabbar a {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: var(--tabbar-h);
  padding: 6px 2px;
  color: var(--text-2);
  font-size: 11px;
  text-decoration: none;
  text-align: center;
  line-height: 1.15;
}
.tabbar a[aria-current="page"] { color: var(--accent); }
.tabbar svg.i { width: 24px; height: 24px; }

/* ---------- toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px);
  z-index: 20;
  max-width: calc(100% - 32px);
  padding: 10px 18px;
  border-radius: 999px;
  background: var(--text);
  color: var(--bg);
  font-size: 14px;
  opacity: 0;
  transform: translate(-50%, 12px);
  transition: opacity 0.2s, transform 0.2s;
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

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