:root {
  --accento: #4f46e5;
  --accento-tenue: #eef0ff;
  --testo: #111318;
  --testo-2: #6b7280;
  --spento: #c9ccd3;
  --bordo: #eceef2;
  --sfondo: #ffffff;
  --superficie: #f5f6f8;
  --ombra: 0 1px 2px rgba(17, 19, 24, .04), 0 4px 16px rgba(17, 19, 24, .06);
  --raggio: 20px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  color-scheme: light;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: var(--sfondo);
  color: var(--testo);
  font-family: Inter, -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

body {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overflow: hidden;
}

button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; touch-action: manipulation; }
button:focus-visible { outline: 3px solid color-mix(in srgb, var(--accento) 45%, transparent); outline-offset: 2px; }

.app {
  height: 100%;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: calc(var(--safe-t) + 8px) calc(var(--safe-r) + 16px) calc(var(--safe-b) + 14px) calc(var(--safe-l) + 16px);
  max-width: 520px;
  margin: 0 auto;
}

/* Header */
.top { display: flex; align-items: center; justify-content: space-between; min-height: 44px; }
.top h1 { font-size: 17px; font-weight: 650; letter-spacing: -.01em; }
.icon-btn {
  width: 44px; height: 44px; margin-right: -10px;
  display: grid; place-items: center; border-radius: 50%; color: var(--testo-2);
  transition: background .15s;
}
.icon-btn:active { background: var(--superficie); }

/* Striscia alfabeto */
.alfabeto {
  display: grid;
  grid-template-columns: repeat(var(--cols, 7), 1fr);
  gap: 4px;
  margin-top: 6px;
}
.alfabeto span {
  display: grid; place-items: center;
  height: 28px; border-radius: 8px;
  font-size: 14px; font-weight: 600;
  color: var(--testo);
  transition: color .2s, background .2s;
}
.alfabeto span.uscita { color: var(--spento); }
.alfabeto span.corrente { background: var(--accento); color: #fff; }

/* Centro: lettera + anello timer */
.centro { flex: 1; min-height: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.anello-wrap {
  position: relative;
  width: min(78vw, 37dvh, 360px);
  aspect-ratio: 1;
  display: grid; place-items: center;
}
.anello { position: absolute; inset: 0; width: 100%; height: 100%; transform: rotate(-90deg); }
.anello circle { fill: none; stroke-width: 7; }
.anello-bg { stroke: var(--superficie); }
.anello-fg {
  stroke: var(--accento);
  stroke-linecap: round;
  stroke-dasharray: 578.05;
  stroke-dashoffset: 0;
}
.lettera {
  font-size: calc(min(78vw, 37dvh, 360px) * .52);
  font-weight: 700;
  line-height: 1;
  letter-spacing: -.04em;
  margin-top: -6%;
}
.lettera.vuota { color: var(--spento); }
.lettera.anim { animation: entra .42s cubic-bezier(.2, .9, .3, 1.25); }
@keyframes entra {
  0% { transform: scale(.55) translateY(8%); opacity: 0; }
  100% { transform: scale(1) translateY(0); opacity: 1; }
}
.tempo {
  position: absolute; bottom: 17%;
  font-size: 18px; font-weight: 600; color: var(--testo-2);
  font-variant-numeric: tabular-nums;
}
.anello-wrap.scaduto .anello-fg { stroke: #e5484d; }
.anello-wrap.scaduto .tempo { color: #e5484d; }
.anello-wrap.lampeggia .anello, .anello-wrap.lampeggia .tempo { animation: lampo .5s steps(1) infinite; }
@keyframes lampo { 50% { opacity: .15; } }

.contatore { margin-top: 10px; font-size: 14px; color: var(--testo-2); min-height: 18px; }

/* Controlli */
.controlli { display: flex; flex-direction: column; gap: 10px; }
.riga-timer { display: flex; gap: 10px; align-items: center; }
.chips {
  flex: 1; display: grid; grid-template-columns: repeat(3, 1fr);
  background: var(--superficie); border-radius: 16px; padding: 4px; gap: 4px;
}
.chip {
  height: 44px; border-radius: 12px; font-size: 15px; font-weight: 600; color: var(--testo-2);
  transition: background .15s, color .15s, box-shadow .15s;
}
.chip.attiva { background: #fff; color: var(--testo); box-shadow: var(--ombra); }

.btn {
  height: 56px; border-radius: 18px; font-size: 17px; font-weight: 600;
  display: grid; place-items: center;
  transition: transform .08s, background .15s, opacity .15s;
}
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .35; pointer-events: none; }
.btn-primario { height: 64px; background: var(--accento); color: #fff; font-size: 19px; box-shadow: 0 6px 20px color-mix(in srgb, var(--accento) 30%, transparent); }
.btn-secondario { background: var(--superficie); color: var(--testo); }
.btn-pausa.in-corso { background: var(--accento-tenue); color: var(--accento); }
.btn-tondo { width: 52px; height: 52px; border-radius: 50%; background: var(--superficie); color: var(--testo-2); flex: none; }
.btn-testo { height: 44px; padding: 0 8px; color: var(--accento); font-size: 16px; }
.riga-due { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* Overlay */
.overlay {
  position: fixed; inset: 0; z-index: 10;
  background: rgba(17, 19, 24, .28);
  display: grid; place-items: center;
  padding: calc(var(--safe-t) + 16px) 16px calc(var(--safe-b) + 16px);
  animation: appare .2s ease-out;
}
.overlay[hidden] { display: none; }
@keyframes appare { from { opacity: 0; } }

.fine-card {
  width: 100%; max-width: 360px; background: #fff; border-radius: 28px; box-shadow: var(--ombra);
  padding: 32px 22px 18px; text-align: center; display: flex; flex-direction: column; gap: 10px;
  animation: sale .3s cubic-bezier(.2, .9, .3, 1.1);
}
.fine-emoji { font-size: 48px; }
.fine-card h2 { font-size: 26px; letter-spacing: -.02em; }
.fine-card p { color: var(--testo-2); margin-bottom: 12px; }

.overlay-sheet { place-items: end center; padding: 0; }
.sheet {
  width: 100%; max-width: 520px; background: #fff;
  border-radius: 28px 28px 0 0;
  padding: 10px calc(var(--safe-r) + 20px) calc(var(--safe-b) + 20px) calc(var(--safe-l) + 20px);
  display: flex; flex-direction: column; gap: 18px;
  max-height: 92dvh; overflow-y: auto;
  animation: sale .28s cubic-bezier(.2, .9, .3, 1);
}
@keyframes sale { from { transform: translateY(40px); opacity: 0; } }
.sheet-maniglia { width: 38px; height: 5px; border-radius: 3px; background: var(--bordo); margin: 0 auto; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: -4px; }
.sheet-head h2 { font-size: 22px; letter-spacing: -.02em; }

.campo { display: flex; flex-direction: column; gap: 8px; }
.campo-switch { flex-direction: row; align-items: center; justify-content: space-between; min-height: 44px; }
.etichetta { font-size: 15px; font-weight: 600; }
.nota { font-size: 13px; color: var(--testo-2); line-height: 1.4; }
.nota:empty { display: none; }
.nota[hidden] { display: none; }

.segmented { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--superficie); border-radius: 14px; padding: 4px; gap: 4px; }
.segmented button { height: 42px; border-radius: 10px; font-size: 15px; font-weight: 600; color: var(--testo-2); }
.segmented button.attiva { background: #fff; color: var(--testo); box-shadow: var(--ombra); }

.switch { position: relative; width: 51px; height: 31px; flex: none; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; z-index: 1; }
.switch span { position: absolute; inset: 0; border-radius: 31px; background: #e3e5e9; transition: background .2s; }
.switch span::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 27px; height: 27px; border-radius: 50%;
  background: #fff; box-shadow: 0 2px 6px rgba(0, 0, 0, .18); transition: transform .2s;
}
.switch input:checked + span { background: var(--accento); }
.switch input:checked + span::after { transform: translateX(20px); }
.switch input:disabled + span { opacity: .4; }

.btn-pieno { width: 100%; margin-top: 4px; }

/* Schermi bassi (iPhone SE, mini) */
@media (max-height: 700px) {
  .btn { height: 50px; }
  .btn-primario { height: 58px; }
  .chip { height: 40px; }
  .btn-tondo { width: 48px; height: 48px; }
  .controlli { gap: 8px; }
  .alfabeto span { height: 24px; font-size: 13px; }
}

@media (prefers-reduced-motion: reduce) {
  .lettera.anim, .fine-card, .sheet, .overlay { animation: none; }
}
