:root {
  color-scheme: dark;
  --bg: #0a0a14;
  --surface: #12121e;
  --surface-2: #181827;
  --border: #2a2a3a;
  --text: #f6f7fb;
  --muted: #a7a9bb;
  --primary: #f5a623;
  --primary-soft: rgba(245, 166, 35, 0.16);
  --success: #28a064;
  --danger: #e05252;
  --warning: #e7ba45;
  --info: #4aa3df;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.32);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  max-width: 100%;
  overflow-x: hidden;
  background: var(--bg);
}

body {
  min-height: 100%;
  max-width: 100%;
  overflow-x: hidden;
  margin: 0;
  background:
    radial-gradient(circle at top left, rgba(245, 166, 35, 0.09), transparent 34rem),
    linear-gradient(180deg, #0d0d18 0%, var(--bg) 42%);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

a {
  color: inherit;
  text-decoration: none;
}

img,
svg,
canvas {
  max-width: 100%;
}

[hidden] {
  display: none !important;
}

.app-shell {
  display: flex;
  flex-direction: column;
  width: 100%;
  min-width: 0;
  min-height: 100vh;
  padding-bottom: calc(84px + env(safe-area-inset-bottom));
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  width: 100%;
  min-width: 0;
  padding: 18px 18px 14px;
  background: rgba(10, 10, 20, 0.88);
  border-bottom: 1px solid rgba(42, 42, 58, 0.78);
  backdrop-filter: blur(18px);
}

.topbar h1,
.login-panel h1,
.section-head h2,
.modal-head h2,
.empty-state h2 {
  margin: 0;
  letter-spacing: 0;
}

.topbar h1 {
  font-size: 1.42rem;
  overflow-wrap: anywhere;
}

.user-greeting {
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 650;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--primary);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

.page {
  width: min(1180px, 100%);
  min-width: 0;
  margin: 0 auto;
  padding: 18px;
}

.login-screen {
  display: grid;
  min-height: 100vh;
  place-items: center;
  padding: 20px;
}

.login-panel {
  width: min(420px, 100%);
  padding: 28px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
}

.login-panel h1 {
  margin-bottom: 24px;
  font-size: 1.8rem;
}

.grid {
  display: grid;
  gap: 14px;
  min-width: 0;
}

.grid.two,
.grid.three,
.grid.four {
  grid-template-columns: 1fr;
}

.stack {
  display: grid;
  gap: 14px;
  min-width: 0;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  min-width: 0;
}

.metric-card,
.card,
.panel,
.empty-state {
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.18);
}

.metric-card {
  min-height: 116px;
  padding: 16px;
}

.metric-card p,
.card p {
  margin: 0;
  color: var(--muted);
}

.metric-card strong {
  display: block;
  margin-top: 8px;
  font-size: 1.55rem;
  line-height: 1.05;
}

.metric-card span {
  display: block;
  margin-top: 8px;
  color: var(--muted);
  font-size: 0.82rem;
}

.card {
  display: grid;
  gap: 12px;
  padding: 16px;
  overflow: hidden;
}

.card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  min-width: 0;
}

.card-head > div {
  min-width: 0;
}

.card-head .pill {
  flex: 0 0 auto;
}

.card h2,
.card h3 {
  margin: 0;
  overflow-wrap: anywhere;
  font-size: 1.02rem;
  letter-spacing: 0;
}

.card h2 a,
.card h3 a {
  color: var(--text);
}

.card h2 a:hover,
.card h3 a:hover {
  color: var(--primary);
}

.machine-title {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.machine-title a,
.machine-title span:last-child {
  min-width: 0;
  overflow-wrap: anywhere;
}

.type-icon {
  display: inline-grid;
  flex: 0 0 auto;
  width: 32px;
  height: 32px;
  place-items: center;
  background: linear-gradient(180deg, rgba(245, 166, 35, 0.22), rgba(245, 166, 35, 0.08));
  border: 1px solid rgba(245, 166, 35, 0.34);
  border-radius: 10px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.06);
  color: var(--text);
  font-size: 1.02rem;
  line-height: 1;
}

.table-machine {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  min-width: 0;
}

.table-machine .type-icon {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  font-size: 0.92rem;
}

.panel {
  padding: 16px;
  overflow: hidden;
}

.section-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  min-width: 0;
  margin-bottom: 14px;
}

.section-head > div {
  min-width: 0;
}

.section-head h2 {
  font-size: 1.05rem;
}

.section-head p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  margin-bottom: 14px;
}

.toolbar > select,
.toolbar > input {
  flex: 1 1 160px;
  width: auto;
  min-width: 0;
}

.toolbar > input[type="date"] {
  flex-basis: 142px;
}

.toolbar > input[type="date"]:disabled {
  display: none;
}

.toolbar > .toolbar-field {
  flex: 0 1 150px;
}

.toolbar .search {
  flex: 1 1 220px;
}

.toolbar-field {
  flex: 1 1 150px;
  min-width: 0;
}

.toolbar-field span {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 850;
  text-transform: uppercase;
}

.btn,
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 42px;
  border: 1px solid transparent;
  border-radius: 12px;
  transition: transform 0.16s ease, border-color 0.16s ease, background 0.16s ease, color 0.16s ease;
}

.btn {
  max-width: 100%;
  padding: 0 14px;
  color: var(--text);
  background: var(--surface-2);
  border-color: var(--border);
  font-weight: 750;
}

.btn:hover,
.icon-btn:hover {
  transform: translateY(-1px);
  border-color: rgba(245, 166, 35, 0.48);
}

.btn.primary {
  color: #15100a;
  background: var(--primary);
  border-color: var(--primary);
}

.btn.success {
  color: #ffffff;
  background: var(--success);
  border-color: var(--success);
}

.btn.danger {
  color: #ffffff;
  background: var(--danger);
  border-color: var(--danger);
}

.btn.ghost {
  color: var(--text);
  background: transparent;
  border-color: var(--border);
}

.btn.full {
  width: 100%;
}

.btn.small {
  min-height: 34px;
  padding-inline: 10px;
  border-radius: 10px;
  font-size: 0.86rem;
}

.icon-btn {
  width: 42px;
  min-width: 42px;
  padding: 0;
  color: var(--text);
  background: var(--surface-2);
  border-color: var(--border);
  font-weight: 900;
}

#logout-btn {
  position: relative;
  overflow: hidden;
  font-size: 0;
}

#logout-btn::before,
#logout-btn::after {
  content: "";
  position: absolute;
  left: 50%;
  display: block;
  pointer-events: none;
}

#logout-btn::before {
  top: 50%;
  width: 16px;
  height: 16px;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  transform: translate(-50%, -40%);
}

#logout-btn::after {
  top: 10px;
  width: 2px;
  height: 13px;
  background: currentColor;
  border-radius: 999px;
  transform: translateX(-50%);
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  min-width: 0;
}

label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 700;
}

input,
select,
textarea {
  width: 100%;
  min-height: 42px;
  padding: 10px 12px;
  color: var(--text);
  background: #0f0f1b;
  border: 1px solid var(--border);
  border-radius: 12px;
  outline: none;
}

textarea {
  min-height: 92px;
  resize: vertical;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

input[readonly] {
  color: var(--muted);
  background: rgba(255, 255, 255, 0.035);
  border-style: dashed;
}

label small {
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 650;
  line-height: 1.35;
}

.form-error {
  min-height: 20px;
  margin: 12px 0 0;
  color: var(--danger);
  font-size: 0.9rem;
}

.form-grid {
  display: grid;
  gap: 12px;
}

.check-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  max-height: 300px;
  overflow: auto;
  padding: 8px;
  background: #0f0f1b;
  border: 1px solid var(--border);
  border-radius: 12px;
}

.check-row {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 9px;
  align-items: center;
  min-height: 34px;
  color: var(--text);
  font-weight: 650;
}

.check-row input {
  width: auto;
  min-height: auto;
}

.pill-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  min-width: 0;
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 28px;
  padding: 4px 9px;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 750;
  max-width: 100%;
  overflow-wrap: anywhere;
}

.pill.primary {
  color: var(--primary);
  background: var(--primary-soft);
  border-color: rgba(245, 166, 35, 0.42);
}

.pill.success {
  color: #72d69f;
  background: rgba(40, 160, 100, 0.16);
  border-color: rgba(40, 160, 100, 0.42);
}

.pill.danger,
.pill.rosso {
  color: #ff9c9c;
  background: rgba(224, 82, 82, 0.16);
  border-color: rgba(224, 82, 82, 0.42);
}

.pill.giallo {
  color: #f4d371;
  background: rgba(231, 186, 69, 0.16);
  border-color: rgba(231, 186, 69, 0.42);
}

.pill.verde {
  color: #72d69f;
  background: rgba(40, 160, 100, 0.16);
  border-color: rgba(40, 160, 100, 0.42);
}

.money-pill {
  white-space: nowrap;
  overflow-wrap: normal;
  word-break: keep-all;
}

.table-wrap {
  width: 100%;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 14px;
}

table {
  width: 100%;
  min-width: 680px;
  border-collapse: collapse;
}

th,
td {
  padding: 12px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}

th {
  color: var(--muted);
  background: rgba(255, 255, 255, 0.03);
  font-size: 0.78rem;
  text-transform: uppercase;
}

tr:last-child td {
  border-bottom: 0;
}

.bar-chart {
  display: grid;
  grid-template-columns: repeat(var(--bar-count, 6), minmax(0, 1fr));
  gap: 8px;
  min-width: 0;
  min-height: 198px;
  overflow-x: visible;
  padding-bottom: 4px;
  align-items: end;
}

.bar-item {
  display: grid;
  gap: 7px;
  min-width: 0;
}

.bar-item strong {
  color: var(--text);
  font-size: 0.74rem;
  line-height: 1.15;
  text-align: center;
  overflow-wrap: anywhere;
}

.bar-track {
  position: relative;
  height: 128px;
  overflow: hidden;
  background: #0f0f1b;
  border: 1px solid var(--border);
  border-radius: 12px;
}

.bar-track span {
  position: absolute;
  right: 0;
  bottom: 0;
  left: 0;
  display: block;
  min-height: 4px;
  background: linear-gradient(180deg, var(--primary), #d7781f);
  border-radius: 12px 12px 0 0;
}

.bar-item small {
  min-height: 32px;
  color: var(--muted);
  font-size: 0.72rem;
  line-height: 1.25;
  text-align: center;
  overflow-wrap: anywhere;
}

.bottom-nav {
  position: fixed;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 30;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 2px;
  padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
  background: rgba(18, 18, 30, 0.96);
  border-top: 1px solid var(--border);
  backdrop-filter: blur(18px);
}

.bottom-nav a {
  display: grid;
  place-items: center;
  gap: 3px;
  min-height: 54px;
  min-width: 0;
  color: var(--muted);
  border-radius: 12px;
  font-size: 0.7rem;
  font-weight: 800;
  transition: background 0.16s ease, color 0.16s ease;
}

.bottom-nav a span {
  font-size: 1.15rem;
  line-height: 1;
}

.bottom-nav a.active {
  color: var(--primary);
  background: var(--primary-soft);
}

.admin-nav-link {
  display: none !important;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  align-items: end;
  padding: 12px;
  background: rgba(0, 0, 0, 0.58);
}

.modal-panel {
  width: min(720px, 100%);
  max-height: min(88vh, 860px);
  margin: 0 auto;
  overflow: auto;
  padding: 18px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
}

.modal-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.modal-head h2 {
  font-size: 1.18rem;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 16px;
}

.modal-copy p {
  margin: 0;
  color: var(--muted);
  line-height: 1.5;
}

.toast {
  position: fixed;
  right: 16px;
  bottom: calc(92px + env(safe-area-inset-bottom));
  z-index: 80;
  max-width: min(420px, calc(100vw - 32px));
  padding: 12px 14px;
  color: #ffffff;
  background: var(--success);
  border-radius: 12px;
  box-shadow: var(--shadow);
}

.toast.error {
  background: var(--danger);
}

.loading {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 160px;
  color: var(--muted);
  justify-content: center;
}

.spinner {
  width: 18px;
  height: 18px;
  border: 2px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.empty-state {
  padding: 24px;
  text-align: center;
}

.empty-state h2 {
  font-size: 1.08rem;
}

.empty-state p {
  margin: 8px auto 0;
  max-width: 56ch;
  color: var(--muted);
}

.signature-grid {
  display: grid;
  gap: 12px;
}

.signature-box {
  display: grid;
  gap: 8px;
}

.signature-box canvas {
  width: 100%;
  height: 150px;
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: 12px;
  touch-action: none;
}

.signature-preview {
  width: 100%;
  max-width: 300px;
  background: #ffffff;
  border-radius: 10px;
}

.photo-preview,
.counter-photo {
  margin: 0;
  padding: 12px;
  background: #0f0f1b;
  border: 1px solid var(--border);
  border-radius: 14px;
}

.photo-preview img,
.counter-photo img {
  display: block;
  width: 100%;
  max-height: 420px;
  object-fit: contain;
  background: #ffffff;
  border-radius: 10px;
}

.photo-preview-meta,
.counter-photo figcaption {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  justify-content: space-between;
  margin-top: 10px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 750;
}

.summary-strip {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
  min-width: 0;
}

.summary-item {
  min-width: 0;
  padding: 14px;
  background: #0f0f1b;
  border: 1px solid var(--border);
  border-radius: 12px;
}

.summary-item span {
  display: block;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 800;
}

.summary-item strong {
  display: block;
  margin-top: 5px;
  font-size: 1.15rem;
  overflow-wrap: anywhere;
}

.control-card {
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.018), transparent),
    var(--surface);
}

.control-form {
  display: grid;
  gap: 10px;
  min-width: 0;
}

.control-form textarea {
  min-height: 62px;
}

.code-update-row {
  display: grid;
  gap: 10px;
  min-width: 0;
}

.code-update-row .btn {
  align-self: end;
}

.code-locale-machines {
  display: grid;
  grid-template-columns: 1fr;
}

.quick-links {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  min-width: 0;
  margin-bottom: 16px;
}

.history-groups {
  display: grid;
  gap: 12px;
}

.history-locale {
  min-width: 0;
  overflow: hidden;
  background: #0f0f1b;
  border: 1px solid var(--border);
  border-radius: 14px;
}

.history-locale summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-width: 0;
  padding: 14px;
  cursor: pointer;
  list-style: none;
}

.history-locale summary::-webkit-details-marker {
  display: none;
}

.history-locale summary::after {
  content: "⌄";
  color: var(--muted);
  font-size: 1.1rem;
  font-weight: 900;
  transition: transform 0.16s ease;
}

.history-locale[open] summary::after {
  transform: rotate(180deg);
}

.history-locale summary > span:first-child {
  display: grid;
  gap: 3px;
  min-width: 0;
}

.history-locale summary strong {
  overflow-wrap: anywhere;
}

.history-locale summary small {
  color: var(--muted);
  line-height: 1.35;
}

.history-locale summary .pill {
  flex: 0 0 auto;
}

.history-receipts {
  display: grid;
  gap: 10px;
  padding: 0 12px 12px;
}

.history-receipt-card {
  background: var(--surface);
  box-shadow: none;
}

.danger-zone {
  border-color: rgba(224, 82, 82, 0.42);
}

.machine-aggio-card {
  transition: opacity 0.16s ease, border-color 0.16s ease;
}

.machine-aggio-card.disabled-card {
  opacity: 0.55;
  border-color: rgba(42, 42, 58, 0.7);
}

.machine-aggio-card .check-row {
  align-items: start;
}

.machine-aggio-card .check-row strong,
.machine-aggio-card .check-row small {
  display: block;
}

.machine-aggio-card .check-row small {
  margin-top: 3px;
  color: var(--muted);
  font-size: 0.78rem;
}

.machine-summary {
  margin-top: 2px;
}

@media (min-width: 680px) {
  .grid.two {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .grid.three {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .grid.four,
  .metric-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .form-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .form-grid .wide {
    grid-column: 1 / -1;
  }

  .summary-strip,
  .signature-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .control-form {
    grid-template-columns: minmax(180px, 0.4fr) minmax(260px, 1fr);
    align-items: end;
  }

  .control-form .actions {
    grid-column: 1 / -1;
  }

  .code-update-row {
    grid-template-columns: minmax(150px, 220px) auto;
    align-items: end;
  }

  .code-locale-machines {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .bar-chart {
    grid-template-columns: repeat(var(--bar-count, 6), minmax(76px, 1fr));
    gap: 12px;
    min-height: 230px;
    overflow-x: auto;
  }

  .bar-track {
    height: 160px;
  }

  .modal-backdrop {
    align-items: center;
  }
}

@media (min-width: 980px) {
  .app-shell {
    padding-bottom: 0;
  }

  .page {
    order: 2;
    padding: 24px;
  }

  .topbar {
    order: 0;
    padding-inline: max(24px, calc((100vw - 1180px) / 2 + 24px));
  }

  .bottom-nav {
    position: static;
    order: 1;
    align-self: flex-start;
    right: auto;
    top: auto;
    bottom: auto;
    left: auto;
    display: flex;
    width: auto;
    max-width: calc(100vw - 48px);
    margin: 16px 0 0 max(24px, calc((100vw - 1180px) / 2 + 24px));
    padding: 8px;
    border: 1px solid var(--border);
    border-radius: 18px;
  }

  body[data-role="admin"] .bottom-nav .admin-nav-link,
  body[data-role="segreteria"] .bottom-nav .admin-nav-link {
    display: grid !important;
  }

  .bottom-nav a {
    grid-template-columns: auto 1fr;
    min-width: 96px;
    padding: 0 12px;
  }

  .quick-links .mobile-admin-action {
    display: none;
  }

  .page {
    padding-top: 18px;
  }
}
