:root {
  --navy: #102a43;
  --navy-2: #243b53;
  --blue: #1769aa;
  --blue-soft: #e8f2fb;
  --gold: #f5b700;
  --surface: #ffffff;
  --background: #f4f7fa;
  --text: #102a43;
  --muted: #627d98;
  --line: #d9e2ec;
  --green: #16835f;
  --orange: #c96a10;
  --red: #ba2d3b;
  --shadow: 0 14px 36px rgba(16, 42, 67, .12);
  --radius: 22px;
}

* { box-sizing: border-box; }
html { min-height: 100%; background: var(--background); }
body {
  min-height: 100%; margin: 0; color: var(--text); background:
    radial-gradient(circle at 10% -10%, rgba(23, 105, 170, .15), transparent 34rem),
    var(--background);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input { font: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.optional { color: var(--muted); font-weight: 500; }

.app-shell { min-height: 100vh; padding-bottom: env(safe-area-inset-bottom); }
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; justify-content: space-between;
  height: calc(70px + env(safe-area-inset-top)); padding: env(safe-area-inset-top) max(18px, env(safe-area-inset-right)) 0 max(18px, env(safe-area-inset-left));
  background: rgba(255,255,255,.88); border-bottom: 1px solid rgba(217,226,236,.9); backdrop-filter: blur(14px);
}
.brand-mini { display: flex; gap: 11px; align-items: center; }
.brand-mini img { border-radius: 12px; box-shadow: 0 4px 12px rgba(16,42,67,.14); }
.brand-mini div { display: flex; flex-direction: column; line-height: 1.15; }
.brand-mini span { margin-top: 4px; color: var(--muted); font-size: .77rem; }
.icon-button, .close-button {
  border: 0; background: transparent; color: var(--navy); border-radius: 50%; width: 42px; height: 42px; font-size: 1.55rem;
}
.icon-button:hover, .close-button:hover { background: var(--blue-soft); }

.screen { min-height: 100vh; }
.login-screen { display: grid; place-items: center; padding: 26px 18px; }
.login-card {
  width: min(100%, 430px); padding: 34px 28px 30px; border: 1px solid rgba(217,226,236,.9); border-radius: 30px;
  background: rgba(255,255,255,.94); box-shadow: var(--shadow); text-align: center;
}
.login-logo { width: 84px; height: 84px; border-radius: 24px; box-shadow: 0 12px 26px rgba(16,42,67,.2); }
.login-card h1 { margin: 8px 0 8px; font-size: clamp(1.75rem, 7vw, 2.4rem); line-height: 1.06; letter-spacing: -.04em; }
.login-card > .muted { margin: 0 auto 25px; max-width: 330px; line-height: 1.5; }
.eyebrow { margin: 18px 0 5px; color: var(--blue); font-size: .72rem; font-weight: 800; letter-spacing: .16em; }

form { display: grid; gap: 16px; }
label { display: grid; gap: 7px; color: var(--navy-2); font-size: .9rem; font-weight: 700; text-align: left; }
input {
  width: 100%; min-height: 48px; padding: 11px 13px; border: 1px solid var(--line); border-radius: 13px; background: #fff; color: var(--text); outline: 0;
}
input:focus { border-color: var(--blue); box-shadow: 0 0 0 4px rgba(23,105,170,.12); }
.form-error { min-height: 1.25rem; margin: 0; color: var(--red); font-size: .88rem; line-height: 1.4; }

.primary, .secondary, .danger, .add-button {
  min-height: 46px; padding: 11px 17px; border-radius: 13px; border: 1px solid transparent; font-weight: 800; transition: transform .12s ease, box-shadow .12s ease, background .12s ease;
}
.primary { background: var(--navy); color: #fff; box-shadow: 0 8px 20px rgba(16,42,67,.18); }
.primary:hover { background: var(--navy-2); transform: translateY(-1px); }
.primary:disabled, .secondary:disabled, .danger:disabled { opacity: .5; cursor: not-allowed; transform: none; }
.secondary { background: #fff; border-color: var(--line); color: var(--navy); }
.secondary:hover { background: var(--blue-soft); border-color: #b8d4eb; }
.danger { background: var(--red); color: #fff; }
.danger-text { color: var(--red); }
.full { width: 100%; }
.compact { min-height: 42px; padding: 8px 13px; }

.page-content { width: min(100%, 920px); margin: 0 auto; padding: 22px 16px 60px; }
.hero-card {
  display: flex; align-items: center; justify-content: space-between; gap: 20px; padding: 26px; border-radius: 26px;
  color: #fff; background: linear-gradient(135deg, var(--navy), #1f5d8c); box-shadow: var(--shadow); overflow: hidden; position: relative;
}
.hero-card::after { content: ""; position: absolute; width: 190px; height: 190px; right: -65px; top: -85px; border-radius: 50%; background: rgba(255,255,255,.09); }
.hero-card .eyebrow { color: #b9ddf8; margin-top: 0; }
.hero-card h2 { margin: 4px 0 3px; font-size: clamp(2rem, 10vw, 3.25rem); letter-spacing: -.055em; }
.hero-card p:last-child { margin: 0; color: #d9eaf7; }
.add-button { z-index: 1; display: inline-flex; gap: 7px; align-items: center; background: var(--gold); color: var(--navy); white-space: nowrap; }
.add-button span { font-size: 1.3rem; }
.toolbar { display: flex; gap: 10px; margin: 18px 0; }
.search-box { flex: 1; display: flex; align-items: center; gap: 8px; min-height: 44px; padding: 0 12px; border: 1px solid var(--line); border-radius: 14px; background: #fff; }
.search-box span { color: var(--muted); font-size: 1.35rem; }
.search-box input { min-height: 42px; border: 0; padding: 0; box-shadow: none; background: transparent; }

.groups { display: grid; gap: 21px; }
.card-group { display: grid; gap: 11px; }
.group-head { display: flex; justify-content: space-between; align-items: end; padding: 0 4px; }
.group-head h3 { margin: 0; font-size: 1.08rem; }
.group-head span { color: var(--muted); font-size: .85rem; }
.card-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.wallet-card {
  position: relative; display: grid; gap: 13px; min-height: 170px; padding: 18px; border: 1px solid rgba(217,226,236,.95); border-radius: 21px;
  background: #fff; box-shadow: 0 7px 20px rgba(16,42,67,.07); text-align: left; overflow: hidden;
}
.wallet-card:hover { transform: translateY(-1px); box-shadow: 0 12px 24px rgba(16,42,67,.11); }
.wallet-card.exhausted { opacity: .72; }
.card-accent { position: absolute; top: 0; left: 0; width: 100%; height: 5px; background: var(--level-color, var(--green)); }
.card-top { display: flex; align-items: start; justify-content: space-between; gap: 10px; }
.card-name { min-width: 0; }
.card-name strong { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 1rem; }
.card-name span { display: block; margin-top: 4px; color: var(--muted); font-size: .79rem; }
.status-pill { flex: none; padding: 5px 8px; border-radius: 999px; background: #e8f7f1; color: var(--green); font-size: .68rem; font-weight: 800; }
.status-pill.exhausted { background: #fbeaec; color: var(--red); }
.card-balance span { display: block; color: var(--muted); font-size: .76rem; }
.card-balance strong { display: block; margin-top: 2px; font-size: 1.55rem; letter-spacing: -.04em; }
.progress-track { height: 8px; overflow: hidden; border-radius: 999px; background: #e7edf3; }
.progress-fill { height: 100%; width: 0; border-radius: inherit; background: var(--level-color, var(--green)); transition: width .25s ease; }
.card-footer { display: flex; justify-content: space-between; color: var(--muted); font-size: .73rem; }
.empty-state { margin-top: 45px; padding: 34px 20px; text-align: center; color: var(--muted); }
.empty-state h3 { color: var(--text); margin: 12px 0 5px; }
.empty-state p { max-width: 420px; margin: 0 auto 18px; line-height: 1.5; }
.empty-icon { font-size: 3.3rem; color: #9fb3c8; }

.modal-backdrop { position: fixed; inset: 0; z-index: 40; background: rgba(16,42,67,.56); backdrop-filter: blur(3px); }
dialog.modal {
  width: min(calc(100% - 24px), 600px); max-height: min(88vh, 820px); margin: auto; padding: 24px; border: 0; border-radius: 26px;
  background: #fff; color: var(--text); box-shadow: 0 26px 80px rgba(16,42,67,.35); overflow: auto;
}
dialog.modal::backdrop { background: rgba(16,42,67,.56); backdrop-filter: blur(3px); }
.small-modal { width: min(calc(100% - 24px), 440px) !important; }
.modal-head { display: flex; align-items: start; justify-content: space-between; gap: 16px; }
.modal-head .eyebrow { margin-top: 0; }
.modal-head h2 { margin: 0 0 17px; font-size: 1.55rem; letter-spacing: -.035em; }
.close-button { margin-top: -8px; margin-right: -9px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-grid label:first-child { grid-column: 1 / -1; }
.scan-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.scan-actions button, .file-button { min-height: 72px; display: flex; align-items: center; justify-content: center; gap: 7px; text-align: center; }
.file-button { padding: 11px 15px; border-radius: 13px; font-weight: 800; cursor: pointer; }
.file-button input { display: none; }
.divider-label { display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: .78rem; }
.divider-label::before, .divider-label::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.scan-result { padding: 12px 14px; border-radius: 12px; background: #e8f7f1; color: #0c6246; font-weight: 700; word-break: break-all; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 4px; }

.scanner-modal { width: min(calc(100% - 18px), 680px) !important; }
.scanner-help { margin: -8px 0 13px; font-size: .9rem; line-height: 1.45; }
.scanner-stage { position: relative; min-height: 270px; border-radius: 20px; overflow: hidden; background: #07131f; }
#reader { min-height: 270px; }
#reader video { width: 100% !important; min-height: 270px; object-fit: cover; }
#reader img { display: none; }
.scan-frame { pointer-events: none; position: absolute; left: 8%; right: 8%; top: 35%; height: 30%; border: 3px solid var(--gold); border-radius: 15px; box-shadow: 0 0 0 9999px rgba(0,0,0,.24); }
.scanner-status { min-height: 1.3rem; margin: 13px 0 0; color: var(--muted); text-align: center; }

.barcode-display {
  display: grid; place-items: center; min-height: 178px; padding: 18px 14px;
  border: 1px solid var(--line); border-radius: 18px; background: #fff; overflow: hidden;
  cursor: zoom-in; touch-action: manipulation; transition: border-color .15s ease, box-shadow .15s ease;
}
.barcode-display:hover { border-color: #9fc3df; box-shadow: 0 7px 20px rgba(16,42,67,.08); }
.barcode-display:focus-visible { outline: 0; border-color: var(--blue); box-shadow: 0 0 0 4px rgba(23,105,170,.14); }
.barcode-display svg, .barcode-display canvas { display: block; max-width: 100%; height: auto; }
.barcode-text { max-width: 100%; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .84rem; word-break: break-all; text-align: center; }
.barcode-tap-hint { margin: 7px 0 0; color: var(--muted); font-size: .75rem; text-align: center; }
.balance-panel { margin-top: 16px; padding: 17px; border-radius: 18px; background: var(--background); }
.balance-panel > span { color: var(--muted); font-size: .8rem; }
.balance-panel > strong { display: block; margin: 2px 0 11px; font-size: 2rem; letter-spacing: -.045em; }
.balance-panel small { display: block; margin-top: 8px; color: var(--muted); }
.spend-form { grid-template-columns: 1fr; margin-top: 16px; }
.spend-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.detail-footer { display: flex; justify-content: space-between; gap: 10px; padding-top: 13px; border-top: 1px solid var(--line); }

.toast { position: fixed; z-index: 100; left: 50%; bottom: calc(22px + env(safe-area-inset-bottom)); transform: translateX(-50%); min-width: min(88vw, 260px); max-width: 90vw; padding: 13px 17px; border-radius: 13px; background: var(--navy); color: #fff; box-shadow: var(--shadow); text-align: center; font-weight: 700; }
.toast.error { background: var(--red); }

@media (max-width: 640px) {
  .page-content { padding: 16px 12px 45px; }
  .hero-card { align-items: stretch; flex-direction: column; padding: 21px; }
  .add-button { justify-content: center; width: 100%; }
  .card-grid { grid-template-columns: 1fr; }
  .toolbar { align-items: stretch; }
  dialog.modal { padding: 20px 17px; border-radius: 22px; max-height: calc(100vh - 18px); }
  .form-grid, .scan-actions, .spend-actions { grid-template-columns: 1fr; }
  .form-grid label:first-child { grid-column: auto; }
  .scan-actions button, .file-button { min-height: 52px; }
}
.barcode-display img {
  display: block; width: 100%; max-width: 100%; height: auto; max-height: none;
  object-fit: contain; object-position: center; -webkit-user-drag: none; user-select: none;
}

/* Barcode a tutto schermo: nessuna deformazione, sfondo bianco e massima area utile. */
.barcode-fullscreen-dialog {
  box-sizing: border-box; width: 100vw; height: 100dvh; max-width: none; max-height: none;
  margin: 0; padding: max(18px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
    max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  border: 0; border-radius: 0; background: #fff; color: #000; overflow: hidden;
}
.barcode-fullscreen-dialog::backdrop { background: #fff; }
.barcode-fullscreen-dialog[open] { display: flex; flex-direction: column; }
.barcode-fullscreen-close {
  position: fixed; z-index: 3; top: max(10px, env(safe-area-inset-top)); right: max(10px, env(safe-area-inset-right));
  display: grid; place-items: center; width: 46px; height: 46px; padding: 0; border: 1px solid #d9e2ec;
  border-radius: 50%; background: rgba(255,255,255,.96); color: #102a43; box-shadow: 0 5px 18px rgba(16,42,67,.14);
  font-size: 1.8rem; line-height: 1;
}
.barcode-fullscreen-stage {
  flex: 1; min-height: 0; display: grid; place-items: center; padding: 44px 0 12px; overflow: hidden; background: #fff;
}
.barcode-fullscreen-stage img {
  display: block; width: min(1600px, calc(100vw - 32px)); max-width: calc(100vw - 32px);
  height: auto; max-height: calc(100dvh - 145px); object-fit: contain; object-position: center;
  -webkit-user-drag: none; user-select: none;
}
.barcode-fullscreen-stage img.is-qr {
  width: min(78vmin, 760px); max-width: min(78vmin, 760px); max-height: min(78vmin, 760px);
}
.barcode-fullscreen-fallback {
  max-width: calc(100vw - 40px); padding: 24px; border: 2px solid #102a43; font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: clamp(1.1rem, 5vw, 2rem); font-weight: 800; text-align: center; word-break: break-all;
}
.barcode-fullscreen-footer {
  flex: none; display: grid; gap: 4px; padding: 8px 52px 0; color: #102a43; text-align: center;
}
.barcode-fullscreen-footer strong {
  overflow-wrap: anywhere; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: clamp(.82rem, 2.7vw, 1.05rem);
}
.barcode-fullscreen-footer span { color: #627d98; font-size: .78rem; line-height: 1.35; }
body.barcode-fullscreen-open { overflow: hidden; }

/* Storico movimenti */
.text-button {
  min-height: 38px; padding: 6px 8px; border: 0; background: transparent; color: var(--blue); font-weight: 800;
}
.text-button:hover { text-decoration: underline; }
.movement-preview { margin-top: 17px; padding: 16px; border: 1px solid var(--line); border-radius: 18px; background: #fff; }
.movement-preview-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 10px; }
.movement-preview-head > div { display: grid; gap: 3px; }
.movement-preview-head span { font-weight: 800; }
.movement-preview-head small { color: var(--muted); }
.history-modal { width: min(calc(100% - 24px), 720px) !important; }
.history-summary { margin: -7px 0 14px; color: var(--muted); font-size: .9rem; }
.movement-list { display: grid; gap: 9px; }
.compact-list { max-height: 280px; overflow: auto; }
.movement-item {
  display: grid; grid-template-columns: 38px minmax(0, 1fr) auto 34px; align-items: center; gap: 10px;
  padding: 12px; border: 1px solid var(--line); border-radius: 15px; background: var(--surface);
}
.movement-icon {
  display: grid; place-items: center; width: 36px; height: 36px; border-radius: 50%; background: #fbeaec; color: var(--red); font-size: 1.25rem; font-weight: 900;
}
.movement-body { min-width: 0; display: grid; gap: 3px; }
.movement-body strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .92rem; }
.movement-body span { color: var(--muted); font-size: .76rem; line-height: 1.35; }
.movement-body .movement-card { color: var(--navy-2); font-weight: 700; }
.movement-amount { color: var(--red); font-size: .94rem; font-weight: 900; white-space: nowrap; }
.movement-delete-button {
  display: grid; place-items: center; width: 32px; height: 32px; padding: 0; border: 0; border-radius: 50%; background: transparent; color: var(--muted); font-size: 1.25rem;
}
.movement-delete-button:hover { background: #fbeaec; color: var(--red); }
.movement-empty { padding: 22px 12px; border: 1px dashed var(--line); border-radius: 14px; color: var(--muted); text-align: center; line-height: 1.45; }
.error-text { color: var(--red); }
.movement-delete-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.movement-warning { margin: 12px 0 0; color: var(--muted); font-size: .78rem; line-height: 1.45; }

@media (max-width: 640px) {
  .toolbar { flex-wrap: wrap; }
  .toolbar .search-box { flex-basis: 100%; }
  .toolbar .history-button { flex: 1; }
  .toolbar .refresh-button { font-size: 0; width: 44px; padding: 0; flex: 0 0 44px; }
  .toolbar .refresh-button::after { content: "↻"; font-size: 1.2rem; }
  .movement-item { grid-template-columns: 34px minmax(0, 1fr) 32px; }
  .movement-amount { grid-column: 2; justify-self: start; }
  .movement-delete-button { grid-column: 3; grid-row: 1 / span 2; }
  .movement-delete-actions { grid-template-columns: 1fr; }
}
