:root {
    --primary: #4f46e5;
    --primary-dark: #4338ca;
    --success: #16a34a;
    --danger: #dc2626;
    --bg: #f4f5f9;
    --card-bg: #ffffff;
    --text: #1e1e2e;
    --text-muted: #6b7280;
    --border: #e5e7eb;
    --radius: 12px;
}

/* Tema scuro: applicato se l'utente lo sceglie manualmente, oppure in automatico
   se il dispositivo/browser è impostato su preferenza scura e l'utente non ha
   scelto esplicitamente "chiaro" (vedi script di rilevamento in header.php/login.php) */
[data-theme="scuro"] {
    --bg: #14141f;
    --card-bg: #1e1e2c;
    --text: #e5e7eb;
    --text-muted: #9ca3af;
    --border: #33334a;
}
[data-theme="scuro"] body { background: var(--bg); color: var(--text); }
[data-theme="scuro"] .sidebar { background: #0c0c14; }
[data-theme="scuro"] .topbar,
[data-theme="scuro"] .bottom-nav { background: var(--card-bg); border-color: var(--border); }
/* Regole tema scuro rafforzate in specificità (prefisso "html") per vincere sempre
   sulle regole base dei campi modulo, indipendentemente dall'ordine nel file */
html[data-theme="scuro"] input,
html[data-theme="scuro"] select,
html[data-theme="scuro"] textarea {
    background-color: #262636 !important;
    color: var(--text) !important;
    border-color: var(--border) !important;
}
html[data-theme="scuro"] input::placeholder,
html[data-theme="scuro"] textarea::placeholder {
    color: #9ca3af;
    opacity: 1;
}
[data-theme="scuro"] table th, [data-theme="scuro"] table td { border-color: var(--border); }
[data-theme="scuro"] .btn-tipo { background: var(--card-bg); border-color: #4b4b66; color: var(--text); opacity: 0.85; }
[data-theme="scuro"] .btn-tipo-uscita.btn-tipo-attivo { border-color: var(--danger); background: #3a1a1a; opacity: 1; }
[data-theme="scuro"] .btn-tipo-entrata.btn-tipo-attivo { border-color: var(--success); background: #12331e; opacity: 1; }
[data-theme="scuro"] .auth-box { background: var(--card-bg); }
[data-theme="scuro"] .auth-box h1,
[data-theme="scuro"] .auth-box label,
[data-theme="scuro"] .auth-box p { color: var(--text); }
[data-theme="scuro"] .auth-box a { color: #a5b4fc; }

* { box-sizing: border-box; min-width: 0; }

html, body { overflow-x: hidden; max-width: 100%; height: 100%; }
img, canvas, svg, video, iframe { max-width: 100%; height: auto; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
}

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

/* --- Layout autenticazione --- */
.auth-page {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 20px;
    padding-bottom: 60px; /* spazio per il copyright fisso in fondo, evita sovrapposizioni */
}
.auth-box {
    background: var(--card-bg);
    padding: 32px;
    border-radius: var(--radius);
    box-shadow: 0 8px 24px rgba(0,0,0,0.08);
    width: 100%;
    max-width: 380px;
}
.auth-box h1 { font-size: 1.4rem; margin-top: 0; }

label { display: block; margin: 14px 0 6px; font-size: 0.9rem; font-weight: 600; }
input[type=text], input[type=password], input[type=email], input[type=number],
input[type=date], input[type=tel], input[type=color], input[type=file], input[type=datetime-local],
select, textarea {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 1rem; /* min 16px: evita lo zoom automatico all'apertura tastiera su iOS Safari */
    -webkit-appearance: none;
    appearance: none;
    background-color: #fff;
    font-family: inherit;
}
input[type=color] { padding: 2px; }
input[type=file] { padding: 8px; }
.btn {
    display: inline-block;
    background: var(--primary);
    color: #fff;
    border: none;
    padding: 11px 20px;
    border-radius: 8px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    margin-top: 16px;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}
.btn:hover { background: var(--primary-dark); }
.btn-danger { background: var(--danger); }

.btn-tipo {
    flex: 1;
    min-width: 0; /* CRITICO: senza questo i flex item non si restringono sotto la larghezza del loro contenuto, causando overflow su schermi stretti */
    padding: 12px 6px;
    border: 2px solid var(--border);
    border-radius: 10px;
    background: #fff;
    font-size: 0.95rem;
    font-weight: 700;
    cursor: pointer;
    opacity: 0.55;
    transition: all 0.15s ease;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.btn-tipo-uscita.btn-tipo-attivo { border-color: var(--danger); background: #fef2f2; opacity: 1; }
.btn-tipo-entrata.btn-tipo-attivo { border-color: var(--success); background: #f0fdf4; opacity: 1; }

.alert { padding: 12px 14px; border-radius: 8px; margin-bottom: 16px; font-size: 0.92rem; }
.alert-error { background: #fee2e2; color: #991b1b; }
.alert-success { background: #dcfce7; color: #166534; }

/* --- Layout app (shell con sidebar) --- */
.app-shell { display: flex; min-height: 100vh; }
.sidebar {
    width: 230px;
    background: #1e1e2e;
    color: #fff;
    display: flex;
    flex-direction: column;
    padding: 20px 0;
    position: fixed;
    top: 0; bottom: 0; left: 0;
    overflow-y: auto;
    transition: transform 0.2s ease;
    z-index: 100;
}
.sidebar-brand {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    gap: 8px;
    font-weight: 700;
    font-size: 1.05rem;
    padding: 0 16px 20px;
}
.sidebar-brand img { max-height: 44px; max-width: 80%; object-fit: contain; }
.sidebar a {
    color: #cbd5e1;
    padding: 12px 20px;
    display: block;
    font-size: 0.95rem;
}
.sidebar a:hover, .sidebar a.active { background: rgba(255,255,255,0.08); color: #fff; }
.sidebar hr { border-color: rgba(255,255,255,0.1); margin: 10px 20px; }

.sidebar-overlay {
    display: none;
    position: fixed;
    top: 0; right: 0; bottom: 0; left: 0; /* fallback esplicito, più compatibile di "inset" sui browser più datati */
    background: rgba(0,0,0,0.4);
    z-index: 99;
}
.sidebar.open ~ .sidebar-overlay,
.sidebar-overlay.visible { display: block; }

/* --- Comprimi/espandi sidebar su desktop --- */
@media (min-width: 861px) {
    .desktop-sidebar-toggle { display: flex; }
    .app-shell.sidebar-collapsed .sidebar { transform: translateX(-100%); }
    .app-shell.sidebar-collapsed .content { margin-left: 0; }
}

.content { flex: 1; margin-left: 230px; padding-bottom: 40px; transition: margin-left 0.2s ease; }
.topbar {
    background: #fff;
    padding: 14px 24px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    position: sticky; top: 0; z-index: 50;
    flex-wrap: wrap;
}
.topbar-left { display: flex; align-items: center; gap: 14px; }
.menu-toggle { display: none; background: none; border: none; font-size: 1.4rem; cursor: pointer; }
.icon-btn {
    background: #f1f5f9;
    border: 1px solid var(--border);
    border-radius: 8px;
    min-width: 36px; height: 36px;
    padding: 0 8px;
    font-size: 1.1rem;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: var(--text);
}
[data-theme="scuro"] .icon-btn { background: #2a2a3d; }
.desktop-sidebar-toggle {
    display: none;
    background: #f1f5f9;
    border: 1px solid var(--border);
    border-radius: 8px;
    width: 36px; height: 36px;
    font-size: 1.1rem;
    cursor: pointer;
    align-items: center;
    justify-content: center;
}
.orologio-app {
    font-size: 0.85rem;
    color: var(--text);
    font-variant-numeric: tabular-nums;
    line-height: 1.35;
}
.orologio-riga-superiore {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 16px;
    font-weight: 700;
    font-size: 0.95rem;
}
#orologioOra { font-weight: 600; color: var(--primary); }
#orologioGiornoData { color: var(--text-muted); font-size: 0.78rem; }

@media (max-width: 640px) {
    .orologio-app { display: none; } /* sulla topbar mobile non c'è spazio: si vede comunque nella bottom-nav o nel widget dedicato */
}
.content-inner { padding: 24px; max-width: 1600px; }

.card {
    background: var(--card-bg);
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: 20px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}
.card h2 { margin-top: 0; font-size: 1.1rem; }

.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 16px;
    margin-bottom: 20px;
}
.stat-card {
    background: var(--card-bg);
    border-radius: var(--radius);
    padding: 18px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.06);
    border-left: 4px solid var(--primary);
}
.stat-card.income { border-left-color: var(--success); }
.stat-card.expense { border-left-color: var(--danger); }
.stat-card.portfolio { border-left-color: #0ea5e9; }
.stat-label { display: block; font-size: 0.8rem; color: var(--text-muted); margin-bottom: 6px; }
.stat-value { display: block; font-size: 1.4rem; font-weight: 700; }

.charts-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
    gap: 20px;
}

.alert-card { border-left: 4px solid #f59e0b; }
.scadenze-list { margin: 0; padding-left: 20px; }
.scadenze-list li { margin-bottom: 8px; }

table { width: 100%; border-collapse: collapse; margin-top: 10px; }
th, td { text-align: left; padding: 10px; border-bottom: 1px solid var(--border); font-size: 0.92rem; }
th { color: var(--text-muted); font-weight: 600; }

.confirm-box { background: #fffbeb; border: 1px solid #fde68a; padding: 16px; border-radius: 8px; margin-top: 10px; }

.badge { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 0.78rem; font-weight: 600; }
.badge-success { background: #dcfce7; color: #166534; }
.badge-warning { background: #fef3c7; color: #92400e; }
.badge-danger { background: #fee2e2; color: #991b1b; }

/* --- Bottom nav mobile (stile app) --- */
.bottom-nav {
    display: none;
    position: fixed;
    left: 0; right: 0; bottom: 0;
    background: #fff;
    border-top: 1px solid var(--border);
    padding: 6px 4px 6px; /* fallback fisso */
    padding-bottom: calc(6px + env(safe-area-inset-bottom, 0px)); /* miglioria per iPhone con notch, se supportata */
    z-index: 200;
    box-shadow: 0 -2px 8px rgba(0,0,0,0.06);
}
.bottom-nav-inner {
    display: flex;
    justify-content: space-around;
    align-items: center;
}
.bottom-nav a, .bottom-nav button {
    all: unset;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    font-size: 0.65rem;
    color: var(--text-muted);
    padding: 4px 6px;
    cursor: pointer;
    min-width: 54px;
    min-height: 44px;
    text-align: center;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}
.bottom-nav .icon { font-size: 1.3rem; line-height: 1; }
.bottom-nav a.active, .bottom-nav button.active { color: var(--primary); font-weight: 700; }

@media (max-width: 860px) {
    .sidebar { transform: translateX(-100%); width: 240px; z-index: 300; }
    .sidebar.open { transform: translateX(0); }
    .sidebar.open ~ .content .bottom-nav { display: none; } /* evita la sovrapposizione dei due menu */
    .content { margin-left: 0; padding-bottom: 78px; }
    .menu-toggle { display: none; }
    .desktop-sidebar-toggle { display: none !important; }
    .bottom-nav { display: block; }
    .content-inner { padding: 16px; }
    .stats-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
    .stat-value { font-size: 1.15rem; }
    table { display: block; overflow-x: auto; white-space: nowrap; }
}

@media (max-width: 420px) {
    .stats-grid { grid-template-columns: 1fr; }
    .btn-tipo { font-size: 0.85rem; padding: 10px 4px; }
}

.app-footer {
    text-align: center;
    padding: 14px;
    font-size: 0.78rem;
    color: var(--text-muted);
}
.app-footer a { color: var(--text-muted); text-decoration: underline; }

/* Sulle pagine di login/auth il body è un contenitore flex che centra il modulo:
   senza questa regola il copyright diventerebbe un secondo elemento flex
   affiancato al modulo invece di comparire sotto, in fondo alla pagina. */
.auth-page .app-footer {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    margin: 0;
    background: var(--bg);
}

/* --- Navigatore mese/anno (Movimenti, riepiloghi annuali) --- */
.nav-periodo {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 14px;
    margin-bottom: 16px;
}
.nav-periodo .titolo-periodo {
    font-size: 1.15rem;
    font-weight: 700;
    min-width: 180px;
    text-align: center;
}
.nav-periodo .icon-btn { text-decoration: none; }

/* --- Icone trasparenti di sfondo nelle card statistiche --- */
.stat-card { position: relative; overflow: hidden; }
.stat-card-icon {
    position: absolute;
    right: 6px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 2.6rem;
    opacity: 0.12;
    pointer-events: none;
    line-height: 1;
}

