/* style.css – Aspetto grafico
   I colori sono "variabili" (--nome): cambiandoli qui cambia tutta l'app.
   Un solo colore d'accento (blu); verde/rosso/ambra solo per esiti e avvisi. */

:root {
  --bg: #ffffff;
  --surface: #f4f5f7;
  --text: #1a1c1f;
  --muted: #5c636d;
  --border: #d6dae0;
  --accent: #1f62d6;
  --accent-text: #ffffff;
  --ok: #1a7f37;
  --ok-bg: #e5f4e9;
  --ko: #b42318;
  --ko-bg: #fdeceb;
  --warn: #7a4b00;
  --warn-bg: #ffefc2;
  --grey-bg: #e9ebee;
  --radius: 12px;
  --tabbar-h: 60px;
}

/* Tema scuro automatico, se il telefono/PC è impostato in modalità scura */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121417;
    --surface: #1d2025;
    --text: #e8eaed;
    --muted: #a0a7b1;
    --border: #353a42;
    --accent: #5b9bff;
    --accent-text: #0b1220;
    --ok: #5fd07f;
    --ok-bg: #16301f;
    --ko: #ff8a80;
    --ko-bg: #3a1a18;
    --warn: #ffd27a;
    --warn-bg: #3a2c0b;
    --grey-bg: #2a2e34;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

button, input, textarea { font: inherit; color: inherit; }

/* ---------- Parte alta ---------- */
.top { position: sticky; top: 0; z-index: 10; background: var(--bg); }

.banner {
  padding: 10px 16px;
  font-size: 14px;
  text-align: center;
  background: var(--warn-bg);
  color: var(--warn);
  border-bottom: 1px solid var(--border);
}
.banner.network { background: var(--grey-bg); color: var(--muted); }

.topbar {
  max-width: 600px;
  margin: 0 auto;
  padding: 4px 8px 4px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
}
.app-title { font-weight: 600; font-size: 15px; color: var(--muted); }

.icon-btn {
  min-width: 44px; min-height: 44px;
  border: 0; background: none; font-size: 22px; cursor: pointer; border-radius: var(--radius);
}

/* ---------- Area centrale ---------- */
.view {
  max-width: 600px;
  margin: 0 auto;
  padding: 16px 16px calc(var(--tabbar-h) + 12px + env(safe-area-inset-bottom));
}

h2 { font-size: 22px; margin: 8px 0 16px; }
h3 { font-size: 16px; margin: 16px 0 8px; }

.empty { text-align: center; color: var(--muted); padding: 48px 0; }
.hint { color: var(--muted); font-size: 14px; }

/* ---------- Esercizio ---------- */
.exercise {
  display: flex;
  flex-direction: column;
  /* occupa tutta l'altezza disponibile: il pulsante va in basso, vicino al pollice */
  /* --top-h = altezza reale della parte alta (banner compreso), misurata da app.js */
  min-height: calc(100dvh - var(--top-h, 53px) - var(--tabbar-h) - 40px - env(safe-area-inset-bottom));
}

.ex-header {
  display: flex; gap: 12px; align-items: center;
  font-size: 14px; color: var(--muted);
}
.ex-profile { font-weight: 600; color: var(--text); }
.ex-count { margin-left: auto; font-variant-numeric: tabular-nums; }

.progress { height: 4px; background: var(--surface); border-radius: 2px; margin: 8px 0 20px; overflow: hidden; }
.progress-fill { height: 100%; background: var(--accent); transition: width .2s; }

.ex-label { font-size: 13px; color: var(--muted); margin: 0 0 8px; text-transform: none; }
.ex-prompt { font-size: 20px; line-height: 1.4; font-weight: 500; margin: 0 0 20px; }

.ex-input {
  width: 100%;
  min-height: 96px;
  padding: 12px 14px;
  font-size: 18px;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  resize: vertical;
}
.ex-input:focus { outline: none; border-color: var(--accent); }
.ex-input.is-correct { border-color: var(--ok); }

.feedback { margin-top: 16px; }
.feedback.ok strong { color: var(--ok); font-size: 18px; }
.rule-line { margin: 4px 0 0; color: var(--muted); }

.answer-wrong, .answer-right {
  margin: 0 0 8px; padding: 10px 14px; border-radius: var(--radius); font-size: 18px;
}
.answer-wrong { background: var(--ko-bg); color: var(--ko); }
.answer-right { background: var(--ok-bg); color: var(--ok); font-weight: 500; }

.why { margin-top: 12px; padding: 12px 14px; background: var(--surface); border-radius: var(--radius); }
.why h3 { margin: 0 0 6px; font-size: 15px; }
.why p { margin: 0 0 6px; }
.tip { color: var(--muted); font-size: 15px; }

.ex-links { display: flex; flex-wrap: wrap; gap: 4px 16px; margin-top: 12px; }
.link {
  min-height: 44px; display: inline-flex; align-items: center;
  border: 0; background: none; padding: 0; cursor: pointer;
  color: var(--accent); text-decoration: none; font-size: 15px;
}

.actions { margin-top: auto; padding-top: 20px; }
/* il pulsante resta sempre visibile sopra la barra in basso, anche con testi lunghi */
.exercise > .actions {
  position: sticky;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: 12px;
  background: var(--bg);
}
.actions.stacked { display: grid; gap: 10px; margin-top: 24px; }

.btn-primary, .btn-secondary {
  width: 100%; min-height: 52px;
  border-radius: var(--radius); font-size: 18px; font-weight: 600; cursor: pointer;
}
.btn-primary { border: 0; background: var(--accent); color: var(--accent-text); }
.btn-secondary { border: 2px solid var(--accent); background: none; color: var(--accent); }
.btn-primary:disabled { opacity: .5; cursor: default; }
.btn-primary:focus { outline: none; }
.btn-primary:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* ---------- Riepilogo ---------- */
.score { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.score-box { padding: 16px; border-radius: var(--radius); text-align: center; }
.score-box strong { display: block; font-size: 32px; }
.score-box.ok { background: var(--ok-bg); color: var(--ok); }
.score-box.ko { background: var(--ko-bg); color: var(--ko); }
.weak-list { padding-left: 20px; }

/* ---------- Regole ---------- */
.rule { border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 10px; padding: 0 14px; }
.rule summary { min-height: 48px; display: flex; align-items: center; font-weight: 600; cursor: pointer; }
.rule[open] { padding-bottom: 12px; }
.rule-summary { font-weight: 500; }
.examples { padding-left: 18px; }
.examples li { margin-bottom: 8px; }
.examples small { color: var(--muted); }

/* ---------- Impostazioni ---------- */
.field-label { display: block; margin-bottom: 14px; font-size: 14px; color: var(--muted); }
.field {
  display: block; width: 100%; margin-top: 4px; min-height: 44px; padding: 8px 12px;
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--bg); font-size: 16px;
}
.topic-list { display: grid; gap: 6px; }
.topic-item {
  display: grid; grid-template-columns: 24px 1fr auto; align-items: center; gap: 10px;
  min-height: 48px; padding: 4px 12px; border: 1px solid var(--border); border-radius: var(--radius);
}
.topic-item input { width: 20px; height: 20px; accent-color: var(--accent); }
.topic-item small { color: var(--muted); }

/* ---------- Barra in basso ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(4, 1fr);
  max-width: 600px; margin: 0 auto;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--bg); border-top: 1px solid var(--border);
}
.tabbar button {
  border: 0; background: none; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: 12px; color: var(--muted);
}
.tabbar button span { font-size: 20px; line-height: 1; }
.tabbar button[aria-current] { color: var(--accent); font-weight: 600; }

.tabbar button { position: relative; }
.badge {
  position: absolute; top: 6px; left: calc(50% + 6px);
  min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px;
  background: var(--accent); color: var(--accent-text);
  font-size: 11px; line-height: 18px; font-weight: 700; text-align: center;
}

/* ---------- Aggiunte fasi 2-4 ---------- */
button.ex-profile { border: 0; background: none; padding: 0; min-height: 32px; cursor: pointer; font-weight: 600; }
.chip {
  display: inline-block; margin-left: 8px; padding: 0 8px; border-radius: 10px;
  background: var(--surface); color: var(--accent); font-size: 12px;
}
.spelling { margin: 6px 0 0; color: var(--warn); }
.spelling del { color: var(--ko); }
.source-note { color: var(--muted); font-size: 13px; margin-top: 6px; }
.loading { display: flex; align-items: center; gap: 10px; color: var(--muted); }
.spinner {
  width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid var(--border); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 3s; } }

.link.muted { color: var(--muted); cursor: default; }
.link.small { font-size: 13px; color: var(--muted); }

.report-panel { margin-top: 8px; padding: 12px; border: 1px solid var(--border); border-radius: var(--radius); }
.report-panel .hint { margin: 0 0 8px; }
.report-options { display: grid; gap: 6px; margin: 10px 0 4px; }
.report-reason {
  min-height: 44px; text-align: left; padding: 8px 12px; cursor: pointer;
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface);
}

.tiles { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.tile { padding: 12px; background: var(--surface); border-radius: var(--radius); }
.tile strong { display: block; font-size: 24px; }
.tile span { font-size: 13px; color: var(--muted); }
@media (min-width: 520px) { .tiles { grid-template-columns: repeat(4, 1fr); } }

.topic-card { padding: 12px 14px; border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 10px; }
.topic-card .hint { margin: 6px 0; }
.topic-card-head { display: flex; justify-content: space-between; gap: 8px; }
.pct { font-variant-numeric: tabular-nums; }
.bar { height: 6px; background: var(--surface); border-radius: 3px; margin-top: 8px; overflow: hidden; }
.bar-fill { height: 100%; background: var(--accent); }
.gen-row { display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: center; }
.field.small { margin-top: 0; font-size: 14px; }
.btn-small {
  min-height: 44px; padding: 0 14px; border-radius: var(--radius); cursor: pointer;
  border: 1px solid var(--accent); background: none; color: var(--accent); font-size: 14px; font-weight: 600;
}
.btn-small:disabled { opacity: .45; cursor: default; }

.plain-list { list-style: none; padding: 0; margin: 12px 0; }
.plain-list li {
  display: grid; gap: 2px; padding: 10px 0; border-bottom: 1px solid var(--border);
}
.plain-list small { color: var(--muted); }
.err-answer { color: var(--ko); }
.err-solution { color: var(--ok); }

.segmented { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-bottom: 12px; }
.segmented button {
  min-height: 44px; border-radius: var(--radius); cursor: pointer;
  border: 1px solid var(--border); background: none;
}
.segmented button[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); font-weight: 600; background: var(--surface); }
.button-list { display: grid; gap: 8px; }
.button-list .btn-secondary { font-size: 16px; min-height: 48px; }
.advanced summary { min-height: 44px; display: flex; align-items: center; cursor: pointer; color: var(--muted); }
.credits a { color: var(--accent); }

/* Su PC la barra in basso ha i bordi laterali per sembrare parte della colonna */
@media (min-width: 640px) {
  .tabbar { border-left: 1px solid var(--border); border-right: 1px solid var(--border); }
}
