/* ============ Base ============ */
:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #1d2330;
  --muted: #6b7385;
  --border: #dde1e8;
  --primary: #1f5fbf;
  --primary-dark: #174a96;
  --secondary: #4b5567;
  --success: #1c7c45;
  --success-bg: #e5f5eb;
  --danger: #c0352b;
  --danger-bg: #fbe9e7;
  --warning: #9a6200;
  --warning-bg: #fff4dc;
  --info-bg: #e8f0fc;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(20, 30, 50, .06), 0 1px 6px rgba(20, 30, 50, .05);
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

h1 { font-size: 1.5rem; margin: 0; line-height: 1.25; }
h2 { font-size: 1.1rem; margin: 1.2em 0 .6em; }
.card > h2:first-child, .card-head h2 { margin-top: 0; }

code, .mono { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: .92em; }

.muted { color: var(--muted); }
.small { font-size: .85em; }
.text-danger { color: var(--danger); }
.text-success { color: var(--success); }
.mt-xs { margin-top: 6px; }
.mt-s { margin-top: 12px; }

/* ============ Layout ============ */
.topbar {
  background: #1d2330;
  color: #fff;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 20px;
  min-height: 56px;
  position: sticky;
  top: 0;
  z-index: 50;
}
.brand { color: #fff; font-weight: 700; font-size: 1.1rem; white-space: nowrap; }
.brand:hover { text-decoration: none; }

.mainnav { display: flex; align-items: center; gap: 4px; flex: 1; }
.mainnav > a {
  color: #c9d0dc;
  padding: 8px 12px;
  border-radius: 6px;
}
.mainnav > a:hover { color: #fff; background: rgba(255,255,255,.08); text-decoration: none; }
.mainnav > a.active { color: #fff; background: rgba(255,255,255,.15); }

.usermenu { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.usermenu form { margin: 0; }
.username { color: #c9d0dc; font-size: .9rem; }
.topbar .btn-ghost { color: #fff; border-color: rgba(255,255,255,.3); }

.nav-toggle {
  display: none;
  margin-left: auto;
  background: none;
  border: 0;
  color: #fff;
  font-size: 1.5rem;
  cursor: pointer;
}

.container { width: 100%; max-width: 1280px; margin: 0 auto; padding: 24px 20px; flex: 1; }
.container-narrow { max-width: 720px; }
body.bare .container { padding-top: 48px; }

.footer { text-align: center; padding: 16px; font-size: .8rem; }

.page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 20px;
}
.page-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  margin-bottom: 20px;
}
.card-flush { padding: 0; overflow: hidden; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 12px; }

.grid-2, .grid-3, .grid-4 { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid-top { align-items: start; }
.grid-2 > .card, .grid-main > .card { margin-bottom: 20px; }
.grid-main { display: grid; grid-template-columns: minmax(0, 2fr) minmax(280px, 1fr); gap: 20px; align-items: start; }

.inline { display: inline; margin: 0; }

/* ============ Form ============ */
.field { display: block; margin-bottom: 14px; }
.field > span { display: block; font-size: .85rem; font-weight: 600; color: var(--secondary); margin-bottom: 4px; }

input[type=text], input[type=password], input[type=email], input[type=date], input[type=number], select, textarea {
  width: 100%;
  font: inherit;
  color: inherit;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 9px 11px;
  min-height: 40px;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(31, 95, 191, .15);
}
textarea { resize: vertical; }

.check { display: flex; align-items: center; gap: 8px; margin: 8px 0 14px; cursor: pointer; }
.check input { width: 18px; height: 18px; }

.input-group { display: flex; gap: 8px; align-items: stretch; }
.input-group > input[type=text] { flex: 1; min-width: 0; }
.grow { flex: 1; min-width: 200px; }

.input-qty { max-width: 90px; text-align: right; }
.input-price { max-width: 100px; text-align: right; }
.input-label { max-width: 200px; }

.form-actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 16px; }

.filters { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; padding: 14px; }
.filters select { width: auto; min-width: 160px; }
.inline-field { display: flex; align-items: center; gap: 6px; font-size: .9rem; color: var(--secondary); }
.inline-field input { width: auto; }

/* ============ Pulsanti ============ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  font: inherit;
  font-weight: 600;
  line-height: 1.2;
  padding: 9px 16px;
  min-height: 40px;
  border-radius: 6px;
  border: 1px solid transparent;
  background: #e9ecf1;
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
  text-decoration: none;
}
.btn:hover { text-decoration: none; filter: brightness(.96); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); filter: none; }
.btn-secondary { background: var(--secondary); color: #fff; }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn-danger-ghost { background: transparent; border-color: #efc4bf; color: var(--danger); }
.btn-small { padding: 5px 10px; min-height: 30px; font-size: .85rem; }
.btn-block { width: 100%; }
.btn-camera { background: #fff; border-color: var(--border); }

/* ============ Avvisi ============ */
.alert { padding: 12px 16px; border-radius: 6px; margin-bottom: 16px; border: 1px solid transparent; }
.alert-success { background: var(--success-bg); color: var(--success); border-color: #bfe3cc; }
.alert-error { background: var(--danger-bg); color: var(--danger); border-color: #f1c8c3; }
.alert-info { background: var(--info-bg); color: var(--primary-dark); border-color: #c9daf5; display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }

.empty { color: var(--muted); padding: 16px; text-align: center; margin: 0; }

/* ============ Badge ============ */
.badge {
  display: inline-block;
  font-size: .72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .03em;
  padding: 2px 8px;
  border-radius: 999px;
  background: #e9ecf1;
  color: var(--secondary);
  vertical-align: middle;
}
.badge-admin { background: #ffe8c2; color: #8a5300; }
.badge-carico { background: var(--success-bg); color: var(--success); }
.badge-scarico { background: var(--danger-bg); color: var(--danger); }
.badge-rettifica { background: var(--warning-bg); color: var(--warning); }
.badge-bozza { background: var(--warning-bg); color: var(--warning); }
.badge-confermato { background: var(--success-bg); color: var(--success); }

/* ============ Tabelle ============ */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: 10px 14px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table thead th { font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); background: #fafbfc; }
.table tbody tr:hover { background: #fafbfd; }
.table tfoot th { background: #fafbfc; }
.table .num { text-align: right; white-space: nowrap; }
.table .actions { width: 1%; white-space: nowrap; }
.card:not(.card-flush) .table th:first-child, .card:not(.card-flush) .table td:first-child { padding-left: 0; }
.row-warning { background: var(--warning-bg) !important; }

.pagination { display: flex; align-items: center; justify-content: center; gap: 12px; margin: 16px 0; flex-wrap: wrap; }

/* ============ Dashboard ============ */
.stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; margin-bottom: 20px; }
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  color: var(--text);
}
a.stat:hover { text-decoration: none; border-color: var(--primary); }
.stat-value { font-size: 1.6rem; font-weight: 700; }
.stat-label { color: var(--muted); font-size: .85rem; }
.stat-warning .stat-value { color: var(--danger); }

/* ============ Scansione ============ */
.scanbar { border-left: 4px solid var(--primary); }
.scanbar-label { display: block; font-weight: 600; margin-bottom: 8px; }
.scan-input { font-size: 1.15rem; min-height: 46px; }
.scan-message { margin-top: 10px; min-height: 1.5em; font-weight: 600; }
.scan-message:empty { display: none; }
.scan-success { color: var(--success); }
.scan-warning { color: var(--warning); }
.scan-error { color: var(--danger); display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.scan-warning-text { font-weight: 400; }

.scanbar-danger { border-left-color: var(--danger); }

/* Scarico rapido */
.quick-last {
  display: flex;
  align-items: center;
  gap: 20px;
  border-left: 4px solid var(--success);
}
.quick-last[hidden] { display: none; }
.quick-last-qty { font-size: 2rem; font-weight: 700; color: var(--danger); white-space: nowrap; }
.quick-last-info { flex: 1; min-width: 0; }
.quick-last-name { font-size: 1.2rem; font-weight: 700; }
.quick-last-stock { text-align: right; display: flex; flex-direction: column; }
.quick-last-stock strong { font-size: 1.4rem; }
.quick-last.flash { animation: quick-flash .6s ease-out; }
@keyframes quick-flash { from { background: var(--success-bg); } to { background: var(--surface); } }
.card-head-pad { padding: 16px 20px 0; }
.row-undone td { color: var(--muted); text-decoration: line-through; }
.row-undone td.actions { text-decoration: none; }

@media (max-width: 640px) {
  .quick-last { flex-wrap: wrap; gap: 8px 16px; }
  .quick-last-info { flex-basis: 100%; order: -1; }
  .quick-last-qty { font-size: 1.5rem; }
}

.request-reason { font-style: italic; }

/* Immagini articoli */
.thumb {
  display: inline-block;
  flex-shrink: 0;
  object-fit: contain;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 6px;
  vertical-align: middle;
}
.thumb-sm { width: 40px; height: 40px; }
.thumb-md { width: 56px; height: 56px; }
.thumb-lg { width: 160px; height: 160px; }
.thumb-xl { width: 110px; height: 110px; }
.thumb-empty {
  background: #f1f3f6 url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23b4bac6' stroke-width='1.5'%3E%3Crect x='6' y='3' width='12' height='18' rx='1.5'/%3E%3Cpath d='M6 8h12'/%3E%3C/svg%3E") center / 55% no-repeat;
}
.art-cell { display: flex; align-items: center; gap: 10px; }
.scan-message .thumb { margin-right: 6px; }
.image-field { display: flex; gap: 20px; align-items: flex-start; flex-wrap: wrap; }
.image-inputs { flex: 1; min-width: 240px; }
.image-inputs input[type=file] { width: 100%; padding: 6px 0; }
.quick-last-image .thumb { display: block; }

@media (max-width: 640px) {
  .thumb-xl { width: 80px; height: 80px; }
  .thumb-lg { width: 120px; height: 120px; }
}

/* Finestra modale generica */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(10, 15, 25, .6);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal[hidden] { display: none; }
.modal-dialog { background: #fff; border-radius: var(--radius); width: 100%; max-width: 480px; padding: 20px; }
.modal-dialog h2 { margin-top: 0; }
.request-article { font-weight: 600; }

/* Schede */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 20px; flex-wrap: wrap; }
.tabs a { padding: 10px 16px; color: var(--secondary); border-bottom: 2px solid transparent; margin-bottom: -1px; font-weight: 600; }
.tabs a:hover { text-decoration: none; color: var(--text); }
.tabs a.active { color: var(--primary); border-bottom-color: var(--primary); }
.tabs-secondary { border-bottom: 0; }
.tabs-secondary a { border: 1px solid var(--border); border-radius: 999px; padding: 6px 14px; margin: 0; background: #fff; }
.tabs-secondary a.active { background: var(--primary); color: #fff; border-color: var(--primary); }

.nav-count {
  display: inline-block;
  min-width: 20px;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--danger);
  color: #fff;
  font-size: .75rem;
  font-weight: 700;
  text-align: center;
  line-height: 20px;
}

/* Richieste di annullo (admin) */
.request-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.request-reason-box { margin: 14px 0; padding: 10px 14px; background: #f7f8fa; border-left: 3px solid var(--warning); border-radius: 4px; }
.request-reason-box footer { margin-top: 6px; }
.request-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

.barcode-rows { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.barcode-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

.scan-modal {
  position: fixed;
  inset: 0;
  background: rgba(10, 15, 25, .7);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.scan-modal[hidden] { display: none; }
.scan-dialog { background: #fff; border-radius: var(--radius); width: 100%; max-width: 520px; padding: 16px; }
.scan-dialog-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.scan-reader { width: 100%; min-height: 200px; background: #000; border-radius: 6px; overflow: hidden; }
.scan-reader video { width: 100% !important; display: block; }
.scan-status { margin: 10px 0 0; }
.scan-last { margin: 6px 0 0; font-weight: 700; color: var(--success); }

/* ============ Varie ============ */
.login-card { max-width: 400px; margin: 0 auto; }
.login-card h1 { margin-bottom: 4px; }

.checklist { list-style: none; padding: 0; margin: 0 0 16px; }
.checklist li { padding: 3px 0; }
.checklist .ok { color: var(--success); }
.checklist .ko { color: var(--danger); font-weight: 600; }
.checklist .warn { color: var(--warning); }

.details { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0; }
.details dt { color: var(--muted); }
.details dd { margin: 0; }

.stock-big { font-size: 2.2rem; font-weight: 700; margin: 0 0 12px; }
.stock-big small { font-size: 1rem; color: var(--muted); font-weight: 400; }

details summary { cursor: pointer; color: var(--primary); font-weight: 600; }

.movements { list-style: none; padding: 0; margin: 0; }
.movements li { padding: 8px 0; border-bottom: 1px solid var(--border); }
.movements li:last-child { border-bottom: 0; }

/* ============ Responsive ============ */
@media (max-width: 1024px) {
  .hide-md { display: none; }
  .grid-main { grid-template-columns: 1fr; }
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 860px) {
  .nav-toggle { display: block; }
  .topbar { flex-wrap: wrap; padding: 0 16px; }
  .mainnav {
    display: none;
    flex-basis: 100%;
    flex-direction: column;
    align-items: stretch;
    padding-bottom: 12px;
  }
  .mainnav.open { display: flex; }
  .usermenu { margin-left: 0; padding-top: 8px; border-top: 1px solid rgba(255,255,255,.15); justify-content: space-between; }
}

@media (max-width: 640px) {
  .container { padding: 16px; }
  .hide-sm { display: none; }
  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; gap: 0; }
  .grid-2.grid-top { gap: 0; }
  .card { padding: 16px; }
  .card-flush { padding: 0; }
  .table th, .table td { padding: 8px 10px; }
  .btn-camera-label { display: none; }
  .filters > * { flex: 1 1 100%; }
  .filters select { width: 100%; }
  .input-label { max-width: none; flex: 1; }
  .barcode-row .input-group { flex-basis: 100%; }
  h1 { font-size: 1.25rem; }
}

/* ============ Stampa ============ */
@media print {
  .topbar, .footer, .no-print, .btn { display: none !important; }
  body { background: #fff; }
  .card { box-shadow: none; border: 0; padding: 0; }
  .container { padding: 0; max-width: none; }
  .hide-sm, .hide-md { display: table-cell !important; }
}
