:root {
  --bg: #f5f4f0;
  --surface: #ffffff;
  --surface-2: #eeede8;
  --border: #e3e1da;
  --text: #1d1c1a;
  --muted: #6d6a63;
  --rec: #e5383b;
  --primary: #1d1c1a;
  --on-primary: #ffffff;
  --danger: #d23a3a;
  --warn: #b97300;
  --ok: #2b8a52;
  --s-preventivo: #8a867d;
  --s-da_girare: #2f6fdf;
  --s-montaggio: #7a4fd8;
  --s-revisione: #d68a00;
  --s-consegnato: #2b8a52;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.05), 0 2px 8px rgb(0 0 0 / 0.04);
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131312;
    --surface: #1c1b19;
    --surface-2: #252421;
    --border: #312f2b;
    --text: #f1efe9;
    --muted: #9c988f;
    --primary: #f1efe9;
    --on-primary: #131312;
    --danger: #f06262;
    --warn: #e6a23c;
    --ok: #4cbb7a;
    --s-preventivo: #9c988f;
    --s-da_girare: #5b93f5;
    --s-montaggio: #a07ff0;
    --s-revisione: #e6a23c;
    --s-consegnato: #4cbb7a;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.4);
    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.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
}
body.modal-open { overflow: hidden; }
h1, h2 { margin: 0; letter-spacing: -0.01em; }
h1 { font-size: 26px; font-weight: 700; }
h2 { font-size: 16px; font-weight: 650; }
p { margin: 0; }
a { color: inherit; }
button { font: inherit; color: inherit; }
.ic { flex-shrink: 0; }
.muted { color: var(--muted); }
.spacer { flex: 1; }

/* ---------- Brand ---------- */
.brand { display: flex; align-items: center; gap: 10px; font-weight: 750; font-size: 19px; letter-spacing: -0.02em; }
.brand.big { font-size: 26px; margin-bottom: 4px; }
.logo-mark {
  width: 26px; height: 26px; border-radius: 8px; background: var(--primary); position: relative; flex-shrink: 0;
}
.logo-mark::after {
  content: ""; position: absolute; width: 10px; height: 10px; border-radius: 50%; background: var(--rec);
  top: 50%; left: 50%; transform: translate(-50%, -50%);
}
.splash { min-height: 100dvh; display: grid; place-items: center; }
.splash .logo-mark { width: 44px; height: 44px; border-radius: 13px; animation: pulse 1.2s ease-in-out infinite; }
.splash .logo-mark::after { width: 16px; height: 16px; }
@keyframes pulse { 50% { opacity: 0.5; } }

/* ---------- Bottoni e campi ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 0 14px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font-weight: 550; cursor: pointer; text-decoration: none;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--primary); color: var(--on-primary); border-color: transparent; }
.btn.primary:hover { opacity: 0.88; }
.btn.block { width: 100%; }
.btn.small { min-height: 32px; padding: 0 10px; font-size: 13.5px; border-radius: 8px; }
.btn.danger-text { color: var(--danger); border-color: transparent; background: none; padding: 0 8px; }
.btn:disabled { opacity: 0.6; cursor: default; }
.icon-btn {
  display: inline-grid; place-items: center; width: 40px; height: 40px; border-radius: 10px;
  border: 0; background: none; cursor: pointer; color: var(--muted);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.link {
  display: inline-flex; align-items: center; gap: 2px; background: none; border: 0; padding: 0;
  color: var(--muted); font-size: 14px; font-weight: 550; cursor: pointer; text-decoration: none;
}
.link:hover { color: var(--text); }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; min-width: 0; }
.field > span { font-size: 13px; font-weight: 600; color: var(--muted); }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
input, select, textarea {
  width: 100%; font: inherit; font-size: 16px; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px; min-height: 44px; outline: none;
}
textarea { resize: vertical; min-height: 80px; }
input:focus, select:focus, textarea:focus { border-color: var(--text); }
input[type="date"] { min-width: 0; }
.hint { color: var(--muted); font-size: 13px; }
.form-error { color: var(--danger); font-size: 14px; margin: 0 0 12px; }

/* ---------- Login ---------- */
.center-screen { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 18px; padding: 28px 24px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 6px;
}
.login-card > p { margin-bottom: 14px; }

/* ---------- Struttura ---------- */
.app { display: flex; min-height: 100dvh; }
.sidebar {
  width: 232px; flex-shrink: 0; position: sticky; top: 0; height: 100dvh;
  padding: 22px 14px; border-right: 1px solid var(--border); display: flex; flex-direction: column; gap: 18px;
}
.sidebar .brand { padding: 0 8px; }
.sidebar nav { display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 10px;
  color: var(--muted); text-decoration: none; font-weight: 550; background: none; border: 0; cursor: pointer; width: 100%;
  text-align: left;
}
.nav-item:hover { background: var(--surface-2); color: var(--text); }
.nav-item.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.sidebar-foot { margin-top: auto; }

.main { flex: 1; min-width: 0; padding: 28px 36px 60px; max-width: 1360px; }
.demo-banner {
  background: color-mix(in srgb, var(--warn) 12%, var(--surface)); border: 1px solid color-mix(in srgb, var(--warn) 30%, transparent);
  padding: 10px 14px; border-radius: 12px; font-size: 13.5px; margin-bottom: 20px;
}
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 20px; }
.page-head p { margin-top: 4px; }
.page-actions { display: flex; gap: 8px; flex-shrink: 0; }
.back-link { display: inline-flex; align-items: center; gap: 4px; color: var(--muted); text-decoration: none; font-weight: 550; margin-bottom: 10px; font-size: 14px; }

.bottom-nav, .fab, .mobile-only { display: none; }

/* ---------- Riquadri ---------- */
.tiles { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 16px; }
.tiles.three { grid-template-columns: repeat(3, 1fr); }
.tile {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px;
  display: flex; flex-direction: column; gap: 2px; text-decoration: none; min-width: 0;
}
a.tile:hover { border-color: color-mix(in srgb, var(--text) 25%, var(--border)); }
.tile-label { font-size: 13px; color: var(--muted); font-weight: 550; }
.tile-value { font-size: 26px; font-weight: 700; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.tile-value.owe { color: var(--warn); }
.tile-sub { font-size: 13px; color: var(--muted); }
.tile-sub.danger { color: var(--danger); font-weight: 600; }

.pipeline { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; margin-bottom: 16px; }
.pipe {
  display: flex; align-items: baseline; gap: 8px; padding: 10px 14px; border-radius: 12px; text-decoration: none;
  background: color-mix(in srgb, var(--s) 10%, var(--surface)); border: 1px solid color-mix(in srgb, var(--s) 22%, transparent);
}
.pipe:hover { background: color-mix(in srgb, var(--s) 16%, var(--surface)); }
.pipe-count { font-size: 20px; font-weight: 700; color: var(--s); font-variant-numeric: tabular-nums; }
.pipe-label { font-size: 13px; font-weight: 600; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; align-items: start; }
.panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px 16px 8px; margin-bottom: 16px; min-width: 0;
}
.grid-2 .panel { margin-bottom: 0; }
.grid-2 { margin-bottom: 16px; }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 6px; min-height: 32px; }

.rows { display: flex; flex-direction: column; }
.row {
  display: flex; align-items: center; gap: 12px; padding: 10px 4px; border-top: 1px solid var(--border); min-width: 0;
}
.rows > .row:first-child { border-top: 0; }
.row-main {
  display: flex; align-items: center; gap: 12px; flex: 1; min-width: 0; background: none; border: 0;
  padding: 0; text-align: left; cursor: pointer; text-decoration: none; color: inherit;
}
.row.row-main { padding: 10px 4px; border-radius: 0; }
.row.row-main:hover .row-title { text-decoration: underline; text-underline-offset: 3px; }
.row-text { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.row-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-sub { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rows-note { font-size: 13px; color: var(--muted); padding: 10px 4px; border-top: 1px solid var(--border); }
.row > .ic:last-child { color: var(--muted); }
.amount { font-weight: 650; font-variant-numeric: tabular-nums; white-space: nowrap; text-align: right; display: flex; flex-direction: column; align-items: flex-end; }
.amount small { font-size: 12px; font-weight: 500; }
.amount.owe { color: var(--warn); }
.amount.in { color: var(--ok); }
.amount.muted { color: var(--muted); font-weight: 500; }
.avatar {
  width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center; flex-shrink: 0;
  background: var(--surface-2); font-size: 13px; font-weight: 700; color: var(--muted);
}
.date-badge {
  flex-shrink: 0; min-width: 58px; text-align: center; font-size: 12.5px; font-weight: 650; color: var(--muted);
  background: var(--surface-2); border-radius: 8px; padding: 4px 6px;
}
.status-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--s); flex-shrink: 0; }
.due { display: inline-flex; align-items: center; gap: 4px; font-size: 13px; font-weight: 600; color: var(--muted); white-space: nowrap; }
.due.warn, .due-inline.warn { color: var(--warn); }
.due.danger, .due-inline.danger { color: var(--danger); }
.empty { padding: 18px 4px 22px; color: var(--muted); display: flex; flex-direction: column; align-items: flex-start; gap: 12px; }

/* ---------- Bacheca ---------- */
.toolbar { display: flex; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; }
.toolbar select { width: auto; min-width: 200px; }
.search {
  flex: 1; min-width: 200px; display: flex; align-items: center; gap: 8px; padding: 0 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; color: var(--muted);
}
.search:focus-within { border-color: var(--text); }
.search input { border: 0; padding: 0; background: none; min-height: 42px; }

.board { display: grid; grid-template-columns: repeat(5, minmax(180px, 1fr)); gap: 12px; align-items: start; overflow-x: auto; padding-bottom: 8px; }
.col {
  background: var(--surface-2); border-radius: var(--radius); padding: 10px; min-height: 160px;
  border: 2px solid transparent; transition: border-color 0.15s, background 0.15s;
}
.col.over { border-color: var(--s); background: color-mix(in srgb, var(--s) 8%, var(--surface-2)); }
.col-head { display: flex; align-items: center; gap: 8px; font-weight: 650; font-size: 14px; padding: 4px 4px 10px; }
.col-count { margin-left: auto; color: var(--muted); font-weight: 600; font-size: 13px; }
.col-body { display: flex; flex-direction: column; gap: 8px; }
.col-empty { color: var(--muted); font-size: 13px; padding: 6px 4px; }
.col-more { padding: 4px; }
.col-add {
  display: flex; align-items: center; gap: 6px; padding: 8px; border-radius: 10px; border: 0; background: none;
  color: var(--muted); font-size: 13.5px; font-weight: 550; cursor: pointer;
}
.col-add:hover { background: var(--surface); color: var(--text); }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 12px;
  box-shadow: var(--shadow); cursor: pointer; display: flex; flex-direction: column; gap: 6px;
}
.card:hover { border-color: color-mix(in srgb, var(--text) 25%, var(--border)); }
.card[draggable="true"]:active { cursor: grabbing; }
.card-title { font-weight: 650; line-height: 1.3; }
.card-client { font-size: 13px; color: var(--muted); margin-top: -4px; }
.card-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 12px; }
.price { font-size: 13px; font-weight: 600; color: var(--muted); display: inline-flex; align-items: center; gap: 3px; font-variant-numeric: tabular-nums; }
.price.paid { color: var(--ok); }
.advance {
  align-self: flex-start; display: inline-flex; align-items: center; gap: 4px; margin-top: 2px;
  font-size: 12.5px; font-weight: 650; padding: 5px 9px; border-radius: 8px; cursor: pointer;
  color: var(--s); background: color-mix(in srgb, var(--s) 11%, transparent); border: 0;
}
.advance:hover { background: color-mix(in srgb, var(--s) 20%, transparent); }

.chips { gap: 6px; overflow-x: auto; margin: 0 -16px 14px; padding: 0 16px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex-shrink: 0; display: inline-flex; align-items: center; gap: 6px; padding: 7px 12px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface); font-weight: 600; font-size: 14px; cursor: pointer;
}
.chip.active { background: var(--s); border-color: var(--s); color: #fff; }
.chip-count { font-size: 12px; opacity: 0.75; }

/* ---------- Cliente ---------- */
.contact { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; align-items: center; }
.contact .btn { max-width: 100%; overflow: hidden; text-overflow: ellipsis; }
.notes { width: 100%; color: var(--muted); font-size: 14px; white-space: pre-wrap; }

/* ---------- Finestre ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 50; background: rgb(0 0 0 / 0.38);
  display: grid; place-items: center; padding: 24px; animation: fade 0.15s ease-out;
}
.sheet {
  width: 100%; max-width: 540px; max-height: calc(100dvh - 48px); overflow-y: auto;
  background: var(--bg); border-radius: 18px; box-shadow: 0 20px 60px rgb(0 0 0 / 0.25);
  animation: rise 0.18s ease-out;
}
.sheet-head {
  display: flex; align-items: center; justify-content: space-between; padding: 14px 12px 6px 20px;
  position: sticky; top: 0; background: var(--bg); z-index: 1;
}
.sheet-body { padding: 8px 20px 6px; }
.sheet-foot {
  display: flex; align-items: center; gap: 8px; padding: 12px 20px 18px; position: sticky; bottom: 0; background: var(--bg);
  border-top: 1px solid var(--border);
}
.segmented { display: flex; flex-wrap: wrap; gap: 6px; }
.segmented button {
  padding: 8px 12px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface);
  font-size: 14px; font-weight: 600; cursor: pointer; color: var(--muted);
}
.segmented button.active { background: var(--s); border-color: var(--s); color: #fff; }
.pay-status {
  display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 14px;
  padding: 10px 12px; border-radius: 10px; background: var(--surface); border: 1px solid var(--border); font-size: 14px;
}
.pay-status > span { display: inline-flex; align-items: center; gap: 6px; }
.chooser { display: flex; flex-direction: column; gap: 8px; padding-bottom: 20px; }
.choice {
  display: flex; align-items: center; gap: 14px; padding: 14px; border-radius: 12px; text-align: left;
  background: var(--surface); border: 1px solid var(--border); cursor: pointer;
}
.choice:hover { border-color: color-mix(in srgb, var(--text) 25%, var(--border)); }
.choice-icon { width: 42px; height: 42px; border-radius: 11px; display: grid; place-items: center; background: var(--surface-2); }

@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { transform: translateY(12px); opacity: 0; } }

.toasts {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: 24px; z-index: 60;
  display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; width: max-content; max-width: calc(100vw - 32px);
}
.toast {
  background: var(--primary); color: var(--on-primary); padding: 10px 16px; border-radius: 12px;
  font-size: 14px; font-weight: 550; box-shadow: 0 8px 24px rgb(0 0 0 / 0.2); animation: rise 0.18s ease-out;
}
.toast.error { background: var(--danger); color: #fff; }

/* ---------- Telefono e tablet ---------- */
@media (max-width: 1100px) {
  .tiles { grid-template-columns: repeat(2, 1fr); }
  .grid-2 { grid-template-columns: 1fr; }
  .grid-2 .panel { margin-bottom: 0; }
}

@media (max-width: 899px) {
  .sidebar, .desktop-only { display: none; }
  .mobile-only { display: inline-grid; }
  .chips.mobile-only { display: flex; }
  .main { padding: 18px 16px calc(96px + env(safe-area-inset-bottom)); padding-top: calc(18px + env(safe-area-inset-top)); }
  h1 { font-size: 24px; }
  .page-head { margin-bottom: 16px; }

  .tiles.three { grid-template-columns: repeat(3, 1fr); }
  .tile { padding: 12px; }
  .tile-value { font-size: 21px; }
  .pipeline { display: flex; overflow-x: auto; margin: 0 -16px 16px; padding: 0 16px; scrollbar-width: none; }
  .pipeline::-webkit-scrollbar { display: none; }
  .pipe { flex-shrink: 0; }

  .toolbar select { flex: 1; min-width: 0; }
  .board { display: block; overflow: visible; }
  .col { display: none; background: none; padding: 0; min-height: 0; border: 0; }
  .col.current { display: block; }
  .col-head { display: none; }

  .bottom-nav {
    display: grid; grid-template-columns: repeat(4, 1fr); position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
    background: color-mix(in srgb, var(--surface) 92%, transparent); backdrop-filter: blur(12px);
    border-top: 1px solid var(--border); padding-bottom: env(safe-area-inset-bottom);
  }
  .bottom-nav a {
    display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 0 7px;
    font-size: 11.5px; font-weight: 600; color: var(--muted); text-decoration: none;
  }
  .bottom-nav a.active { color: var(--text); }
  .fab {
    display: grid; place-items: center; position: fixed; right: 16px; z-index: 41;
    bottom: calc(76px + env(safe-area-inset-bottom)); width: 56px; height: 56px; border-radius: 18px; border: 0;
    background: var(--primary); color: var(--on-primary); box-shadow: 0 6px 20px rgb(0 0 0 / 0.25); cursor: pointer;
  }
  .toasts { bottom: calc(150px + env(safe-area-inset-bottom)); }

  .overlay { padding: 0; place-items: end stretch; }
  .sheet {
    max-width: none; max-height: 92dvh; border-radius: 20px 20px 0 0;
    padding-bottom: env(safe-area-inset-bottom);
  }
  .sheet-body { padding: 8px 16px 6px; }
  .sheet-foot { padding: 12px 16px 14px; }
  .sheet-head { padding-left: 16px; }
}

@media (max-width: 480px) {
  .tiles.three { grid-template-columns: 1fr 1fr; }
  .tiles.three .tile:last-child { grid-column: span 2; }
  .field-row { grid-template-columns: 1fr; gap: 0; }
}

@media (max-width: 899px) {
  .toolbar .search { min-width: 0; flex: 1 1 55%; }
  .toolbar select { flex: 1 1 35%; }
}
