/* ── Variables / reset ─────────────────────────────────────────────── */
:root {
    --bg: #0b0f14;
    --surface: #11161d;
    --surface-2: #161d26;
    --border: rgba(255, 255, 255, 0.06);
    --text: #e5e7eb;
    --text-dim: #9ca3af;
    --text-hint: #6b7280;
    --green: #22c55e;
    --red: #ef4444;
    --amber: #eab308;
    --blue: #60a5fa;
    --radius: 14px;
    --tabbar-h: 58px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
    background: var(--bg);
    color: var(--text);
    font: 15px/1.45 -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    -webkit-font-smoothing: antialiased;
    height: 100%;
}

/* ── Layout ────────────────────────────────────────────────────────── */
.topbar {
    position: sticky; top: 0; z-index: 20;
    display: flex; align-items: center; justify-content: space-between;
    padding: calc(env(safe-area-inset-top) + 12px) 16px 12px;
    background: rgba(11, 15, 20, 0.92);
    backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border);
}
.brand { font-weight: 800; font-size: 17px; letter-spacing: 0.3px; }
.brand span { color: var(--text-hint); font-weight: 500; font-size: 13px; }
.freshness { font-size: 12px; color: var(--text-hint); }

#view {
    max-width: 760px;
    margin: 0 auto;
    padding: 14px 14px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 20px);
}

.tabbar {
    position: fixed; bottom: 0; left: 0; right: 0; z-index: 20;
    display: flex;
    background: rgba(11, 15, 20, 0.95);
    backdrop-filter: blur(12px);
    border-top: 1px solid var(--border);
    padding-bottom: env(safe-area-inset-bottom);
    height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
}
.tabbar a {
    flex: 1;
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 2px;
    color: var(--text-hint);
    text-decoration: none;
    font-size: 10.5px; font-weight: 600;
}
.tabbar a .ico { font-size: 17px; line-height: 1; }
.tabbar a.active { color: var(--blue); }

/* Dashboard: su mobile .dash-top/.dash-side/.dash-main sono blocchi normali
   (le card hanno già il loro margin); su desktop largo diventano 2 colonne. */
@media (min-width: 1100px) {
    #view:has(.dash-top) { max-width: 1320px; }
    .dash-top {
        display: grid;
        grid-template-columns: 380px minmax(0, 1fr);
        gap: 12px;
        align-items: start;
    }
    .dash-main { position: sticky; top: 70px; }
    .dash-main .chart-main { height: clamp(480px, 72vh, 860px); }
}

/* Desktop: nav in alto */
@media (min-width: 768px) {
    .tabbar {
        top: 0; bottom: auto;
        left: auto; right: 16px;
        width: auto; height: auto;
        background: transparent; border: none; backdrop-filter: none;
        padding: 14px 0;
        gap: 4px;
    }
    .tabbar a { flex-direction: row; gap: 6px; padding: 6px 12px; border-radius: 8px; font-size: 13px; }
    .tabbar a.active { background: var(--surface-2); }
    #view { padding-bottom: 40px; }
    .topbar { padding-right: 480px; }
}

/* ── Cards / typography ────────────────────────────────────────────── */
.card {
    background: linear-gradient(180deg, rgba(255,255,255,0.025), transparent 36%), var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    margin-bottom: 12px;
    box-shadow: 0 1px 8px rgba(0, 0, 0, 0.25);
}
.card h3 {
    font-size: 13px; font-weight: 700;
    color: var(--text-dim);
    text-transform: uppercase; letter-spacing: 0.6px;
    margin-bottom: 10px;
}
.card p { margin: 6px 0; }
.hint { color: var(--text-hint); font-size: 12.5px; }
.mono { font-family: 'SF Mono', ui-monospace, monospace; font-size: 13px; }
.center { text-align: center; }
.pos { color: var(--green); }
.neg { color: var(--red); }

.error-card { border-color: rgba(239, 68, 68, 0.3); color: #fca5a5; }
.warn-card { border-color: rgba(234, 179, 8, 0.3); color: #fde68a; font-size: 13.5px; }

/* ── Badges / chips ────────────────────────────────────────────────── */
.badge {
    display: inline-block;
    padding: 2px 8px; border-radius: 6px;
    font-size: 10.5px; font-weight: 800; letter-spacing: 0.5px;
}
.badge.long { background: rgba(34, 197, 94, 0.15); color: var(--green); }
.badge.short { background: rgba(239, 68, 68, 0.15); color: var(--red); }
.badge.neutral { background: rgba(156, 163, 175, 0.12); color: var(--text-dim); }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0; justify-content: center; }
.chip {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 5px 12px;
    font-size: 12.5px; color: var(--text-dim);
}
.chip b { color: var(--text); }

.meta-row {
    display: flex; flex-wrap: wrap; gap: 14px;
    align-items: baseline;
    font-size: 13.5px; color: var(--text-dim);
    margin-top: 8px;
}
.meta-row b { color: var(--text); font-size: 15px; }

/* ── Stats grid ────────────────────────────────────────────────────── */
.stat-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
    gap: 8px;
    margin: 8px 0;
}
.stat {
    background: var(--surface-2);
    border-radius: 10px;
    padding: 9px 12px;
    display: flex; flex-direction: column; gap: 1px;
}
.stat .hint { font-size: 11px; text-transform: uppercase; letter-spacing: 0.4px; }
.stat b { font-size: 15.5px; font-variant-numeric: tabular-nums; }

/* Riga di contesto storico sotto la stat: "▲ +2,1% · 87° pct" */
.stat-ctx {
    font-size: 11px;
    color: var(--text-hint);
    font-variant-numeric: tabular-nums;
    margin-top: 1px;
}
.stat-ctx .pos { color: var(--green); }
.stat-ctx .neg { color: var(--red); }

/* ── Signals ───────────────────────────────────────────────────────── */
.signal-row { padding: 10px 0; border-bottom: 1px solid var(--border); }
.signal-row:last-child { border-bottom: none; }
.signal-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.tool-name { font-weight: 700; font-size: 13.5px; text-transform: uppercase; letter-spacing: 0.4px; }
.signal-rationale { color: var(--text-dim); font-size: 13.5px; margin-top: 3px; }

/* ── Tables ────────────────────────────────────────────────────────── */
.tbl { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.tbl th {
    text-align: left; padding: 6px 8px;
    color: var(--text-hint); font-size: 11px;
    text-transform: uppercase; letter-spacing: 0.5px;
    border-bottom: 1px solid var(--border);
}
.tbl td { padding: 8px; border-bottom: 1px solid var(--border); }
.tbl tr:last-child td { border-bottom: none; }
.row-sup td:first-child { border-left: 3px solid var(--green); }
.row-res td:first-child { border-left: 3px solid var(--red); }

/* ── Charts (ECharts: il div .ec È il contenitore con altezza) ─────── */
.ec { width: 100%; height: 100%; }
.chart-wrap { position: relative; height: clamp(220px, 34vh, 320px); }
.chart-tall { position: relative; height: clamp(320px, 52vh, 520px); }
.chart-xtall { position: relative; height: clamp(380px, 60vh, 620px); }
.chart-main { position: relative; height: clamp(400px, 58vh, 720px); touch-action: pan-y pinch-zoom; }

/* Card header con azioni (es. bottone Espandi) */
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.card-head h3 { margin-bottom: 10px; }

/* Overlay fullscreen del chart */
.chart-overlay {
    position: fixed; inset: 0; z-index: 100;
    background: var(--bg);
    display: flex; flex-direction: column;
    padding: calc(env(safe-area-inset-top) + 8px) 8px env(safe-area-inset-bottom);
}
.chart-overlay-bar {
    display: flex; align-items: center; justify-content: space-between;
    padding: 6px 8px 10px;
    font-size: 14px; color: var(--text-dim);
}
.chart-overlay-body { flex: 1; min-height: 0; touch-action: none; }
.gauge-wrap { position: relative; height: 150px; max-width: 300px; margin: 0 auto; }

/* ── Hero dashboard ────────────────────────────────────────────────── */
.hero { text-align: center; }
.hero-spot { display: flex; align-items: baseline; justify-content: center; gap: 10px; }
.hero-price {
    font-size: 40px; font-weight: 800; letter-spacing: -0.5px;
    font-variant-numeric: tabular-nums;
    color: var(--text);
}
.hero-change { font-size: 16px; font-weight: 700; font-variant-numeric: tabular-nums; }
.hero-change.pos { color: var(--green); }
.hero-change.neg { color: var(--red); }

/* ── Transizione di rotta ──────────────────────────────────────────── */
.view-fade { animation: viewFade 0.18s ease-out; }
@keyframes viewFade {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: none; }
}

/* Numeri sempre tabulari */
.stat b, .meta-row b, .tbl td, .mono, .chip b { font-variant-numeric: tabular-nums; }

/* Segmented control sticky (pagina Opzioni) */
.sticky-pills {
    position: sticky;
    top: calc(env(safe-area-inset-top) + 50px);
    z-index: 10;
    background: rgba(11, 15, 20, 0.92);
    backdrop-filter: blur(10px);
    margin: -4px -14px 8px;
    padding: 8px 14px;
}

/* ── Pills / buttons / form ────────────────────────────────────────── */
.pills { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 8px; margin-bottom: 4px; }
.pills.small { padding-bottom: 0; }
.pill {
    flex-shrink: 0;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 7px 14px;
    color: var(--text-dim);
    font-size: 13px; font-weight: 600;
    text-decoration: none;
    cursor: pointer;
}
.pill.active { background: var(--blue); border-color: var(--blue); color: #0b0f14; }

.btn {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 10px;
    color: var(--text);
    padding: 9px 14px;
    font-size: 13.5px; font-weight: 600;
    cursor: pointer;
}
.btn.primary { background: var(--blue); border-color: var(--blue); color: #0b0f14; }
.btn.danger { color: var(--red); }
.btn.small { padding: 5px 10px; font-size: 12px; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }

.form-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 10px; }
.input {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 10px;
    color: var(--text);
    padding: 9px 12px;
    font-size: 14px;
    min-width: 0; flex: 1;
}
select.input { appearance: none; }

.alert-row {
    display: flex; align-items: center; justify-content: space-between;
    padding: 10px 0;
    border-bottom: 1px solid var(--border);
}
.alert-row:last-child { border-bottom: none; }
.alert-row .hint { display: block; }
.alert-row.triggered { opacity: 0.5; }

/* ── Report /sp ────────────────────────────────────────────────────── */
details summary {
    cursor: pointer;
    color: var(--blue);
    font-weight: 600; font-size: 13.5px;
    padding: 4px 0;
}
.report-text {
    white-space: pre-wrap;
    font-size: 13.5px;
    line-height: 1.55;
    margin-top: 10px;
    overflow-wrap: break-word;
}
.report-text code, .report-text pre {
    font-family: 'SF Mono', ui-monospace, monospace;
    font-size: 12.5px;
    background: var(--surface-2);
    border-radius: 4px;
    padding: 1px 4px;
}
.report-text blockquote {
    border-left: 3px solid var(--border);
    padding-left: 10px;
    color: var(--text-dim);
    margin: 6px 0;
}

/* ── Skeleton / toast ──────────────────────────────────────────────── */
.skeleton {
    border-radius: 10px;
    background: linear-gradient(100deg, var(--surface-2) 40%, #1c2530 50%, var(--surface-2) 60%);
    background-size: 200% 100%;
    animation: shimmer 1.4s infinite;
}
@keyframes shimmer { to { background-position: -200% 0; } }

.toast {
    position: fixed;
    bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 14px);
    left: 50%;
    transform: translateX(-50%) translateY(16px);
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 10px 18px;
    font-size: 13.5px;
    opacity: 0; pointer-events: none;
    transition: opacity 0.25s, transform 0.25s;
    z-index: 50;
    max-width: 90vw;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { border-color: rgba(239, 68, 68, 0.4); color: #fca5a5; }
