:root {
  --bg: #0d1117;
  --bg-card: #161b22;
  --bg-card-hover: #1c2230;
  --bg-input: #0d1117;
  --border: #30363d;
  --border-hover: #484f58;
  --text: #c9d1d9;
  --text-muted: #8b949e;
  --text-strong: #f0f6fc;
  --accent: #58a6ff;
  --green: #3fb950;
  --yellow: #d29922;
  --red: #f85149;
  --brand: #ff5a8a;
  --brand-soft: rgba(255, 90, 138, 0.12);
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.45);
}

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

html, body { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Inter", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

button { font-family: inherit; cursor: pointer; }
input, select, textarea { font-family: inherit; }

/* ===== TOPBAR ===== */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 18px 32px;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
}

.brand-icon {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--brand) 0%, #ff8db3 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 22px;
  color: #fff;
  box-shadow: 0 4px 16px rgba(255, 90, 138, 0.3);
}

.brand-text h1 {
  font-size: 18px;
  font-weight: 700;
  color: var(--text-strong);
  letter-spacing: -0.2px;
}

.brand-text .subtitle {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}

.header-actions {
  display: flex;
  gap: 10px;
}

/* ===== BUTTONS ===== */
.btn {
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text);
  padding: 8px 16px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  transition: all 0.15s ease;
}

.btn:hover {
  border-color: var(--border-hover);
  background: var(--bg-card-hover);
}

.btn-primary {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
  font-weight: 600;
}

.btn-primary:hover {
  background: #ff7099;
  border-color: #ff7099;
}

.btn-ghost {
  background: transparent;
}

.btn.full {
  width: 100%;
  padding: 10px 16px;
  margin-top: 8px;
}

.btn-small {
  padding: 4px 10px;
  font-size: 12px;
}

/* ===== CONTAINER ===== */
.container {
  max-width: 1500px;
  margin: 0 auto;
  padding: 24px 32px 60px;
}

/* ===== KPI CARDS ===== */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 24px;
}

.kpi-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 18px 20px;
  position: relative;
  overflow: hidden;
}

.kpi-card::before {
  content: "";
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 3px;
  background: linear-gradient(90deg, var(--brand) 0%, var(--accent) 100%);
}

.kpi-label {
  font-size: 12px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.4px;
  margin-bottom: 8px;
}

.kpi-value {
  font-size: 32px;
  font-weight: 700;
  color: var(--text-strong);
  line-height: 1.1;
}

.kpi-value .unit {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-muted);
}

.kpi-foot {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 6px;
}

/* ===== DASHBOARD GRID ===== */
.dashboard-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  gap: 16px;
}

@media (max-width: 1100px) {
  .dashboard-grid {
    grid-template-columns: 1fr;
  }
  .kpi-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* ===== CARDS ===== */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
  gap: 12px;
}

.card-header.tight {
  padding: 14px 18px;
}

.card-header h2 {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-strong);
  letter-spacing: 0.1px;
}

.card-body {
  padding: 18px 20px;
}

/* ===== FILTERS ===== */
.filters {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.search-input,
.filter-select {
  background: var(--bg-input);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 7px 12px;
  border-radius: 6px;
  font-size: 13px;
  outline: none;
  transition: border-color 0.15s ease;
}

.search-input { min-width: 220px; }
.filter-select { min-width: 130px; cursor: pointer; }

.search-input:focus,
.filter-select:focus {
  border-color: var(--accent);
}

/* ===== QUEUE TABLE ===== */
.table-wrapper {
  overflow-x: auto;
}

.queue-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.queue-table thead th {
  text-align: left;
  padding: 12px 16px;
  background: rgba(255, 255, 255, 0.02);
  color: var(--text-muted);
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  border-bottom: 1px solid var(--border);
}

.queue-table tbody tr {
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.1s ease;
}

.queue-table tbody tr:hover {
  background: var(--bg-card-hover);
}

.queue-table td {
  padding: 14px 16px;
  vertical-align: middle;
}

.queue-table .client-name {
  color: var(--text-strong);
  font-weight: 600;
}

.queue-table .client-meta {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
}

.loading {
  text-align: center;
  padding: 32px !important;
  color: var(--text-muted);
}

.no-results {
  text-align: center;
  padding: 32px !important;
  color: var(--text-muted);
}

/* ===== BADGES ===== */
.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.2px;
  text-transform: uppercase;
}

.badge-status-in_lavorazione {
  background: rgba(88, 166, 255, 0.15);
  color: var(--accent);
}

.badge-status-qa {
  background: rgba(210, 153, 34, 0.15);
  color: var(--yellow);
}

.badge-status-delivered {
  background: rgba(63, 185, 80, 0.15);
  color: var(--green);
}

.badge-status-blocked {
  background: rgba(248, 81, 73, 0.15);
  color: var(--red);
}

.badge-priorita-alta {
  background: rgba(248, 81, 73, 0.12);
  color: var(--red);
}

.badge-priorita-media {
  background: rgba(210, 153, 34, 0.12);
  color: var(--yellow);
}

.badge-priorita-bassa {
  background: rgba(139, 148, 158, 0.12);
  color: var(--text-muted);
}

.cluster-tag {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 4px;
  background: var(--brand-soft);
  color: var(--brand);
  font-size: 11px;
  font-weight: 500;
}

/* ===== SIDEBAR ===== */
.sidebar {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.sidebar .card-body { padding: 16px 18px; }

/* ===== ROUTING FORM ===== */
.hint {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 14px;
  line-height: 1.5;
}

.route-form { display: flex; flex-direction: column; gap: 4px; }

.route-form label,
.new-client-form label {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.4px;
  margin-top: 8px;
  margin-bottom: 4px;
  display: block;
}

.route-form select,
.route-form input,
.new-client-form select,
.new-client-form input {
  background: var(--bg-input);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 13px;
  outline: none;
  width: 100%;
  transition: border-color 0.15s ease;
}

.route-form input:focus,
.route-form select:focus,
.new-client-form input:focus,
.new-client-form select:focus {
  border-color: var(--brand);
}

.route-result {
  margin-top: 16px;
  padding: 14px;
  background: var(--brand-soft);
  border: 1px solid rgba(255, 90, 138, 0.3);
  border-radius: 8px;
  font-size: 13px;
}

.route-result .target-mvp {
  color: var(--brand);
  font-weight: 700;
  font-size: 15px;
  margin-bottom: 6px;
}

.route-result .target-desc {
  color: var(--text);
  font-size: 12px;
  margin-bottom: 8px;
}

.route-result .motivazione {
  color: var(--text-muted);
  font-size: 12px;
  font-style: italic;
  line-height: 1.5;
}

/* ===== MODAL ===== */
.modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.modal.hidden { display: none; }

.modal-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(4px);
}

.modal-content {
  position: relative;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  max-width: 720px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  padding: 28px;
  box-shadow: var(--shadow);
}

.modal-close {
  position: absolute;
  top: 14px;
  right: 14px;
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 28px;
  width: 36px;
  height: 36px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.modal-close:hover {
  background: var(--bg-card-hover);
  color: var(--text-strong);
}

/* ===== CLIENT DRILL-DOWN ===== */
.drill-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 20px;
  gap: 16px;
}

.drill-title h2 {
  color: var(--text-strong);
  font-size: 22px;
  font-weight: 700;
  margin-bottom: 6px;
}

.drill-title .meta-line {
  color: var(--text-muted);
  font-size: 13px;
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
}

.drill-title .meta-line span::before {
  content: "•";
  margin-right: 8px;
  color: var(--border-hover);
}

.drill-title .meta-line span:first-child::before { content: ""; margin: 0; }

.drill-section {
  margin-top: 22px;
}

.drill-section h3 {
  font-size: 12px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 10px;
  font-weight: 600;
}

.drill-row {
  display: flex;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 12px;
}

.drill-row label {
  width: 140px;
  flex-shrink: 0;
  color: var(--text-muted);
  font-size: 12px;
  padding-top: 4px;
}

.drill-row .value {
  color: var(--text);
  font-size: 13px;
}

/* ===== TIMELINE ===== */
.timeline {
  position: relative;
  padding-left: 22px;
}

.timeline::before {
  content: "";
  position: absolute;
  left: 6px;
  top: 6px;
  bottom: 6px;
  width: 2px;
  background: var(--border);
}

.timeline-item {
  position: relative;
  padding-bottom: 14px;
}

.timeline-item::before {
  content: "";
  position: absolute;
  left: -22px;
  top: 4px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--bg-card);
  border: 2px solid var(--accent);
}

.timeline-item.tipo-milestone::before { border-color: var(--accent); }
.timeline-item.tipo-approval::before { border-color: var(--green); background: var(--green); }
.timeline-item.tipo-review::before { border-color: var(--yellow); }
.timeline-item.tipo-block::before { border-color: var(--red); background: var(--red); }
.timeline-item.tipo-escalation::before { border-color: var(--red); }
.timeline-item.tipo-carrier::before { border-color: var(--brand); background: var(--brand); }
.timeline-item.tipo-status_change::before { border-color: var(--brand); }

.timeline-item .ts {
  font-size: 11px;
  color: var(--text-muted);
}

.timeline-item .event {
  color: var(--text);
  font-size: 13px;
  margin: 2px 0 2px;
}

.timeline-item .actor {
  font-size: 11px;
  color: var(--text-muted);
  font-style: italic;
}

/* ===== DELIVERABLES ===== */
.deliverables-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.deliverable-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 14px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.deliverable-name {
  color: var(--text-strong);
  font-weight: 500;
  font-size: 13px;
}

.deliverable-type {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 4px;
  background: var(--bg-card-hover);
  color: var(--text-muted);
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  margin-left: 8px;
}

.deliverable-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

/* ===== STATUS UPDATE WIDGET ===== */
.status-update {
  display: flex;
  gap: 10px;
  align-items: stretch;
  margin-top: 12px;
}

.status-update select,
.status-update input {
  background: var(--bg-input);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 13px;
  outline: none;
}

.status-update select { min-width: 160px; }
.status-update input { flex: 1; }

/* ===== NEW CLIENT FORM ===== */
.new-client-form {
  margin-top: 16px;
}

.form-row { margin-top: 6px; }

.form-grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

#newClientModal h2 {
  color: var(--text-strong);
  font-size: 20px;
  font-weight: 700;
}

/* ===== TOAST ===== */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-left: 4px solid var(--green);
  color: var(--text-strong);
  padding: 12px 20px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  box-shadow: var(--shadow);
  z-index: 2000;
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.toast.error { border-left-color: var(--red); }
.toast.hidden { opacity: 0; pointer-events: none; transform: translateX(-50%) translateY(20px); }

/* ===== FOOTER ===== */
.footer {
  text-align: center;
  padding: 18px;
  color: var(--text-muted);
  font-size: 11px;
  border-top: 1px solid var(--border);
  background: var(--bg-card);
  display: flex;
  justify-content: center;
  gap: 20px;
}

.footer .version { color: var(--brand); font-weight: 600; }

/* ===== UTILITY ===== */
.hidden { display: none !important; }
