/* =========================================================================
   Cashflow — design tokens
   Concept: registro contabile digitale. Sfondo "inchiostro" profondo,
   accento ottone per i richiami di valore, monospace per ogni cifra e data
   (leggibilità in colonna, come un vero libro mastro). Entrate/uscite hanno
   colori dedicati usati SOLO per quello, mai come decorazione.
   ========================================================================= */

:root {
  --color-ink: #10191b;
  --color-ink-raised: #142326;
  --color-surface: #17262a;
  --color-surface-2: #1e2f33;
  --color-border: rgba(201, 162, 75, 0.16);
  --color-border-strong: rgba(201, 162, 75, 0.32);

  --color-brass: #c9a24b;
  --color-brass-strong: #e0bd6b;
  --color-income: #4fb286;
  --color-income-bg: rgba(79, 178, 134, 0.14);
  --color-expense: #c1553d;
  --color-expense-bg: rgba(193, 85, 61, 0.14);

  --color-text: #ede9e2;
  --color-text-muted: #93a3a3;
  --color-text-faint: #62716f;

  --font-display: 'Space Grotesk', 'IBM Plex Sans', system-ui, sans-serif;
  --font-body: 'IBM Plex Sans', system-ui, sans-serif;
  --font-mono: 'IBM Plex Mono', 'DejaVu Sans Mono', monospace;

  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 20px;
  --radius-pill: 999px;

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;

  --shadow-card: 0 8px 24px -12px rgba(0, 0, 0, 0.5);
  --app-max-width: 480px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--color-ink);
  color: var(--color-text);
  font-family: var(--font-body);
  -webkit-font-smoothing: antialiased;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
}

h1,
h2,
h3 {
  font-family: var(--font-display);
  font-weight: 600;
  margin: 0;
  letter-spacing: -0.01em;
}

p {
  margin: 0;
}

a {
  color: inherit;
}

button,
input,
select,
textarea {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
}

input,
select,
textarea {
  background: var(--color-surface-2);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: var(--space-3);
  width: 100%;
}

input:focus-visible,
select:focus-visible,
textarea:focus-visible,
button:focus-visible,
a:focus-visible {
  outline: 2px solid var(--color-brass-strong);
  outline-offset: 2px;
}

::placeholder {
  color: var(--color-text-faint);
}

.tabular {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

/* ---------------------------------------------------------------------
   App shell
   --------------------------------------------------------------------- */

#app-shell {
  max-width: var(--app-max-width);
  margin: 0 auto;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--color-ink);
  position: relative;
}

@media (min-width: 560px) {
  body {
    background: var(--color-ink-raised);
  }

  #app-shell {
    min-height: 92vh;
    margin-top: 4vh;
    border-radius: var(--radius-lg);
    overflow: hidden;
    box-shadow: var(--shadow-card);
    border: 1px solid var(--color-border);
  }
}

.app-header {
  padding: var(--space-5) var(--space-4) var(--space-3);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.app-header .brand {
  font-family: var(--font-display);
  font-size: 1.1rem;
  letter-spacing: 0.01em;
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.app-header .brand .mark {
  font-family: var(--font-mono);
  font-weight: 700;
  color: var(--color-brass);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-sm);
  padding: 2px 6px;
  font-size: 0.85rem;
}

.icon-btn {
  background: transparent;
  border: none;
  color: var(--color-text-muted);
  padding: var(--space-2);
  min-width: 40px;
  min-height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  cursor: pointer;
  line-height: 1;
  font-size: 1.1rem;
}

.icon-btn:hover {
  color: var(--color-text);
  background: var(--color-surface);
}

main#view {
  flex: 1;
  overflow-y: auto;
  padding: 0 var(--space-4) calc(84px + var(--space-4));
}

.offline-banner {
  background: var(--color-expense-bg);
  color: var(--color-expense);
  font-size: 0.82rem;
  text-align: center;
  padding: var(--space-2);
  display: none;
}

.offline-banner.visible {
  display: block;
}

.auth-mode .app-header,
.auth-mode .bottom-nav {
  display: none;
}

/* ---------------------------------------------------------------------
   Bottom navigation
   --------------------------------------------------------------------- */

.bottom-nav {
  position: sticky;
  bottom: 0;
  display: flex;
  border-top: 1px solid var(--color-border);
  background: rgba(23, 38, 42, 0.92);
  backdrop-filter: blur(10px);
  padding-bottom: env(safe-area-inset-bottom, 0);
}

.bottom-nav button {
  flex: 1;
  background: none;
  border: none;
  color: var(--color-text-faint);
  padding: var(--space-2) 0 calc(var(--space-2) + 2px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  font-size: 0.68rem;
  cursor: pointer;
  border-top: 2px solid transparent;
}

.bottom-nav button .nav-icon {
  font-size: 1.25rem;
  line-height: 1;
}

.bottom-nav button.active {
  color: var(--color-brass-strong);
  border-top-color: var(--color-brass);
}

/* ---------------------------------------------------------------------
   Cards & list rows
   --------------------------------------------------------------------- */

.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-4);
}

.card + .card,
.section + .section {
  margin-top: var(--space-4);
}

.section-title {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--color-text-faint);
  margin-bottom: var(--space-2);
}

.list {
  display: flex;
  flex-direction: column;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  overflow: hidden;
}

.list-row {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  cursor: pointer;
}

.list-row:last-child {
  border-bottom: none;
}

.list-row:hover {
  background: var(--color-surface-2);
}

.list-row .dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

.list-row .row-main {
  flex: 1;
  min-width: 0;
}

.list-row .row-title {
  font-size: 0.92rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.list-row .row-sub {
  font-size: 0.76rem;
  color: var(--color-text-muted);
  margin-top: 1px;
}

.list-row .row-amount {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: 0.94rem;
  white-space: nowrap;
}

.amount-income {
  color: var(--color-income);
}

.amount-expense {
  color: var(--color-expense);
}

.badge {
  font-size: 0.66rem;
  padding: 2px 7px;
  border-radius: var(--radius-pill);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  white-space: nowrap;
}

.badge-previsto {
  background: rgba(201, 162, 75, 0.16);
  color: var(--color-brass-strong);
}

.badge-confermato {
  background: rgba(79, 178, 134, 0.14);
  color: var(--color-income);
}

.badge-pending {
  background: rgba(147, 163, 163, 0.16);
  color: var(--color-text-muted);
}

.empty-state {
  text-align: center;
  padding: var(--space-6) var(--space-4);
  color: var(--color-text-muted);
}

.empty-state .empty-title {
  color: var(--color-text);
  font-family: var(--font-display);
  margin-bottom: var(--space-1);
}

/* ---------------------------------------------------------------------
   Hero saldo + ledger bar (elemento firma della dashboard)
   --------------------------------------------------------------------- */

.balance-hero {
  padding: var(--space-5) var(--space-2) var(--space-4);
  text-align: center;
}

.balance-hero .balance-label {
  font-size: 0.76rem;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.1em;
}

.balance-hero .balance-value {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: 2.6rem;
  font-weight: 700;
  margin-top: var(--space-1);
  letter-spacing: -0.02em;
  word-break: break-word;
}

/* Telefoni molto stretti (iPhone SE, Android compatti a 320-360px):
   la cifra del saldo a piena taglia rischia di andare a capo in modo
   brutto se il numero è grande. La riduciamo leggermente qui invece
   di forzare font-size piu' piccolo per tutti. */
@media (max-width: 360px) {
  .balance-hero .balance-value {
    font-size: 2.1rem;
  }

  .balance-hero {
    padding-left: 0;
    padding-right: 0;
  }
}

.ledger-bar {
  margin-top: var(--space-4);
}

.ledger-bar svg {
  width: 100%;
  height: auto;
  display: block;
}

.ledger-bar .ledger-legend {
  display: flex;
  justify-content: space-between;
  font-size: 0.68rem;
  color: var(--color-text-faint);
  margin-top: var(--space-1);
}

.ledger-bar .ledger-legend span.income {
  color: var(--color-income);
}

.ledger-bar .ledger-legend span.expense {
  color: var(--color-expense);
}

.stat-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: var(--space-2) 0;
  border-bottom: 1px solid var(--color-border);
}

.stat-row:last-child {
  border-bottom: none;
}

.stat-row .stat-label {
  font-size: 0.82rem;
  color: var(--color-text-muted);
}

.stat-row .stat-value {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.projection-row {
  display: flex;
  justify-content: space-between;
  padding: var(--space-2) 0;
  font-size: 0.86rem;
  border-bottom: 1px solid var(--color-border);
}

.projection-row:last-child {
  border-bottom: none;
}

.projection-row .month {
  color: var(--color-text-muted);
  text-transform: capitalize;
}

.projection-row .value {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.chart-wrap {
  position: relative;
  height: 220px;
  padding: var(--space-3);
}

.list-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-2);
}

/* ---------------------------------------------------------------------
   Stampa / export PDF (Fase 5)
   Il contenuto normale dell'app viene nascosto, resta visibile solo
   #print-sheet, popolato dalla view Report con un riepilogo pulito.
   --------------------------------------------------------------------- */

.print-only {
  display: none;
}

@media print {
  body * {
    visibility: hidden;
  }

  .print-only,
  .print-only * {
    visibility: visible;
  }

  .print-only {
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    background: #ffffff;
    color: #111111;
    padding: 24px;
  }

  .print-only h1 {
    font-family: var(--font-display);
    font-size: 20px;
    margin-bottom: 4px;
  }

  .print-only h2 {
    font-family: var(--font-display);
    font-size: 14px;
    margin-top: 20px;
    margin-bottom: 8px;
  }

  .print-table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 12px;
  }

  .print-table td,
  .print-table th {
    padding: 4px 8px;
    border-bottom: 1px solid #cccccc;
    text-align: left;
    font-family: var(--font-mono);
    font-size: 11px;
  }
}

/* ---------------------------------------------------------------------
   Buttons
   --------------------------------------------------------------------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  border: none;
  border-radius: var(--radius-sm);
  padding: var(--space-3) var(--space-4);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  width: 100%;
}

.btn-primary {
  background: var(--color-brass);
  color: #1a1409;
}

.btn-primary:hover {
  background: var(--color-brass-strong);
}

.btn-secondary {
  background: var(--color-surface-2);
  color: var(--color-text);
  border: 1px solid var(--color-border-strong);
}

.btn-danger {
  background: transparent;
  color: var(--color-expense);
  border: 1px solid rgba(193, 85, 61, 0.4);
}

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

.btn-small {
  padding: var(--space-2) var(--space-3);
  font-size: 0.8rem;
  width: auto;
}

.fab {
  position: fixed;
  bottom: calc(84px + var(--space-4));
  right: max(calc((100vw - var(--app-max-width)) / 2 + var(--space-4)), var(--space-4));
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: var(--color-brass);
  color: #1a1409;
  border: none;
  font-size: 1.6rem;
  cursor: pointer;
  box-shadow: 0 10px 24px -8px rgba(201, 162, 75, 0.55);
  z-index: 20;
}

/* ---------------------------------------------------------------------
   Forms
   --------------------------------------------------------------------- */

.field {
  margin-bottom: var(--space-4);
}

.field label {
  display: block;
  font-size: 0.78rem;
  color: var(--color-text-muted);
  margin-bottom: var(--space-1);
}

.field-row {
  display: flex;
  gap: var(--space-3);
}

.field-row .field {
  flex: 1;
}

.radio-group {
  display: flex;
  gap: var(--space-2);
}

.radio-group label {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-1);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: var(--space-2);
  font-size: 0.82rem;
  color: var(--color-text-muted);
  cursor: pointer;
}

.radio-group input {
  width: auto;
}

.radio-group input:checked + span {
  color: var(--color-brass-strong);
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: 0.86rem;
  color: var(--color-text-muted);
}

.checkbox-row input {
  width: auto;
}

.color-swatch-input {
  width: 44px;
  height: 44px;
  padding: 0;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: none;
}

.form-error {
  background: var(--color-expense-bg);
  color: var(--color-expense);
  border-radius: var(--radius-sm);
  padding: var(--space-2) var(--space-3);
  font-size: 0.82rem;
  margin-bottom: var(--space-3);
}

.filter-chips {
  display: flex;
  gap: var(--space-2);
  overflow-x: auto;
  padding-bottom: var(--space-1);
  margin-bottom: var(--space-3);
}

.filter-chips::-webkit-scrollbar {
  display: none;
}

.chip {
  flex-shrink: 0;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-pill);
  padding: var(--space-2) var(--space-3);
  font-size: 0.78rem;
  color: var(--color-text-muted);
  background: transparent;
  cursor: pointer;
  white-space: nowrap;
}

.chip.active {
  border-color: var(--color-brass);
  color: var(--color-brass-strong);
  background: rgba(201, 162, 75, 0.1);
}

/* ---------------------------------------------------------------------
   Bottom sheet (modali)
   --------------------------------------------------------------------- */

.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(8, 12, 13, 0.6);
  z-index: 30;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.sheet {
  width: 100%;
  max-width: var(--app-max-width);
  max-height: 88vh;
  overflow-y: auto;
  background: var(--color-surface);
  border: 1px solid var(--color-border-strong);
  border-bottom: none;
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  padding: var(--space-5) var(--space-4) calc(var(--space-5) + env(safe-area-inset-bottom, 0));
  animation: sheet-up 0.2s ease-out;
}

@keyframes sheet-up {
  from {
    transform: translateY(16px);
    opacity: 0;
  }

  to {
    transform: translateY(0);
    opacity: 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  .sheet {
    animation: none;
  }

  .count-up {
    transition: none !important;
  }
}

.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-4);
}

/* ---------------------------------------------------------------------
   Toast
   --------------------------------------------------------------------- */

.toast-stack {
  position: fixed;
  bottom: calc(84px + var(--space-4));
  left: 50%;
  transform: translateX(-50%);
  width: calc(100% - var(--space-6));
  max-width: calc(var(--app-max-width) - var(--space-6));
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  z-index: 40;
  pointer-events: none;
}

.toast {
  background: var(--color-surface-2);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-sm);
  padding: var(--space-3);
  font-size: 0.84rem;
  box-shadow: var(--shadow-card);
}

.toast.toast-error {
  border-color: rgba(193, 85, 61, 0.5);
  color: var(--color-expense);
}

.toast.toast-success {
  border-color: rgba(79, 178, 134, 0.5);
  color: var(--color-income);
}

/* ---------------------------------------------------------------------
   Auth view
   --------------------------------------------------------------------- */

.auth-view {
  padding: var(--space-6) var(--space-1);
}

.auth-view .mark-large {
  font-family: var(--font-mono);
  font-weight: 700;
  color: var(--color-brass);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-md);
  width: 56px;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto var(--space-4);
  font-size: 1.3rem;
}

.auth-view h1 {
  text-align: center;
  font-size: 1.3rem;
  margin-bottom: var(--space-1);
}

.auth-view .auth-subtitle {
  text-align: center;
  color: var(--color-text-muted);
  font-size: 0.86rem;
  margin-bottom: var(--space-5);
}

.auth-switch {
  text-align: center;
  margin-top: var(--space-4);
  font-size: 0.84rem;
  color: var(--color-text-muted);
}

.auth-switch button {
  background: none;
  border: none;
  color: var(--color-brass-strong);
  cursor: pointer;
  font-size: inherit;
  padding: 0;
  text-decoration: underline;
}

.spinner {
  width: 18px;
  height: 18px;
  border: 2px solid rgba(255, 255, 255, 0.25);
  border-top-color: currentColor;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  display: inline-block;
}

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

.loading-block {
  display: flex;
  justify-content: center;
  padding: var(--space-6) 0;
  color: var(--color-text-muted);
}
