:root {
  --bg: #f2f5f4;
  --surface: #ffffff;
  --surface-2: #f6f8f7;
  --text: #15201c;
  --muted: #5f6d68;
  --border: #dbe3e0;
  --primary: #0f7b5f;
  --primary-hover: #0b6650;
  --primary-ink: #ffffff;
  --primary-soft: #e1f2eb;
  --wa: #1c9d55;
  --wa-hover: #17864a;
  --wa-ink: #ffffff;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --warn-ink: #7a4b00;
  --warn-soft: #fff3d6;
  --fisso-ink: #4b5563;
  --fisso-soft: #eceff2;
  --radius: 12px;
  --shadow: 0 1px 2px rgb(0 0 0 / .05), 0 6px 20px rgb(0 0 0 / .04);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1311;
    --surface: #161c1a;
    --surface-2: #1c2421;
    --text: #e5ebe8;
    --muted: #97a59f;
    --border: #2a3430;
    --primary: #2fb38b;
    --primary-hover: #3cc49a;
    --primary-ink: #03140d;
    --primary-soft: #153029;
    --wa: #2bbf6a;
    --wa-hover: #37d077;
    --wa-ink: #03140d;
    --danger: #f2877d;
    --danger-soft: #3a1d1a;
    --warn-ink: #f0c56a;
    --warn-soft: #33290f;
    --fisso-ink: #b9c3bf;
    --fisso-soft: #26302c;
    --shadow: none;
    color-scheme: dark;
  }
}

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

a { color: var(--primary); }
h1, h2 { margin: 0; line-height: 1.2; }
h2 { font-size: 1.05rem; }
code, kbd {
  font: .85em ui-monospace, Consolas, monospace;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0 .3em;
}

/* ---------- accesso ---------- */

body.loading main { visibility: hidden; }

.login {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  padding: 16px;
  background: var(--bg);
}
.login-card {
  width: min(360px, 100%);
  display: grid;
  gap: 12px;
  justify-items: center;
  text-align: center;
  padding: 28px 24px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.login-card p { margin: 0; color: var(--muted); font-size: .9rem; }
.login-card input, .login-card .btn { width: 100%; }
.login-card .status { min-height: 0; }

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

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 5;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand h1 { font-size: 1.15rem; }
.brand p { margin: 2px 0 0; color: var(--muted); font-size: .85rem; }
.topbar-actions { display: flex; gap: 8px; flex-shrink: 0; }

main {
  max-width: 1400px;
  margin: 0 auto;
  padding: 20px 24px 80px;
  display: grid;
  gap: 16px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px 20px;
  min-width: 0;
}
.card > h2 { margin-bottom: 14px; }
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.stats { margin: 0; color: var(--muted); font-size: .88rem; }

.banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  border-radius: var(--radius);
  background: var(--warn-soft);
  color: var(--warn-ink);
}

.top-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}

/* ---------- consumo API ---------- */

.usage { padding-bottom: 14px; }
.title-note { font-size: .78rem; font-weight: 500; color: var(--muted); margin-left: 6px; }
.small-link { font-size: .85rem; }
.tiles { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
.tile {
  display: grid;
  gap: 4px;
  align-content: start;
  padding: 12px 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
}
.tile-label { font-size: .75rem; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.tile-value { font-size: 1.35rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.tile-sub { font-size: .82rem; color: var(--muted); }
.meter { height: 6px; border-radius: 999px; background: var(--border); overflow: hidden; margin: 2px 0; }
.meter span { display: block; height: 100%; width: 0; background: var(--primary); border-radius: inherit; transition: width .3s; }
.meter span.over { background: var(--danger); }
.link-btn {
  font: inherit;
  padding: 0;
  border: 0;
  background: none;
  color: var(--primary);
  text-decoration: underline;
  cursor: pointer;
}
.usage-history { margin-top: 12px; }
.usage-history summary { cursor: pointer; font-size: .88rem; font-weight: 600; color: var(--muted); }
.usage-history summary:hover { color: var(--text); }
.usage-history .table-wrap { margin-top: 10px; max-width: 640px; }
table.mini th, table.mini td { padding: 6px 12px; }
table.mini td { font-variant-numeric: tabular-nums; }
table.mini tr.idle td { color: var(--muted); }
.num { text-align: right; }

/* ---------- messaggio ---------- */

.message-card { display: grid; gap: 10px; }
.message-card .card-head { margin-bottom: 0; }
.message-card textarea { width: 100%; min-height: 130px; }
.chips { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.chips-label { font-size: .82rem; color: var(--muted); margin-right: 2px; }
.chip {
  font: .8rem ui-monospace, Consolas, monospace;
  padding: 3px 9px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
}
.chip:hover { border-color: var(--primary); color: var(--primary); }
.preview { display: grid; gap: 6px; }
.preview-label { font-size: .8rem; color: var(--muted); }
.preview-label strong { color: var(--text); font-weight: 600; }
.bubble {
  justify-self: start;
  max-width: 100%;
  max-height: 180px;
  overflow: auto;
  padding: 9px 12px;
  border-radius: 10px 10px 10px 2px;
  background: var(--primary-soft);
  color: var(--text);
  font-size: .9rem;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* ---------- form ---------- */

label { display: grid; gap: 5px; font-size: .85rem; font-weight: 600; color: var(--muted); }

input, select, textarea {
  font: inherit;
  font-weight: 400;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 11px;
  min-width: 0;
}
textarea { resize: vertical; line-height: 1.4; }
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}

.search-grid {
  display: grid;
  grid-template-columns: 1.2fr 1.2fr 150px auto;
  gap: 12px;
  align-items: end;
}
.search-grid.busy { opacity: .7; }

.hint { margin: 8px 0 0; color: var(--muted); font-size: .82rem; font-weight: 400; }
.status { margin: 10px 0 0; font-size: .9rem; min-height: 1.3em; }
.status.ok { color: var(--primary); }
.status.error { color: var(--danger); }

/* ---------- buttons ---------- */

.btn {
  font: inherit;
  font-weight: 600;
  font-size: .9rem;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 9px 14px;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color .15s, border-color .15s;
}
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--primary); color: var(--primary-ink); }
.btn.primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn.wa { background: var(--wa); color: var(--wa-ink); }
.btn.wa:hover:not(:disabled) { background: var(--wa-hover); }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn.ghost:hover:not(:disabled) { background: var(--surface-2); }
.btn.danger { background: transparent; color: var(--danger); border-color: currentColor; }
.btn.danger:hover { background: var(--danger-soft); }

.icon-btn {
  font: inherit;
  width: 30px;
  height: 30px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

/* ---------- toolbar ---------- */

.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px; margin-bottom: 12px; }
.toolbar input[type="search"] { flex: 1 1 200px; }
#f-city { flex: 0 1 300px; font-weight: 600; }
#f-city.active { border-color: var(--primary); background: var(--primary-soft); color: var(--primary); }
.toolbar #btn-next { margin-left: auto; }

.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.segmented button {
  font: inherit;
  font-size: .87rem;
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 8px 12px;
  cursor: pointer;
}
.segmented button + button { border-left: 1px solid var(--border); }
.segmented button[aria-pressed="true"] { background: var(--primary-soft); color: var(--primary); font-weight: 600; }

.check { display: inline-flex; align-items: center; gap: 6px; font-weight: 500; color: var(--text); cursor: pointer; }
.check input { margin: 0; accent-color: var(--primary); }

/* ---------- table ---------- */

.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 10px; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { padding: 10px 12px; text-align: left; vertical-align: top; border-bottom: 1px solid var(--border); }
tbody tr:last-child td { border-bottom: 0; }
th {
  background: var(--surface-2);
  font-size: .78rem;
  text-transform: uppercase;
  letter-spacing: .03em;
  color: var(--muted);
  white-space: nowrap;
  position: sticky;
  top: 0;
}
th[data-sort] { cursor: pointer; user-select: none; }
th[data-sort]:hover { color: var(--text); }
th[data-dir="asc"]::after { content: " ▲"; }
th[data-dir="desc"]::after { content: " ▼"; }
tbody tr:hover { background: var(--surface-2); }
tr.sent td:not(.actions), tr.nophone td { color: var(--muted); }

.name { font-weight: 600; color: var(--text); }
.sub { color: var(--muted); font-size: .8rem; margin-top: 2px; }
.addr { min-width: 220px; }
.city { white-space: nowrap; font-weight: 500; }
.site { max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nowrap { white-space: nowrap; }
.muted { color: var(--muted); }
.phone { font-variant-numeric: tabular-nums; }
.actions { white-space: nowrap; text-align: right; }
.actions .icon-btn { margin-left: 6px; vertical-align: middle; }
.actions-col { width: 1%; }
.idx { width: 1%; text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }

.badge {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 7px;
  border-radius: 999px;
  font-size: .72rem;
  font-weight: 600;
  vertical-align: 1px;
}
.badge.mobile { background: var(--primary-soft); color: var(--primary); }
.badge.fisso { background: var(--fisso-soft); color: var(--fisso-ink); }

.sent-pill { display: inline-block; padding: 6px 10px; color: var(--primary); font-weight: 600; font-size: .85rem; }

.empty { text-align: center; color: var(--muted); padding: 28px 12px 12px; margin: 0; }

/* ---------- dialog ---------- */

dialog {
  width: min(640px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 0;
}
dialog::backdrop { background: rgb(0 0 0 / .45); }
dialog form { display: grid; gap: 12px; padding: 22px; }
dialog h2 { font-size: 1.2rem; margin-bottom: 4px; }
dialog .hint { margin-top: -6px; }
fieldset { border: 1px solid var(--border); border-radius: 10px; padding: 10px 14px 14px; margin: 0; display: grid; gap: 10px; }
legend { font-size: .85rem; font-weight: 600; color: var(--muted); padding: 0 6px; }
.radio { display: flex; align-items: flex-start; gap: 10px; font-weight: 400; color: var(--text); font-size: .9rem; cursor: pointer; }
.radio input[type="radio"] { margin-top: 3px; accent-color: var(--primary); }
.inline-num { width: 64px; padding: 2px 6px; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; }
.price-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 110px 130px;
  gap: 8px 10px;
  align-items: center;
  font-size: .88rem;
}
.price-grid small { color: var(--muted); }
.price-head { font-size: .78rem; font-weight: 600; color: var(--muted); }
#review textarea { width: 100%; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; flex-wrap: wrap; }
.dialog-actions .logout { margin-right: auto; }

/* ---------- toasts ---------- */

.toasts {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: grid;
  gap: 8px;
  z-index: 20;
  width: min(560px, calc(100vw - 32px));
}
.toast {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  border-radius: 10px;
  background: #1f2a26;
  color: #eef3f1;
  border: 1px solid rgb(255 255 255 / .08);
  box-shadow: 0 8px 24px rgb(0 0 0 / .25);
  font-size: .9rem;
  animation: toast-in .18s ease-out;
}
.toast.error { background: #7a1c14; }
.toast.warn { background: #6b4700; }
.toast button {
  font: inherit;
  font-weight: 700;
  background: transparent;
  border: 0;
  color: #7fe3bf;
  cursor: pointer;
  padding: 2px 4px;
}
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } }

/* ---------- responsive ---------- */

@media (max-width: 1100px) {
  .top-grid { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 860px) {
  .search-grid { grid-template-columns: 1fr 1fr; }
  .search-grid .btn { grid-column: 1 / -1; }
}

@media (max-width: 560px) {
  .topbar { padding: 12px 16px; }
  .brand p, .btn-label { display: none; }
  .topbar-actions .btn { padding: 8px 11px; }
  main { padding: 16px 16px 80px; }
  .card { padding: 16px; }
  .search-grid, .two-col { grid-template-columns: 1fr; }
  .tiles { grid-template-columns: 1fr; }
  .tile-value { font-size: 1.15rem; }
  .price-grid { grid-template-columns: minmax(0, 1fr) 80px 90px; }
  .toolbar #btn-next { margin-left: 0; width: 100%; }
  #f-city { flex-basis: 100%; }
}
