:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --border: #e2e5ea;
  --text: #1c2330;
  --muted: #6a7383;
  --primary: #2f5bea;
  --primary-hover: #2449c4;
  --primary-text: #ffffff;
  --focus: rgba(47, 91, 234, .25);
  --danger: #c4314b;
  --success-bg: #e7f6ec; --success-fg: #1d7a3e;
  --error-bg: #fdecee;   --error-fg: #b42335;
  --info-bg: #eaf0fe;    --info-fg: #2449c4;

  --s-open-bg: #e6efff;     --s-open-fg: #2449c4;
  --s-progress-bg: #fff3dc; --s-progress-fg: #9a5b00;
  --s-waiting-bg: #f1eafe;  --s-waiting-fg: #6b3fc4;
  --s-resolved-bg: #e5f6ea; --s-resolved-fg: #1d7a3e;
  --s-closed-bg: #eef0f3;   --s-closed-fg: #5b6474;

  --p-low: #8a93a3; --p-normal: #2f5bea; --p-high: #d97706; --p-urgent: #dc2626;

  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1218;
    --surface: #171b23;
    --surface-2: #1e232d;
    --border: #2a303c;
    --text: #e6e9ef;
    --muted: #949cab;
    --primary: #5b80ff;
    --primary-hover: #7895ff;
    --focus: rgba(91, 128, 255, .35);
    --success-bg: #12301d; --success-fg: #6fd593;
    --error-bg: #3a151c;   --error-fg: #ff8a9b;
    --info-bg: #17244a;    --info-fg: #9db4ff;
    --s-open-bg: #17244a;     --s-open-fg: #9db4ff;
    --s-progress-bg: #3a2a0c; --s-progress-fg: #f5c26b;
    --s-waiting-bg: #2a1d47;  --s-waiting-fg: #c3a6ff;
    --s-resolved-bg: #12301d; --s-resolved-fg: #6fd593;
    --s-closed-bg: #232833;   --s-closed-fg: #a0a8b6;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.5rem; line-height: 1.25; margin: 0 0 .5rem; }
h2 { font-size: 1rem; margin: 0 0 .75rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.center { text-align: center; }

/* Layout */
.container { max-width: 1100px; margin: 0 auto; padding: 24px 16px 64px; }
.container.narrow { max-width: 720px; }
.container.auth { max-width: 420px; padding-top: 10vh; }

.topbar { background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 10; }
.topbar-inner { max-width: 1100px; margin: 0 auto; padding: 10px 16px; display: flex; align-items: center; gap: 16px; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; color: var(--text); font-size: 1.05rem; }
.brand svg { color: var(--primary); }
.brand:hover { text-decoration: none; }
.nav { display: flex; gap: 16px; flex: 1; }
.nav a { color: var(--muted); font-weight: 500; }
.nav a:hover { color: var(--text); text-decoration: none; }

.usermenu { position: relative; }
.usermenu summary { list-style: none; cursor: pointer; }
.usermenu summary::-webkit-details-marker { display: none; }
.usermenu-panel {
  position: absolute; right: 0; top: calc(100% + 8px); min-width: 220px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px; box-shadow: 0 8px 24px rgba(0,0,0,.12); display: grid; gap: 8px;
}
.usermenu-name { font-weight: 600; }

.avatar {
  flex: none; width: 34px; height: 34px; border-radius: 50%;
  display: inline-grid; place-items: center;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--border);
  font-size: .8rem; font-weight: 700;
}

/* Componenti */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 8px 14px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit; font-weight: 500;
  cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-ghost { border-color: transparent; background: transparent; color: var(--muted); }
.btn-block { width: 100%; }
.btn-google { width: 100%; padding: 10px 14px; background: var(--surface); border: 1px solid var(--border); color: var(--text); font: inherit; font-weight: 600; border-radius: 8px; display: flex; gap: 10px; justify-content: center; align-items: center; cursor: pointer; }
.btn-google:hover { background: var(--surface-2); }
.linklike { background: none; border: 0; padding: 0; color: var(--primary); font: inherit; cursor: pointer; text-align: left; }

.badge {
  display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: .78rem; font-weight: 600;
  background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.status-aperto { background: var(--s-open-bg); color: var(--s-open-fg); }
.status-in_lavorazione { background: var(--s-progress-bg); color: var(--s-progress-fg); }
.status-in_attesa { background: var(--s-waiting-bg); color: var(--s-waiting-fg); }
.status-risolto { background: var(--s-resolved-bg); color: var(--s-resolved-fg); }
.status-chiuso { background: var(--s-closed-bg); color: var(--s-closed-fg); }
.badge[class*="prio-"] { background: transparent; padding-left: 0; }
.badge[class*="prio-"]::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; vertical-align: 1px; background: currentColor; }
.prio-1 { color: var(--p-low); }
.prio-2 { color: var(--p-normal); }
.prio-3 { color: var(--p-high); }
.prio-4 { color: var(--p-urgent); }
.ticket-head .badge[class*="prio-"] { background: var(--surface-2); padding-left: 9px; }
.badge-staff { background: var(--info-bg); color: var(--info-fg); font-size: .7rem; }

.alert { padding: 10px 14px; border-radius: 8px; margin-bottom: 12px; background: var(--info-bg); color: var(--info-fg); }
.alert-success { background: var(--success-bg); color: var(--success-fg); }
.alert-error { background: var(--error-bg); color: var(--error-fg); }

/* Form */
.field { margin-bottom: 16px; }
.field label { display: block; font-weight: 600; margin-bottom: 6px; font-size: .9rem; }
.field .muted { font-weight: 400; }
input[type=text], input[type=email], input[type=password], input[type=search], select, textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text); font: inherit;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--focus); }
input[type=file] { font: inherit; font-size: .9rem; max-width: 100%; }
.help { color: var(--muted); font-size: .82rem; margin-top: 4px; }
.error { color: var(--danger); font-size: .85rem; margin-top: 4px; }
.has-error input, .has-error textarea, .has-error select { border-color: var(--danger); }
.checkbox { display: flex; align-items: center; gap: 8px; margin-bottom: 16px; font-size: .9rem; }
.actions { display: flex; justify-content: flex-end; gap: 8px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }
.back { display: inline-block; margin-bottom: 12px; color: var(--muted); font-size: .9rem; }

/* Login */
.auth-card { padding: 28px; }
.auth-brand { display: flex; align-items: center; gap: 10px; color: var(--primary); margin-bottom: 4px; }
.auth-brand h1 { margin: 0; color: var(--text); }
.auth-card > .muted { margin: 0 0 20px; }
.divider { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: .8rem; margin: 18px 0; }
.divider::before, .divider::after { content: ""; flex: 1; border-top: 1px solid var(--border); }

/* Lista */
.page-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; overflow-x: auto; overflow-y: hidden; scrollbar-width: none; }
.tab { padding: 8px 12px; color: var(--muted); font-weight: 500; border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap; }
.tab:hover { color: var(--text); text-decoration: none; }
.tab.active { color: var(--text); border-bottom-color: var(--primary); }
.tab .count { font-size: .75rem; background: var(--surface-2); border-radius: 999px; padding: 1px 7px; margin-left: 4px; }
.filters { display: flex; gap: 8px; margin-bottom: 16px; flex-wrap: wrap; }
.filters input[type=search] { flex: 1 1 260px; }
.filters select { width: auto; flex: 0 1 auto; }
.table-card { padding: 0; overflow: hidden; }
table.tickets { width: 100%; border-collapse: collapse; }
.tickets th { text-align: left; font-size: .75rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; padding: 10px 16px; background: var(--surface-2); border-bottom: 1px solid var(--border); }
.tickets td { padding: 12px 16px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.tickets tbody tr:last-child td { border-bottom: 0; }
.tickets tbody tr { cursor: pointer; }
.tickets tbody tr:hover { background: var(--surface-2); }
.ticket-title { color: var(--text); font-weight: 600; }
.col-id { width: 56px; }
.mobile-only { display: none; }
.col-date { white-space: nowrap; }
.empty { text-align: center; padding: 40px 20px; }
.empty p { margin: 0 0 8px; }
.empty .btn { margin-top: 8px; }
.pagination { display: flex; justify-content: center; align-items: center; gap: 16px; margin-top: 16px; }

/* Dettaglio */
.detail { display: grid; grid-template-columns: minmax(0, 1fr) 280px; gap: 24px; align-items: start; }
.detail-side { display: grid; gap: 16px; position: sticky; top: 76px; }
.ticket-head { margin-bottom: 20px; }
.ticket-head h1 { overflow-wrap: anywhere; }
.badges { display: flex; gap: 6px; flex-wrap: wrap; }
.thread { list-style: none; margin: 0 0 20px; padding: 0; display: grid; gap: 14px; }
.msg { display: flex; gap: 12px; align-items: flex-start; }
.msg-body { flex: 1; min-width: 0; padding: 14px 16px; }
.msg-staff .msg-body { border-left: 3px solid var(--primary); }
.msg-staff .avatar { background: var(--info-bg); color: var(--info-fg); }
.msg-meta { font-size: .9rem; margin-bottom: 6px; }
.msg-text { overflow-wrap: anywhere; }
.msg-text p { margin: 0 0 .6em; }
.msg-text p:last-child { margin-bottom: 0; }
.attachments { list-style: none; margin: 10px 0 0; padding: 10px 0 0; border-top: 1px dashed var(--border); display: grid; gap: 4px; font-size: .9rem; }
.reply-row { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }
.facts { margin: 0; display: grid; gap: 2px; font-size: .9rem; }
.facts dt { color: var(--muted); font-size: .78rem; margin-top: 8px; }
.facts dt:first-child { margin-top: 0; }
.facts dd { margin: 0; overflow-wrap: anywhere; }

@media (max-width: 800px) {
  .detail { grid-template-columns: 1fr; }
  .detail-side { position: static; }
  .reply-row, .grid-2 { grid-template-columns: 1fr; }
  .nav { display: none; }
  .topbar-inner { justify-content: space-between; }
  .tickets thead { display: none; }
  .tickets tr { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; padding: 12px 16px; border-bottom: 1px solid var(--border); }
  .tickets tbody tr:last-child { border-bottom: 0; }
  .tickets td { padding: 0; border: 0; font-size: .85rem; }
  .tickets td.col-id { display: none; }
  .mobile-only { display: inline; }
  .tickets td:nth-child(2) { flex: 1 1 100%; font-size: inherit; }
}
