:root {
  --bg: #f6f3ee;
  --surface: #ffffff;
  --surface-2: #efebe4;
  --text: #1d1b18;
  --muted: #6f6a62;
  --border: #e2dcd2;
  --danger: #c4372b;
  --game: #7b5cd6;
  --on-game: #ffffff;
  --radius: 16px;
  --ease-out: cubic-bezier(0.23, 1, 0.32, 1);
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141311;
    --surface: #1f1d1a;
    --surface-2: #2a2723;
    --text: #f1ede6;
    --muted: #a39d93;
    --border: #34302b;
    --danger: #ff6b5e;
  }
}

* { box-sizing: border-box; }

html {
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
  background: var(--bg);
}

body {
  margin: 0;
  font-family: var(--font);
  font-size: 17px;
  line-height: 1.45;
  color: var(--text);
  background: var(--bg);
}

h1, h2, p { margin: 0; }
.muted { color: var(--muted); }
[hidden] { display: none !important; }

button, a, label, [role="button"] {
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }

input, textarea { font: inherit; font-size: 17px; color: var(--text); }

svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.screen {
  min-height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left);
}

/* ---------- Login ---------- */

.login {
  max-width: 360px;
  margin: 0 auto;
  padding: 18vh 24px 24px;
  display: grid;
  gap: 14px;
  text-align: center;
}
.login-icon { justify-self: center; border-radius: 18px; }
.login h1 { font-size: 26px; }
.login form { display: grid; gap: 12px; margin-top: 8px; }
.login input {
  padding: 14px 16px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  text-align: center;
}
.error { color: var(--danger); font-size: 15px; min-height: 1.4em; }

.primary {
  padding: 14px 16px;
  border-radius: 12px;
  background: var(--text);
  color: var(--bg);
  font-weight: 600;
  transition: transform 120ms var(--ease-out), opacity 120ms;
}
.primary:active { transform: scale(0.97); opacity: 0.85; }

/* ---------- Home ---------- */

.home-header { padding: 28px 20px 8px; }
.home-header h1 { font-size: 34px; letter-spacing: -0.02em; }
.home-main {
  padding: 12px 16px calc(32px + env(safe-area-inset-bottom));
  max-width: 640px;
  margin: 0 auto;
}

.game-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.game-card {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px;
  text-align: left;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  transition: transform 140ms var(--ease-out), background 140ms;
}
.game-card:active { transform: scale(0.98); background: var(--surface-2); }
.game-tile {
  flex: none;
  width: 52px;
  height: 52px;
  display: grid;
  place-items: center;
  font-size: 28px;
  border-radius: 14px;
  background: color-mix(in srgb, var(--tile) 18%, transparent);
}
.game-info { flex: 1; min-width: 0; }
.game-name { font-weight: 600; font-size: 18px; }
.game-status { font-size: 14px; color: var(--muted); }
.game-status.missing { color: var(--danger); }
.chevron { color: var(--muted); width: 20px; height: 20px; }

.section-label {
  margin: 28px 4px 8px;
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}
.settings { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  min-height: 52px;
}
.setting-row + .setting-row { border-top: 1px solid var(--border); }

.segmented { display: flex; background: var(--surface-2); border-radius: 10px; padding: 2px; }
.segmented button {
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 15px;
  color: var(--muted);
  transition: background 160ms var(--ease-out), color 160ms;
}
.segmented button[aria-checked="true"] {
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
  box-shadow: 0 1px 3px rgb(0 0 0 / 0.12);
}

.switch {
  appearance: none;
  -webkit-appearance: none;
  flex: none;
  width: 51px;
  height: 31px;
  margin: 0;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  position: relative;
  transition: background 200ms var(--ease-out);
}
.switch::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 25px;
  height: 25px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgb(0 0 0 / 0.25);
  transition: transform 200ms var(--ease-out);
}
.switch:checked { background: #34c759; border-color: #34c759; }
.switch:checked::after { transform: translateX(20px); }

/* ---------- Chat ---------- */

.chat {
  height: 100dvh;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.chat-title { flex: 1; display: flex; align-items: center; gap: 10px; min-width: 0; }
.chat-title > div { min-width: 0; }
.chat-emoji { font-size: 26px; }
.chat-name { font-weight: 600; }
.chat-sub { font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.icon-button {
  flex: none;
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  color: var(--text);
  transition: transform 120ms var(--ease-out), background 120ms;
}
.icon-button:active { transform: scale(0.92); background: var(--surface-2); }

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 16px 16px 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.empty {
  margin: auto 0;
  text-align: center;
  display: grid;
  gap: 10px;
  justify-items: center;
  padding: 24px 8px;
}
.empty-emoji { font-size: 48px; }
.empty .muted { max-width: 300px; }
.suggestions { display: grid; gap: 8px; width: 100%; max-width: 360px; margin-top: 8px; }
.suggestion {
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  text-align: left;
  font-size: 16px;
  transition: transform 120ms var(--ease-out), background 120ms;
}
.suggestion:active { transform: scale(0.98); background: var(--surface-2); }
.suggestion-primary { border-color: var(--game); font-weight: 600; }

.msg-user {
  align-self: flex-end;
  max-width: 85%;
  display: grid;
  gap: 6px;
  justify-items: end;
}
.msg-user .bubble {
  padding: 10px 14px;
  border-radius: 20px 20px 6px 20px;
  background: var(--game);
  color: var(--on-game);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg-user img {
  max-width: 220px;
  max-height: 220px;
  border-radius: 14px;
  object-fit: cover;
}
.msg-user .photo-note { font-size: 13px; color: var(--muted); }

.msg-answer {
  align-self: stretch;
  padding: 14px 16px;
  border-radius: 20px 20px 20px 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  overflow-wrap: anywhere;
}
.answer-body p + p, .answer-body p + ul, .answer-body ul + p, .answer-body p + ol, .answer-body ol + p { margin-top: 10px; }
.answer-body ul, .answer-body ol { margin: 6px 0 0; padding-left: 22px; }
.answer-body li + li { margin-top: 4px; }
.answer-body code { font-size: 0.9em; background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }
.answer-body sup.cite {
  display: inline-grid;
  place-items: center;
  min-width: 18px;
  height: 18px;
  margin-left: 2px;
  padding: 0 4px;
  border-radius: 9px;
  font-size: 11px;
  font-weight: 600;
  line-height: 1;
  vertical-align: 2px;
  background: color-mix(in srgb, var(--game) 16%, transparent);
  color: var(--game);
}

.thinking { display: inline-flex; gap: 5px; padding: 6px 0; }
.thinking span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted);
  animation: pulse 1.2s infinite ease-in-out;
}
.thinking span:nth-child(2) { animation-delay: 0.15s; }
.thinking span:nth-child(3) { animation-delay: 0.3s; }
@keyframes pulse { 0%, 80%, 100% { opacity: 0.25; } 40% { opacity: 1; } }

.sources { margin-top: 12px; border-top: 1px solid var(--border); padding-top: 8px; display: grid; gap: 2px; }
.sources-label { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.source summary {
  list-style: none;
  font-size: 14px;
  color: var(--muted);
  padding: 4px 0;
  cursor: pointer;
}
.source summary::-webkit-details-marker { display: none; }
.source summary b { color: var(--game); }
.source blockquote {
  margin: 2px 0 8px;
  padding: 8px 10px;
  border-left: 3px solid var(--game);
  background: var(--surface-2);
  border-radius: 0 8px 8px 0;
  font-size: 14px;
  white-space: pre-wrap;
}

.answer-actions { display: flex; gap: 4px; margin: 8px -10px -8px; }
.answer-action {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
  border-radius: 10px;
  font-size: 14px;
  color: var(--muted);
  transition: transform 120ms var(--ease-out), background 120ms;
}
.answer-action svg { width: 18px; height: 18px; }
.answer-action:active { transform: scale(0.95); background: var(--surface-2); }
.answer-action.speaking { color: var(--game); }

.msg-error { color: var(--danger); font-size: 15px; }

/* ---------- Composer ---------- */

.composer {
  border-top: 1px solid var(--border);
  background: var(--bg);
  padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
}
.composer-row { display: flex; align-items: flex-end; gap: 4px; }
.composer textarea {
  flex: 1;
  min-height: 44px;
  max-height: 140px;
  resize: none;
  padding: 10px 14px;
  border-radius: 22px;
  border: 1px solid var(--border);
  background: var(--surface);
  line-height: 1.35;
  outline: none;
}
.composer textarea:focus { border-color: var(--game); }

.send-button {
  flex: none;
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--game);
  color: var(--on-game);
  transition: transform 120ms var(--ease-out), opacity 120ms;
}
.send-button:active { transform: scale(0.9); }
.send-button:disabled { opacity: 0.4; }

.mic-button.listening { color: #fff; background: var(--danger); border-radius: 50%; animation: listen 1.4s infinite var(--ease-out); }
@keyframes listen {
  0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--danger) 50%, transparent); }
  100% { box-shadow: 0 0 0 12px transparent; }
}

.attachment { position: relative; width: 72px; margin: 2px 0 8px 52px; }
.attachment img { width: 72px; height: 72px; object-fit: cover; border-radius: 12px; border: 1px solid var(--border); }
.attachment-remove {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--text);
  color: var(--bg);
  font-size: 18px;
  line-height: 1;
}

/* ---------- Toast ---------- */

.toast {
  position: fixed;
  left: 16px;
  right: 16px;
  bottom: calc(84px + env(safe-area-inset-bottom));
  margin: 0 auto;
  max-width: 420px;
  padding: 12px 16px;
  border-radius: 14px;
  background: var(--text);
  color: var(--bg);
  font-size: 15px;
  text-align: center;
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.2);
  animation: toast-in 220ms var(--ease-out);
}
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

@media (hover: hover) and (pointer: fine) {
  .game-card:hover, .suggestion:hover { background: var(--surface-2); }
  .icon-button:hover, .answer-action:hover { background: var(--surface-2); }
}
