: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); }

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;
}

.spinner {
  width: 44px;
  height: 44px;
  border: 4px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* --- 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: 12px 16px 14px;
  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;
}

.clue-list .row {
  display: flex;
  align-items: baseline;
  gap: 10px;
  flex-wrap: wrap;
}

.clue-list .indizio {
  font-family: var(--font-display);
  font-size: 1.3rem;
  font-weight: 600;
  font-style: italic;
  color: var(--ink);
}

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

.clue-list .parola {
  font-weight: 700;
  letter-spacing: 1px;
  color: var(--accent);
}

/* --- 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 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;
}

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

footer p { margin: 0; }
