/* ChiParla – stile pensato per iPhone, uso con una mano. */

:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #eceef2;
  --text: #15171b;
  --muted: #5f6672;
  --border: #d8dce3;
  --primary: #2156d9;
  --primary-fg: #ffffff;
  --danger: #d23228;
  --ok: #17803d;
  --warn: #8a5a00;
  --warn-bg: #fff3d4;
  --bad: #c42b1f;
  --bad-bg: #fde7e5;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #181b21;
    --surface-2: #242830;
    --text: #eef0f3;
    --muted: #9ba3ae;
    --border: #2d323b;
    --primary: #5b86ff;
    --ok: #5fd08a;
    --warn: #ffd46b;
    --warn-bg: #3a2f10;
    --bad: #ff8a80;
    --bad-bg: #4a1714;
  }
}

/* La schermata di registrazione è sempre scura: meno distrazione e meno batteria sugli OLED */
body.recording {
  --bg: #000000;
  --surface: #181b21;
  --surface-2: #242830;
  --text: #eef0f3;
  --muted: #9ba3ae;
  --border: #2d323b;
  --primary: #5b86ff;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html,
body {
  margin: 0;
  min-height: 100%;
  overscroll-behavior: none; /* niente "tira per ricaricare" che interromperebbe la registrazione */
}

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

body.recording { overflow: hidden; }

a { color: var(--primary); }
h1, h2 { line-height: 1.2; }
.muted { color: var(--muted); }
.small { font-size: 14px; }
.center { text-align: center; }
.mono { font-variant-numeric: tabular-nums; font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; }
[hidden] { display: none !important; }

.screen {
  max-width: 640px;
  margin: 0 auto;
  padding: calc(var(--safe-top) + 12px) 16px calc(var(--safe-bottom) + 24px);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.topbar { display: flex; align-items: center; gap: 8px; min-height: 48px; }
.topbar h1 { font-size: 20px; margin: 0; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.brand { display: flex; align-items: center; gap: 10px; }
.brand-dot { width: 14px; height: 14px; border-radius: 50%; background: #e53935; box-shadow: 0 0 0 5px rgba(229, 57, 53, .22); }
.title-btn {
  flex: 1; min-width: 0; font: inherit; font-size: 18px; font-weight: 700; color: var(--text);
  background: none; border: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 8px 4px;
}
.section-title { font-size: 16px; margin: 6px 0 0; color: var(--muted); font-weight: 600; }

/* ---------- pulsanti ---------- */

.btn {
  appearance: none;
  border: 1px solid transparent;
  border-radius: 12px;
  padding: 10px 16px;
  min-height: 48px;
  font: inherit;
  font-weight: 600;
  background: var(--surface-2);
  color: var(--text);
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  touch-action: manipulation;
}
.btn.primary { background: var(--primary); color: var(--primary-fg); }
.btn.secondary { background: var(--surface); border-color: var(--border); }
.btn.ghost { background: transparent; color: var(--primary); padding-inline: 8px; }
.btn.danger { background: var(--danger); color: #fff; }
.btn.small { min-height: 38px; padding: 6px 12px; font-size: 14px; }
.btn.xl { width: 100%; min-height: 60px; font-size: 19px; border-radius: 16px; }
.btn:disabled { opacity: .55; }
.btn:active { filter: brightness(.92); }
.link { background: none; border: none; padding: 0; font: inherit; color: var(--primary); text-decoration: underline; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }

/* ---------- blocchi ---------- */

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; }
.card > :first-child { margin-top: 0; }
.card > :last-child { margin-bottom: 0; }
.card-warn { background: var(--warn-bg); border-color: transparent; }
.card summary { font-weight: 600; cursor: pointer; padding: 2px 0; }
details[open] > summary { margin-bottom: 10px; }
.banner { display: block; padding: 12px 14px; border-radius: 12px; text-decoration: none; font-weight: 700; }
.banner-rec { background: #b71c1c; color: #fff; }
.footer p { margin: 4px 0; }

.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.item { display: flex; align-items: center; gap: 10px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px; }
.item-main { flex: 1; min-width: 0; display: flex; flex-direction: column; color: var(--text); text-decoration: none; }
.item-main strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- moduli ---------- */

.field { display: flex; flex-direction: column; gap: 6px; }
.field > span:first-child { font-weight: 600; font-size: 15px; }
input[type="text"],
textarea,
select {
  width: 100%;
  font: inherit;
  font-size: 17px; /* almeno 16px: evita lo zoom automatico di iOS */
  padding: 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}
textarea { resize: vertical; min-height: 96px; }
.check { display: flex; gap: 10px; align-items: flex-start; margin: 10px 0; }
.check input { width: 22px; height: 22px; flex: none; margin: 1px 0 0; }
.check.small { margin: 0; align-items: center; }
.add-row { display: flex; gap: 8px; }
.add-row input { flex: 1; min-width: 0; }
.settings .field { margin-top: 10px; }
.inline-select { width: auto; display: inline-block; padding: 4px 8px; font-size: 16px; }
.card .check { margin: 0 0 6px; }
.card p.muted { margin: 0; }

.chips { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.chip { display: flex; align-items: center; gap: 4px; background: var(--surface); border: 1px solid var(--border); border-left: 6px solid var(--c); border-radius: 12px; padding: 4px; }
.chip-color { width: 34px; height: 34px; border-radius: 50%; background: var(--c); border: 3px solid var(--surface); flex: none; }
.chip-name { flex: 1; min-width: 0; text-align: left; font: inherit; font-weight: 600; font-size: 17px; background: none; border: none; color: var(--text); padding: 8px 4px; overflow: hidden; text-overflow: ellipsis; }
.chip-btn { width: 40px; height: 40px; border: none; border-radius: 10px; background: var(--surface-2); color: var(--text); font-size: 16px; }
.recent { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.pill { font: inherit; font-size: 14px; border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 999px; padding: 6px 12px; }

.sticky-bottom {
  position: sticky;
  bottom: 0;
  padding: 12px 0 calc(var(--safe-bottom) + 4px);
  background: linear-gradient(to bottom, transparent, var(--bg) 18%);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* ---------- registrazione ---------- */

.rec-screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: calc(var(--safe-top) + 6px) 12px calc(var(--safe-bottom) + 10px);
  background: #000;
  color: #f1f3f5;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
.rec-top { display: grid; grid-template-columns: 88px 1fr 88px; align-items: center; }
.rec-status { display: flex; align-items: center; gap: 6px; font-weight: 800; letter-spacing: .08em; color: #ff5b52; font-size: 15px; }
.rec-dot { width: 14px; height: 14px; border-radius: 50%; background: #ff3b30; animation: blink 1.2s ease-in-out infinite; }
.rec-status[data-state="interrupted"] { color: #ffb74d; }
.rec-status[data-state="interrupted"] .rec-dot { background: #ffb74d; animation: none; }
.rec-status[data-state="starting"],
.rec-status[data-state="stopping"] { color: #aaa; }
.rec-status[data-state="starting"] .rec-dot,
.rec-status[data-state="stopping"] .rec-dot { background: #888; }
.rec-timer { text-align: center; font-size: clamp(40px, 13vw, 66px); font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: .01em; line-height: 1.1; }
.icon-btn { justify-self: end; width: 46px; height: 46px; border-radius: 50%; border: 1px solid #333; background: #16181c; color: #fff; font-size: 24px; touch-action: manipulation; }
.rec-health { display: flex; gap: 12px; justify-content: center; align-items: center; flex-wrap: wrap; font-size: 12px; color: #9aa0a6; }
.rec-health .bad { color: #ffb74d; }
.mic-bar { height: 6px; background: #1c1f25; border-radius: 3px; overflow: hidden; }
.mic-fill { display: block; height: 100%; width: 0; background: linear-gradient(90deg, #2fbf5b, #b5e655); transition: width .12s linear; }
.live-text {
  display: flex; flex-direction: column; gap: 3px; min-height: 86px; padding: 8px 12px;
  background: #0c0e11; border: 1px solid #262a31; border-radius: 14px;
}
.live-text.off { min-height: 0; }
.live-head { display: flex; justify-content: space-between; gap: 8px; font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: #8b939e; }
.live-state { color: #ffb74d; }
.live-state.on { color: #43c463; }
.live-body {
  margin: 0; font-size: 15px; line-height: 1.35; overflow: hidden;
  display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; line-clamp: 3;
}
.live-who { font-weight: 700; color: var(--c); }
.live-final { color: #f1f3f5; }
.live-interim { color: #9aa3ad; font-style: italic; }
.live-muted { color: #8b939e; font-size: 13px; }
.rec-warnings { display: flex; flex-direction: column; gap: 6px; }
.warn { display: flex; gap: 10px; align-items: center; padding: 8px 10px; border-radius: 12px; font-size: 14px; line-height: 1.3; }
.warn > span { flex: 1; }
.warn-warn { background: #3a2f10; color: #ffd66b; }
.warn-error { background: #5a1510; color: #ffc2bb; }
.warn-btn { flex: none; min-height: 40px; min-width: 44px; padding: 6px 12px; border-radius: 10px; border: none; font: inherit; font-weight: 700; background: #fff; color: #000; touch-action: manipulation; }
.rec-current {
  display: flex; align-items: baseline; gap: 10px; min-height: 60px; padding: 10px 14px; border-radius: 16px;
  background: var(--c); color: var(--fg); transition: background .15s;
}
.rec-current small { font-size: 11px; text-transform: uppercase; letter-spacing: .08em; opacity: .85; flex: none; }
.rec-current strong { flex: 1; min-width: 0; font-size: 28px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rec-current.empty { background: #16181c; color: #9aa0a6; }
.rec-current.empty strong { font-size: 18px; font-weight: 500; }
.rec-since { font-variant-numeric: tabular-nums; font-weight: 600; flex: none; }
.speaker-grid { flex: 1; min-height: 0; display: grid; gap: 10px; grid-auto-rows: minmax(64px, 1fr); overflow-y: auto; padding: 2px; }
.speaker-btn {
  position: relative; overflow: hidden; min-width: 0;
  border: 3px solid var(--c); border-radius: 18px; background: #111317; color: #fff;
  font: inherit; font-size: clamp(20px, 6.2vw, 32px); font-weight: 700; padding: 8px 12px 8px 22px;
  touch-action: manipulation; transition: transform .08s, background .12s;
}
.speaker-btn::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 12px; background: var(--c); }
.speaker-btn.active { background: var(--c); color: var(--fg); box-shadow: 0 0 0 3px #fff; }
.speaker-btn.active::after { content: "PARLA"; position: absolute; top: 6px; right: 10px; font-size: 11px; letter-spacing: .12em; opacity: .9; }
.speaker-btn:active { transform: scale(.97); }
.speaker-btn.pulse { animation: pulse .4s ease-out; }
.speaker-name { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rec-actions { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; }
.act {
  min-height: 66px; border-radius: 14px; border: 1px solid #30343b; background: #16181c; color: #e9ecef;
  font: inherit; font-size: 13px; font-weight: 600; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  touch-action: manipulation;
}
.act:active { background: #262a31; }
.act-ico { font-size: 23px; line-height: 1; }
.act-stop { border-color: #ff453a; color: #ff6b61; }

@keyframes blink { 50% { opacity: .2; } }
@keyframes pulse { from { filter: brightness(1.7); } to { filter: brightness(1); } }

/* ---------- timeline ---------- */

.audio-status { display: flex; flex-direction: column; gap: 6px; }
.line { font-size: 14px; line-height: 1.35; }
.line.ok { color: var(--ok); }
.line.warn { color: var(--warn); }
.line.bad { color: var(--bad); }
.line.info, .line.muted { color: var(--muted); }
.audio-status .line { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 8px 10px; }
.audio-status .line .btn { margin-top: 6px; }
.player { position: sticky; top: 0; z-index: 5; background: var(--bg); padding: calc(var(--safe-top) + 4px) 0 6px; display: flex; flex-direction: column; gap: 4px; }
.player audio { width: 100%; }
.player-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.player-row select { width: auto; flex: 1; padding: 8px; font-size: 16px; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.events { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 5px; }
.ev { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; align-items: center; gap: 6px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 4px; }
.ev-time { font: inherit; font-variant-numeric: tabular-nums; font-weight: 600; font-size: 15px; background: var(--surface-2); color: var(--text); border: none; border-radius: 9px; padding: 8px; min-height: 42px; }
.ev-body { display: flex; align-items: center; gap: 8px; min-width: 0; min-height: 42px; padding: 4px; text-align: left; font: inherit; color: inherit; background: none; border: none; overflow: hidden; }
.ev-body > span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ev-dot { width: 12px; height: 12px; border-radius: 50%; flex: none; display: inline-block; }
.ev-ico { flex: none; width: 18px; text-align: center; }
.ev-play { width: 42px; height: 42px; border-radius: 50%; border: none; background: var(--surface-2); color: var(--primary); font-size: 15px; }
.ev-note .ev-body > span:last-child, .ev-mark .ev-body > span:last-child { white-space: normal; }
.ev-speech { background: var(--surface-2); border-color: transparent; margin-left: 14px; }
.ev-speech .ev-time { background: none; font-weight: 400; color: var(--muted); }
.ev-speech .ev-body { align-items: flex-start; }
.ev-speech .ev-body .ev-dot { margin-top: 6px; }
.ev-speech .ev-body > span:last-child { white-space: normal; font-size: 15px; }
.ev-sys { grid-template-columns: auto minmax(0, 1fr); background: transparent; border-style: dashed; color: var(--muted); font-size: 13px; padding: 2px 6px; }
.ev-sys .ev-time { background: none; font-weight: 400; min-height: 0; font-size: 13px; padding: 4px; }
.talk { display: flex; flex-direction: column; gap: 8px; margin: 10px 0; }
.talk-row { display: grid; grid-template-columns: minmax(0, 7fr) minmax(0, 6fr); grid-template-areas: "name val" "bar bar"; gap: 2px 8px; align-items: center; }
.talk-name { grid-area: name; display: flex; align-items: center; gap: 8px; font: inherit; font-weight: 600; background: none; border: none; color: var(--text); padding: 2px 0; text-align: left; min-width: 0; }
.talk-val { grid-area: val; text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }
.talk-bar { grid-area: bar; height: 8px; background: var(--surface-2); border-radius: 4px; overflow: hidden; }
.talk-bar span { display: block; height: 100%; }

/* ---------- finestre ---------- */

dialog#modal {
  border: none; padding: 0; background: transparent; color: var(--text);
  width: min(560px, calc(100vw - 24px)); max-width: none;
  margin: calc(var(--safe-top) + 16px) auto auto;
}
dialog#modal::backdrop { background: rgba(0, 0, 0, .55); }
.modal-card {
  background: var(--surface); border-radius: 18px; padding: 18px; display: flex; flex-direction: column; gap: 12px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, .35); max-height: calc(100dvh - var(--safe-top) - 40px); overflow: auto;
}
.modal-card h2 { margin: 0; font-size: 19px; }
.modal-card p { margin: 0; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.modal-actions .danger { margin-right: auto; }
.edit-form { display: flex; flex-direction: column; gap: 12px; }
.nudge { display: flex; gap: 6px; flex-wrap: wrap; }
.seg { display: flex; gap: 4px; background: var(--surface-2); border-radius: 12px; padding: 4px; }
.seg button { flex: 1; font: inherit; font-size: 14px; font-weight: 600; min-height: 40px; border: none; border-radius: 9px; background: none; color: var(--text); }
.seg button.on { background: var(--surface); box-shadow: 0 1px 3px rgba(0, 0, 0, .2); }
.speaker-pick { display: flex; flex-wrap: wrap; gap: 6px; }
.pick { font: inherit; font-weight: 600; min-height: 44px; padding: 6px 14px; border-radius: 12px; border: 2px solid var(--c); background: var(--surface); color: var(--text); }
.pick.on { background: var(--c); color: var(--fg); }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 96px); z-index: 60;
  transform: translate(-50%, 16px); opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
  display: flex; gap: 12px; align-items: center; max-width: calc(100vw - 24px);
  background: #23262d; color: #fff; padding: 10px 14px; border-radius: 12px; font-size: 15px; box-shadow: 0 6px 24px rgba(0, 0, 0, .3);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
.toast-action { flex: none; font: inherit; font-weight: 700; color: #8fb0ff; background: none; border: none; padding: 6px; min-height: 40px; }

.update-banner {
  position: fixed; left: 12px; right: 12px; bottom: calc(var(--safe-bottom) + 12px); z-index: 70;
  display: flex; gap: 10px; align-items: center; justify-content: space-between;
  background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: 14px; padding: 10px 12px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .25);
}

/* ---------- esportazione e diagnostica ---------- */

.files { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.file { display: grid; grid-template-columns: 36px minmax(0, 1fr); grid-template-areas: "ico main" "act act"; gap: 6px 8px; align-items: center; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px; }
.file-ico { grid-area: ico; font-size: 20px; text-align: center; font-weight: 700; }
.file-main { grid-area: main; display: flex; flex-direction: column; min-width: 0; }
.file-main .muted { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-actions { grid-area: act; display: flex; gap: 8px; justify-content: flex-end; }
.preview { white-space: pre-wrap; word-break: break-word; font-size: 12px; max-height: 50vh; overflow: auto; background: var(--surface-2); border-radius: 10px; padding: 10px; margin: 0; }
.danger-zone summary { color: var(--bad); }
.checks { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }

@media (min-width: 700px) {
  .file { grid-template-columns: 36px minmax(0, 1fr) auto; grid-template-areas: "ico main act"; }
}
