:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1e222b;
  --line: #2a2f3a;
  --text: #e7e9ee;
  --muted: #98a0b0;
  --accent: #e6c17a;
  --accent-ink: #14161b;
  --up: #f7768e;
  --down: #9ece6a;
  --warn: #e0af68;
  --radius: 10px;
  --chart-line: #e8bd63;
  --chart-grid: #262b36;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f6f7f9;
    --panel: #ffffff;
    --panel-2: #f0f2f5;
    --line: #dfe3ea;
    --text: #1a1d23;
    --muted: #5f6673;
    --accent: #8a6a1f;
    --accent-ink: #ffffff;
    --chart-line: #a06a00;
    --chart-grid: #e6e8ec;
    --up: #c2255c;
    --down: #2f7a34;
    --warn: #9a6b00;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

a { color: var(--accent); }

header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  padding: 12px 20px;
}

.bar { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; }
.spacer { flex: 1; }

.brand { display: flex; align-items: center; gap: 10px; margin-right: 4px; }
.brand-logo {
  width: 36px; height: 36px; border-radius: 9px; display: block; flex: none;
  box-shadow: 0 1px 4px rgba(0, 0, 0, .35);
}
.brand-text { display: flex; flex-direction: column; line-height: 1.2; min-width: 0; }
.brand-name { font-weight: 650; font-size: 17px; letter-spacing: .01em; }
.brand-text small { color: var(--muted); font-size: 12px; }

.pill {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 10px; border-radius: 999px;
  background: var(--panel-2); border: 1px solid var(--line);
  font-size: 13px; color: var(--muted);
}
.pill.live { color: var(--accent); border-color: var(--accent); }
.pill.err { color: var(--up); border-color: var(--up); }
.dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.pill.live .dot { animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .25; } }

button {
  font: inherit; font-size: 14px;
  padding: 6px 12px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text);
  cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: .45; cursor: not-allowed; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
button.danger:hover { border-color: var(--up); color: var(--up); }

progress { width: 100%; height: 4px; accent-color: var(--accent); }

/* Il cartello dell'aggiornamento in corso: visibile senza rubare la scena. */
.runbanner {
  display: flex; flex-direction: column; gap: 8px;
  margin-top: 10px; padding: 8px 12px;
  border: 1px solid var(--accent); border-radius: var(--radius);
  background: var(--panel-2);
}
/* `hidden` deve vincere su qualunque display delle classi (.tag e' inline-block):
   senza, un avviso nascosto si vede lo stesso come una pillola vuota. */
[hidden] { display: none !important; }
.runbanner[hidden] { display: none; }
.runbanner .head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; color: var(--accent); font-size: 14px; }
.runbanner .head .dot { animation: pulse 1.2s ease-in-out infinite; flex: none; }
.runbanner .head small { color: var(--muted); font-size: 12px; }

nav { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 10px; }
nav button { background: none; border: 1px solid transparent; color: var(--muted); }
nav button.active { color: var(--text); background: var(--panel-2); border-color: var(--line); }
nav button .badge {
  margin-left: 6px; padding: 0 6px; border-radius: 999px;
  background: var(--line); color: var(--text); font-size: 12px;
}

main { padding: 20px; max-width: 1400px; margin: 0 auto; }
section[hidden] { display: none; }

.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 18px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.card .k { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
.card .v { font-size: 24px; font-weight: 650; margin-top: 2px; }
.card .v.sm { font-size: 17px; line-height: 1.35; margin-top: 4px; }
.card .s { color: var(--muted); font-size: 12px; }

.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; margin-bottom: 18px; }
.panel > h2 { margin: 0; padding: 12px 14px; font-size: 14px; border-bottom: 1px solid var(--line); background: var(--panel-2); }
.panel > .body { padding: 14px; }

.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
input[type="search"], input.price, select {
  font: inherit; padding: 6px 10px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--panel); color: var(--text);
}
input[type="search"] { min-width: 220px; flex: 1; max-width: 380px; }
input.price { width: 110px; }
input.price.invalid { border-color: var(--up); }

.tablewrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 14px; }
th, td { text-align: left; padding: 7px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { position: sticky; top: 0; background: var(--panel-2); color: var(--muted); font-weight: 600; font-size: 12px;
     text-transform: uppercase; letter-spacing: .03em; cursor: pointer; user-select: none; }
th.nosort { cursor: default; }
tbody tr:hover { background: var(--panel-2); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; font-family: var(--mono); }
td.wrap { white-space: normal; min-width: 260px; }
.mono { font-family: var(--mono); font-size: 13px; }
.muted { color: var(--muted); }
.up { color: var(--up); }
.down { color: var(--down); }
.warn { color: var(--warn); }
.tag {
  display: inline-block; padding: 1px 7px; border-radius: 999px; font-size: 12px;
  background: var(--panel-2); border: 1px solid var(--line); color: var(--muted);
}
.tag.ok { color: var(--down); }
.tag.bad { color: var(--up); border-color: var(--up); }
.tag.partial { color: var(--warn); }

.links { display: flex; flex-wrap: wrap; gap: 8px; }
.links a {
  text-decoration: none; padding: 6px 12px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text); font-size: 14px;
}
.links a:hover { border-color: var(--accent); color: var(--accent); }

.log { font-family: var(--mono); font-size: 12.5px; max-height: 380px; overflow-y: auto; }
.log div { padding: 2px 0; border-bottom: 1px solid transparent; }
.log .t { color: var(--muted); margin-right: 8px; }

.empty { color: var(--muted); padding: 24px; text-align: center; }
footer { color: var(--muted); font-size: 12px; padding: 0 20px 24px; max-width: 1400px; margin: 0 auto; }

/* --- storico prezzi ------------------------------------------------------ */
dialog {
  border: 1px solid var(--line); border-radius: var(--radius); padding: 0;
  background: var(--panel); color: var(--text);
  width: min(760px, 94vw); max-height: 88vh; overflow: auto;
}
dialog::backdrop { background: rgba(0, 0, 0, .55); }
.dialog-head {
  display: flex; align-items: flex-start; gap: 12px;
  padding: 14px 16px; border-bottom: 1px solid var(--line); background: var(--panel-2);
  position: sticky; top: 0;
}
.dialog-head h2 { margin: 0; font-size: 16px; }
.dialog-head p { margin: 2px 0 0; font-size: 12px; }
.dialog-head button { margin-left: auto; }
#historyBody, .dialog-body { padding: 16px; }
.dialog-body label { display: block; margin-bottom: 6px; }
#refDialog { width: min(520px, 94vw); }

label.filebutton {
  padding: 6px 12px; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text); font-size: 14px;
}
label.filebutton:hover { border-color: var(--accent); color: var(--accent); }

.hero { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; margin-bottom: 4px; }
.hero .price { font-size: 30px; font-weight: 650; }
.hero .delta { font-size: 15px; }

.chart-wrap { position: relative; margin: 10px 0 18px; }
.chart { width: 100%; height: auto; display: block; overflow: visible; }
.chart .grid { stroke: var(--chart-grid); stroke-width: 1; }
.chart .line { fill: none; stroke: var(--chart-line); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .dot { fill: var(--chart-line); stroke: var(--panel); stroke-width: 2; }
.chart .tick { fill: var(--muted); font-size: 11px; font-family: var(--mono); }
.chart .label { fill: var(--text); font-size: 12px; font-weight: 600; }
.chart .crosshair { stroke: var(--muted); stroke-width: 1; }
.chart .hit { fill: transparent; cursor: crosshair; }

.tip {
  position: absolute; pointer-events: none; transform: translate(-50%, -100%);
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px;
  padding: 5px 9px; font-size: 12px; white-space: nowrap; box-shadow: 0 2px 10px rgba(0,0,0,.25);
}
.tip b { font-family: var(--mono); }

button.link {
  border: none; background: none; color: var(--accent); padding: 2px 4px;
  font-size: 13px; text-decoration: underline; cursor: pointer;
}
button.link:hover { color: var(--text); }

/* La stellina degli osservati: piena e in colore quando il prodotto e' seguito. */
button.star {
  border: none; background: none; padding: 0 4px; font-size: 17px; line-height: 1;
  color: var(--muted); cursor: pointer;
}
button.star:hover, button.star[aria-pressed="true"] { color: var(--accent); }

/* --------------------------------------------------------------- accesso */

.loginpage {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.loginbox { width: 100%; max-width: 380px; }

.loginbox .panel { margin: 0; }

.loginbox .body { display: flex; flex-direction: column; gap: 14px; }

.loginhead { display: flex; align-items: center; gap: 12px; margin-bottom: 6px; }

.loginlabel {
  font-size: 13px;
  color: var(--muted);
  letter-spacing: 0.03em;
  text-transform: uppercase;
}

.loginbox input[type="password"] {
  font: inherit;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  width: 100%;
}

.loginbox input[type="password"]:focus-visible,
.loginbox button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.loginbox button { padding: 10px 12px; }

.loginerror {
  margin: 0;
  color: var(--up);
  font-size: 14px;
}

/* Lo usa la nota sotto i download: era un attributo style, che la CSP vieta. */
.tight { margin-bottom: 0; }

/*
 * Il messaggio di errore per chi non ha JavaScript: il server rimanda a
 * /login.html#errore e :target lo accende. Senza fragment resta spento, quindi
 * alla prima visita non si vede - che e' il motivo per cui non basta un
 * <noscript>, che comparirebbe sempre.
 */
.loginerror.fallback { display: none; }
.loginerror.fallback:target { display: block; }
