/* ─────────────────────────────────────────────────────────────────────────────
   ALFA – stile mobile-first. Tema scuro predefinito, chiaro se richiesto dal
   sistema. Testi grandi e alto contrasto: l'app si usa a distanza di braccio.
   ───────────────────────────────────────────────────────────────────────── */
:root {
  --bg: #0e1116;
  --surface: #171b22;
  --surface-2: #1f2530;
  --border: #2a313d;
  --text: #eef1f5;
  --muted: #9aa4b2;
  --accent: #4f8cff;
  --accent-2: #2dd4bf;
  --warn: #f59e0b;
  --danger: #f43f5e;
  --them: #1f2a3d;
  --me: #173a35;
  --radius: 18px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f6f7f9;
    --surface: #ffffff;
    --surface-2: #eef1f5;
    --border: #dde2e9;
    --text: #12161c;
    --muted: #5d6673;
    --them: #e6eefc;
    --me: #dcf5ef;
    color-scheme: light;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

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

.app {
  display: flex;
  flex-direction: column;
  height: 100vh; /* fallback */
  height: 100dvh;
  max-width: 760px;
  margin: 0 auto;
}

/* ── Topbar ─────────────────────────────────────────────────────────────── */
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(10px + var(--safe-top)) 12px 10px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.brand { display: none; align-items: center; gap: 8px; font-weight: 700; letter-spacing: .04em; }
@media (min-width: 560px) { .brand { display: flex; } }

.langs { flex: 1; display: flex; align-items: flex-end; gap: 6px; min-width: 0; }
.lang-pick { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.lang-role { font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); padding-left: 4px; }
.lang-pick select {
  width: 100%;
  appearance: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 9px 10px;
  font-size: 15px;
  text-overflow: ellipsis;
}

.icon-btn {
  display: inline-grid;
  place-items: center;
  min-width: 40px;
  height: 40px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }

/* ── Banner & toast ─────────────────────────────────────────────────────── */
.banner {
  display: flex; align-items: center; gap: 8px;
  margin: 8px 12px 0; padding: 8px 8px 8px 14px;
  background: var(--surface-2); border-radius: 14px; font-size: 14px;
}
.banner span { flex: 1; }
.toast {
  margin: 8px 12px 0; padding: 10px 14px;
  border-radius: 14px; font-size: 14px;
  background: color-mix(in srgb, var(--warn) 18%, var(--surface));
  border: 1px solid color-mix(in srgb, var(--warn) 45%, transparent);
}
.toast.error {
  background: color-mix(in srgb, var(--danger) 16%, var(--surface));
  border-color: color-mix(in srgb, var(--danger) 45%, transparent);
}

/* ── Conversazione ──────────────────────────────────────────────────────── */
.conversation {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 14px 12px 24px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scroll-behavior: smooth;
}

.empty { margin: auto 4px; color: var(--muted); max-width: 520px; }
.empty h1 { color: var(--text); font-size: 28px; margin: 0 0 8px; }
.empty ol { padding-left: 20px; }
.empty .hint { font-size: 14px; }

.turn {
  max-width: 88%;
  padding: 12px 14px;
  border-radius: var(--radius);
  background: var(--them);
  align-self: flex-start;
  border-bottom-left-radius: 6px;
  animation: pop .18s ease-out;
}
.turn.me { align-self: flex-end; background: var(--me); border-bottom-left-radius: var(--radius); border-bottom-right-radius: 6px; }
.turn .meta { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.turn .translation { font-size: 21px; line-height: 1.35; font-weight: 500; min-height: 1.35em; overflow-wrap: anywhere; }
.turn .source { margin-top: 6px; font-size: 14px; color: var(--muted); overflow-wrap: anywhere; }
.turn.translating .translation:empty::after { content: "…"; animation: blink 1s infinite; }
.turn.discarded { opacity: .45; }
.turn.discarded .translation::after { content: "(rumore ignorato)"; font-size: 14px; font-weight: 400; color: var(--muted); }
.turn.error .translation { color: var(--danger); font-size: 15px; }
.hide-source .turn .source { display: none; }

@keyframes pop { from { transform: translateY(6px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes blink { 50% { opacity: .2; } }

/* ── Dock inferiore ─────────────────────────────────────────────────────── */
.dock {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 14px calc(14px + var(--safe-bottom));
  border-top: 1px solid var(--border);
  background: var(--surface);
}
.live { flex: 1; min-width: 0; }
.listening-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.phase { font-weight: 600; }
.interim {
  margin: 4px 0 0;
  min-height: 1.4em;
  color: var(--muted);
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  direction: rtl; /* mostra la parte finale del testo in corso */
  text-align: left;
}
.chip {
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 13px;
  cursor: pointer;
}

/* L'orb è sia pulsante Avvia/Stop sia indicatore di stato */
.orb {
  --level: 0;
  position: relative;
  width: 78px; height: 78px;
  flex: none;
  border-radius: 50%;
  border: 0;
  cursor: pointer;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  box-shadow: 0 6px 24px color-mix(in srgb, var(--accent) 45%, transparent);
  transition: background .25s, transform .1s;
}
.orb:active { transform: scale(.96); }
.orb-ring {
  position: absolute; inset: -6px;
  border-radius: 50%;
  border: 3px solid currentColor;
  opacity: 0;
  transform: scale(calc(1 + var(--level) * .25));
  transition: transform .08s linear, opacity .2s;
  pointer-events: none;
  color: var(--accent);
}
.orb-label { position: relative; font-size: 15px; }
.orb[data-phase="listening"], .orb[data-phase="hearing"] { background: var(--accent-2); color: #04201c; }
.orb[data-phase="listening"] .orb-ring, .orb[data-phase="hearing"] .orb-ring { opacity: .7; color: var(--accent-2); }
.orb[data-phase="hearing"] .orb-ring { animation: pulse 1s infinite; }
.orb[data-phase="translating"] { background: var(--warn); color: #2a1a00; }
.orb[data-phase="speaking"] { background: var(--accent); }
.orb[data-phase="speaking"] .orb-ring { opacity: .8; animation: pulse 1.2s infinite; }

@keyframes pulse { 0% { transform: scale(1); opacity: .8; } 100% { transform: scale(1.35); opacity: 0; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* ── Pulsanti ───────────────────────────────────────────────────────────── */
.btn {
  border: 0; border-radius: 12px; padding: 12px 18px;
  background: var(--accent); color: #fff; font-weight: 600; cursor: pointer;
}
.btn-small { padding: 6px 12px; font-size: 14px; }
.btn-ghost { background: var(--surface-2); color: var(--text); }

/* ── Foglio impostazioni ────────────────────────────────────────────────── */
.sheet {
  width: min(560px, 100%);
  max-height: 92dvh;
  margin: auto auto 0;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 22px 22px 0 0;
  background: var(--surface);
  color: var(--text);
}
@media (min-width: 600px) { .sheet { margin: auto; border-radius: 22px; } }
.sheet::backdrop { background: rgb(0 0 0 / .5); }
.sheet form { padding: 4px 18px calc(18px + var(--safe-bottom)); display: grid; gap: 14px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; position: sticky; top: 0; background: var(--surface); padding-top: 10px; }
.sheet h2 { margin: 0; font-size: 20px; }
fieldset { border: 1px solid var(--border); border-radius: 16px; padding: 10px 14px 14px; display: grid; gap: 12px; margin: 0; }
legend { padding: 0 6px; color: var(--muted); font-size: 13px; text-transform: uppercase; letter-spacing: .06em; }
.field { display: grid; gap: 4px; font-size: 14px; }
.field select, .field input[type="password"] {
  width: 100%; padding: 10px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface-2);
}
.field input[type="range"] { width: 100%; accent-color: var(--accent); }
.switch { display: flex; gap: 10px; align-items: flex-start; font-size: 15px; }
.switch input { width: 20px; height: 20px; margin-top: 2px; accent-color: var(--accent); flex: none; }
.switch small { display: block; color: var(--muted); font-size: 13px; }
.server-info { margin: 0; font-size: 12px; color: var(--muted); }
[hidden] { display: none !important; }
