/* ── Variables ───────────────────────────────────────────────────────────── */
:root {
    --primary:        #1E6FD9;
    --primary-light:  #E8F1FD;
    --primary-dark:   #1558B0;
    --secondary:      #38BDF8;
    --bg:             #F8FAFC;
    --bg-card:        #FFFFFF;
    --border:         #E2E8F0;
    --text:           #1A202C;
    --text-muted:     #64748B;
    --success:        #10B981;
    --warning:        #F59E0B;
    --danger:         #EF4444;
    --radius:         10px;
    --shadow:         0 1px 4px rgba(0,0,0,0.08);
    --shadow-md:      0 4px 16px rgba(0,0,0,0.10);
    --sidebar-width:  240px;
    --topbar-height:  60px;
    --font:           'Inter', -apple-system, sans-serif;
}

/* ── Reset ───────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; font-family: var(--font); background: var(--bg); color: var(--text); }
a { text-decoration: none; color: inherit; }
button { cursor: pointer; font-family: var(--font); }

/* ── Layout ──────────────────────────────────────────────────────────────── */
.app-layout {
    display: flex;
    min-height: 100vh;
}

.sidebar {
    width: var(--sidebar-width);
    background: #0F172A;
    color: #CBD5E1;
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0; left: 0; bottom: 0;
    z-index: 200;
}

.sidebar-logo {
    padding: 20px;
    display: flex;
    align-items: center;
    gap: 10px;
    border-bottom: 1px solid #1E293B;
}

.sidebar-logo h1 {
    font-size: 1.1rem;
    font-weight: 700;
    color: white;
}

.sidebar-nav {
    flex: 1;
    padding: 12px 0;
    overflow-y: auto;
}

.nav-section-title {
    font-size: 0.68rem;
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: #475569;
    padding: 12px 20px 4px;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 20px;
    font-size: 0.88rem;
    font-weight: 500;
    color: #94A3B8;
    border-radius: 0;
    transition: all 0.15s;
    cursor: pointer;
    border: none;
    background: none;
    width: 100%;
    text-align: left;
}

.nav-item:hover { background: #1E293B; color: white; }
.nav-item.active { background: var(--primary); color: white; }
.nav-item .icon { font-size: 1rem; width: 20px; text-align: center; }

.sidebar-footer {
    padding: 16px 20px;
    border-top: 1px solid #1E293B;
    font-size: 0.8rem;
    color: #475569;
}

.sidebar-footer .user-name { color: #CBD5E1; font-weight: 600; }

/* ── Main content ────────────────────────────────────────────────────────── */
.main-content {
    margin-left: var(--sidebar-width);
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

.topbar {
    height: var(--topbar-height);
    background: white;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 24px;
    position: sticky;
    top: 0;
    z-index: 100;
}

.topbar h2 { font-size: 1.1rem; font-weight: 600; }

.page-content { padding: 24px; flex: 1; }

/* ── Cards ───────────────────────────────────────────────────────────────── */
.card {
    background: var(--bg-card);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
}

.card-header {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.card-header h3 { font-size: 1rem; font-weight: 600; }
.card-body { padding: 20px; }

/* ── Stat cards ──────────────────────────────────────────────────────────── */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 16px;
    margin-bottom: 24px;
}

.stat-card {
    background: white;
    border-radius: var(--radius);
    padding: 20px;
    border: 1px solid var(--border);
    box-shadow: var(--shadow);
    display: flex;
    align-items: center;
    gap: 16px;
}

.stat-icon {
    width: 48px; height: 48px;
    border-radius: 12px;
    display: flex; align-items: center; justify-content: center;
    font-size: 1.4rem;
    flex-shrink: 0;
}

.stat-icon.blue   { background: var(--primary-light); }
.stat-icon.green  { background: #D1FAE5; }
.stat-icon.orange { background: #FEF3C7; }
.stat-icon.red    { background: #FEE2E2; }

.stat-value { font-size: 1.8rem; font-weight: 700; line-height: 1; }
.stat-label { font-size: 0.8rem; color: var(--text-muted); margin-top: 2px; }

/* ── Buttons ─────────────────────────────────────────────────────────────── */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border-radius: 8px;
    font-size: 0.875rem;
    font-weight: 500;
    border: none;
    transition: all 0.15s;
}

.btn-primary   { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-secondary { background: var(--primary-light); color: var(--primary); }
.btn-secondary:hover { background: #d4e6fb; }
.btn-danger    { background: #FEE2E2; color: var(--danger); }
.btn-danger:hover { background: #fecaca; }
.btn-success   { background: #D1FAE5; color: var(--success); }
.btn-sm        { padding: 5px 10px; font-size: 0.8rem; }
.btn-icon      { padding: 8px; border-radius: 8px; background: var(--bg); border: 1px solid var(--border); }

/* ── Badges ──────────────────────────────────────────────────────────────── */
.badge {
    display: inline-flex;
    align-items: center;
    padding: 3px 10px;
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 600;
}

.badge-blue    { background: var(--primary-light); color: var(--primary); }
.badge-green   { background: #D1FAE5; color: #065F46; }
.badge-orange  { background: #FEF3C7; color: #92400E; }
.badge-red     { background: #FEE2E2; color: #991B1B; }
.badge-gray    { background: #F1F5F9; color: #475569; }
.badge-purple  { background: #EDE9FE; color: #5B21B6; }

/* ── Forms ───────────────────────────────────────────────────────────────── */
.form-group { margin-bottom: 16px; }
.form-label { display: block; font-size: 0.85rem; font-weight: 500; margin-bottom: 6px; color: var(--text); }

.form-control {
    width: 100%;
    padding: 9px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 0.875rem;
    font-family: var(--font);
    color: var(--text);
    background: white;
    transition: border-color 0.15s;
    outline: none;
}

.form-control:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-light); }
.form-select { appearance: none; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%2364748B' d='M6 8L1 3h10z'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 12px center; padding-right: 32px; }

/* ── Table ───────────────────────────────────────────────────────────────── */
.table { width: 100%; border-collapse: collapse; font-size: 0.875rem; }
.table th { padding: 10px 16px; text-align: left; font-weight: 600; font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--text-muted); border-bottom: 1px solid var(--border); background: var(--bg); }
.table td { padding: 12px 16px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tr:last-child td { border-bottom: none; }
.table tr:hover td { background: #F8FAFC; }

/* ── Login page ──────────────────────────────────────────────────────────── */
.login-page {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #0F172A 0%, #1E3A5F 100%);
}

.login-card {
    background: white;
    border-radius: 16px;
    padding: 40px;
    width: 100%;
    max-width: 400px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}

.login-logo {
    text-align: center;
    margin-bottom: 28px;
}

.login-logo .logo-circle {
    width: 56px; height: 56px;
    background: var(--primary);
    border-radius: 14px;
    display: inline-flex; align-items: center; justify-content: center;
    font-size: 1.6rem;
    margin-bottom: 12px;
}

.login-logo h1 { font-size: 1.4rem; font-weight: 700; color: #1A202C; }
.login-logo p  { font-size: 0.85rem; color: #64748B; margin-top: 4px; }

/* ── Alert ───────────────────────────────────────────────────────────────── */
.alert {
    padding: 12px 16px;
    border-radius: 8px;
    font-size: 0.875rem;
    margin-bottom: 16px;
}

.alert-danger  { background: #FEE2E2; color: #991B1B; border: 1px solid #FECACA; }
.alert-success { background: #D1FAE5; color: #065F46; border: 1px solid #A7F3D0; }
.alert-info    { background: var(--primary-light); color: var(--primary-dark); border: 1px solid #BFDBFE; }

/* ── Spinner ─────────────────────────────────────────────────────────────── */
.spinner {
    width: 32px; height: 32px;
    border: 3px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin .7s linear infinite;
    margin: 0 auto;
}

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

.loading-center { display: flex; justify-content: center; align-items: center; padding: 60px; }

/* ── Upgrade Banner ──────────────────────────────────────────────────────── */
.upgrade-banner {
    display: flex; align-items: center; gap: 12px;
    background: linear-gradient(135deg, #D97706, #F59E0B);
    color: white; padding: 10px 20px; border-radius: 8px;
    margin-bottom: 16px; font-size: 0.9rem; flex-wrap: wrap;
}
.upgrade-banner-btn {
    background: white; color: #D97706; font-weight: 600;
    padding: 4px 14px; border-radius: 6px; text-decoration: none;
    font-size: 0.85rem; white-space: nowrap;
}
.upgrade-banner-btn:hover { background: #FEF3C7; }
.upgrade-banner-close {
    background: none; border: none; color: white; font-size: 1rem;
    cursor: pointer; margin-left: auto; padding: 0 4px; line-height: 1;
}

/* ── Plan card ───────────────────────────────────────────────────────────── */
.plan-card {
    border-radius: 12px; padding: 20px 24px; margin-bottom: 20px;
    border: 2px solid var(--border);
}
.plan-card.plan-trial    { background: #F0FDF4; border-color: #86EFAC; }
.plan-card.plan-starter  { background: #EFF6FF; border-color: #93C5FD; }
.plan-card.plan-pro      { background: #FFF7ED; border-color: #FCA5A5; }
.plan-card.plan-business { background: #FAF5FF; border-color: #C4B5FD; }
.plan-badge {
    display: inline-block; padding: 4px 14px; border-radius: 20px;
    font-weight: 700; font-size: 0.9rem; margin-bottom: 6px;
}
.plan-badge.trial    { background: #D1FAE5; color: #065F46; }
.plan-badge.starter  { background: #DBEAFE; color: #1E40AF; }
.plan-badge.pro      { background: #FED7AA; color: #9A3412; }
.plan-badge.business { background: #EDE9FE; color: #5B21B6; }

/* ── Credits bar ─────────────────────────────────────────────────────────── */
.credits-bar-wrap { background: #E2E8F0; border-radius: 6px; height: 8px; overflow: hidden; margin: 8px 0; }
.credits-bar-fill { height: 100%; border-radius: 6px; background: linear-gradient(90deg, #10B981, #3B82F6); transition: width .4s; }

/* ── Modal ───────────────────────────────────────────────────────────────── */
.modal-overlay {
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.5);
    display: flex; align-items: center; justify-content: center;
    z-index: 500;
}

.modal {
    background: white;
    border-radius: 12px;
    width: 100%;
    max-width: 520px;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: var(--shadow-md);
}

.modal-header {
    padding: 20px 24px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.modal-header h3 { font-size: 1rem; font-weight: 600; }
.modal-body  { padding: 24px; }
.modal-footer { padding: 16px 24px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 8px; }

/* ── Empty state ─────────────────────────────────────────────────────────── */
.empty-state { text-align: center; padding: 48px 24px; color: var(--text-muted); }
.empty-state .empty-icon { font-size: 3rem; margin-bottom: 12px; }
.empty-state p { font-size: 0.9rem; }

/* ── Utilities ───────────────────────────────────────────────────────────── */
.flex { display: flex; }
.flex-between { display: flex; justify-content: space-between; align-items: center; }
.gap-2 { gap: 8px; }
.gap-3 { gap: 12px; }
.mb-3 { margin-bottom: 12px; }
.mb-4 { margin-bottom: 16px; }
.mb-6 { margin-bottom: 24px; }
.text-muted { color: var(--text-muted); font-size: 0.85rem; }
.text-success { color: var(--success); }
.text-danger  { color: var(--danger); }
.fw-600 { font-weight: 600; }

/* ── View toggle (Lista / Mappa) ─────────────────────────────────────────── */
.view-toggle {
    display: flex;
    border: 1px solid var(--border);
    border-radius: 8px;
    overflow: hidden;
}

.view-toggle-btn {
    padding: 6px 14px;
    font-size: 0.82rem;
    font-weight: 500;
    background: white;
    color: var(--text-muted);
    border: none;
    cursor: pointer;
    transition: all 0.15s;
}

.view-toggle-btn:hover { background: var(--bg); color: var(--text); }
.view-toggle-btn.active { background: var(--primary); color: white; }

/* ── Map editor ──────────────────────────────────────────────────────────── */
.map-toolbar {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 12px;
}

/* ── Mobile hamburger button (hidden on desktop) ─────────────────────────── */
.mobile-menu-btn {
    display: none;
    align-items: center;
    justify-content: center;
    width: 38px; height: 38px;
    background: none;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 1.1rem;
    cursor: pointer;
    color: var(--text);
    flex-shrink: 0;
}

/* ── Bottom navigation (hidden on desktop) ───────────────────────────────── */
.bottom-nav {
    display: none;
    position: fixed;
    bottom: 0; left: 0; right: 0;
    background: var(--bg-card);
    border-top: 1px solid var(--border);
    z-index: 250;
    height: 62px;
    padding: 0 4px;
    padding-bottom: env(safe-area-inset-bottom);
    justify-content: space-around;
    align-items: center;
    box-shadow: 0 -2px 16px rgba(0,0,0,0.08);
}

.bottom-nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    padding: 6px 10px;
    font-size: 0.62rem;
    font-weight: 600;
    color: var(--text-muted);
    text-decoration: none;
    border-radius: 10px;
    transition: color 0.15s, background 0.15s;
    flex: 1;
    max-width: 72px;
    letter-spacing: 0.01em;
}

.bottom-nav-item .bnav-icon { font-size: 1.35rem; line-height: 1; }
.bottom-nav-item.active { color: var(--primary); }
.bottom-nav-item:active { background: var(--primary-light); }

/* ── Mobile sidebar overlay ──────────────────────────────────────────────── */
.mobile-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.45);
    z-index: 299;
    backdrop-filter: blur(1px);
}
.mobile-overlay.open { display: block; }

/* ── Responsive: mobile breakpoint ──────────────────────────────────────── */
@media (max-width: 768px) {
    /* Sidebar slides in from left */
    .sidebar {
        transform: translateX(-100%);
        transition: transform 0.25s cubic-bezier(.4,0,.2,1);
        z-index: 300;
        box-shadow: 4px 0 24px rgba(0,0,0,0.25);
    }
    .sidebar.open { transform: translateX(0); }

    /* Main content full width */
    .main-content {
        margin-left: 0;
        padding-bottom: calc(62px + env(safe-area-inset-bottom));
    }

    /* Reduce page padding on mobile */
    .page-content { padding: 14px; }

    /* Show hamburger and bottom nav */
    .mobile-menu-btn { display: flex; }
    .bottom-nav      { display: flex; }

    /* Topbar adjustments */
    .topbar { padding: 0 12px; gap: 8px; }

    /* Stats grid: 2 columns on mobile */
    .stats-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }

    /* Modal full-width on mobile */
    .modal { max-width: 100%; border-radius: 16px 16px 0 0; margin-top: auto; }
    .modal-overlay { align-items: flex-end; }

    /* Tables: scroll horizontally */
    .table-responsive { overflow-x: auto; -webkit-overflow-scrolling: touch; }
}

/* ── Dark mode ───────────────────────────────────────────────────────────── */
[data-theme="dark"] {
    --bg:           #0F172A;
    --bg-card:      #1E293B;
    --border:       #334155;
    --text:         #F1F5F9;
    --text-muted:   #94A3B8;
    --primary-light:#1E3A5F;
    --shadow:       0 1px 4px rgba(0,0,0,.30);
    --shadow-md:    0 4px 16px rgba(0,0,0,.40);
}

/* Elementi con background:white hardcoded */
[data-theme="dark"] .stat-card           { background: var(--bg-card); }
[data-theme="dark"] .modal               { background: var(--bg-card); }
[data-theme="dark"] .form-control        { background: var(--bg-card); color: var(--text); border-color: var(--border); }
[data-theme="dark"] .form-control::placeholder { color: #475569; }
[data-theme="dark"] .view-toggle-btn     { background: var(--bg-card); }
[data-theme="dark"] .view-toggle-btn:hover { background: #253044; }

/* Topbar (usata anche da MainLayout) */
[data-theme="dark"] .topbar              { background: var(--bg-card); }

/* Tabelle */
[data-theme="dark"] .table tr:hover td  { background: #253044; }

/* Icone stat */
[data-theme="dark"] .stat-icon.green    { background: #052e16; }
[data-theme="dark"] .stat-icon.orange   { background: #431407; }
[data-theme="dark"] .stat-icon.red      { background: #450a0a; }

/* Badge */
[data-theme="dark"] .badge-green        { background: #052e16; color: #4ade80; }
[data-theme="dark"] .badge-orange       { background: #431407; color: #fbbf24; }
[data-theme="dark"] .badge-red          { background: #450a0a; color: #f87171; }
[data-theme="dark"] .badge-gray         { background: #253044;  color: #94a3b8; }
[data-theme="dark"] .badge-purple       { background: #2e1065; color: #c4b5fd; }

/* Pulsanti */
[data-theme="dark"] .btn-danger         { background: #450a0a; color: #f87171; }
[data-theme="dark"] .btn-danger:hover   { background: #5b1111; }
[data-theme="dark"] .btn-success        { background: #052e16; color: #4ade80; }
[data-theme="dark"] .btn-success:hover  { background: #063c1d; }
[data-theme="dark"] .btn-icon           { background: var(--bg-card); border-color: var(--border); color: var(--text); }

/* Alert */
[data-theme="dark"] .alert-danger       { background: #450a0a; color: #fca5a5; border-color: #7f1d1d; }
[data-theme="dark"] .alert-success      { background: #052e16; color: #86efac; border-color: #166534; }
[data-theme="dark"] .alert-info         { background: #1e3a5f; color: #93c5fd; border-color: #1e40af; }

/* Barra crediti AI */
[data-theme="dark"] .credits-bar-wrap   { background: var(--border); }

/* Bottom nav dark */
[data-theme="dark"] .bottom-nav         { background: var(--bg-card); border-color: var(--border); }
[data-theme="dark"] .mobile-menu-btn    { border-color: var(--border); color: var(--text); }

/* Plan cards */
[data-theme="dark"] .plan-card.plan-trial    { background: #0a1f12; border-color: #166534; }
[data-theme="dark"] .plan-card.plan-starter  { background: #0f1f36; border-color: #1e3a5f; }
[data-theme="dark"] .plan-card.plan-pro      { background: #1f1206; border-color: #7c2d12; }
[data-theme="dark"] .plan-card.plan-business { background: #1a0d2e; border-color: #4c1d95; }
