:root {
  /* Surfaces */
  --bg: #fafafa;
  --bg-elev: #ffffff;
  --bg-elev-2: #f4f4f5;
  --bg-elev-3: #e8e8ec;

  /* Lines */
  --border: #e5e7eb;
  --border-strong: #d1d5db;

  /* Text */
  --fg: #18181b;
  --fg-muted: #6b7280;
  --fg-dim: #9ca3af;

  /* Accent (indigo) */
  --accent: #4f46e5;
  --accent-hover: #4338ca;
  --accent-fg: #ffffff;
  --accent-bg: #eef2ff;
  --accent-border: #c7d2fe;

  /* States */
  --success: #15803d;
  --success-bg: #dcfce7;
  --success-border: #bbf7d0;
  --warning: #b45309;
  --warning-bg: #fef3c7;
  --warning-border: #fde68a;
  --danger: #b91c1c;
  --danger-bg: #fee2e2;
  --danger-border: #fecaca;
  --info: #1d4ed8;
  --info-bg: #dbeafe;
  --info-border: #bfdbfe;

  /* Action category colors */
  --replied: #15803d;
  --replied-bg: #dcfce7;
  --draft: #1d4ed8;
  --draft-bg: #dbeafe;
  --escalated: #b45309;
  --escalated-bg: #fef3c7;
  --ignored: #6b7280;
  --ignored-bg: #f3f4f6;

  /* Tokens */
  --radius: 8px;
  --radius-lg: 12px;
  --radius-xl: 16px;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow-md: 0 1px 3px rgba(15, 23, 42, 0.05), 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow-lg: 0 4px 16px -2px rgba(15, 23, 42, 0.06), 0 2px 4px -1px rgba(15, 23, 42, 0.04);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Inter", "SF Pro Text",
    "Segoe UI", Roboto, system-ui, sans-serif;
  font-size: 14px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; text-decoration-color: rgba(79, 70, 229, 0.4); text-underline-offset: 3px; }

code, .mono {
  font-family: "SF Mono", "Fira Code", "JetBrains Mono", ui-monospace, Menlo, Consolas, monospace;
  font-size: 12.5px;
}

/* ---------- layout ---------- */
.app {
  display: grid;
  grid-template-columns: 232px 1fr;
  min-height: 100vh;
}

.sidebar {
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  padding: 22px 14px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  position: sticky;
  top: 0;
  height: 100vh;
}

.sidebar .brand {
  font-weight: 600;
  letter-spacing: -0.01em;
  font-size: 15px;
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 0 8px;
}
.sidebar .brand .dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--accent);
}

.nav { display: flex; flex-direction: column; gap: 1px; }
.nav a {
  display: flex; align-items: center; gap: 10px;
  padding: 7px 10px;
  border-radius: 6px;
  color: var(--fg-muted);
  font-size: 13.5px;
  font-weight: 500;
  transition: background 0.12s, color 0.12s;
}
.nav a:hover { background: var(--bg-elev-2); color: var(--fg); text-decoration: none; }
.nav a.active { background: var(--accent-bg); color: var(--accent); }
.nav a.active svg { color: var(--accent); }
.nav a svg { width: 16px; height: 16px; opacity: 0.85; flex: none; }

.sidebar-footer {
  margin-top: auto;
  padding-top: 14px;
  border-top: 1px solid var(--border);
  font-size: 12.5px;
  color: var(--fg-muted);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}
.sidebar-footer span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sidebar-footer a { color: var(--fg-muted); }
.sidebar-footer a:hover { color: var(--fg); }

.main { padding: 32px 36px; min-width: 0; }

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 22px;
  gap: 16px;
}
.page-header h1 {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
  letter-spacing: -0.018em;
  color: var(--fg);
}
.page-header .sub { color: var(--fg-muted); font-size: 13px; margin-top: 4px; }

/* ---------- pills / status ---------- */
.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 500;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  color: var(--fg-muted);
}
.pill .pulse {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--fg-dim);
}
.pill.ok    { color: var(--success); background: var(--success-bg); border-color: var(--success-border); }
.pill.ok    .pulse { background: var(--success); }
.pill.warn  { color: var(--warning); background: var(--warning-bg); border-color: var(--warning-border); }
.pill.warn  .pulse { background: var(--warning); }
.pill.err   { color: var(--danger);  background: var(--danger-bg);  border-color: var(--danger-border); }
.pill.err   .pulse { background: var(--danger); }
.pill.info  { color: var(--info);    background: var(--info-bg);    border-color: var(--info-border); }
.pill.info  .pulse { background: var(--info); }
.pill.muted { color: var(--fg-muted); }

.role-badge {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 6px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  border: 1px solid transparent;
}
.role-admin   { color: var(--accent);  background: var(--accent-bg);  border-color: var(--accent-border); }
.role-user    { color: var(--success); background: var(--success-bg); border-color: var(--success-border); }
.role-pending { color: var(--warning); background: var(--warning-bg); border-color: var(--warning-border); }

.action-badge {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 5px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: uppercase;
}
.action-replied_sent  { color: var(--replied);   background: var(--replied-bg); }
.action-replied_draft { color: var(--draft);     background: var(--draft-bg); }
.action-escalated     { color: var(--escalated); background: var(--escalated-bg); }
.action-ignored       { color: var(--ignored);   background: var(--ignored-bg); }

.sentiment-angry      { color: var(--danger);  font-weight: 500; }
.sentiment-frustrated { color: var(--warning); font-weight: 500; }
.sentiment-positive   { color: var(--success); font-weight: 500; }
.sentiment-neutral    { color: var(--fg-muted); }

/* ---------- cards / metrics ---------- */
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
  margin-bottom: 22px;
}
.card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 18px 20px;
  box-shadow: var(--shadow-sm);
}
.card h3 {
  margin: 0 0 8px 0;
  font-size: 11.5px;
  font-weight: 500;
  color: var(--fg-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.card .big {
  font-size: 28px;
  font-weight: 600;
  letter-spacing: -0.022em;
  display: flex;
  align-items: baseline;
  gap: 8px;
  color: var(--fg);
}
.card .big small { font-size: 12px; color: var(--fg-muted); font-weight: 500; }
.card .big.ok    { color: var(--success); }
.card .big.warn  { color: var(--warning); }
.card .big.muted { color: var(--fg-muted); }

.section {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  margin-bottom: 22px;
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}
.section-header {
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.section-header h2 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: -0.005em;
}
.section-body { padding: 0; }
.section-empty { padding: 60px 24px; text-align: center; color: var(--fg-muted); }

/* ---------- chart ---------- */
.chart {
  padding: 20px;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  height: 180px;
}
.bar-col {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  min-width: 0;
}
.bar-stack {
  width: 100%;
  display: flex;
  flex-direction: column-reverse;
  background: var(--bg-elev-2);
  border-radius: 6px;
  overflow: hidden;
  height: 130px;
  border: 1px solid var(--border);
}
.bar-seg { width: 100%; transition: opacity 0.15s; }
.bar-seg.replied   { background: var(--replied); }
.bar-seg.draft     { background: var(--draft); }
.bar-seg.escalated { background: var(--escalated); }
.bar-seg.ignored   { background: var(--ignored); }
.bar-col .lbl { font-size: 11px; color: var(--fg-muted); }

.legend {
  display: flex;
  gap: 16px;
  padding: 0 20px 18px;
  font-size: 12px;
  color: var(--fg-muted);
}
.legend .swatch {
  display: inline-block;
  width: 8px; height: 8px;
  border-radius: 2px;
  margin-right: 6px;
}

/* ---------- tables ---------- */
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
table th {
  text-align: left;
  font-weight: 500;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--fg-muted);
  padding: 10px 16px;
  background: var(--bg-elev-2);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0;
}
table td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
  color: var(--fg);
}
table tr:last-child td { border-bottom: 0; }
table tr.clickable { cursor: pointer; transition: background 0.12s; }
table tr.clickable:hover td { background: var(--bg-elev-2); }
.col-mono { font-family: "SF Mono", ui-monospace, Menlo, Consolas, monospace; font-size: 12.5px; color: var(--fg-muted); }
.truncate { max-width: 360px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- buttons / forms ---------- */
button, .btn {
  font: inherit;
  font-weight: 500;
  padding: 7px 14px;
  border-radius: 7px;
  border: 1px solid var(--border-strong);
  background: var(--bg-elev);
  color: var(--fg);
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s, transform 0.05s, box-shadow 0.12s;
  box-shadow: var(--shadow-sm);
}
button:hover, .btn:hover {
  background: var(--bg-elev-2);
  border-color: var(--border-strong);
  text-decoration: none;
}
button:active { transform: translateY(1px); box-shadow: none; }
button.primary, .btn.primary {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: var(--accent);
  font-weight: 600;
  box-shadow: 0 1px 2px rgba(79, 70, 229, 0.2);
}
button.primary:hover, .btn.primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
  color: var(--accent-fg);
}
button.danger { color: var(--danger); border-color: var(--danger-border); background: var(--danger-bg); }
button.danger:hover { background: var(--danger); border-color: var(--danger); color: #fff; }
button.small { padding: 4px 10px; font-size: 12px; border-radius: 6px; }

input[type="email"], input[type="text"], input[type="number"],
input[type="password"], input[type="search"], input[type="date"],
input[type="datetime-local"], select, textarea {
  font: inherit;
  width: 100%;
  padding: 9px 12px;
  border-radius: 7px;
  border: 1px solid var(--border-strong);
  background: var(--bg-elev);
  color: var(--fg);
  outline: none;
  transition: border-color 0.12s, box-shadow 0.12s;
}
input:focus, select:focus, textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.15);
}
label {
  display: block;
  font-size: 11.5px;
  color: var(--fg-muted);
  margin: 12px 0 6px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 500;
}

.form-actions {
  display: flex;
  gap: 8px;
  margin-top: 16px;
  align-items: center;
}
form.inline { display: inline; }

/* ---------- auth ---------- */
.auth-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(circle at 25% 0%, rgba(79, 70, 229, 0.05), transparent 60%),
    radial-gradient(circle at 75% 100%, rgba(29, 78, 216, 0.04), transparent 60%),
    var(--bg);
}
.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 32px;
  box-shadow: var(--shadow-lg);
}
.auth-card .brand {
  display: flex; align-items: center; gap: 9px;
  margin-bottom: 26px;
  font-weight: 600;
  font-size: 15px;
}
.auth-card .brand .dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--accent);
}
.auth-card h1 {
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -0.01em;
  margin: 0 0 6px 0;
}
.auth-card .lead {
  color: var(--fg-muted);
  font-size: 13.5px;
  margin: 0 0 20px 0;
}
.auth-card .small-link {
  margin-top: 18px;
  font-size: 13px;
  color: var(--fg-muted);
}
.auth-card .alert {
  margin-bottom: 14px;
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 13px;
  background: var(--warning-bg);
  border: 1px solid var(--warning-border);
  color: var(--warning);
}
.auth-card .alert.danger {
  background: var(--danger-bg);
  border-color: var(--danger-border);
  color: var(--danger);
}
.auth-card .alert.info {
  background: var(--info-bg);
  border-color: var(--info-border);
  color: var(--info);
}

.code-input {
  letter-spacing: 0.4em;
  font-family: "SF Mono", ui-monospace, Menlo, Consolas, monospace;
  text-align: center;
  font-size: 18px;
  padding: 14px 12px;
}

/* ---------- inbox ---------- */
.inbox {
  display: grid;
  grid-template-columns: 380px 1fr;
  gap: 14px;
  min-height: calc(100vh - 110px);
}
.inbox-list {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-sm);
}
.inbox-tabs {
  display: flex;
  gap: 2px;
  padding: 8px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elev-2);
  overflow-x: auto;
}
.inbox-tabs a {
  padding: 5px 11px;
  border-radius: 6px;
  color: var(--fg-muted);
  font-size: 12.5px;
  font-weight: 500;
  white-space: nowrap;
  border: 1px solid transparent;
  transition: background 0.12s, color 0.12s;
}
.inbox-tabs a:hover { background: rgba(0,0,0,0.04); text-decoration: none; color: var(--fg); }
.inbox-tabs a.active { background: var(--bg-elev); color: var(--fg); border-color: var(--border); box-shadow: var(--shadow-sm); }
.inbox-tabs .badge {
  display: inline-block;
  margin-left: 5px;
  padding: 0 6px;
  border-radius: 8px;
  background: var(--bg-elev-3);
  font-size: 11px;
  color: var(--fg-muted);
}
.inbox-tabs a.active .badge { background: var(--accent-bg); color: var(--accent); }
.inbox-rows { overflow-y: auto; flex: 1; }
.inbox-row {
  display: block;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  color: inherit;
  position: relative;
  border-left: 2px solid transparent;
  transition: background 0.12s, border-left-color 0.12s;
}
.inbox-row:hover { background: var(--bg-elev-2); text-decoration: none; }
.inbox-row.unread { border-left-color: var(--accent); }
.inbox-row.selected { background: var(--accent-bg); border-left-color: var(--accent); }
.inbox-row .from {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  font-weight: 500;
  margin-bottom: 2px;
  gap: 8px;
}
.inbox-row .when { color: var(--fg-muted); font-size: 11.5px; flex: none; font-weight: 400; }
.inbox-row .subject { font-size: 13px; margin-bottom: 3px; color: var(--fg); }
.inbox-row .snippet { color: var(--fg-muted); font-size: 12px; line-height: 1.4; }
.inbox-row .pills { margin-top: 6px; display: flex; gap: 5px; flex-wrap: wrap; align-items: center; }
.inbox-row .order-tag {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 11px; color: var(--fg-muted);
  padding: 1px 7px;
  border-radius: 5px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
}

.inbox-detail {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  display: grid;
  grid-template-columns: 1fr 320px;
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}
.inbox-detail .pane {
  padding: 24px 28px;
  overflow-y: auto;
}
.inbox-detail .pane.right {
  background: var(--bg);
  border-left: 1px solid var(--border);
  padding: 22px 20px;
  font-size: 12.5px;
}
.detail-header { margin-bottom: 18px; }
.detail-header h1 {
  margin: 0 0 6px 0;
  font-size: 18px;
  font-weight: 600;
  letter-spacing: -0.01em;
}
.detail-meta {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  font-size: 12px;
  color: var(--fg-muted);
  margin-bottom: 14px;
}
.detail-from { color: var(--fg-muted); font-size: 13px; margin-bottom: 14px; font-family: "SF Mono", ui-monospace, Menlo, monospace; }
.detail-body {
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px 18px;
  white-space: pre-wrap;
  font-size: 13.5px;
  line-height: 1.6;
  margin-bottom: 22px;
  max-height: 360px;
  overflow-y: auto;
  color: var(--fg);
}

/* iMessage-style conversation thread */
.thread {
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin-bottom: 24px;
  padding: 4px 0;
}
.bubble {
  max-width: 78%;
  display: flex;
  flex-direction: column;
}
.bubble.them { align-self: flex-start; }
.bubble.us   { align-self: flex-end; }
.bubble-meta {
  font-size: 11px;
  color: var(--fg-muted);
  margin-bottom: 5px;
  padding: 0 10px;
  display: flex;
  gap: 8px;
  align-items: baseline;
}
.bubble.us .bubble-meta { justify-content: flex-end; }
.bubble-name { font-weight: 500; color: var(--fg); }
.bubble-time { color: var(--fg-dim); font-size: 10.5px; }
.bubble-body {
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  font-size: 14px;
  line-height: 1.5;
  padding: 10px 14px;
  border-radius: 18px;
}
.bubble.them .bubble-body {
  background: var(--bg-elev-2);
  color: var(--fg);
  border: 1px solid var(--border);
  border-bottom-left-radius: 6px;
}
.bubble.us .bubble-body {
  background: var(--accent);
  color: #fff;
  border: 1px solid var(--accent);
  border-bottom-right-radius: 6px;
}
.draft-section h3 {
  margin: 0 0 8px 0;
  font-size: 13px;
  font-weight: 600;
  color: var(--fg);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}
.draft-section textarea {
  font: inherit;
  width: 100%;
  min-height: 220px;
  padding: 14px;
  background: var(--bg-elev);
  color: var(--fg);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  resize: vertical;
  outline: none;
  line-height: 1.6;
  box-shadow: var(--shadow-sm);
}
.draft-section textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.15);
}
.draft-actions {
  display: flex;
  gap: 8px;
  margin-top: 12px;
  flex-wrap: wrap;
  align-items: center;
}
.classifier-row {
  display: grid;
  grid-template-columns: 110px 1fr;
  gap: 8px 14px;
  margin-bottom: 14px;
  font-size: 12.5px;
}
.classifier-row dt { color: var(--fg-muted); }
.classifier-row dd { margin: 0; color: var(--fg); }
.classifier-row code { color: var(--accent); background: var(--accent-bg); padding: 1px 6px; border-radius: 4px; font-size: 12px; }

.order-panel { font-size: 12.5px; }
.order-panel h3 {
  margin: 0 0 12px 0;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--fg-muted);
  font-weight: 500;
}
.order-panel .ord-name {
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.005em;
  margin-bottom: 6px;
}
.order-panel .ord-status { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 14px; }
.order-panel .ord-line {
  margin-bottom: 6px;
  padding-left: 12px;
  position: relative;
  color: var(--fg);
}
.order-panel .ord-line::before {
  content: "";
  position: absolute;
  left: 0; top: 8px;
  width: 4px; height: 4px;
  border-radius: 50%;
  background: var(--fg-dim);
}
.order-panel .ord-block {
  padding: 12px 0;
  border-top: 1px solid var(--border);
  margin-top: 12px;
}
.order-panel .ord-block:first-child { border-top: 0; padding-top: 0; }
.order-panel .ord-label {
  text-transform: uppercase;
  font-size: 10.5px;
  letter-spacing: 0.05em;
  color: var(--fg-muted);
  margin-bottom: 5px;
}
.order-panel .ord-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 12px;
  padding: 7px 12px;
  border-radius: 7px;
  border: 1px solid var(--border-strong);
  background: var(--bg-elev);
  color: var(--fg);
  font-size: 12px;
  font-weight: 500;
  box-shadow: var(--shadow-sm);
}
.order-panel .ord-link:hover { background: var(--bg-elev-2); text-decoration: none; }

.empty-detail {
  display: grid;
  place-items: center;
  color: var(--fg-muted);
  font-size: 13px;
  height: 100%;
  padding: 60px 20px;
  text-align: center;
}
.empty-detail code {
  background: var(--bg-elev-2);
  padding: 1px 6px;
  border-radius: 4px;
  border: 1px solid var(--border);
}

/* ---------- toggle / settings ---------- */
.toggle-group {
  display: flex;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 3px;
  width: fit-content;
}
.toggle-group label {
  margin: 0;
  padding: 7px 14px;
  font-size: 12.5px;
  font-weight: 500;
  text-transform: none;
  letter-spacing: 0;
  color: var(--fg-muted);
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.12s, color 0.12s;
}
.toggle-group input { display: none; }
.toggle-group label:has(input:checked) {
  background: var(--bg-elev);
  color: var(--fg);
  box-shadow: var(--shadow-sm);
}
.toggle-group label:hover { color: var(--fg); }

input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

.field-grid {
  display: grid;
  grid-template-columns: 240px 1fr;
  gap: 24px;
  margin-bottom: 24px;
  align-items: start;
}
.field-grid h4 {
  margin: 0 0 4px 0;
  font-size: 13px;
  font-weight: 600;
  color: var(--fg);
}
.field-grid p {
  margin: 0;
  color: var(--fg-muted);
  font-size: 12.5px;
  line-height: 1.55;
}

/* ---------- catalog ---------- */
.search-bar {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}
.search-bar input { flex: 1; }
.search-bar select { width: auto; min-width: 120px; }

.product-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 14px;
}
.product-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  text-decoration: none;
  color: inherit;
  transition: border-color 0.15s, box-shadow 0.15s, transform 0.05s;
  box-shadow: var(--shadow-sm);
}
.product-card:hover {
  border-color: var(--border-strong);
  box-shadow: var(--shadow-md);
  text-decoration: none;
  transform: translateY(-1px);
}
.product-card .img {
  background: var(--bg-elev-2);
  aspect-ratio: 1 / 1;
  display: grid;
  place-items: center;
  overflow: hidden;
}
.product-card .img img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.product-card .img.empty {
  color: var(--fg-dim);
  font-size: 32px;
}
.product-card .body {
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.product-card .body .title {
  font-size: 13px;
  font-weight: 500;
  letter-spacing: -0.005em;
  line-height: 1.35;
  color: var(--fg);
}
.product-card .body .meta {
  font-size: 11.5px;
  color: var(--fg-muted);
  display: flex;
  gap: 8px;
  align-items: center;
}

.status-active   { color: var(--success); font-weight: 500; }
.status-draft    { color: var(--warning); font-weight: 500; }
.status-archived { color: var(--fg-dim); }

.product-detail {
  display: grid;
  grid-template-columns: 1fr 320px;
  gap: 22px;
}
.product-detail .gallery {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-sm);
}
.product-detail .gallery .main-img {
  background: var(--bg-elev-2);
  aspect-ratio: 4 / 3;
  display: grid;
  place-items: center;
}
.product-detail .gallery .main-img img { max-width: 100%; max-height: 100%; object-fit: contain; }
.product-detail .gallery .thumbs {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(70px, 1fr));
  gap: 6px;
  padding: 10px;
  border-top: 1px solid var(--border);
  background: var(--bg-elev-2);
}
.product-detail .gallery .thumbs img {
  width: 100%; aspect-ratio: 1 / 1;
  object-fit: cover; border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg-elev);
}
.product-detail .info {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 22px;
  box-shadow: var(--shadow-sm);
}
.product-detail .desc {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 22px;
  margin-top: 14px;
  line-height: 1.65;
  color: var(--fg);
  font-size: 13.5px;
  box-shadow: var(--shadow-sm);
}
.product-detail .desc :where(p, ul, ol, h1, h2, h3) { margin: 0 0 12px 0; }
.product-detail .desc :where(h1, h2, h3) { color: var(--fg); font-weight: 600; }
.product-detail .variants {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0;
  margin-top: 14px;
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}

/* ============================================================== *
 * CRM dashboard (hero / kpi / worklist / integrations / feed)
 * ============================================================== */
.hero {
  background: linear-gradient(135deg, var(--bg-elev) 0%, var(--accent-bg) 100%);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 24px 28px;
  margin-bottom: 18px;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 24px;
  align-items: center;
  box-shadow: var(--shadow-sm);
}
.hero-eyebrow {
  font-size: 11.5px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--fg-muted);
}
.hero-greet h1 {
  margin: 4px 0 8px;
  font-size: 24px;
  letter-spacing: -0.018em;
  font-weight: 600;
}
.hero-status {
  margin: 0;
  font-size: 14px;
  color: var(--fg);
}
.hero-status .hero-sub { color: var(--fg-muted); font-weight: 400; }
.hero-status.ok::before     { content: "●"; color: var(--success); margin-right: 8px; }
.hero-status.warn::before   { content: "●"; color: var(--warning); margin-right: 8px; }
.hero-status.danger::before { content: "●"; color: var(--danger);  margin-right: 8px; }
.hero-side { display: flex; flex-direction: column; gap: 12px; align-items: flex-end; }
.hero-persona {
  display: flex; align-items: center; gap: 10px;
  background: var(--bg-elev);
  padding: 8px 12px 8px 8px;
  border-radius: 40px;
  border: 1px solid var(--border);
}
.hero-persona .avatar {
  width: 36px; height: 36px; border-radius: 50%;
  background: linear-gradient(135deg, var(--accent) 0%, #8b5cf6 100%);
  color: #fff;
  display: grid; place-items: center;
  font-weight: 600; font-size: 14px;
  letter-spacing: 0.02em;
}
.persona-name { font-weight: 600; font-size: 13px; }
.persona-role { font-size: 11.5px; color: var(--fg-muted); }
.hero-pills { display: flex; gap: 6px; }

/* KPI strip */
.kpis {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 12px;
  margin-bottom: 18px;
}
.kpi {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px 16px;
  box-shadow: var(--shadow-sm);
}
.kpi-label {
  font-size: 11px;
  color: var(--fg-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 8px;
  font-weight: 500;
}
.kpi-value {
  font-size: 26px;
  font-weight: 600;
  letter-spacing: -0.02em;
  line-height: 1.1;
}
.kpi-value.ok    { color: var(--success); }
.kpi-value.warn  { color: var(--warning); }
.kpi-value.muted { color: var(--fg-muted); }
.kpi-foot {
  margin-top: 6px;
  font-size: 11.5px;
  color: var(--fg-muted);
  display: flex; align-items: center; gap: 6px;
}
.delta { font-weight: 600; font-size: 11px; }
.delta.up    { color: var(--success); }
.delta.down  { color: var(--danger);  }
.delta.flat  { color: var(--fg-dim);  }

/* Two-column dashboard grid */
.dash-grid {
  display: grid;
  grid-template-columns: 1.6fr 1fr;
  gap: 16px;
  margin-bottom: 18px;
}
.dash-grid:last-of-type { grid-template-columns: 1fr 1fr; }
.dash-side { display: flex; flex-direction: column; gap: 16px; }
.dash-block {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}
.dash-block-head {
  padding: 14px 18px 10px;
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px;
  border-bottom: 1px solid var(--border);
}
.dash-block-head h2 {
  margin: 0; font-size: 14px; font-weight: 600;
  letter-spacing: -0.005em;
}
.dash-block-head .sub { color: var(--fg-muted); font-size: 12px; }

/* Worklist tiles */
.worklist {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 10px;
  padding: 14px;
}
.work-tile {
  display: grid;
  grid-template-columns: 36px 1fr 16px;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--fg);
  transition: background .15s, border-color .15s, transform .15s;
}
.work-tile:hover {
  background: var(--accent-bg);
  border-color: var(--accent-border);
  text-decoration: none;
  transform: translateY(-1px);
}
.work-tile .work-icon  { font-size: 22px; line-height: 1; text-align: center; }
.work-tile .work-count {
  font-size: 22px; font-weight: 600; letter-spacing: -0.02em;
  color: var(--fg-muted);
}
.work-tile.has .work-count   { color: var(--fg); }
.work-tile.alert.has .work-count { color: var(--danger); }
.work-tile.alert.has         { border-color: var(--danger-border, #fecaca); background: #fff5f5; }
.work-tile .work-label { font-size: 12.5px; color: var(--fg-muted); margin-top: 2px; }
.work-tile .work-foot  { font-size: 11px; margin-top: 4px; }
.work-tile .work-foot.ok    { color: var(--success); }
.work-tile .work-foot.warn  { color: var(--warning); }
.work-tile .work-arrow {
  color: var(--fg-dim);
  font-weight: 600;
  transition: transform .15s, color .15s;
}
.work-tile:hover .work-arrow { color: var(--accent); transform: translateX(2px); }

/* Category chips */
.cat-strip { padding: 0 18px 16px; }
.cat-strip .sub { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--fg-muted); margin-bottom: 6px; }
.cat-list { display: flex; gap: 6px; flex-wrap: wrap; }
.cat-chip {
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 12px;
  color: var(--fg-muted);
}
.cat-chip b { color: var(--fg); margin-left: 4px; font-weight: 600; }

/* Integrations panel */
.integ-grid { padding: 6px 0; }
.integ-row {
  display: grid;
  grid-template-columns: 12px 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 8px 18px;
  font-size: 13px;
}
.integ-row + .integ-row { border-top: 1px solid var(--border); }
.integ-row .dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--fg-dim);
}
.integ-row.ok  .dot { background: var(--success); box-shadow: 0 0 0 3px rgba(34,197,94,0.15); }
.integ-row.off .dot { background: var(--danger);  box-shadow: 0 0 0 3px rgba(239,68,68,0.12); }
/* Optional-but-not-configured: visually muted (gray dot), so it
   doesn't compete for attention with a real "broken required" red. */
.integ-row.opt .dot { background: #cbd5e1;        box-shadow: 0 0 0 3px rgba(148,163,184,0.18); }
.integ-row[title] { cursor: help; }
.integ-name { font-weight: 500; }
.integ-hint { color: var(--fg-muted); font-size: 11.5px; }

/* Poll progress: inline spinner shown during "Esegui ora" so the
   operator knows the click did something even when the poll takes a
   few seconds to talk to Gmail. */
.poll-progress {
  display: flex; align-items: center; gap: 8px;
  font-size: 12.5px; color: var(--fg-muted);
  margin-top: 4px;
}
.poll-spinner {
  width: 12px; height: 12px; border-radius: 50%;
  border: 2px solid rgba(99,102,241,0.25);
  border-top-color: var(--accent, #6366f1);
  animation: poll-spin 0.8s linear infinite;
  display: inline-block;
}
@keyframes poll-spin { to { transform: rotate(360deg); } }

/* Poll info */
.poll-info {
  padding: 12px 18px 16px;
  font-size: 12.5px;
  display: flex; flex-direction: column; gap: 6px;
}
.poll-info .sub { color: var(--fg-muted); margin-right: 4px; }
.poll-err { color: var(--danger); font-size: 12px; }

/* Live feed */
.feed { list-style: none; margin: 0; padding: 4px 0; }
.feed-row {
  display: grid;
  grid-template-columns: 70px 18px 1fr;
  gap: 10px;
  align-items: baseline;
  padding: 9px 18px;
  font-size: 13px;
  border-top: 1px solid var(--border);
}
.feed-row:first-child { border-top: none; }
.feed-time { font-size: 11.5px; color: var(--fg-muted); }
.feed-icon { color: var(--fg-muted); display: flex; align-items: center; }
.feed-text { min-width: 0; }
.feed-action b { font-size: 12.5px; }
.feed-action b.ok     { color: var(--success); }
.feed-action b.warn   { color: var(--warning); }
.feed-action b.danger { color: var(--danger); }
.feed-action b.muted  { color: var(--fg-muted); }
.feed-action b.wa     { color: #25D366; }
.feed-who  { font-weight: 500; margin-left: 6px; }
.feed-what { color: var(--fg-muted); margin-left: 4px; }
.feed-tag {
  display: inline-block;
  margin-left: 6px;
  padding: 0 6px;
  font-size: 11px;
  background: var(--bg-elev-2);
  border-radius: 999px;
  color: var(--fg-muted);
}
.feed-tag.danger { background: #fee2e2; color: var(--danger); }
.feed-empty { padding: 32px 18px; color: var(--fg-muted); text-align: center; }

/* Maintenance collapsible */
.maint {
  margin-top: 8px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
}
.maint > summary {
  cursor: pointer;
  padding: 12px 18px;
  font-size: 13px;
  font-weight: 500;
  color: var(--fg-muted);
  list-style: none;
}
.maint > summary::after { content: " ▾"; }
.maint[open] > summary::after { content: " ▴"; }
.maint-body { padding: 4px 18px 16px; display: flex; flex-direction: column; gap: 12px; }
.maint-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.maint-diag {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 10px;
  font-size: 12.5px;
}
.maint-diag b { font-size: 16px; font-weight: 600; display: block; }
.maint-diag b.ok   { color: var(--success); }
.maint-diag b.warn { color: var(--warning); }

/* Toast banners */
.toast {
  padding: 10px 14px;
  border-radius: 8px;
  margin-bottom: 14px;
  font-size: 13px;
}
.toast.info { background: var(--info-bg); border: 1px solid var(--info-border); color: var(--info); }

/* ---------- playbook tool inventory ---------- */
.caps-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(380px, 1fr));
  gap: 12px;
}
.cap-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px 16px;
  box-shadow: var(--shadow-sm);
}
.cap-card.cap-auto  { border-left: 3px solid var(--success); }
.cap-card.cap-draft { border-left: 3px solid var(--warning); }
.cap-head {
  display: flex; justify-content: space-between; gap: 12px;
  align-items: flex-start;
  margin-bottom: 10px;
}
.cap-label { font-weight: 600; font-size: 14px; }
.cap-handler {
  display: inline-block; margin-top: 2px;
  font-size: 11.5px; color: var(--fg-muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.cap-badge {
  font-size: 10.5px;
  padding: 2px 8px;
  border-radius: 999px;
  font-weight: 600;
  letter-spacing: 0.03em;
  white-space: nowrap;
}
.cap-badge.ok    { background: #e6f4ea; color: #0f7b3a; }
.cap-badge.warn  { background: #fff4e0; color: #9a6300; }
.cap-badge.muted { background: var(--bg-elev-2); color: var(--fg-muted); }
.cap-row {
  display: grid;
  grid-template-columns: 80px 1fr;
  gap: 10px;
  font-size: 12.5px;
  padding: 4px 0;
  border-top: 1px dashed var(--border);
}
.cap-row > span { color: var(--fg-muted); font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; padding-top: 2px; }
.cap-row code, code.shopify-call {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11.5px;
  background: var(--bg-elev-2);
  padding: 1px 6px;
  border-radius: 4px;
  color: var(--fg);
}

/* ---------- responsive ---------- */
@media (max-width: 1100px) {
  .dash-grid, .dash-grid:last-of-type { grid-template-columns: 1fr; }
}
@media (max-width: 820px) {
  /* App shell: sidebar collapses to a compact horizontal icon bar
     pinned to the top. Each nav item is icon-only (label hidden) so
     all 10+ sections fit on one row and the chat / inbox content
     gets the full vertical real estate. */
  .app {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }
  .sidebar {
    position: sticky;
    top: 0;
    height: auto;
    padding: 6px 8px;
    flex-direction: row;
    align-items: center;
    gap: 8px;
    border-right: none;
    border-bottom: 1px solid var(--border);
    z-index: 20;
  }
  .sidebar .brand { font-size: 0; padding: 0; flex: none; }
  .sidebar .brand .dot { width: 10px; height: 10px; }
  .sidebar .nav {
    flex-direction: row;
    flex: 1;
    overflow-x: auto;
    overflow-y: hidden;
    flex-wrap: nowrap;
    gap: 2px;
    scrollbar-width: none;
  }
  .sidebar .nav::-webkit-scrollbar { display: none; }
  .sidebar .nav a {
    flex: none;
    padding: 7px 9px;
    font-size: 0;
    border-radius: 8px;
    position: relative;
  }
  .sidebar .nav a svg { width: 20px; height: 20px; opacity: 1; }
  /* Pill counters next to nav items shrink to a small dot on mobile
     — the label is already hidden, the operator just needs to know
     'something needs attention here'. Inline styles in the template
     set font-size:10.5px + padding which override class rules, so
     we !important everything here to flatten them to a clean dot. */
  .sidebar .nav a .pill {
    position: absolute !important;
    top: 4px !important;
    right: 4px !important;
    width: 8px !important;
    height: 8px !important;
    margin: 0 !important;
    padding: 0 !important;
    font-size: 0 !important;
    line-height: 0 !important;
    color: transparent !important;
    border-radius: 50% !important;
    border: 2px solid var(--bg-elev) !important;
    background: var(--warning) !important;
    overflow: hidden;
  }
  .sidebar .nav a .pill * { display: none !important; }
  .sidebar-footer {
    margin-top: 0;
    padding-top: 0;
    border-top: none;
    flex: none;
    font-size: 0;
  }
  .sidebar-footer a svg { width: 18px; height: 18px; }
  .sidebar-footer span { display: none; }

  /* Tighter main content */
  .main { padding: 14px 14px 24px; }
  .page-header { margin-bottom: 14px; gap: 10px; flex-wrap: wrap; }
  .page-header h1 { font-size: 19px; }
  .page-header .sub { font-size: 12px; margin-top: 2px; }

  /* Bigger touch targets across buttons/inputs */
  button, .btn, input[type=submit] { min-height: 38px; }
  .btn.small, button.small { min-height: 32px; padding: 6px 12px; font-size: 12.5px; }
  input[type=text], input[type=email], input[type=password],
  input[type=number], input[type=search], select, textarea {
    font-size: 16px;  /* prevents iOS auto-zoom on focus */
  }

  /* Grids stack */
  .inbox { grid-template-columns: 1fr; }
  .inbox-detail { grid-template-columns: 1fr; }
  .product-detail { grid-template-columns: 1fr; }
  .field-grid { grid-template-columns: 1fr; gap: 12px; }
  .hero { grid-template-columns: 1fr; }
  .hero-side { align-items: flex-start; }
  .feed-row { grid-template-columns: 60px 16px 1fr; }

  /* Dashboard tightening */
  .dash-block-head { padding: 12px 14px; }
  .dash-block-head h2 { font-size: 13.5px; }
  .integ-row { padding: 10px 14px; font-size: 13.5px; }
  .poll-info { padding: 10px 14px 14px; }

  /* KPI grid on mobile: a tight 2×N grid. Cards are compact rows
     with the label up top and the number on the right edge — same
     density as Apple Health widgets, not the giant desktop cards
     stacked vertically that the previous attempt produced. */
  .kpis {
    grid-template-columns: 1fr 1fr !important;
    gap: 6px;
    margin-bottom: 12px;
  }
  .kpi {
    padding: 8px 10px;
    border-radius: 10px;
    min-width: 0;
  }
  .kpi-label {
    font-size: 10.5px !important;
    letter-spacing: 0.04em;
    margin-bottom: 2px !important;
    line-height: 1.2;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .kpi-value { font-size: 22px !important; margin: 0 !important; }
  .kpi-foot { font-size: 10px !important; margin-top: 2px !important; }

  /* Reorder dashboard sections on mobile: Hero → 'Da gestire'
     (actionable) → KPIs (observational) → rest. The desktop order
     keeps KPIs after hero for ambient awareness, but on a phone
     the operator wants the to-do list FIRST. */
  .main { display: flex; flex-direction: column; }
  .main > .hero { order: 1; }
  .main > .dash-grid:first-of-type { order: 2; }
  .main > .kpis { order: 3; }
  .main > .dash-grid { order: 4; }

  /* Hero: trim aggressively for phone. */
  .hero { padding: 14px 14px; margin-bottom: 12px; }
  .hero-eyebrow { font-size: 10.5px; }
  .hero-greet h1 { font-size: 19px; margin: 2px 0 4px !important; }
  .hero-status { font-size: 12.5px; }
  .hero-side { gap: 6px; }
  .hero-persona { padding: 5px 10px 5px 5px; }
  .hero-persona .avatar { width: 28px; height: 28px; font-size: 12px; }
  .persona-name { font-size: 12px; }
  .persona-role { font-size: 10.5px; }
  .hero-pills .pill { font-size: 10.5px; padding: 2px 8px; }

  /* "Da gestire" worklist: force 2-col on phone with compact tiles
     so labels don't truncate. Removes the desktop arrow which has
     no affordance on touch. */
  .worklist {
    grid-template-columns: 1fr 1fr !important;
    gap: 8px !important;
    padding: 10px !important;
  }
  .work-tile {
    grid-template-columns: 28px 1fr !important;
    gap: 8px !important;
    padding: 10px 12px !important;
    align-items: start !important;
  }
  .work-tile .work-icon { font-size: 18px !important; }
  .work-tile .work-count { font-size: 20px !important; }
  .work-tile .work-label { font-size: 11.5px !important; line-height: 1.25; }
  .work-tile .work-arrow { display: none !important; }

  /* Compact dash-block titles + content. */
  .dash-block { border-radius: 10px; }
  .dash-block-head { padding: 10px 14px; }
  .dash-block-head h2 { font-size: 13px; }
  .dash-block-head .sub { font-size: 11px; }

  /* Categories chips stay small and tight. */
  .cat-chips { gap: 4px !important; }
  .cat-chips .chip {
    font-size: 11px !important;
    padding: 3px 8px !important;
  }

  /* Section padding shrinks too — the desktop 24px is wasteful on
     small screens. */
  .section { padding: 14px !important; }

  /* Tables become horizontally scrollable rather than overflowing. */
  table { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; }
}

@media (max-width: 360px) {
  /* Only on the smallest legacy phones (iPhone SE 1st-gen etc) we
     collapse the KPIs to a single column — modern iPhones (390pt+)
     fit two compact cards comfortably. */
  .kpis { grid-template-columns: 1fr !important; }
}
