/* ===================================================================
   KRONO — Design system "Camera Oscura" / "Banco luminoso"
   Stessi token e componenti di theme-template.html
   =================================================================== */
:root {
  --void: #15100C;
  --void-deep: #0C0906;
  --surface: #201811;
  --surface-raised: #2C2116;
  --paper: #F3E8D6;
  --paper-dim: #A9998A;
  --line: #3C2E22;
  --line-soft: #2A1F17;
  --safelight: #FF5722;
  --safelight-deep: #C43A0E;
  --safelight-glow: rgba(255,87,34,.22);
  --good: #5FBF7A;
  --bad: #FF5C72;
  --radius: 3px;
  --font-display: 'Fraunces', Georgia, serif;
  --font-body: 'Manrope', -apple-system, system-ui, sans-serif;
  --font-mono: 'Space Mono', Menlo, Consolas, monospace;

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 16px;
  --space-4: 24px;
  --space-5: 32px;
}
:root[data-theme="light"] {
  --void: #F7F4EC;
  --void-deep: #1C140D;
  --surface: #FFFFFF;
  --surface-raised: #F1ECE1;
  --paper: #22190F;
  --paper-dim: #7C6C5C;
  --line: #E3D9C4;
  --line-soft: #EEE7D8;
  --safelight-glow: rgba(255,87,34,.14);
  --good: #2E8B4F;
  --bad: #D93A50;
}

* { box-sizing: border-box; }
html { color-scheme: dark; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--void);
  color: var(--paper);
  font-family: var(--font-body);
}

body {
  min-height: 100vh;
  padding-bottom: var(--space-5);
  position: relative;
  overflow-x: hidden;
}

body::before {
  content: ''; position: fixed; inset: 0; pointer-events: none; z-index: 0;
  opacity: .05; mix-blend-mode: overlay;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='120' height='120'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='2' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}
body::after {
  content: ''; position: fixed; top: -260px; left: 50%; transform: translateX(-50%);
  width: 900px; height: 900px; pointer-events: none; z-index: 0;
  background: radial-gradient(circle, var(--safelight-glow) 0%, transparent 62%);
  animation: glow-pulse 9s ease-in-out infinite;
}
@keyframes glow-pulse { 0%, 100% { opacity: .55; } 50% { opacity: 1; } }
@media (prefers-reduced-motion: reduce) { body::after { animation: none; } }

h2, h3, h4 {
  font-family: var(--font-display);
  font-weight: 700;
  margin: var(--space-3) 0 var(--space-2);
  color: var(--paper);
}

.muted {
  color: var(--paper-dim);
}

/* ---- Top bar ---- */
.topbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1.4rem;
  letter-spacing: .01em;
  color: var(--safelight);
  margin-right: var(--space-3);
}

.main-nav {
  display: flex;
  gap: var(--space-2);
  flex: 1;
  flex-wrap: wrap;
}

.nav-btn {
  background: var(--surface-raised);
  border: 1px solid var(--line);
  color: var(--paper-dim);
  padding: var(--space-2) var(--space-3);
  border-radius: 2px;
  font-family: var(--font-mono);
  font-size: .8rem;
  font-weight: 700;
  letter-spacing: .06em;
  text-transform: uppercase;
  cursor: pointer;
  transition: all .15s;
}

.nav-btn:hover {
  border-color: var(--safelight);
  color: var(--paper);
}

.nav-btn.is-active {
  background: var(--safelight);
  border-color: var(--safelight-deep);
  color: var(--void-deep);
  box-shadow: 0 0 0 3px var(--safelight-glow);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.icon-btn {
  background: var(--surface-raised);
  border: 1px solid var(--line);
  color: var(--paper);
  padding: var(--space-2) var(--space-3);
  border-radius: 2px;
  cursor: pointer;
  font-family: var(--font-mono);
  font-size: .75rem;
  font-weight: 700;
  letter-spacing: .04em;
  text-transform: uppercase;
  transition: border-color .15s, color .15s;
}

.icon-btn:hover {
  border-color: var(--safelight);
  color: var(--safelight);
}

.theme-toggle {
  flex-shrink: 0; width: 38px; height: 38px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: var(--surface-raised); border: 1px solid var(--line); color: var(--paper);
  cursor: pointer; transition: border-color .15s, color .15s; padding: 0;
}
.theme-toggle:hover { border-color: var(--safelight); color: var(--safelight); }
.theme-toggle svg { width: 17px; height: 17px; }
.theme-toggle .icon-moon { display: none; }
:root[data-theme="light"] .theme-toggle .icon-sun { display: none; }
:root[data-theme="light"] .theme-toggle .icon-moon { display: block; }

.sound-toggle .icon-sound-off { display: none; }
.sound-toggle.is-muted .icon-sound-on { display: none; }
.sound-toggle.is-muted .icon-sound-off { display: block; }

/* ---- Banners / toast ---- */
.banner {
  position: relative;
  z-index: 1;
  margin: var(--space-3);
  padding: var(--space-3);
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--line);
}

.banner-warning {
  border-color: var(--bad);
  color: var(--bad);
}

.banner-recovery {
  border-color: var(--safelight);
}

.banner-actions {
  display: flex;
  gap: var(--space-2);
  margin-top: var(--space-2);
}

.toast {
  position: fixed;
  bottom: var(--space-4);
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface);
  border: 1px solid var(--safelight);
  color: var(--paper);
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius);
  z-index: 50;
  font-family: var(--font-mono);
  font-size: .85rem;
  box-shadow: 0 4px 24px rgba(0,0,0,.35);
}

/* ---- Layout / views ---- */
main {
  position: relative;
  z-index: 1;
  padding: var(--space-3);
  max-width: 900px;
  margin: 0 auto;
}

.view {
  display: none;
}

.view.is-active {
  display: block;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: var(--space-4);
  position: relative;
}
.panel::before, .panel::after,
.card::before, .card::after {
  content: ''; position: absolute; width: 0; height: 0; pointer-events: none;
}
.panel::before, .card::before {
  top: -1px; left: -1px; border-style: solid; border-width: 13px 13px 0 0;
  border-color: var(--line-soft) transparent transparent transparent;
}
.panel::after, .card::after {
  bottom: -1px; right: -1px; border-style: solid; border-width: 0 0 13px 13px;
  border-color: transparent transparent var(--line-soft) transparent;
}

/* ---- Dashboard ---- */
.session-header {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-bottom: var(--space-2);
}

.session-header h2 {
  margin: 0;
  flex: 1;
  min-width: 0;
  overflow-wrap: anywhere;
}

.badge {
  background: var(--safelight);
  color: var(--void-deep);
  font-family: var(--font-mono);
  font-size: .7rem;
  font-weight: 700;
  letter-spacing: .04em;
  text-transform: uppercase;
  padding: 3px 10px;
  border-radius: 999px;
}

.link-btn {
  background: none;
  border: none;
  color: var(--paper-dim);
  text-decoration: underline;
  cursor: pointer;
  font-size: .82rem;
  padding: var(--space-1) var(--space-2);
  font-family: var(--font-body);
}
.link-btn:hover { color: var(--safelight); }
.link-btn-whatsapp:hover { color: #25D366; }

.timer-display {
  font-family: var(--font-mono);
  font-size: clamp(2.5rem, 10vw, 5rem);
  text-align: center;
  font-weight: 700;
  letter-spacing: 0.02em;
  padding: var(--space-4) 0;
  color: var(--paper);
  text-shadow: 0 0 28px var(--safelight-glow);
}

.timer-controls {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--space-2);
  margin-bottom: var(--space-3);
}

.btn {
  min-height: 48px;
  border-radius: 2px;
  border: 1px solid var(--line);
  background: var(--surface-raised);
  color: var(--paper);
  font-family: var(--font-mono);
  font-size: .85rem;
  font-weight: 700;
  letter-spacing: .05em;
  text-transform: uppercase;
  cursor: pointer;
  transition: all .15s;
}

.btn:hover { border-color: var(--safelight); color: var(--paper); }
.btn:active { transform: translateY(1px); }

.btn-primary {
  background: var(--safelight);
  border-color: var(--safelight-deep);
  color: var(--void-deep);
}
.btn-primary:hover { background: #FF6B3D; box-shadow: 0 0 0 4px var(--safelight-glow); }

.btn-secondary {
  background: var(--surface-raised);
}

.btn-danger {
  background: transparent;
  border-color: var(--bad);
  color: var(--bad);
}
.btn-danger:hover { background: var(--bad); color: var(--void-deep); }

.btn-lap {
  display: block;
  width: 100%;
  min-height: 96px;
  font-family: var(--font-mono);
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  background: var(--safelight);
  border: 1px solid var(--safelight-deep);
  border-radius: 2px;
  color: var(--void-deep);
  cursor: pointer;
  margin-bottom: var(--space-3);
  transition: background .15s, box-shadow .15s, transform .1s;
}

.btn-lap:hover { background: #FF6B3D; box-shadow: 0 0 0 4px var(--safelight-glow); }
.btn-lap:active { background: var(--safelight-deep); transform: translateY(1px); }

.session-athletes-manager {
  background: var(--surface-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: var(--space-3);
  margin-bottom: var(--space-3);
}
.session-athletes-manager summary {
  cursor: pointer;
  font-family: var(--font-mono);
  font-size: .8rem;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--paper-dim);
}
.session-athletes-manager[open] summary { margin-bottom: var(--space-2); }

.session-athletes-list {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2) var(--space-3);
}

.selected-athlete-row {
  text-align: center;
  margin-bottom: var(--space-3);
  font-size: 1.05rem;
  color: var(--paper-dim);
}
.selected-athlete-row strong { color: var(--paper); }

.quick-select-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: var(--space-2);
}
.quick-select-toolbar h3 { margin: var(--space-3) 0 var(--space-2); }

.multi-toggle {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-family: var(--font-mono);
  font-size: 0.78rem;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--paper-dim);
  cursor: pointer;
}

.switch { position: relative; width: 40px; height: 22px; flex-shrink: 0; display: inline-block; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider {
  position: absolute; inset: 0; background: var(--surface-raised);
  border: 1px solid var(--line); border-radius: 12px; cursor: pointer; transition: .2s;
}
.slider::before {
  content: ''; position: absolute; width: 16px; height: 16px; border-radius: 50%;
  left: 2px; top: 2px; background: var(--paper-dim); transition: .2s;
}
.switch input:checked + .slider { background: var(--safelight); border-color: var(--safelight-deep); }
.switch input:checked + .slider::before { transform: translateX(18px); background: var(--void-deep); }
.switch input:focus-visible + .slider { outline: 2px solid var(--safelight); outline-offset: 2px; }

.quick-select-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-2) var(--space-3);
  margin-bottom: var(--space-2);
}
.quick-select-actions[hidden] { display: none; }

.hint {
  color: var(--paper-dim);
  font-size: 0.78rem;
  margin: 0;
  flex-basis: 100%;
}

.quick-select-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: var(--space-2);
  margin-bottom: var(--space-3);
}

.quick-select-btn {
  min-height: 56px;
  border: 2px solid var(--line);
  border-radius: 2px;
  background: var(--surface-raised);
  color: var(--paper);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  cursor: pointer;
  font-family: var(--font-body);
  font-size: 1rem;
  transition: border-color .15s;
}
.quick-select-btn:hover { border-color: var(--athlete-color, var(--safelight)); }

.quick-select-btn.is-selected {
  background: var(--athlete-color, var(--safelight));
  border-color: var(--athlete-color, var(--safelight-deep));
  color: var(--void-deep);
  font-weight: 700;
  box-shadow: 0 0 0 3px var(--safelight-glow);
}

.quick-select-num {
  font-family: var(--font-mono);
  opacity: 0.8;
}

.shortcuts-box {
  background: var(--surface-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: var(--space-3);
  margin-bottom: var(--space-3);
}

.shortcuts-box h4 {
  margin-top: 0;
  font-family: var(--font-mono);
  font-size: .8rem;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--paper-dim);
}

.shortcuts-box ul {
  margin: 0;
  padding-left: 1.1em;
  color: var(--paper-dim);
  font-family: var(--font-mono);
  font-size: 0.82rem;
}

/* ---- Table ---- */
.table-wrap {
  overflow-x: auto;
}

.laps-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.laps-table th,
.laps-table td {
  padding: var(--space-2);
  text-align: left;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
  font-family: var(--font-mono);
}

.laps-table tbody tr:hover {
  background: var(--surface-raised);
}

.laps-table th {
  color: var(--paper-dim);
  font-weight: 700;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.athlete-dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-right: var(--space-2);
}

.delta-improved {
  color: var(--good);
  font-weight: 700;
}

.delta-regressed {
  color: var(--bad);
  font-weight: 700;
}

.delta-unchanged {
  color: var(--paper-dim);
}

/* ---- Forms / cards ---- */
.form-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  margin-bottom: var(--space-4);
  position: relative;
}
.form-card::before {
  content: ''; position: absolute; top: -1px; left: -1px; width: 0; height: 0;
  pointer-events: none; border-style: solid; border-width: 13px 13px 0 0;
  border-color: var(--line-soft) transparent transparent transparent;
}
.form-card::after {
  content: ''; position: absolute; bottom: -1px; right: -1px; width: 0; height: 0;
  pointer-events: none; border-style: solid; border-width: 0 0 13px 13px;
  border-color: transparent transparent var(--line-soft) transparent;
}

.form-card h3 {
  margin-top: 0;
}

.form-card label {
  display: block;
  font-family: var(--font-mono);
  font-size: 0.72rem;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--paper-dim);
  margin-top: var(--space-2);
}

.form-card input,
.form-card textarea,
.form-card select {
  width: 100%;
  background: var(--surface-raised);
  border: 1px solid var(--line);
  border-radius: 2px;
  color: var(--paper);
  padding: var(--space-2);
  font-size: 0.95rem;
  font-family: var(--font-body);
}

.form-card input:focus,
.form-card textarea:focus,
.form-card select:focus {
  border-color: var(--safelight);
  outline: 2px solid var(--safelight-glow);
  outline-offset: 1px;
}

.form-card input[type="color"] {
  padding: 2px;
  height: 42px;
  cursor: pointer;
}

.form-card button {
  margin-top: var(--space-3);
}

.list-cards {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: var(--space-3);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.card-active {
  border-color: var(--safelight);
  box-shadow: 0 0 0 1px var(--safelight-glow);
}

.card-title {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1.05rem;
  display: flex;
  align-items: center;
  color: var(--paper);
}

.card-meta {
  color: var(--paper-dim);
  font-family: var(--font-mono);
  font-size: 0.82rem;
}

.card-actions {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex-wrap: wrap;
}

.checkbox-label {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  color: var(--paper-dim);
  font-size: 0.85rem;
}
.checkbox-label input[type="checkbox"] {
  accent-color: var(--safelight);
  width: 16px;
  height: 16px;
}

a:focus-visible, button:focus-visible, input:focus-visible, textarea:focus-visible {
  outline: 2px solid var(--safelight); outline-offset: 2px;
}

/* ---- Responsive ---- */
@media (min-width: 640px) {
  .timer-controls {
    grid-template-columns: repeat(4, minmax(0, 160px));
    justify-content: center;
  }
}

@media (min-width: 768px) {
  main {
    padding: var(--space-4);
  }
}

/* ---- Stats view ---- */
.stats-picker {
  max-width: 320px;
}

.stats-summary {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: var(--space-2);
  margin-bottom: var(--space-4);
}

.stats-tile {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: var(--space-3);
}

.stats-tile-label {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--paper-dim);
}

.stats-tile-value {
  font-family: var(--font-mono);
  font-size: 1.4rem;
  color: var(--paper);
  margin-top: var(--space-1);
}

.stats-tile-value.stats-improved { color: var(--good); }
.stats-tile-value.stats-regressed { color: var(--bad); }

.stats-chart {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: var(--space-3);
}

.stats-chart-svg {
  width: 100%;
  height: auto;
  display: block;
}

.stats-gridline {
  stroke: var(--line);
  stroke-width: 1;
  stroke-dasharray: 3 3;
}

.stats-line-avg {
  fill: none;
  stroke: var(--safelight);
  stroke-width: 2;
}

.stats-line-best {
  fill: none;
  stroke: var(--good);
  stroke-width: 2;
  stroke-dasharray: 4 3;
}

.stats-point-avg { fill: var(--safelight); }
.stats-point-best { fill: var(--good); }

.stats-chart-labels {
  display: flex;
  justify-content: space-between;
  gap: var(--space-1);
  margin-top: var(--space-2);
  font-family: var(--font-mono);
  font-size: 0.72rem;
  color: var(--paper-dim);
  flex-wrap: wrap;
}

.stats-legend {
  display: flex;
  gap: var(--space-3);
  margin-top: var(--space-3);
  font-size: 0.82rem;
  color: var(--paper-dim);
}

.stats-legend-item {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
}

.stats-legend-swatch {
  width: 14px;
  height: 3px;
  display: inline-block;
  border-radius: 2px;
}

.stats-legend-avg { background: var(--safelight); }
.stats-legend-best { background: var(--good); }

.stats-pb-list {
  display: flex;
  flex-direction: column;
  gap: 1px;
  background: var(--line);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}

.stats-pb-row {
  background: var(--surface);
  padding: var(--space-2) var(--space-3);
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas: "type time" "meta meta";
  gap: 2px var(--space-2);
  align-items: baseline;
}

.stats-pb-row-overall {
  background: var(--surface-raised);
}

.stats-pb-type {
  grid-area: type;
  font-family: var(--font-mono);
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--paper-dim);
}

.stats-pb-row-overall .stats-pb-type {
  color: var(--safelight);
}

.stats-pb-time {
  grid-area: time;
  font-family: var(--font-mono);
  font-size: 1.05rem;
  color: var(--paper);
}

.stats-pb-meta {
  grid-area: meta;
  font-size: 0.78rem;
  color: var(--paper-dim);
}

@media (min-width: 640px) {
  .stats-pb-row {
    grid-template-columns: 1fr auto 1fr;
    grid-template-areas: "type time meta";
    align-items: center;
  }
  .stats-pb-meta {
    text-align: right;
  }
}
