:root {
  --bg: #14110f;
  --panel: #1e1915;
  --ink: #f2e9dc;
  --muted: #a89a88;
  --accent: #c8963e;
  --accent-ink: #1b140a;
  --user: #2c241c;
  --danger: #e0715f;
  --listen: #4fae7b;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f6f0e6;
    --panel: #fffaf1;
    --ink: #2a2118;
    --muted: #7a6b58;
    --accent: #9a6a1c;
    --accent-ink: #fffaf1;
    --user: #ebe0cc;
    --danger: #b3402e;
    --listen: #2f8a5a;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 18px/1.5 Georgia, "Times New Roman", serif;
}
main {
  max-width: 720px;
  min-height: 100%;
  margin: 0 auto;
  padding: 24px 16px 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
}
header { text-align: center; }
main > header, .hero {
  width: 100%; padding-bottom: 14px; margin-bottom: 18px;
  border-bottom: 1px solid var(--muted);
  border-bottom: 1px solid color-mix(in srgb, var(--accent) 40%, transparent);
}
.landing .hero { margin-bottom: 0; }
h1 { margin: 0; font-size: 1.9rem; letter-spacing: .02em; color: var(--accent); }
.sub { margin: 4px 0 16px; color: var(--muted); font-size: .95rem; }

.log {
  width: 100%;
  flex: 1;
  min-height: 200px;
  max-height: 46vh;
  overflow-y: auto;
  padding: 12px;
  background: var(--panel);
  border-radius: 12px;
}
.hint { margin: 0; color: var(--muted); text-align: center; font-style: italic; }
.msg { margin: 0 0 12px; padding: 10px 14px; border-radius: 12px; max-width: 92%; }
.msg.user { background: var(--user); margin-left: auto; }
.msg.prof { border-left: 3px solid var(--accent); }
.msg .who { display: block; font-size: .75rem; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.msg.error { border-left-color: var(--danger); color: var(--danger); }

.interim { margin: 0; color: var(--muted); font-style: italic; text-align: center; }
.status { margin: 0; font-size: .9rem; color: var(--muted); text-align: center; }

.mic {
  position: relative;
  width: 104px; height: 104px;
  border-radius: 50%;
  border: 0;
  background: var(--accent);
  color: var(--accent-ink);
  cursor: pointer;
  display: grid; place-items: center;
  touch-action: manipulation;
}
.mic:focus-visible { outline: 3px solid var(--ink); outline-offset: 4px; }
.mic-ring {
  position: absolute; inset: -10px; border-radius: 50%;
  border: 3px solid transparent; pointer-events: none;
}
body[data-state="listening"] .mic { background: var(--listen); }
body[data-state="listening"] .mic-ring { border-color: var(--listen); animation: pulse 1.2s ease-out infinite; }
body[data-state="thinking"] .mic { opacity: .6; cursor: progress; }
body[data-state="thinking"] .mic-ring { border-color: var(--accent); border-top-color: transparent; animation: spin 1s linear infinite; }
body[data-state="speaking"] .mic-ring { border-color: var(--accent); animation: pulse 1.6s ease-out infinite; }
body[data-state="error"] .mic { background: var(--danger); }
@keyframes pulse { 0% { transform: scale(.95); opacity: 1; } 100% { transform: scale(1.18); opacity: 0; } }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .mic-ring { animation: none !important; } }

.keys { margin: 0; color: var(--muted); font-size: .8rem; text-align: center; }
footer { margin-top: auto; padding-top: 20px; color: var(--muted); font-size: .75rem; line-height: 1.4; text-align: center; }

/* Pagina di accesso */
.login { display: flex; flex-direction: column; gap: 12px; width: 100%; max-width: 320px; margin-top: 16px; }
.login label { color: var(--muted); font-size: 15px; }
.login input {
  font: inherit; padding: 10px 12px; border-radius: 8px;
  border: 1px solid var(--muted); background: var(--panel); color: var(--ink);
}
.login input:focus-visible, .login button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.login button {
  font: inherit; padding: 10px 12px; border: 0; border-radius: 8px; cursor: pointer;
  background: var(--accent); color: var(--accent-ink);
}
.login-error { margin: 0; color: var(--danger); font-size: 15px; }

/* Schermate */
[hidden] { display: none !important; }
.screen { width: 100%; display: flex; flex-direction: column; align-items: center; }
.msg-line { margin: 0 0 10px; min-height: 1.4em; color: var(--muted); font-size: .95rem; text-align: center; }
.msg-line.error { color: var(--danger); }
.btn {
  font: inherit; font-size: .95rem; padding: 9px 16px; border-radius: 8px; border: 1px solid transparent;
  cursor: pointer; touch-action: manipulation;
}
.btn:disabled { opacity: .55; cursor: default; }
.btn:focus-visible, .card-open:focus-visible, .card-del:focus-visible, .new-form input:focus-visible,
.voice-sel select:focus-visible { outline: 3px solid var(--ink); outline-offset: 2px; }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-secondary { background: transparent; color: var(--ink); border-color: var(--muted); }

/* Griglia di carte */
.grid {
  list-style: none; margin: 0; padding: 0; width: 100%;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 14px;
}
.card {
  position: relative; background: var(--panel); border-radius: 12px; border: 1px solid transparent;
  display: flex; flex-direction: column;
}
.card:hover { border-color: var(--accent); }
.card-open {
  all: unset; box-sizing: border-box; cursor: pointer; padding: 12px; width: 100%;
  display: flex; flex-direction: column; align-items: center; gap: 4px; text-align: center; height: 100%;
}
.card-name { font-size: 1rem; font-weight: bold; line-height: 1.25; overflow-wrap: anywhere; }
.card-role { font-size: .8rem; color: var(--muted); line-height: 1.3; overflow-wrap: anywhere; }
.card-del {
  all: unset; box-sizing: border-box; position: absolute; top: 6px; right: 6px; width: 28px; height: 28px;
  display: grid; place-items: center; border-radius: 50%; cursor: pointer;
  font-size: .8rem; color: var(--muted); opacity: .75; background: var(--bg);
}
.card-del:hover, .card-del:focus-visible { color: var(--danger); opacity: 1; }
.card-new { padding: 12px; border: 1px dashed var(--muted); background: transparent; }
.new-form { display: flex; flex-direction: column; align-items: center; gap: 8px; }
.new-form .card-name { text-align: center; }
.card-plus { font-size: 2.6rem; line-height: 1; color: var(--accent); }
.new-form input {
  font: inherit; font-size: .95rem; width: 100%; padding: 8px 10px; border-radius: 8px;
  border: 1px solid var(--muted); background: var(--panel); color: var(--ink);
}

/* Foto e segnaposto */
.portrait {
  position: relative; display: block; overflow: hidden; background: var(--user); flex: none;
}
.portrait img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; object-position: 50% 18%; }
.portrait-ph {
  position: absolute; inset: 0; display: grid; place-items: center;
  color: var(--accent); font-weight: bold; letter-spacing: .04em;
}
.portrait-card { width: 96px; height: 96px; border-radius: 50%; }
.portrait-card .portrait-ph { font-size: 1.9rem; }
.portrait-head { width: 88px; height: 88px; border-radius: 12px; }
.portrait-head .portrait-ph { font-size: 1.8rem; }
.persona-photo { width: 88px; height: 88px; border-radius: 12px; overflow: hidden; position: relative; flex: none; background: var(--user); }
.persona-photo img { width: 100%; height: 100%; object-fit: cover; object-position: 50% 18%; display: block; }

/* Preparazione */
.pg-title { margin: 0 0 12px; font-size: 1.3rem; text-align: center; overflow-wrap: anywhere; }
.pg-phase { margin: 0 0 8px; min-height: 1.5em; font-size: 1.05rem; text-align: center; }
.bar { width: 100%; max-width: 480px; height: 14px; border-radius: 7px; background: var(--user); overflow: hidden; }
.bar-fill { height: 100%; width: 0; background: var(--accent); transition: width .6s ease; }
.steps {
  list-style: none; margin: 12px 0; padding: 0; width: 100%; max-width: 480px;
  display: flex; justify-content: space-between; gap: 4px; font-size: .75rem; color: var(--muted); text-align: center;
}
.steps li { flex: 1; padding-top: 6px; border-top: 3px solid var(--user); }
.steps li.done { border-top-color: var(--accent); color: var(--ink); }
.steps li.current { border-top-color: var(--accent); color: var(--ink); font-weight: bold; }
.pg-identity { margin: 4px 0 8px; text-align: center; overflow-wrap: anywhere; min-height: 1.4em; }
.actions { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; margin-top: 8px; }
@media (prefers-reduced-motion: reduce) { .bar-fill { transition: none; } }

/* Conversazione */
.persona { display: flex; align-items: center; gap: 14px; width: 100%; margin-bottom: 10px; }
.persona-text { min-width: 0; }
.persona-name { margin: 0; font-size: 1.3rem; font-weight: bold; color: var(--accent); overflow-wrap: anywhere; }
.persona-role { margin: 0; font-size: .9rem; color: var(--muted); overflow-wrap: anywhere; }
.persona-credit { margin: 2px 0 0; font-size: .75rem; color: var(--muted); overflow-wrap: anywhere; }
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; width: 100%; }
.voice-sel { margin-left: auto; font-size: .85rem; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.voice-sel select {
  font: inherit; font-size: .85rem; padding: 6px 8px; border-radius: 8px;
  border: 1px solid var(--muted); background: var(--panel); color: var(--ink);
}
#screen-chat { align-items: center; }
.disclaimer {
  margin: 8px 0 0; padding: 4px 10px; border-radius: 8px; max-width: 100%;
  border: 1px solid var(--accent); color: var(--ink); font-size: .75rem; line-height: 1.3; text-align: center;
}

/* PWA / mobile: aree sicure dello schermo, bersagli da dito, niente zoom automatico di iOS sui campi */
html { -webkit-text-size-adjust: 100%; }
body { -webkit-tap-highlight-color: transparent; overscroll-behavior-y: contain; }
main {
  padding-top: max(24px, env(safe-area-inset-top));
  padding-right: max(16px, env(safe-area-inset-right));
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  padding-left: max(16px, env(safe-area-inset-left));
}
.sub:empty { display: none; }
.btn { min-height: 44px; }
.card-del::after { content: ""; position: absolute; inset: -9px; }
.new-form input, .login input, .login button, .voice-sel select { font-size: 16px; }
.voice-sel select { min-height: 44px; min-width: 56px; }
@media (display-mode: standalone) {
  body { user-select: none; -webkit-user-select: none; }
  .log, .msg, input { user-select: text; -webkit-user-select: text; }
}

/* Pagina di accesso / presentazione */
.landing main { max-width: 640px; align-items: stretch; gap: 28px; padding-bottom: max(32px, env(safe-area-inset-bottom)); }
.hero { text-align: center; margin-top: 8px; }
.hero h1 { font-size: 2.3rem; }
.hero .lead { margin: 6px 0 12px; font-size: 1.25rem; color: var(--ink); }
.hero .intro { margin: 0 auto; max-width: 34em; color: var(--muted); font-size: 1rem; }
.landing h2 { margin: 0 0 12px; font-size: 1.15rem; color: var(--accent); letter-spacing: .02em; }
.access { background: var(--panel); border-radius: 14px; padding: 20px 20px 22px; }
.access h2 { text-align: center; }
.access .login { margin: 0 auto; }
.how-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 12px; counter-reset: passo; }
.how-list li { position: relative; padding: 14px 16px 14px 56px; background: var(--panel); border-radius: 12px; counter-increment: passo; display: grid; gap: 2px; }
.how-list li::before {
  content: counter(passo); position: absolute; left: 14px; top: 14px; width: 30px; height: 30px; border-radius: 50%;
  display: grid; place-items: center; background: var(--accent); color: var(--accent-ink); font-weight: bold;
}
.how-list strong { font-size: 1.02rem; }
.how-list span { color: var(--muted); font-size: .95rem; line-height: 1.45; }
.feats ul { margin: 0; padding: 0; list-style: none; display: grid; gap: 8px; }
.feats li { padding-left: 22px; position: relative; color: var(--ink); font-size: .98rem; }
.feats li::before { content: "•"; position: absolute; left: 6px; color: var(--accent); }
.notice { margin: 0; padding-top: 14px; border-top: 1px solid var(--muted); color: var(--muted); font-size: .8rem; line-height: 1.45; text-align: center; }
@media (min-width: 760px) { .hero h1 { font-size: 2.8rem; } .landing main { max-width: 700px; } }

/* Invito a installare l'app (PWA) */
.install { display: flex; align-items: center; gap: 10px; margin: 0 0 14px; padding: 10px 12px; background: var(--panel); border: 1px solid var(--accent); border-radius: 12px; font-size: .92rem; }
.install[hidden] { display: none; }
.install span { flex: 1; line-height: 1.35; }
.install .btn { min-height: 40px; padding: 6px 14px; }
.install-close { all: unset; box-sizing: border-box; width: 36px; height: 36px; display: grid; place-items: center; border-radius: 50%; cursor: pointer; color: var(--muted); }
.install-close:focus-visible { outline: 3px solid var(--ink); outline-offset: 2px; }

/* Schermata di conversazione: app a tutta altezza, trascrizione scorrevole sopra, microfono grande nel piede */
.chat-top { width: 100%; flex: none; }
.chat-foot {
  width: 100%; flex: none; display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 10px 0 4px;
}
.chat-foot .interim { width: 100%; min-height: 2.8em; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; line-height: 1.4; }
.chat-foot .mic { margin: 8px 0 6px; flex: none; }
body[data-screen="chat"] { overflow: hidden; }
body[data-screen="chat"] main {
  height: 100vh; height: 100dvh; min-height: 0; overflow: hidden;
  padding-top: max(10px, env(safe-area-inset-top));
  padding-bottom: max(10px, env(safe-area-inset-bottom));
}
/* il titolo resta anche nella conversazione, in versione compatta */
body[data-screen="chat"] main > header { padding-bottom: 6px; margin-bottom: 8px; }
body[data-screen="chat"] main > header h1 { font-size: 1.35rem; }
body[data-screen="chat"] #screen-chat { flex: 1; min-height: 0; align-items: stretch; }
body[data-screen="chat"] .persona { margin-bottom: 8px; gap: 12px; }
body[data-screen="chat"] .persona-photo { width: 56px; height: 56px; }
body[data-screen="chat"] .persona-photo .portrait-head { width: 56px; height: 56px; }
body[data-screen="chat"] .persona-photo .portrait-ph { font-size: 1.2rem; }
body[data-screen="chat"] .persona-name { font-size: 1.1rem; line-height: 1.25; }
body[data-screen="chat"] .persona-role { font-size: .8rem; line-height: 1.3; }
body[data-screen="chat"] .persona-credit { font-size: .7rem; line-height: 1.25; }
body[data-screen="chat"] .log {
  flex: 1 1 0; min-height: 0; max-height: none; margin-top: 8px;
  -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
}
body[data-screen="chat"] .toolbar .btn { min-height: 40px; padding: 6px 14px; }
body[data-screen="chat"] .voice-sel select { min-height: 40px; }
body[data-screen="chat"] main > footer { display: none; }
@media (hover: none) { .info-panel .keys { display: none; } }
.chat-foot { position: relative; padding-left: 52px; padding-right: 52px; }
.info-btn {
  all: unset; box-sizing: border-box; position: absolute; right: 0; bottom: 2px; width: 44px; height: 44px;
  display: grid; place-items: center; border-radius: 50%; cursor: pointer; color: var(--muted); touch-action: manipulation;
}
.info-btn:hover, .info-btn[aria-expanded="true"] { color: var(--accent); }
.info-btn:focus-visible, .info-close:focus-visible, .info-panel:focus-visible { outline: 3px solid var(--ink); outline-offset: 2px; }
.info-panel {
  position: absolute; right: 0; bottom: calc(100% + 6px); z-index: 5; width: min(100%, 360px); max-height: 50vh; max-height: 50dvh;
  overflow-y: auto; overscroll-behavior: contain; padding: 14px 44px 12px 14px; text-align: left;
  background: var(--panel); color: var(--ink); border: 1px solid var(--accent); border-radius: 12px;
  box-shadow: 0 6px 24px rgba(0,0,0,.35); font-size: .85rem; line-height: 1.45;
}
.info-panel p { margin: 0 0 8px; }
.info-panel p:last-child { margin-bottom: 0; }
.info-panel .keys { color: var(--muted); text-align: left; }
.info-close { all: unset; box-sizing: border-box; position: absolute; top: 2px; right: 2px; width: 40px; height: 40px; display: grid; place-items: center; border-radius: 50%; cursor: pointer; color: var(--muted); }
@media (max-height: 560px) {
  .chat-foot .mic { width: 84px; height: 84px; margin: 4px 0; }
  body[data-screen="chat"] .persona-photo, body[data-screen="chat"] .persona-photo .portrait-head { width: 44px; height: 44px; }
}

/* Telefono in orizzontale (poco spazio in altezza): piede compatto, trascrizione con altezza minima, scroll della pagina come ripiego */
@media (max-height: 480px) {
  body[data-screen="chat"] main { overflow-y: auto; }
  body[data-screen="chat"] .persona-credit, body[data-screen="chat"] .chat-foot .interim { display: none; }
  body[data-screen="chat"] .persona { margin-bottom: 4px; }
  body[data-screen="chat"] .log { flex: 1 0 96px; min-height: 96px; margin-top: 4px; }
  .chat-foot { flex-direction: row; justify-content: center; gap: 14px; padding-top: 6px; padding-bottom: 2px; }
  .chat-foot .mic { width: 72px; height: 72px; margin: 4px 0; }
  .chat-foot .mic svg { width: 36px; height: 36px; }
  .info-btn { bottom: 14px; }
  .disclaimer { margin-top: 4px; }
}

/* Piede della conversazione ridotto del ~40% (216 px -> ~125 px): microfono 80 px, interim sovrapposto invece che riservato */
.chat-foot { gap: 2px; padding-top: 6px; padding-bottom: 2px; }
.chat-foot .mic { width: 80px; height: 80px; margin: 6px 0 4px; }
.chat-foot .mic svg { width: 40px; height: 40px; }
.chat-foot .status { margin: 0; font-size: .85rem; line-height: 1.3; min-height: 1.3em; }
.chat-foot .interim {
  position: absolute; left: 0; right: 0; bottom: 100%; margin: 0 0 4px; min-height: 0; padding: 6px 12px; z-index: 2;
  background: var(--panel); border-radius: 10px; text-align: center; font-size: .9rem;
}
.chat-foot .interim:empty { display: none; }
.info-btn { bottom: 10px; width: 40px; height: 40px; }

/* Intestazione con il titolo sempre visibile (anche nella PWA): resta ferma in alto mentre la pagina scorre */
body[data-screen] main { padding-top: 0; }
body[data-screen] main > header {
  position: sticky; top: 0; z-index: 20; width: 100%; background: var(--bg);
  padding-top: max(12px, env(safe-area-inset-top)); padding-bottom: 8px; margin-bottom: 12px;
}
body[data-screen] main > header h1 { font-size: 1.5rem; }
body[data-screen] main > header .sub { margin: 2px 0 0; font-size: .85rem; }
body[data-screen="chat"] main > header { margin-bottom: 6px; }
body[data-screen="chat"] main > header h1 { font-size: 1.35rem; }

/* Lo stato sotto il microfono resta su una sola riga */
.chat-foot .status { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }

/* Home: icona in basso a sinistra nel piede, speculare all'icona info */
.home-btn {
  all: unset; box-sizing: border-box; position: absolute; left: 0; bottom: 10px; width: 40px; height: 40px;
  display: grid; place-items: center; border-radius: 50%; cursor: pointer; color: var(--muted); touch-action: manipulation;
}
.home-btn:hover { color: var(--accent); }
.home-btn:focus-visible { outline: 3px solid var(--ink); outline-offset: 2px; }
@media (max-height: 480px) { .home-btn { bottom: 14px; } }
