:root {
  --bg: #f4f5f4;
  --surface: #ffffff;
  --text: #17201e;
  --muted: #5f6b68;
  --accent: #0f766e;
  --accent-text: #ffffff;
  --mine: #d8f0ec;
  --line: #dfe4e2;
  --danger: #b42318;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101514;
    --surface: #1a2220;
    --text: #e8eeec;
    --muted: #9aa8a4;
    --accent: #2dd4bf;
    --accent-text: #06221f;
    --mine: #164e48;
    --line: #2a3532;
    --danger: #f97066;
  }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 17px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

[hidden] { display: none !important; }

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

/* --- Codice d'invito --- */

.pair {
  max-width: 22rem;
  margin: 0 auto;
  padding: calc(3rem + env(safe-area-inset-top)) 1.25rem 2rem;
  text-align: center;
}

.pair h1 { margin: 0 0 1.5rem; font-size: 1.8rem; }

.pair form { display: grid; grid-template-columns: minmax(0, 1fr); gap: 0.75rem; }

.pair label { color: var(--muted); font-size: 0.95rem; }

.pair input {
  width: 100%;
  min-width: 0;
  padding: 0.8rem 0.4rem 0.8rem 0.8rem;
  border: 1px solid var(--line);
  border-radius: 0.75rem;
  background: var(--surface);
  font-size: 1.8rem;
  letter-spacing: 0.4em;
  text-align: center;
}

.pair button {
  padding: 0.85rem;
  border: 0;
  border-radius: 0.75rem;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}

.hint { color: var(--muted); font-size: 0.95rem; }

.error { color: var(--danger); margin: 0; }

/* --- Chat --- */

.chat {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 44rem;
  margin: 0 auto;
}

header {
  padding: calc(0.5rem + env(safe-area-inset-top)) 0.75rem 0.5rem;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.shortcuts { display: flex; gap: 0.5rem; }

.shortcuts > button {
  flex: 1;
  min-height: 2.75rem;
  border: 1px solid var(--line);
  border-radius: 0.75rem;
  background: var(--bg);
  font-weight: 600;
  cursor: pointer;
}

.shortcuts > .more-btn {
  flex: 0 0 2.75rem;
  padding: 0;
  font-size: 1.15rem;
}

.more-panel {
  padding: 0.4rem;
  border: 1px solid var(--line);
  border-radius: 0.9rem;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.14);
  min-width: min(17.5rem, calc(100vw - 1.5rem));
  max-width: min(19rem, calc(100vw - 1.5rem));
  max-height: min(70dvh, 26rem);
  overflow-y: auto;
}

.more-panel:popover-open {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.35rem;
}

.more-panel button {
  min-height: 2.75rem;
  padding: 0.45rem 0.65rem;
  border: 1px solid var(--line);
  border-radius: 0.65rem;
  background: var(--bg);
  color: var(--text);
  font-weight: 600;
  font-size: 0.92rem;
  text-align: left;
  cursor: pointer;
}

.more-panel button:hover {
  border-color: var(--accent);
}

@supports (position-area: block-end) {
  [popover].more-panel {
    inset: auto;
    margin: 0.35rem 0 0;
    position-area: block-end span-inline-start;
    position-try-fallbacks: flip-block, flip-inline, flip-block flip-inline;
  }
}

@supports not (position-area: block-end) {
  [popover].more-panel {
    position: fixed;
    inset: calc(3.75rem + env(safe-area-inset-top)) max(0.75rem, calc((100vw - 44rem) / 2 + 0.75rem)) auto auto;
    margin: 0;
  }
}

.more-panel.no-popover {
  display: none;
  position: fixed;
  inset: calc(3.75rem + env(safe-area-inset-top)) max(0.75rem, calc((100vw - 44rem) / 2 + 0.75rem)) auto auto;
  z-index: 20;
  margin: 0;
}

.more-panel.no-popover.open {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.35rem;
}

@media (prefers-reduced-motion: no-preference) {
  [popover].more-panel {
    opacity: 0;
    translate: 0 -0.25rem;
    transition: opacity, translate, display, overlay;
    transition-duration: 0.14s;
    transition-behavior: allow-discrete;
  }

  [popover].more-panel:popover-open {
    opacity: 1;
    translate: 0 0;
    transition-duration: 0.1s;
    @starting-style { opacity: 0; translate: 0 -0.25rem; }
  }
}

.push-on {
  width: 100%;
  min-height: 2.75rem;
  margin-top: 0.5rem;
  border: 0;
  border-radius: 0.75rem;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}

.notice {
  padding: 0.4rem 0.75rem;
  background: var(--mine);
  color: var(--text);
  font-size: 0.9rem;
  text-align: center;
}

.messages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.msg {
  max-width: 88%;
  align-self: flex-start;
}

.msg.me { align-self: flex-end; }

.bubble {
  padding: 0.6rem 0.8rem;
  border-radius: 1rem;
  background: var(--surface);
  border: 1px solid var(--line);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.msg.me .bubble { background: var(--mine); border-color: transparent; }

.bubble a { color: var(--accent); }

.bubble code, .bubble pre {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.92em;
}

.bubble blockquote {
  margin: 0.3rem 0;
  padding-left: 0.6rem;
  border-left: 3px solid var(--line);
  color: var(--muted);
}

.time {
  margin: 0.15rem 0.4rem 0;
  color: var(--muted);
  font-size: 0.75rem;
}

.msg.me .time { text-align: right; }

.keys { display: grid; gap: 0.35rem; margin-top: 0.35rem; }

.keys .row { display: flex; gap: 0.35rem; }

.keys .row.cal-row { gap: 0.2rem; }

.keys button {
  flex: 1;
  min-height: 2.75rem;
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--line);
  border-radius: 0.75rem;
  background: var(--surface);
  color: var(--accent);
  font-weight: 600;
}

.keys .row.cal-row button {
  min-width: 0;
  min-height: 2.35rem;
  padding: 0.25rem 0.1rem;
  font-size: 0.85rem;
  border-radius: 0.5rem;
}

.keys button:disabled, .shortcuts > button:disabled, .more-panel button:disabled { opacity: 0.5; }

.keys button.cal-noop {
  border-color: transparent;
  background: transparent;
  color: var(--muted);
  opacity: 1;
  font-weight: 500;
  cursor: default;
}

.typing .bubble { color: var(--muted); letter-spacing: 0.2em; }

/* --- Scrivere --- */

.composer {
  display: flex;
  align-items: flex-end;
  gap: 0.4rem;
  padding: 0.5rem 0.6rem calc(0.5rem + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--line);
}

.composer textarea {
  flex: 1;
  min-height: 2.75rem;
  max-height: 8rem;
  padding: 0.6rem 0.8rem;
  border: 1px solid var(--line);
  border-radius: 1.2rem;
  background: var(--bg);
  resize: none;
}

.icon-button {
  display: grid;
  place-items: center;
  width: 2.75rem;
  height: 2.75rem;
  flex: none;
  border: 0;
  border-radius: 50%;
  background: transparent;
  font-size: 1.35rem;
  cursor: pointer;
  /* Tenere premuto il microfono non deve selezionare testo né aprire il menu di iOS. */
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  touch-action: none;
}

.icon-button.send { background: var(--accent); color: var(--accent-text); font-size: 1.1rem; }

.icon-button.recording { background: var(--danger); }

button:focus-visible, textarea:focus-visible, input:focus-visible, .icon-button:focus-within {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* --- Banner Staging (visibile solo quando APP_ENV=staging) --- */

.staging-banner {
  background: #fef3c7;
  color: #92400e;
  border-bottom: 1px solid #f59e0b;
  padding: calc(0.3rem + env(safe-area-inset-top)) 0.75rem 0.3rem;
  font-size: 0.8rem;
  font-weight: 700;
  text-align: center;
  letter-spacing: 0.03em;
  flex: none;
}

@media (prefers-color-scheme: dark) {
  .staging-banner {
    background: #451a03;
    color: #fcd34d;
    border-bottom-color: #b45309;
  }
}

body:has(.staging-banner) {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
}

body:has(.staging-banner) .pair {
  flex: 1;
  width: 100%;
  overflow-y: auto;
  padding-top: 2rem;
}

body:has(.staging-banner) .chat {
  flex: 1;
  min-height: 0;
  width: 100%;
  height: auto;
}

body:has(.staging-banner) header {
  padding-top: 0.5rem;
}
