:root {
  --bg: #0f1923;
  --panel: #16222e;
  --panel-2: #1c2b39;
  --sidebar: #1a2a3a;
  --border: #263745;
  --text: #e7edf2;
  --text-light: #8ea3b3;
  --accent: #e8820c;
  --accent-dark: #c66c05;
  --blue: #2f8fd6;
  --green: #2bb673;
  --red: #e0503f;
  --radius: 12px;
  --radius-lg: 18px;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
}

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

body {
  font-family: "Inter", "Segoe UI", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

a { text-decoration: none; color: inherit; }
ul { list-style: none; }

.i {
  width: 1em;
  height: 1em;
  flex-shrink: 0;
  vertical-align: -0.125em;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* Auth screens */
.auth-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at 20% 20%, #1c3146, var(--bg));
  padding: 20px;
}

.auth-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 40px 36px;
  width: 100%;
  max-width: 380px;
}

.auth-card h1 {
  font-size: 1.4rem;
  color: var(--text);
  margin-bottom: 6px;
}

.auth-card p.subtitle {
  color: var(--text-light);
  font-size: 0.9rem;
  margin-bottom: 24px;
}

.field { margin-bottom: 16px; }

.field label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-light);
  margin-bottom: 6px;
}

.field input,
.field select,
.field textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.95rem;
  font-family: inherit;
  outline: none;
  background: var(--panel-2);
  color: var(--text);
}

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

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 18px;
  border-radius: 8px;
  font-weight: 600;
  font-size: 0.92rem;
  border: 1px solid transparent;
  cursor: pointer;
  transition: transform 0.15s, background 0.15s;
}

.btn-block { width: 100%; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-dark); }
.btn-accent { background: var(--accent); color: #fff; }
.btn-accent:hover { background: var(--accent-dark); }
.btn-blue { background: var(--blue); color: #fff; }
.btn-blue:hover { filter: brightness(1.1); }
.btn-outline { background: transparent; border-color: var(--border); color: var(--text); }
.btn-outline:hover { border-color: var(--accent); color: var(--accent); }
.btn-danger { background: transparent; color: var(--red); border-color: var(--red); }
.btn-danger:hover { background: var(--red); color: #fff; }
.btn-sm { padding: 6px 12px; font-size: 0.82rem; }

.error-msg {
  background: rgba(224, 80, 63, 0.12);
  color: #ff8a7a;
  border: 1px solid rgba(224, 80, 63, 0.4);
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 0.85rem;
  margin-bottom: 14px;
  display: none;
}
.error-msg.visible { display: block; }

/* Shell layout */
.crm-shell { display: flex; min-height: 100vh; }

.crm-sidebar {
  width: 240px;
  background: var(--sidebar);
  color: #b9c9d6;
  padding: 22px 14px;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
}

.crm-brand {
  color: #fff;
  font-weight: 700;
  font-size: 1.05rem;
  margin-bottom: 24px;
  padding-left: 8px;
}
.crm-brand span { display: block; font-size: 0.72rem; color: #6f8aa0; font-weight: 500; }

.crm-sidebar-close { display: none; }

.crm-global-search { display: flex; gap: 6px; margin-bottom: 16px; }
.crm-global-search input {
  flex: 1; min-width: 0; background: rgba(255, 255, 255, 0.06); border: 1px solid var(--border);
  border-radius: 8px; padding: 7px 10px; color: var(--text); font-size: 0.85rem;
}
.crm-global-search input::placeholder { color: #6f8aa0; }
.crm-global-search button {
  background: rgba(255, 255, 255, 0.06); border: 1px solid var(--border); border-radius: 8px;
  color: #b9c9d6; padding: 0 10px; cursor: pointer;
}
.crm-global-search button:hover { color: #fff; }

.crm-mobile-toggle {
  display: none;
  position: fixed; top: 14px; left: 14px; z-index: 250;
  width: 42px; height: 42px; border-radius: 10px;
  background: var(--sidebar); border: 1px solid var(--border); color: #fff;
  align-items: center; justify-content: center; cursor: pointer;
}

.crm-nav { display: flex; flex-direction: column; gap: 3px; flex: 1; }

.crm-nav a {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 500;
  color: #b9c9d6;
  transition: background 0.15s, color 0.15s;
}
.crm-nav a .i { width: 17px; height: 17px; opacity: 0.85; }
.crm-nav a:hover { background: rgba(255, 255, 255, 0.06); color: #fff; }
.crm-nav a.active { background: var(--accent); color: #fff; }
.crm-nav a.active .i { opacity: 1; }

.crm-nav-badge {
  margin-left: auto;
  background: var(--red);
  color: #fff;
  font-size: 0.7rem;
  font-weight: 700;
  padding: 1px 7px;
  border-radius: 30px;
}

.crm-user-box {
  border-top: 1px solid var(--border);
  padding-top: 14px;
  font-size: 0.85rem;
}
.crm-user-box strong { display: block; color: #fff; }
.crm-user-box .role-tag { font-size: 0.75rem; color: #6f8aa0; text-transform: capitalize; }

.crm-logout {
  margin-top: 10px;
  width: 100%;
  background: rgba(255, 255, 255, 0.05);
  color: #b9c9d6;
  border: none;
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.82rem;
  display: flex;
  align-items: center;
  gap: 6px;
}
.crm-logout:hover { background: rgba(255, 255, 255, 0.12); color: #fff; }

.crm-main { margin-left: 240px; flex: 1; padding: 26px 32px 60px; max-width: 1280px; }

.crm-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 22px;
  flex-wrap: wrap;
  gap: 12px;
}
.crm-topbar h1 { font-size: 1.45rem; color: #fff; font-weight: 700; }
.crm-topbar .crm-actions { display: flex; gap: 10px; flex-wrap: wrap; }

/* Stats */
.stat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 24px; }
.stat-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
}
.stat-card .stat-label { font-size: 0.78rem; color: var(--text-light); margin-bottom: 6px; }
.stat-card .stat-value { font-size: 1.6rem; font-weight: 700; color: #fff; }
.stat-card.accent { border-color: var(--accent); }
.stat-card.accent .stat-value { color: var(--accent); }

/* Panels */
.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
  margin-bottom: 22px;
}
.panel h2 { font-size: 1rem; color: #fff; margin-bottom: 14px; font-weight: 700; }

/* Tables */
.crm-table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.crm-table th {
  text-align: left; color: var(--text-light); font-weight: 600; font-size: 0.74rem;
  text-transform: uppercase; letter-spacing: 0.03em; padding: 8px 10px; border-bottom: 1px solid var(--border);
  cursor: pointer; user-select: none;
}
.crm-table th:hover { color: #fff; }
.crm-table td { padding: 11px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.crm-table tr:hover td { background: rgba(255, 255, 255, 0.025); }
.crm-table tbody tr:nth-child(even) td { background: rgba(255, 255, 255, 0.012); }
.crm-table th[data-sort-dir="asc"]::after { content: " ▲"; font-size: 0.65rem; }
.crm-table th[data-sort-dir="desc"]::after { content: " ▼"; font-size: 0.65rem; }
.crm-table a.row-link { color: var(--blue); font-weight: 600; }
.crm-table a.row-link:hover { text-decoration: underline; }

.empty-state { text-align: center; color: var(--text-light); padding: 28px 10px; font-size: 0.9rem; }

.badge {
  display: inline-flex; align-items: center; gap: 4px; padding: 3px 10px;
  border-radius: 30px; font-size: 0.73rem; font-weight: 700;
}
.badge-green { background: rgba(43, 182, 115, 0.15); color: var(--green); }
.badge-orange { background: rgba(232, 130, 12, 0.15); color: var(--accent); }
.badge-red { background: rgba(224, 80, 63, 0.15); color: var(--red); }
.badge-gray { background: rgba(255, 255, 255, 0.08); color: var(--text-light); }

/* Forms inline */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-grid .field-full { grid-column: 1 / -1; }
.toggle-form { display: none; }
.toggle-form.visible { display: block; }

/* Detail layout */
.detail-grid { display: grid; grid-template-columns: 320px 1fr; gap: 20px; }
.info-row { display: flex; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 0.9rem; }
.info-row:last-child { border-bottom: none; }
.info-row span:first-child { color: var(--text-light); }

.line-items { width: 100%; border-collapse: collapse; margin-bottom: 14px; }
.line-items th, .line-items td { padding: 8px; border-bottom: 1px solid var(--border); font-size: 0.86rem; text-align: left; }
.line-items input { width: 100%; border: 1px solid var(--border); border-radius: 6px; padding: 6px 8px; font-size: 0.86rem; background: var(--panel-2); color: var(--text); }
.quote-total { text-align: right; font-size: 1.05rem; font-weight: 700; color: #fff; margin-top: 6px; }

/* Calendar */
.calendar-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; }
.calendar-dow { text-align: center; font-size: 0.74rem; color: var(--text-light); text-transform: uppercase; padding-bottom: 6px; }
.calendar-day {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px;
  min-height: 78px; padding: 6px; font-size: 0.8rem; position: relative;
}
.calendar-day.other-month { opacity: 0.35; }
.calendar-day.today { border-color: var(--accent); }
.calendar-day .day-num { font-weight: 700; color: var(--text-light); font-size: 0.78rem; }
.calendar-day.today .day-num { color: var(--accent); }
.calendar-event {
  margin-top: 4px; background: rgba(232, 130, 12, 0.18); color: var(--accent);
  border-radius: 5px; padding: 2px 5px; font-size: 0.7rem; overflow: hidden; white-space: nowrap;
  text-overflow: ellipsis; display: block;
}
.calendar-event.scaduta { background: rgba(224, 80, 63, 0.18); color: #ff8a7a; }

/* KPI dashboard mini calendar */
.mini-cal-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); display: inline-block; margin: 0 1px; }

/* Toasts */
.toast-container { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 10px; z-index: 500; }
.toast {
  background: var(--panel); border: 1px solid var(--border); border-left: 4px solid var(--accent);
  border-radius: 8px; padding: 12px 16px; box-shadow: var(--shadow); font-size: 0.88rem; min-width: 240px;
  animation: toast-in 0.25s ease;
}
.toast.error { border-left-color: var(--red); }
.toast.success { border-left-color: var(--green); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* Modal */
.crm-modal-overlay {
  position: fixed; inset: 0; background: rgba(0, 0, 0, 0.6); display: none;
  align-items: center; justify-content: center; z-index: 400; padding: 20px;
}
.crm-modal-overlay.open { display: flex; }
.crm-modal {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius-lg);
  box-shadow: var(--shadow); width: 100%; max-width: 560px; max-height: 88vh; overflow-y: auto; padding: 26px;
}
.crm-modal h2 { color: #fff; margin-bottom: 16px; }

/* Stock badge */
.stock-low { color: var(--red); font-weight: 700; }

@media (max-width: 900px) {
  .crm-sidebar {
    display: flex;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    z-index: 260;
    box-shadow: var(--shadow);
  }
  .crm-sidebar.open { transform: translateX(0); }
  .crm-sidebar-close { display: block; position: absolute; top: 16px; right: 14px; background: none; border: none; color: #b9c9d6; cursor: pointer; }
  .crm-mobile-toggle { display: flex; }
  .crm-main { margin-left: 0; padding: 18px 18px 18px 70px; }
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
  .detail-grid { grid-template-columns: 1fr; }
  .form-grid { grid-template-columns: 1fr; }
  .calendar-day { min-height: 50px; font-size: 0.7rem; }
}

@media print {
  .crm-sidebar, .crm-topbar .crm-actions, .no-print { display: none !important; }
  .crm-main { margin-left: 0; }
  body { background: #fff; color: #000; }
  .panel { border: 1px solid #ccc; background: #fff; }
}
