:root {
  --bg: #faf6ef;
  --card: #ffffff;
  --border: #e7ddcd;
  --text: #2b241c;
  --muted: #93866f;
  --accent: #d4571e;
  --accent-soft: #fbe9dd;
  --ink: #3b3427;
  --ok: #2f9e5f;
  --err: #c93a4b;
  --radius: 14px;
  --font-display: "Fraunces", Georgia, serif;
  --font-body: "Space Grotesk", system-ui, sans-serif;
}

* { box-sizing: border-box; }

/* l'attributo hidden deve vincere anche su display:flex delle .view */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  min-height: 100dvh;
}

body {
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 14px 16px 6px;
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
}

h1 {
  margin: 0;
  min-width: 0;
  font-family: var(--font-display);
  font-weight: 800;
  font-size: clamp(1.1rem, 5.2vw, 1.45rem);
  letter-spacing: 0.2px;
  white-space: nowrap;
}

h1 span { color: var(--accent); }

.status-row {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-shrink: 0;
}

.pill {
  font-size: 0.72rem;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 9px;
  color: var(--muted);
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  line-height: 1;
}

.pill.conn.online { color: var(--ok); }
.pill.conn.offline { color: var(--err); }

.pill.flow-btn {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 700;
  font-family: var(--font-body);
  cursor: pointer;
  transition: transform 0.08s ease;
}

.pill.flow-btn:active { transform: scale(0.92); }

.pill.settings-btn {
  cursor: pointer;
  padding: 5px 8px;
  color: var(--muted);
  transition: transform 0.08s ease, color 0.15s ease;
}

.pill.settings-btn:active { transform: scale(0.92) rotate(30deg); }
.pill.settings-btn:hover { color: var(--accent); }
.pill.settings-btn svg { display: block; }

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding: 12px 16px 24px;
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
}

.view {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  animation: fadein 0.25s ease;
}

@keyframes fadein {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}

.hint-text {
  color: var(--muted);
  text-align: center;
  margin: 8px 0 0;
}

/* --- griglia lettere --- */

.letter-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 10px;
  width: 100%;
  margin-top: 8px;
}

.letter-grid button {
  aspect-ratio: 1;
  font-family: var(--font-display);
  font-size: 1.6rem;
  font-weight: 600;
  color: var(--ink);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 1px 2px rgba(60, 45, 20, 0.06);
  cursor: pointer;
  transition: transform 0.08s ease, background 0.15s ease;
}

.letter-grid button:active {
  transform: scale(0.92);
  background: var(--accent-soft);
}

.prefix-box {
  width: 100%;
  color: var(--muted);
  font-size: 0.9rem;
  margin-top: 4px;
}

.prefix-box summary { cursor: pointer; padding: 6px 2px; }

.prefix-box form { display: flex; gap: 8px; margin-top: 8px; }

.prefix-box input {
  flex: 1;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 1.1rem;
  padding: 10px 14px;
  text-transform: uppercase;
  font-family: var(--font-body);
}

.prefix-box button {
  background: var(--ink);
  color: var(--bg);
  border: none;
  border-radius: var(--radius);
  padding: 0 20px;
  font-size: 1rem;
  font-family: var(--font-body);
  cursor: pointer;
}

/* --- lettera gigante + loading --- */

.big-letter {
  font-family: var(--font-display);
  font-size: clamp(6rem, 40vw, 11rem);
  font-weight: 800;
  line-height: 1;
  color: var(--accent);
  margin-top: 12px;
  text-align: center;
  word-break: break-all;
}

.big-letter.small {
  font-size: clamp(3.5rem, 20vw, 6rem);
  margin-top: 0;
}

/* --- loader "gara di velocità" (lumaca vs coniglio) --- */

.loader-slot {
  width: 100%;
  display: flex;
  justify-content: center;
}

.ai-loader {
  width: 100%;
  max-width: 420px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  padding: 18px 6px 4px;
}

.ai-loader .track {
  position: relative;
  width: 100%;
  height: 14px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 999px;
  /* tacche da pista di atletica */
  background-image: repeating-linear-gradient(
    90deg,
    transparent 0 34px,
    var(--border) 34px 35px
  );
}

.ai-loader .fill {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 4%;
  background: linear-gradient(90deg, var(--accent-soft), var(--accent));
  border-radius: 999px;
  transition-property: width;
  /* parte spedita e rallenta sempre di più: al traguardo non ci arriva mai */
  transition-timing-function: cubic-bezier(0.1, 0.6, 0.2, 1);
}

.ai-loader .runner {
  position: absolute;
  right: -8px;
  bottom: 7px;
  font-size: 1.7rem;
  line-height: 1;
  filter: drop-shadow(0 2px 2px rgba(60, 45, 20, 0.25));
}

/* la lumaca guarda avanti (l'emoji nasce rivolta a sinistra) e ondeggia */
.ai-loader .runner.snail {
  animation: snail-wobble 1.6s ease-in-out infinite;
}

@keyframes snail-wobble {
  0%, 100% { transform: scaleX(-1) rotate(0deg); }
  25% { transform: scaleX(-1) rotate(-6deg) translateY(-1px); }
  75% { transform: scaleX(-1) rotate(5deg); }
}

/* il coniglio guarda avanti anche lui, e saltella */
.ai-loader .runner.rabbit {
  animation: rabbit-hop 0.5s ease-in-out infinite;
}

@keyframes rabbit-hop {
  0%, 100% { transform: scaleX(-1) translateY(0); }
  50% { transform: scaleX(-1) translateY(-7px); }
}

.ai-loader .finish {
  position: absolute;
  right: -4px;
  bottom: 10px;
  font-size: 1.1rem;
  line-height: 1;
}

.ai-loader .loader-msg {
  margin: 0;
  min-height: 1.5em;
  color: var(--ink);
  font-style: italic;
  font-size: 0.92rem;
  text-align: center;
  transition: opacity 0.25s ease;
}

.ai-loader .loader-msg.fade { opacity: 0; }

.ai-loader.compact { gap: 8px; padding: 12px 2px 2px; max-width: none; }
.ai-loader.compact .track { height: 10px; }
.ai-loader.compact .runner { font-size: 1.3rem; bottom: 5px; right: -6px; }
.ai-loader.compact .finish { font-size: 0.9rem; bottom: 7px; }
.ai-loader.compact .loader-msg { font-size: 0.82rem; }

/* --- lista indizi --- */

.clue-list {
  list-style: none;
  margin: 0;
  padding: 0;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.clue-list li {
  position: relative;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 1px 2px rgba(60, 45, 20, 0.06);
  padding: 11px 14px 12px;
  animation: fadein 0.3s ease;
}

.clue-list .categoria {
  display: block;
  font-size: 0.68rem;
  font-weight: 500;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 4px;
}

/* La riga sta SEMPRE su una sola linea: niente wrap, così ogni card ha la
   stessa altezza. Con parole lunghe il testo si rimpicciolisce via --fit
   (impostato dal JS); come ultima rete di sicurezza c'è l'ellissi. */
.clue-list .row {
  display: flex;
  align-items: baseline;
  gap: 0.5em; /* in em: si stringe insieme al testo quando la riga è piena */
  flex-wrap: nowrap;
  white-space: nowrap;
  min-width: 0;
  font-size: calc(1rem * var(--fit, 1));
}

.clue-list .indizio {
  font-family: var(--font-display);
  font-size: 1.15em;
  font-weight: 600;
  font-style: italic;
  color: var(--ink);
  flex: 0 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

.clue-list .arrow { color: var(--muted); flex: 0 0 auto; }

.clue-list .parola {
  font-weight: 700;
  font-size: 0.92em;
  letter-spacing: 0.5px;
  color: var(--accent);
  flex: 0 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* --- pulsante "i" + tooltip definizione --- */

.info-btn {
  flex: 0 0 auto;
  align-self: center;
  width: 15px;
  height: 15px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--bg);
  color: var(--muted);
  font-family: var(--font-display);
  font-style: italic;
  font-weight: 600;
  font-size: 0.62rem;
  line-height: 1;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.info-btn:hover,
.info-btn.active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}

/* Il "?" spiega il collegamento della riga intera: sta da solo sul bordo
   destro, staccato dalle "i" delle singole parole. Il contorno rossino lo
   mette in risalto rispetto alle "i". */
.info-btn.why-btn {
  margin-left: auto;
  font-style: normal;
  font-family: var(--font-body);
  font-weight: 700;
  border-color: #e59a8e;
  color: #cd4f43;
}

.info-btn.why-btn:hover,
.info-btn.why-btn.active {
  background: #fdeceb;
  border-color: var(--err);
  color: var(--err);
}

/* Il tooltip è un popover: fluttua sopra la card, senza allargarla. */
.tooltip {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: calc(100% - 4px);
  z-index: 30;
  padding: 11px 13px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(60, 45, 20, 0.2);
  font-size: 0.9rem;
  line-height: 1.42;
  color: var(--ink);
  animation: fadein 0.15s ease;
}

/* codina che punta verso la card */
.tooltip::after {
  content: '';
  position: absolute;
  bottom: -7px;
  left: var(--caret, 22px);
  width: 12px;
  height: 12px;
  background: var(--card);
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  transform: rotate(45deg);
}

.tooltip .def { margin: 0 0 6px; }
.tooltip .def:last-of-type { margin-bottom: 0; }

.tooltip .def-pos {
  font-size: 0.66rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--muted);
  margin-right: 6px;
}

.tooltip .tip-source {
  display: block;
  margin-top: 8px;
  font-size: 0.72rem;
  color: var(--muted);
}

.tooltip .tip-source a { color: var(--muted); }

.tooltip .tip-loading,
.tooltip .tip-error {
  color: var(--muted);
  font-style: italic;
}

.clue-list li.loading-row {
  text-align: center;
  color: var(--muted);
  font-style: italic;
}

/* --- bottoni azione --- */

.actions {
  display: flex;
  gap: 12px;
  width: 100%;
  margin-top: 4px;
}

.btn {
  flex: 1;
  border: none;
  border-radius: var(--radius);
  font-family: var(--font-body);
  font-size: 1.02rem;
  font-weight: 500;
  padding: 14px 10px;
  cursor: pointer;
  transition: transform 0.08s ease;
}

.btn:active { transform: scale(0.96); }
.btn:disabled { opacity: 0.5; cursor: default; }

.btn.primary { background: var(--accent); color: #fff; }

.btn.secondary {
  background: var(--card);
  color: var(--ink);
  border: 1px solid var(--border);
}

.error {
  color: var(--err);
  text-align: center;
  margin: 4px 0 0;
}

/* --- modalità Flow --- */

.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(43, 36, 28, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  animation: fadein 0.2s ease;
}

.modal {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 12px 40px rgba(60, 45, 20, 0.25);
  width: 100%;
  max-width: 560px;
  max-height: 85dvh;
  overflow-y: auto;
  padding: 14px 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-head h2 {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 1.2rem;
  color: var(--accent);
}

.modal-close {
  background: none;
  border: none;
  font-size: 1.7rem;
  line-height: 1;
  color: var(--muted);
  cursor: pointer;
  padding: 2px 6px;
}

#flow-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

#flow-form .hint-text { margin: 0; }

#flow-input {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 1.1rem;
  padding: 12px 14px;
  text-transform: uppercase;
  font-family: var(--font-body);
}

#flow-results {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.flow-word {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: clamp(2rem, 12vw, 3rem);
  line-height: 1.1;
  color: var(--accent);
  text-align: center;
  word-break: break-word;
}

/* --- impostazioni --- */

.setting-block {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.setting-title {
  margin: 0;
  font-family: var(--font-body);
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  color: var(--muted);
}

.model-switch {
  display: flex;
  align-items: stretch;
  justify-content: center;
  gap: 12px;
}

.model-side {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  text-align: center;
  padding: 12px 8px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  opacity: 0.55;
  transition: opacity 0.2s ease, border-color 0.2s ease, box-shadow 0.2s ease;
}

.model-side.active {
  opacity: 1;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.model-side .model-emoji { font-size: 1.7rem; line-height: 1; }

.model-side strong {
  font-size: 0.92rem;
  letter-spacing: 0.3px;
}

.model-side small {
  color: var(--muted);
  font-size: 0.75rem;
  line-height: 1.35;
}

/* switch a levetta */
.switch {
  position: relative;
  align-self: center;
  flex: 0 0 auto;
  width: 52px;
  height: 30px;
}

.switch input {
  position: absolute;
  opacity: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  cursor: pointer;
}

.switch .slider {
  position: absolute;
  inset: 0;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 999px;
  pointer-events: none;
  transition: background 0.2s ease;
}

.switch .slider::before {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  background: var(--accent);
  border-radius: 50%;
  transition: transform 0.2s ease;
}

.switch input:checked + .slider::before {
  transform: translateX(22px);
}

footer {
  text-align: center;
  color: var(--muted);
  font-size: 0.75rem;
  padding: 10px 20px 18px;
}

footer p { margin: 0; }
