/* =============================================================
   Studio — design system 2026
   Canoni: Linear / Vercel / Raycast / Cal.com
   Font: Geist Sans + Geist Mono (tabular nums)
   Color: OKLCH, auto light/dark
   Grid: 4/8/12/16/24 (8pt)
   ============================================================= */

:root {
  color-scheme: light dark;

  /* --- Light tokens (più contrasto) --- */
  --bg:            oklch(98% 0.004 240);
  --bg-elevated:   oklch(100% 0 0);
  --surface:       oklch(96% 0.006 240);
  --surface-2:     oklch(94% 0.008 240);
  --surface-hover: oklch(91% 0.01 240);
  --border:        oklch(88% 0.012 240);
  --border-strong: oklch(78% 0.018 240);

  --text:          oklch(13% 0.025 240);
  --text-dim:      oklch(38% 0.02 240);
  --text-mute:     oklch(52% 0.015 240);

  --accent:        oklch(55% 0.20 250);
  --accent-hover:  oklch(48% 0.22 250);
  --accent-soft:   oklch(96% 0.04 250);
  --accent-fg:     oklch(100% 0 0);

  --ok:            oklch(60% 0.17 155);
  --ok-soft:       oklch(95% 0.04 155);
  --warn:          oklch(72% 0.18 75);
  --warn-soft:     oklch(96% 0.05 75);
  --err:           oklch(58% 0.22 25);
  --err-soft:      oklch(96% 0.04 25);

  --shadow-sm:     0 1px 2px 0 oklch(20% 0.02 240 / 0.06);
  --shadow-md:     0 4px 12px -2px oklch(20% 0.02 240 / 0.08),
                   0 2px 4px -1px oklch(20% 0.02 240 / 0.05);
  --shadow-lg:     0 16px 32px -8px oklch(20% 0.02 240 / 0.12);

  --radius-sm:     6px;
  --radius:        10px;
  --radius-lg:     14px;

  --font-sans:     'Geist', 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-mono:     'Geist Mono', 'JetBrains Mono', 'SF Mono', Consolas, monospace;

  --motion-fast:   120ms cubic-bezier(0.4, 0, 0.2, 1);
  --motion:        200ms cubic-bezier(0.4, 0, 0.2, 1);
  --motion-slow:   400ms cubic-bezier(0.4, 0, 0.2, 1);
}

[data-theme="dark"],
:root[data-theme="dark"] {
  --bg:            oklch(14% 0.01 240);
  --bg-elevated:   oklch(18% 0.012 240);
  --surface:       oklch(20% 0.015 240);
  --surface-2:     oklch(24% 0.018 240);
  --surface-hover: oklch(28% 0.02 240);
  --border:        oklch(28% 0.02 240);
  --border-strong: oklch(36% 0.025 240);

  --text:          oklch(96% 0.005 240);
  --text-dim:      oklch(72% 0.01 240);
  --text-mute:     oklch(55% 0.015 240);

  --accent:        oklch(70% 0.18 250);
  --accent-hover:  oklch(78% 0.16 250);
  --accent-soft:   oklch(28% 0.08 250);
  --accent-fg:     oklch(14% 0.01 240);

  --ok:            oklch(72% 0.18 155);
  --ok-soft:       oklch(28% 0.08 155);
  --warn:          oklch(78% 0.17 75);
  --warn-soft:     oklch(28% 0.08 75);
  --err:           oklch(70% 0.22 25);
  --err-soft:      oklch(28% 0.1 25);

  --shadow-sm:     0 1px 2px 0 oklch(0% 0 0 / 0.3);
  --shadow-md:     0 4px 12px -2px oklch(0% 0 0 / 0.4),
                   0 2px 4px -1px oklch(0% 0 0 / 0.3);
  --shadow-lg:     0 16px 32px -8px oklch(0% 0 0 / 0.5);
}

/* System preference auto (se nessun data-theme) */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg:            oklch(14% 0.01 240);
    --bg-elevated:   oklch(18% 0.012 240);
    --surface:       oklch(20% 0.015 240);
    --surface-2:     oklch(24% 0.018 240);
    --surface-hover: oklch(28% 0.02 240);
    --border:        oklch(28% 0.02 240);
    --border-strong: oklch(36% 0.025 240);
    --text:          oklch(96% 0.005 240);
    --text-dim:      oklch(72% 0.01 240);
    --text-mute:     oklch(55% 0.015 240);
    --accent:        oklch(70% 0.18 250);
    --accent-hover:  oklch(78% 0.16 250);
    --accent-soft:   oklch(28% 0.08 250);
    --accent-fg:     oklch(14% 0.01 240);
    --ok:            oklch(72% 0.18 155);
    --ok-soft:       oklch(28% 0.08 155);
    --warn:          oklch(78% 0.17 75);
    --warn-soft:     oklch(28% 0.08 75);
    --err:           oklch(70% 0.22 25);
    --err-soft:      oklch(28% 0.1 25);
    --shadow-sm:     0 1px 2px 0 oklch(0% 0 0 / 0.3);
    --shadow-md:     0 4px 12px -2px oklch(0% 0 0 / 0.4),
                     0 2px 4px -1px oklch(0% 0 0 / 0.3);
    --shadow-lg:     0 16px 32px -8px oklch(0% 0 0 / 0.5);
  }
}

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

html, body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.55;
  font-feature-settings: "cv11", "ss01", "ss02";
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

a { color: inherit; text-decoration: none; }
button { font-family: inherit; font-size: inherit; cursor: pointer; border: none; background: none; color: inherit; }

/* =============================================================
   LAYOUT
   ============================================================= */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: oklch(from var(--bg) l c h / 0.85);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--border);
  padding: 14px 32px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
  letter-spacing: -0.01em;
  font-size: 14px;
}
.brand-mark {
  width: 26px;
  height: 26px;
  border-radius: 7px;
  background: linear-gradient(135deg, var(--accent) 0%, oklch(65% 0.2 290) 100%);
  display: grid;
  place-items: center;
  color: oklch(100% 0 0);
  font-weight: 700;
  font-size: 13px;
  box-shadow: var(--shadow-sm);
}

.topbar-end {
  display: flex;
  gap: 8px;
  align-items: center;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 12px;
  color: var(--text-dim);
  font-weight: 500;
  transition: all var(--motion-fast);
}
.chip .dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--ok);
  box-shadow: 0 0 0 3px oklch(from var(--ok) l c h / 0.15);
}
.chip.mock .dot { background: var(--warn); box-shadow: 0 0 0 3px oklch(from var(--warn) l c h / 0.15); }
.chip.off .dot { background: var(--text-mute); box-shadow: none; }

.icon-btn {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: var(--radius-sm);
  color: var(--text-dim);
  transition: all var(--motion-fast);
}
.icon-btn:hover { background: var(--surface-hover); color: var(--text); }
.icon-btn svg { width: 18px; height: 18px; }

main {
  max-width: 1240px;
  margin: 0 auto;
  padding: 40px 32px 96px;
}

.page-header { margin-bottom: 32px; }
.page-title {
  font-size: 28px;
  font-weight: 600;
  letter-spacing: -0.02em;
  margin: 0 0 6px;
  color: var(--text);
}
.page-sub {
  color: var(--text-dim);
  font-size: 14px;
  max-width: 640px;
  margin: 0;
}

/* =============================================================
   AUTO-POLL PANEL
   ============================================================= */

.autopoll {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 20px;
  margin-bottom: 24px;
  display: flex;
  align-items: center;
  gap: 20px;
  flex-wrap: wrap;
}
.autopoll-info { flex: 1; min-width: 240px; }
.autopoll-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 2px;
  letter-spacing: -0.005em;
}
.autopoll-status {
  font-size: 12px;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}
.autopoll-status.active { color: var(--ok); }

.select {
  appearance: none;
  -webkit-appearance: none;
  padding: 8px 32px 8px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12' fill='none' stroke='currentColor' stroke-width='1.5'%3E%3Cpath d='M3 5l3 3 3-3'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  background-size: 12px;
  transition: all var(--motion-fast);
}
.select:hover:not(:disabled) { background-color: var(--surface-hover); }
.select:disabled { opacity: 0.5; cursor: not-allowed; }

/* =============================================================
   CARDS GRID
   ============================================================= */

.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
@media (max-width: 900px) { .grid-2, .grid-3 { grid-template-columns: 1fr; } }

.card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  transition: all var(--motion);
  display: flex;
  flex-direction: column;
}
.card.hover { cursor: pointer; }
.card.hover:hover {
  border-color: var(--border-strong);
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}

.card-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 8px;
}
.card h3 {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.005em;
}
.card .desc {
  color: var(--text-dim);
  font-size: 13px;
  margin: 0 0 16px;
  line-height: 1.55;
}

.stat-row {
  display: flex;
  gap: 24px;
  flex-wrap: wrap;
  padding-top: 14px;
  border-top: 1px solid var(--border);
  margin-top: auto;
}
.stat { display: flex; flex-direction: column; gap: 2px; }
.stat .k {
  font-size: 11px;
  color: var(--text-mute);
  font-weight: 500;
  letter-spacing: 0.02em;
}
.stat .v {
  font-size: 18px;
  font-weight: 600;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.01em;
}
.stat.dim .v {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-dim);
}

/* =============================================================
   BUTTONS
   ============================================================= */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: var(--accent-fg);
  font-weight: 500;
  font-size: 13px;
  transition: all var(--motion-fast);
  white-space: nowrap;
  letter-spacing: -0.005em;
}
.btn:hover { background: var(--accent-hover); }
.btn:active { transform: scale(0.97); }
.btn.ghost {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-strong);
}
.btn.ghost:hover { background: var(--surface-hover); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* =============================================================
   PANEL + TABLES
   ============================================================= */

.panel {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.panel-head {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}
.panel-head h2 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: -0.005em;
}
.panel-head .muted {
  color: var(--text-dim);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
thead th {
  text-align: left;
  padding: 10px 16px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-mute);
  font-weight: 500;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
tbody td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
  color: var(--text);
}
tbody tr:last-child td { border-bottom: none; }
tbody tr { transition: background var(--motion-fast); }
tbody tr:hover td { background: var(--surface); }

td.num, th.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-family: var(--font-mono);
  font-size: 12.5px;
}
td.mono { font-family: var(--font-mono); font-size: 12.5px; color: var(--text-dim); }
td.num.positive { color: var(--ok); font-weight: 600; }
td.num.negative { color: var(--err); font-weight: 600; }

.badge {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 5px;
  font-size: 11.5px;
  font-weight: 600;
  letter-spacing: 0.005em;
  font-variant-numeric: tabular-nums;
  border: 1px solid transparent;
}
.badge.ok {
  background: oklch(from var(--ok) 94% 0.06 h);
  color: oklch(from var(--ok) 28% c h);
  border-color: oklch(from var(--ok) l c h / 0.45);
}
.badge.partial {
  background: oklch(from var(--warn) 94% 0.08 h);
  color: oklch(from var(--warn) 35% c h);
  border-color: oklch(from var(--warn) l c h / 0.55);
}
.badge.error {
  background: oklch(from var(--err) 94% 0.07 h);
  color: oklch(from var(--err) 35% c h);
  border-color: oklch(from var(--err) l c h / 0.5);
}
.badge.running {
  background: oklch(from var(--accent) 94% 0.07 h);
  color: oklch(from var(--accent) 35% c h);
  border-color: oklch(from var(--accent) l c h / 0.5);
}
.badge.sent {
  background: oklch(from var(--accent) 94% 0.07 h);
  color: oklch(from var(--accent) 30% c h);
  border-color: oklch(from var(--accent) l c h / 0.5);
}
[data-theme="dark"] .badge.ok {
  background: oklch(from var(--ok) 22% 0.06 h);
  color: oklch(from var(--ok) 82% c h);
  border-color: oklch(from var(--ok) l c h / 0.5);
}
[data-theme="dark"] .badge.partial {
  background: oklch(from var(--warn) 22% 0.07 h);
  color: oklch(from var(--warn) 85% c h);
  border-color: oklch(from var(--warn) l c h / 0.55);
}
[data-theme="dark"] .badge.error {
  background: oklch(from var(--err) 22% 0.08 h);
  color: oklch(from var(--err) 82% c h);
  border-color: oklch(from var(--err) l c h / 0.55);
}
[data-theme="dark"] .badge.running,
[data-theme="dark"] .badge.sent {
  background: oklch(from var(--accent) 22% 0.07 h);
  color: oklch(from var(--accent) 82% c h);
  border-color: oklch(from var(--accent) l c h / 0.5);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .badge.ok {
    background: oklch(from var(--ok) 22% 0.06 h);
    color: oklch(from var(--ok) 82% c h);
    border-color: oklch(from var(--ok) l c h / 0.5);
  }
  :root:not([data-theme="light"]) .badge.partial {
    background: oklch(from var(--warn) 22% 0.07 h);
    color: oklch(from var(--warn) 85% c h);
    border-color: oklch(from var(--warn) l c h / 0.55);
  }
  :root:not([data-theme="light"]) .badge.error {
    background: oklch(from var(--err) 22% 0.08 h);
    color: oklch(from var(--err) 82% c h);
    border-color: oklch(from var(--err) l c h / 0.55);
  }
  :root:not([data-theme="light"]) .badge.running,
  :root:not([data-theme="light"]) .badge.sent {
    background: oklch(from var(--accent) 22% 0.07 h);
    color: oklch(from var(--accent) 82% c h);
    border-color: oklch(from var(--accent) l c h / 0.5);
  }
}

.steps-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 16px;
}
.step-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px 4px 4px;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 999px;
  font-size: 11.5px;
  color: var(--text-dim);
  line-height: 1;
}
.step-pill .n {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-fg);
  display: grid;
  place-items: center;
  font-size: 9.5px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.breadcrumb {
  color: var(--text-dim);
  font-size: 12px;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 4px;
}
.breadcrumb a { color: var(--text-dim); transition: color var(--motion-fast); }
.breadcrumb a:hover { color: var(--text); }

.empty {
  padding: 48px 20px;
  text-align: center;
  color: var(--text-dim);
  font-size: 13px;
}

.link-ghost {
  color: var(--text-dim);
  text-decoration: underline;
  text-decoration-color: var(--border-strong);
  text-underline-offset: 3px;
  text-decoration-thickness: 1px;
  transition: all var(--motion-fast);
}
.link-ghost:hover {
  color: var(--text);
  text-decoration-color: var(--accent);
}

.toolbar {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: center;
}
.toolbar .spacer { flex: 1; }

/* =============================================================
   TOAST
   ============================================================= */

.toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 100;
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 12px 16px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  box-shadow: var(--shadow-lg);
  opacity: 0;
  transform: translateY(8px);
  transition: all var(--motion);
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translateY(0); pointer-events: auto; }
.toast.ok { border-left: 3px solid var(--ok); }
.toast.err { border-left: 3px solid var(--err); }

/* =============================================================
   MODAL
   ============================================================= */

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: oklch(from var(--bg) calc(l - 5%) c h / 0.65);
  backdrop-filter: blur(4px);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
}
.modal-backdrop.show { display: flex; }
.modal {
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  max-width: 720px;
  width: 100%;
  max-height: 85vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-lg);
}
.modal-head {
  padding: 18px 22px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}
.modal-head h3 { margin: 0; font-size: 15px; font-weight: 600; letter-spacing: -0.005em; }
.modal-body {
  padding: 20px 22px;
  overflow-y: auto;
  flex: 1;
  font-size: 14px;
  line-height: 1.6;
  color: var(--text);
}
.modal-body .meta-row {
  display: flex;
  gap: 16px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.modal-body .meta-row .k { color: var(--text-mute); width: 90px; flex-shrink: 0; font-weight: 500; }
.modal-body .body-text {
  margin-top: 16px;
  padding: 14px;
  background: var(--surface);
  border-radius: var(--radius-sm);
  white-space: pre-wrap;
  font-family: var(--font-mono);
  font-size: 12.5px;
  line-height: 1.6;
  color: var(--text-dim);
}

/* Collapsible storico */
.storico-collapsed {
  padding: 14px 20px;
  color: var(--text-dim);
  font-size: 13px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
  transition: background var(--motion-fast);
}
.storico-collapsed:hover { background: var(--surface); color: var(--text); }
.storico-collapsed .chevron {
  transition: transform var(--motion-fast);
}
.storico-collapsed.open .chevron { transform: rotate(180deg); }

/* =============================================================
   SPINNER
   ============================================================= */

.spinner {
  width: 14px;
  height: 14px;
  border: 1.5px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 700ms linear infinite;
  display: inline-block;
  vertical-align: middle;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* =============================================================
   THEME TOGGLE
   ============================================================= */

.theme-toggle {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: var(--radius-sm);
  color: var(--text-dim);
  transition: all var(--motion-fast);
  position: relative;
}
.theme-toggle:hover { background: var(--surface-hover); color: var(--text); }
.theme-toggle svg { width: 16px; height: 16px; }
.theme-toggle .sun { display: none; }
.theme-toggle .moon { display: block; }
[data-theme="dark"] .theme-toggle .sun { display: block; }
[data-theme="dark"] .theme-toggle .moon { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .theme-toggle .sun { display: block; }
  :root:not([data-theme="light"]) .theme-toggle .moon { display: none; }
}
