:root {
  --bg: #EEF1F4;
  --surface: #FFFFFF;
  --surface-2: #F6F8FA;
  --ink: #17212B;
  --muted: #5B6875;
  --line: #DCE2E8;
  --call: #0B6E69;
  --call-ink: #FFFFFF;
  --call-soft: #DDF0EE;
  --hang: #C8372D;
  --ring: #B7791F;
  --ring-soft: #FBF0DC;
  --danger-soft: #FBE4E1;
  --focus: #2F7DE1;
  --radius-lg: 18px;
  --radius: 10px;
  --radius-sm: 6px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12181F;
    --surface: #1A222B;
    --surface-2: #212B35;
    --ink: #E6EBF0;
    --muted: #94A1AE;
    --line: #2E3944;
    --call: #2BA59C;
    --call-ink: #06201E;
    --call-soft: #173532;
    --hang: #E5584D;
    --ring: #E0A64A;
    --ring-soft: #3A2E18;
    --danger-soft: #3B1F1D;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 'Schibsted Grotesk', system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--call); }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
[hidden] { display: none !important; }

/* ---------- login ---------- */
.login {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}
.login form {
  width: min(380px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 32px 28px 28px;
}
.login h1 { font-size: 26px; line-height: 1.15; margin: 14px 0 4px; letter-spacing: -0.01em; }
.login p { margin: 0 0 24px; }
.brand-mark {
  width: 40px; height: 40px; border-radius: 11px; background: var(--call);
  display: grid; place-items: center; color: var(--call-ink);
}
.brand-mark svg { width: 22px; height: 22px; }

/* ---------- campi ---------- */
.field { display: grid; gap: 6px; margin-bottom: 16px; }
.field label { font-weight: 500; font-size: 14px; }
.field .hint { font-size: 13px; color: var(--muted); }
input[type=text], input[type=password], input[type=search], input[type=date], input[type=tel], input[type=url], select, textarea {
  width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 9px 11px;
  min-height: 40px;
}
textarea { min-height: 70px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--focus); outline-offset: -1px; border-color: transparent; }
.check { display: flex; gap: 10px; align-items: flex-start; margin-bottom: 14px; }
.check input { width: 18px; height: 18px; margin: 2px 0 0; accent-color: var(--call); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  border-radius: var(--radius-sm); padding: 8px 14px; min-height: 40px;
  font-weight: 500; cursor: pointer; text-decoration: none;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--call); border-color: var(--call); color: var(--call-ink); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--hang); }
.btn.block { width: 100%; }
.btn.sm { min-height: 32px; padding: 4px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.form-error { color: var(--hang); font-size: 14px; min-height: 21px; margin: 0 0 10px; }

/* ---------- struttura ---------- */
.shell { display: grid; grid-template-columns: 232px 1fr auto; min-height: 100dvh; }
.side {
  border-right: 1px solid var(--line);
  padding: 20px 14px;
  display: flex; flex-direction: column; gap: 4px;
  position: sticky; top: 0; height: 100dvh;
}
.side .brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; padding: 0 8px 18px; }
.side .brand .brand-mark { width: 32px; height: 32px; border-radius: 9px; }
.side .brand .brand-mark svg { width: 18px; height: 18px; }
.nav-group { font-size: 13px; color: var(--muted); padding: 16px 10px 4px; }
.side nav a {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; border-radius: var(--radius-sm);
  color: var(--ink); text-decoration: none; font-weight: 500;
}
.side nav a svg { width: 18px; height: 18px; color: var(--muted); flex: none; }
.side nav a:hover { background: var(--surface); }
.side nav a[aria-current=page] { background: var(--surface); box-shadow: inset 3px 0 0 var(--call); }
.side nav a[aria-current=page] svg { color: var(--call); }
.side .me { margin-top: auto; padding: 12px 10px 0; border-top: 1px solid var(--line); display: grid; gap: 2px; }
.side .me strong { font-weight: 600; }
.side .me .row { display: flex; gap: 12px; margin-top: 6px; }
.linkish { background: none; border: 0; padding: 0; color: var(--muted); cursor: pointer; font-size: 13px; text-decoration: underline; text-underline-offset: 3px; }
.linkish:hover { color: var(--ink); }

main { padding: 28px 32px 48px; min-width: 0; container-type: inline-size; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 22px; flex-wrap: wrap; }
.page-head h1 { font-size: 26px; line-height: 1.15; margin: 0; letter-spacing: -0.01em; }
.page-head p { margin: 4px 0 0; color: var(--muted); }

.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.panel-pad { padding: 20px 22px; }
.panel h2 { font-size: 17px; margin: 0 0 4px; }
.panel h2 + p { margin: 0 0 18px; color: var(--muted); font-size: 14px; }

.notice {
  border-radius: var(--radius); padding: 12px 14px; margin-bottom: 18px; font-size: 14px;
  background: var(--ring-soft); border: 1px solid color-mix(in srgb, var(--ring) 35%, transparent);
}
.notice strong { display: block; margin-bottom: 2px; }

/* ---------- telefono ---------- */
.phone-layout { display: grid; grid-template-columns: minmax(300px, 400px) 1fr; gap: 24px; align-items: start; }
.dialer { padding: 22px; border-radius: var(--radius-lg); }
.display {
  border-radius: var(--radius); background: var(--surface-2); padding: 18px 16px 14px;
  text-align: center; margin-bottom: 16px; position: relative;
}
.display .status { font-size: 14px; color: var(--muted); min-height: 21px; display: flex; justify-content: center; align-items: center; gap: 8px; }
.display input {
  border: 0; background: transparent; text-align: center; padding: 6px 0; min-height: 0;
  font-size: clamp(26px, 8vw, 34px); font-weight: 600; letter-spacing: 0.02em; font-variant-numeric: tabular-nums;
}
.display input:placeholder-shown { font-size: 22px; }
.display input:focus { outline: none; }
.display input::placeholder { color: color-mix(in srgb, var(--muted) 55%, transparent); font-weight: 500; }
.display .timer { font-size: 15px; font-variant-numeric: tabular-nums; color: var(--muted); min-height: 22px; }
.dialer[data-state=ringing] .display { background: var(--ring-soft); }
.dialer[data-state=live] .display { background: var(--call-soft); }
.pulse { width: 9px; height: 9px; border-radius: 50%; background: currentColor; flex: none; }
.dialer[data-state=ringing] .status { color: var(--ring); }
.dialer[data-state=live] .status { color: var(--call); }
.dialer[data-state=ringing] .pulse, .dialer[data-state=live] .pulse { animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 0%,100% { opacity: 1; transform: scale(1); } 50% { opacity: .35; transform: scale(.7); } }
@media (prefers-reduced-motion: reduce) { .pulse { animation: none !important; } }

.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 16px; }
.key {
  border: 1px solid var(--line); background: var(--surface); border-radius: 14px;
  height: 58px; cursor: pointer; display: grid; place-items: center; line-height: 1.05;
}
.key:hover { background: var(--surface-2); }
.key:active { transform: scale(.97); }
.key b { font-size: 22px; font-weight: 600; }
.key small { font-size: 10.5px; color: var(--muted); letter-spacing: .08em; min-height: 12px; }
.dial-actions { display: grid; grid-template-columns: 58px 1fr 58px; gap: 10px; align-items: center; }
.round {
  height: 58px; border-radius: 14px; border: 1px solid var(--line); background: var(--surface);
  display: grid; place-items: center; cursor: pointer; color: var(--muted);
}
.round:hover { background: var(--surface-2); color: var(--ink); }
.round svg { width: 22px; height: 22px; }
.round[aria-pressed=true] { background: var(--ink); color: var(--surface); border-color: var(--ink); }
.call-btn {
  height: 58px; border-radius: 14px; border: 0; cursor: pointer;
  background: var(--call); color: var(--call-ink); font-weight: 600; font-size: 16px;
  display: flex; align-items: center; justify-content: center; gap: 10px;
}
.call-btn svg { width: 20px; height: 20px; }
.call-btn.hangup { background: var(--hang); color: #fff; }
.call-btn:disabled { opacity: .45; cursor: not-allowed; }
.dial-foot { margin-top: 14px; font-size: 13px; color: var(--muted); text-align: center; }
.rec-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--hang); margin-right: 6px; vertical-align: 1px; }

.today { display: flex; gap: 28px; flex-wrap: wrap; padding: 16px 22px; border-bottom: 1px solid var(--line); }
.today div { display: grid; }
.today b { font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; line-height: 1.2; }
.today span { font-size: 13px; color: var(--muted); }

/* ---------- liste e tabelle ---------- */
.list { list-style: none; margin: 0; padding: 0; }
.list li { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; padding: 12px 22px; border-bottom: 1px solid var(--line); align-items: center; }
.list li:last-child { border-bottom: 0; }
.list .who { font-weight: 600; font-variant-numeric: tabular-nums; }
.list .meta { font-size: 13px; color: var(--muted); }
.list .again { grid-row: span 2; }
.empty { padding: 36px 22px; text-align: center; color: var(--muted); }
.empty strong { display: block; color: var(--ink); margin-bottom: 4px; }

.filters { display: flex; gap: 10px; flex-wrap: wrap; padding: 14px 16px; border-bottom: 1px solid var(--line); align-items: flex-end; }
.filters .field { margin: 0; gap: 4px; }
.filters .field label { font-size: 13px; color: var(--muted); font-weight: 400; }
.filters input, .filters select { min-height: 36px; padding: 6px 10px; }
.filters .grow { flex: 1 1 180px; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; font-weight: 500; color: var(--muted); font-size: 13px; padding: 10px 16px; border-bottom: 1px solid var(--line); white-space: nowrap; }
td { padding: 11px 16px; border-bottom: 1px solid var(--line); vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
td.right, th.right { text-align: right; }
td.nowrap { white-space: nowrap; }
tr.player-row td { background: var(--surface-2); padding: 10px 16px 14px; }
tr.player-row audio { width: 100%; max-width: 560px; height: 40px; display: block; }
.player-tools { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

.badge {
  display: inline-flex; align-items: center; gap: 6px; font-size: 13px; font-weight: 500;
  padding: 2px 9px; border-radius: 99px; background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.badge::before { content: ''; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.badge.ok { background: var(--call-soft); color: var(--call); }
.badge.warn { background: var(--ring-soft); color: var(--ring); }
.badge.bad { background: var(--danger-soft); color: var(--hang); }
.badge.plain::before { display: none; }

.pager { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; border-top: 1px solid var(--line); font-size: 14px; color: var(--muted); gap: 10px; }
.pager div { display: flex; gap: 8px; }

/* ---------- impostazioni / utenti ---------- */
.stack { display: grid; gap: 20px; max-width: 760px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }
.state-line { display: flex; align-items: center; gap: 10px; font-size: 14px; margin-bottom: 4px; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 18px; font-size: 14px; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-variant-numeric: tabular-nums; word-break: break-all; }
.form-actions { display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; margin-top: 6px; }

dialog {
  border: 1px solid var(--line); border-radius: var(--radius-lg); background: var(--surface); color: var(--ink);
  padding: 24px; width: min(440px, calc(100vw - 32px));
}
dialog::backdrop { background: rgb(10 16 22 / .45); }
dialog h2 { margin: 0 0 16px; font-size: 19px; }

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px);
  background: var(--ink); color: var(--surface); padding: 10px 16px; border-radius: var(--radius);
  font-size: 14px; opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; max-width: calc(100vw - 32px);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.error { background: var(--hang); color: #fff; }

/* ---------- rubrica e credito ---------- */
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--surface-2); }
.sync-line {
  display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 10px 16px; border-bottom: 1px solid var(--line); font-size: 13px; color: var(--muted);
}
.sync-actions { display: flex; gap: 14px; align-items: center; }
dialog:has(.contact-form) { width: min(560px, calc(100vw - 32px)); }
.crm-note { margin: -8px 0 16px; font-size: 14px; }
.today .low b, .low-text { color: var(--hang); }
.state-line #s-balance { margin-left: 4px; }

/* ---------- blocco note degli script ---------- */
.notes {
  position: sticky; top: 0; height: 100dvh;
  border-left: 1px solid var(--line); background: var(--surface);
  display: flex;
}
.notes-tab {
  width: 44px; border: 0; background: none; cursor: pointer; color: var(--muted);
  display: flex; flex-direction: column; align-items: center; gap: 10px; padding: 20px 0;
  font-weight: 500; font-size: 14px;
}
.notes-tab:hover { color: var(--ink); background: var(--surface-2); }
.notes-tab svg { width: 20px; height: 20px; flex: none; }
.notes-tab span { writing-mode: vertical-rl; }
.notes-panel { width: clamp(300px, 26vw, 420px); display: flex; flex-direction: column; padding: 18px 18px 16px; gap: 12px; }
.notes[data-open=true] .notes-tab, .notes[data-open=false] .notes-panel { display: none; }
.notes-head { display: flex; align-items: flex-start; gap: 10px; }
.notes-title { flex: 1; min-width: 0; display: grid; }
.notes-title strong { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.notes-title span { color: var(--muted); min-height: 20px; }
.notes-title span.unsaved { color: var(--ring); }
.round.sm { width: 34px; height: 34px; border-radius: var(--radius-sm); flex: none; }
.round.sm svg { width: 18px; height: 18px; }
.notes-tools { display: flex; gap: 8px; }
.notes-tools .btn { flex: 1; }
.notes textarea {
  flex: 1; min-height: 0; resize: none;
  font-size: 16px; line-height: 1.6; padding: 14px;
}
.script-picker #nl-q { margin-bottom: 8px; }
.script-picker .list { max-height: min(50dvh, 420px); overflow-y: auto; border: 1px solid var(--line); border-radius: var(--radius-sm); margin-bottom: 12px; }
.script-picker .list li { padding: 0 10px 0 0; grid-template-columns: 1fr auto; }
.script-picker .empty { padding: 24px 14px; }
.pick { display: grid; text-align: left; background: none; border: 0; padding: 10px 14px; cursor: pointer; min-width: 0; }
.pick:hover .who { color: var(--call); }
.pick .who { overflow-wrap: anywhere; }

@container (max-width: 680px) {
  .phone-layout { grid-template-columns: 1fr; }
  .dialer { max-width: 440px; width: 100%; margin: 0 auto; }
}

/* ---------- mobile ---------- */
@media (max-width: 900px) {
  .phone-layout { grid-template-columns: 1fr; }
  .dialer { max-width: 440px; width: 100%; margin: 0 auto; }
}
@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; align-content: start; }
  .notes { position: fixed; z-index: 10; top: auto; height: auto; border: 0; background: none; }
  .notes[data-open=false] { right: 16px; bottom: 16px; }
  .notes-tab {
    width: auto; flex-direction: row; padding: 10px 16px; border-radius: 99px;
    background: var(--ink); color: var(--surface); box-shadow: 0 4px 14px rgb(0 0 0 / .2);
  }
  .notes-tab:hover { background: var(--ink); color: var(--surface); }
  .notes-tab span { writing-mode: horizontal-tb; }
  .notes[data-open=true] { left: 0; right: 0; bottom: 0; height: 62dvh; }
  .notes-panel {
    width: 100%; background: var(--surface); border-top: 1px solid var(--line);
    border-radius: var(--radius-lg) var(--radius-lg) 0 0; box-shadow: 0 -6px 24px rgb(0 0 0 / .18); padding: 16px;
  }
  .side {
    position: sticky; top: 0; z-index: 5; height: auto; flex-direction: row; align-items: center;
    padding: 10px 16px; border-right: 0; border-bottom: 1px solid var(--line); background: var(--bg); gap: 6px;
    overflow-x: auto;
  }
  .side .brand { padding: 0 8px 0 0; }
  .side .brand span, .nav-group, .side .me strong, .side .me .muted { display: none; }
  .side nav { display: flex; gap: 2px; }
  .side nav a { padding: 8px; white-space: nowrap; }
  .side nav a span { display: none; }
  .side nav a[aria-current=page] { box-shadow: inset 0 -2px 0 var(--call); }
  .side .me { margin: 0 0 0 auto; border: 0; padding: 0; }
  .side .me .row { margin: 0; }
  main { padding: 20px 16px 40px; }
  .grid-2 { grid-template-columns: 1fr; }
  .hide-sm { display: none; }
  td, th { padding: 10px 12px; }
}
