:root {
  color-scheme: dark;
  --ink: #f8f5ee;
  --muted: #aaa6bd;
  --panel: rgba(35, 31, 62, 0.82);
  --panel-solid: #24203f;
  --line: rgba(255, 255, 255, 0.12);
  --purple: #9c7dff;
  --purple-dark: #7252da;
  --coral: #ff8b70;
  --green: #62d6a6;
  --red: #ff7591;
  --shadow: 0 24px 80px rgba(4, 3, 15, 0.4);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-synthesis: none;
}

* {
  box-sizing: border-box;
}

html {
  background: #111020;
}

body {
  min-width: 320px;
  min-height: 100dvh;
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(circle at 18% 0%, rgba(122, 88, 218, 0.42), transparent 38rem),
    radial-gradient(circle at 92% 92%, rgba(255, 123, 95, 0.2), transparent 32rem),
    #111020;
}

button,
textarea {
  font: inherit;
}

button {
  -webkit-tap-highlight-color: transparent;
}

[hidden] {
  display: none !important;
}

.app-shell {
  position: relative;
  width: min(100%, 520px);
  min-height: 100dvh;
  margin: 0 auto;
  overflow: hidden;
  background: linear-gradient(180deg, rgba(24, 21, 45, 0.4), rgba(16, 15, 31, 0.6));
}

.screen {
  min-height: 100dvh;
  padding: max(28px, env(safe-area-inset-top)) 22px max(24px, env(safe-area-inset-bottom));
  animation: screen-in 220ms ease-out both;
}

@keyframes screen-in {
  from { opacity: 0; transform: translateY(5px); }
  to { opacity: 1; transform: translateY(0); }
}

.setup-screen,
.results-screen {
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.setup-screen {
  justify-content: flex-start;
}

.brand-mark {
  display: grid;
  width: 58px;
  height: 58px;
  margin-bottom: 36px;
  place-items: center;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 18px;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.06);
  box-shadow: var(--shadow);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 24px;
  font-weight: 700;
}

.brand-mark span {
  color: var(--coral);
}

.eyebrow {
  margin: 0 0 10px;
  color: var(--purple);
  font-size: 0.73rem;
  font-weight: 800;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

h1 {
  max-width: 430px;
  margin: 0;
  font-size: clamp(2.35rem, 11vw, 4rem);
  font-weight: 760;
  letter-spacing: -0.055em;
  line-height: 0.98;
}

.intro {
  max-width: 420px;
  margin: 18px 0 34px;
  color: var(--muted);
  font-size: 1rem;
  line-height: 1.6;
}

.connection-card {
  margin-bottom: 25px;
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 15px;
  background: rgba(255, 255, 255, 0.045);
  transition: padding 180ms ease, background 180ms ease;
}

.connection-card.connected {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  background: rgba(98, 214, 166, 0.055);
}

.connection-status {
  display: flex;
  align-items: center;
  gap: 11px;
}

.connection-status div {
  min-width: 0;
}

.connection-status strong,
.connection-status span {
  display: block;
}

.connection-status strong {
  color: #efebf5;
  font-size: 0.88rem;
}

.connection-status div span {
  margin-top: 2px;
  color: #8d899d;
  font-size: 0.72rem;
}

.connection-dot {
  width: 11px;
  height: 11px;
  flex: 0 0 auto;
  border-radius: 50%;
  background: var(--coral);
  box-shadow: 0 0 0 5px rgba(255, 139, 112, 0.1);
}

.connection-dot.connected {
  background: var(--green);
  box-shadow: 0 0 0 5px rgba(98, 214, 166, 0.1);
}

.connect-button,
.disconnect-button {
  width: 100%;
  min-height: 43px;
  margin-top: 13px;
  border-radius: 13px;
  cursor: pointer;
  font-size: 0.78rem;
  font-weight: 800;
}

.connection-card.connected .disconnect-button {
  width: auto;
  min-height: 34px;
  margin-top: 0;
  padding: 0 11px;
  border-radius: 10px;
  font-size: 0.7rem;
}

.connection-card.connected .connection-error {
  grid-column: 1 / -1;
}

.connect-button {
  border: 0;
  color: #171127;
  background: #b19aff;
}

.disconnect-button {
  border: 1px solid var(--line);
  color: #bcb6ca;
  background: transparent;
}

.key-fallback {
  margin-top: 12px;
  color: #9994a9;
  font-size: 0.72rem;
}

.key-fallback summary {
  cursor: pointer;
  text-align: center;
}

.key-fallback label {
  display: block;
  margin: 13px 0 7px;
  color: #cbc6d6;
  font-weight: 750;
}

.key-input-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 7px;
}

.key-input-row input {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 11px;
  outline: none;
  padding: 10px 11px;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.055);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.key-input-row input:focus {
  border-color: var(--purple);
}

.key-input-row button {
  border: 0;
  border-radius: 11px;
  padding: 0 13px;
  color: #171127;
  background: var(--green);
  cursor: pointer;
  font-weight: 800;
}

.connection-error {
  min-height: 0;
  margin: 9px 0 0;
  color: #ff9bae;
  font-size: 0.74rem;
}

.connection-error:empty {
  margin: 0;
}

form label {
  display: block;
  margin-bottom: 9px;
  color: #d9d5e5;
  font-size: 0.82rem;
  font-weight: 700;
}

.model-picker {
  margin-bottom: 20px;
}

.model-picker select {
  width: 100%;
  min-height: 48px;
  border: 1px solid var(--line);
  border-radius: 14px;
  outline: none;
  padding: 0 42px 0 13px;
  color: var(--ink);
  background: var(--panel-solid);
  cursor: pointer;
  font-weight: 700;
  transition: border-color 160ms ease, box-shadow 160ms ease;
}

.model-picker select:focus {
  border-color: var(--purple);
  box-shadow: 0 0 0 4px rgba(156, 125, 255, 0.14);
}

.model-picker p {
  margin: 7px 2px 0;
  color: #777388;
  font-size: 0.68rem;
}

textarea {
  display: block;
  width: 100%;
  min-height: 118px;
  resize: vertical;
  border: 1px solid var(--line);
  border-radius: 18px;
  outline: none;
  padding: 16px;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.06);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04);
  line-height: 1.55;
  transition: border-color 160ms ease, box-shadow 160ms ease;
}

textarea:focus {
  border-color: var(--purple);
  box-shadow: 0 0 0 4px rgba(156, 125, 255, 0.14);
}

.seed-suggestions {
  display: flex;
  gap: 8px;
  margin: 11px 0 18px;
  overflow-x: auto;
  scrollbar-width: none;
}

.seed-suggestions::-webkit-scrollbar {
  display: none;
}

.seed-chip,
.secondary-button {
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 9px 14px;
  color: #d8d4e4;
  background: transparent;
  cursor: pointer;
}

.seed-chip {
  flex: 0 0 auto;
  font-size: 0.78rem;
}

.seed-chip:active,
.secondary-button:active {
  background: rgba(255, 255, 255, 0.08);
}

.form-error {
  min-height: 1.2rem;
  margin: 0 0 8px;
  color: #ff9bae;
  font-size: 0.84rem;
}

.primary-button {
  display: flex;
  width: 100%;
  min-height: 58px;
  align-items: center;
  justify-content: space-between;
  border: 0;
  border-radius: 18px;
  padding: 0 19px;
  color: #171127;
  background: linear-gradient(135deg, #b19aff, #ff9a7d);
  box-shadow: 0 14px 40px rgba(126, 88, 218, 0.25);
  cursor: pointer;
  font-weight: 800;
  transition: transform 140ms ease, box-shadow 160ms ease, filter 160ms ease;
}

.primary-button:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 18px 46px rgba(126, 88, 218, 0.34);
}

.primary-button:active:not(:disabled) {
  transform: translateY(1px) scale(0.995);
}

.primary-button:disabled {
  cursor: wait;
  filter: grayscale(0.5);
  opacity: 0.65;
}

.button-arrow {
  font-size: 1.35rem;
}

.setup-note {
  margin: 18px 0 0;
  color: #777388;
  font-size: 0.72rem;
  text-align: center;
}

.game-screen {
  display: flex;
  min-height: 100dvh;
  flex-direction: column;
  padding-right: 18px;
  padding-left: 18px;
}

.game-header {
  display: grid;
  grid-template-columns: 52px 1fr 52px;
  align-items: center;
}

.game-header h1 {
  font-size: 1rem;
  letter-spacing: -0.02em;
  text-align: center;
}

.game-title-wrap {
  min-width: 0;
  text-align: center;
}

.game-model {
  display: block;
  max-width: 100%;
  margin-top: 3px;
  overflow: hidden;
  color: #8c879e;
  font-size: 0.58rem;
  font-weight: 750;
  letter-spacing: 0.04em;
  text-overflow: ellipsis;
  text-transform: uppercase;
  white-space: nowrap;
}

.icon-button {
  display: grid;
  width: 42px;
  height: 42px;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 14px;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.04);
  cursor: pointer;
  font-size: 1.45rem;
}

.score-box {
  text-align: right;
}

.score-box span {
  display: block;
  color: #777388;
  font-size: 0.58rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.score-box strong {
  font-size: 1.1rem;
}

.progress-track {
  height: 3px;
  margin: 20px 0 14px;
  overflow: hidden;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.09);
}

.progress-value {
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--purple), var(--coral));
  transition: width 260ms ease;
}

.round-meta {
  display: flex;
  justify-content: space-between;
  margin-bottom: 12px;
  color: #8c879e;
  font-size: 0.72rem;
  font-weight: 750;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.streak {
  color: var(--coral);
}

.text-card {
  min-height: 170px;
  max-height: min(36dvh, 310px);
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 24px;
  padding: 20px;
  background: var(--panel);
  box-shadow: var(--shadow);
  overscroll-behavior: contain;
  scrollbar-width: thin;
}

.story-text {
  display: inline;
  margin: 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  color: #efebf5;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: clamp(0.98rem, 4vw, 1.08rem);
  line-height: 1.75;
}

.latest-token {
  border-radius: 4px;
  animation: token-arrive 360ms ease-out both;
}

@keyframes token-arrive {
  from {
    color: #171127;
    background: var(--coral);
    box-shadow: 0 0 0 3px rgba(255, 139, 112, 0.18);
  }
  to {
    color: inherit;
    background: transparent;
    box-shadow: 0 0 0 0 transparent;
  }
}

.cursor {
  display: inline-block;
  width: 0.55em;
  height: 1.1em;
  margin-left: 2px;
  border-radius: 2px;
  vertical-align: -0.18em;
  background: var(--coral);
  animation: blink 1.1s steps(2) infinite;
}

@keyframes blink {
  50% { opacity: 0.25; }
}

.question-area {
  margin-top: auto;
  padding: 22px 3px 12px;
}

.question-area .eyebrow {
  margin-bottom: 5px;
}

.token-hint {
  margin: 0;
  color: #777388;
  font-size: 0.72rem;
}

.token-hint strong {
  color: #bcb6ca;
}

.desktop-shortcuts {
  display: none;
  margin: 9px 0 0;
  color: #777388;
  font-size: 0.68rem;
}

.desktop-shortcuts kbd,
.choice-shortcut {
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-bottom-color: rgba(255, 255, 255, 0.3);
  border-radius: 5px;
  color: #c7c2d2;
  background: rgba(255, 255, 255, 0.06);
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.08);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.desktop-shortcuts kbd {
  padding: 2px 5px;
}

.choices {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 11px;
}

.token-choice {
  position: relative;
  min-width: 0;
  min-height: 94px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 14px 12px;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.055);
  cursor: pointer;
  transition: transform 120ms ease, border-color 180ms ease, background 180ms ease;
}

.token-choice > span {
  position: relative;
  z-index: 1;
}

.choice-shortcut {
  position: absolute;
  top: 9px;
  right: 9px;
  display: none;
  min-width: 22px;
  padding: 2px 5px;
  font-size: 0.65rem;
  font-weight: 700;
  text-align: center;
}

.token-choice:active:not(:disabled) {
  transform: scale(0.975);
}

.token-choice:hover:not(:disabled) {
  border-color: rgba(156, 125, 255, 0.52);
  background: rgba(156, 125, 255, 0.09);
  transform: translateY(-2px);
}

.token-choice:focus-visible {
  outline: 3px solid rgba(156, 125, 255, 0.52);
  outline-offset: 2px;
}

.token-choice:disabled {
  cursor: default;
}

.token-confidence {
  position: absolute !important;
  z-index: 0 !important;
  bottom: 0;
  left: 0;
  width: 0;
  height: 4px;
  background: currentColor;
  opacity: 0.5;
  transition: width 240ms ease;
}

.token-choice.revealed .token-confidence {
  width: var(--confidence);
}

.choice-result {
  position: absolute !important;
  top: 8px;
  left: 10px;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 900;
}

.token-value {
  display: block;
  overflow: hidden;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: clamp(1rem, 5.2vw, 1.35rem);
  font-weight: 760;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.token-probability {
  display: block;
  min-height: 1.1em;
  margin-top: 8px;
  color: transparent;
  font-size: 0.72rem;
  font-weight: 750;
}

.token-choice.revealed .token-probability {
  color: var(--muted);
}

.token-choice.correct {
  color: var(--green);
  border-color: rgba(98, 214, 166, 0.75);
  background: rgba(98, 214, 166, 0.13);
}

.token-choice.correct .token-probability {
  color: var(--green);
}

.token-choice.wrong-choice {
  color: var(--red);
  border-color: rgba(255, 117, 145, 0.7);
  background: rgba(255, 117, 145, 0.1);
}

.round-feedback {
  min-height: 25px;
  margin: 10px 0 0;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 700;
  text-align: center;
}

.round-feedback.is-correct {
  color: var(--green);
}

.round-feedback.is-wrong {
  color: #ff9bae;
}

.results-screen {
  align-items: center;
  text-align: center;
}

.result-orbit {
  display: grid;
  width: 92px;
  height: 92px;
  margin-bottom: 30px;
  place-items: center;
  border: 1px solid rgba(156, 125, 255, 0.42);
  border-radius: 50%;
  box-shadow: 0 0 0 15px rgba(156, 125, 255, 0.06), 0 0 0 30px rgba(156, 125, 255, 0.025);
}

.result-orbit span {
  display: grid;
  width: 54px;
  height: 54px;
  place-items: center;
  border-radius: 50%;
  color: #171127;
  background: linear-gradient(135deg, var(--purple), var(--green));
  font-size: 1.6rem;
  font-weight: 900;
}

.results-screen h1 {
  font-size: clamp(2.2rem, 10vw, 3.4rem);
}

.result-stats {
  display: grid;
  width: min(100%, 330px);
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin: 27px 0 12px;
}

.result-stats div {
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 15px 10px 13px;
  background: rgba(255, 255, 255, 0.045);
}

.result-stats strong {
  display: block;
  font-size: 2rem;
  letter-spacing: -0.06em;
  line-height: 1;
}

.result-stats span {
  display: block;
  margin-top: 6px;
  color: var(--muted);
  font-size: 0.65rem;
  font-weight: 800;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.final-summary {
  margin: 5px 0 12px;
  color: var(--muted);
}

.result-model-pill {
  margin: 0 0 16px;
  border-radius: 999px;
  padding: 6px 10px;
  color: #8c879e;
  background: rgba(255, 255, 255, 0.045);
  font-size: 0.68rem;
}

.result-model-pill strong {
  color: #c9c3d4;
}

.result-text-card {
  width: min(100%, 390px);
  max-height: 132px;
  margin-bottom: 20px;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 14px;
  background: var(--panel);
  text-align: left;
  scrollbar-width: thin;
}

.result-text-card > span {
  display: block;
  margin-bottom: 7px;
  color: var(--purple);
  font-size: 0.62rem;
  font-weight: 800;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.result-text-card p {
  margin: 0;
  white-space: pre-wrap;
  color: #ddd8e7;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.78rem;
  line-height: 1.55;
}

.result-actions {
  width: 100%;
  max-width: 360px;
}

.result-actions .secondary-button {
  width: 100%;
  min-height: 50px;
  margin-top: 10px;
}

.result-shortcuts {
  text-align: center;
}

.loading-overlay {
  position: fixed;
  z-index: 10;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  padding: 24px;
  background: rgba(12, 11, 24, 0.88);
  backdrop-filter: blur(14px);
}

.loading-overlay p {
  max-width: 320px;
  margin-top: 18px;
  color: #d5d0df;
  font-size: 0.9rem;
  text-align: center;
}

.loader {
  display: flex;
  gap: 8px;
}

.loader span {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--purple);
  animation: bounce 900ms infinite alternate;
}

.loader span:nth-child(2) {
  animation-delay: 160ms;
  background: #cf86c5;
}

.loader span:nth-child(3) {
  animation-delay: 320ms;
  background: var(--coral);
}

@keyframes bounce {
  to { transform: translateY(-12px); opacity: 0.5; }
}

@media (min-width: 680px) {
  body {
    padding: 24px;
  }

  .app-shell {
    min-height: calc(100dvh - 48px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 34px;
    box-shadow: var(--shadow);
  }

  .screen {
    min-height: calc(100dvh - 48px);
    padding-right: 34px;
    padding-left: 34px;
  }

  .game-screen {
    min-height: calc(100dvh - 48px);
  }
}

@media (hover: hover) and (pointer: fine) {
  .desktop-shortcuts,
  .choice-shortcut {
    display: block;
  }
}

@media (min-height: 980px) {
  .setup-screen {
    justify-content: center;
  }
}

@media (max-height: 700px) {
  .brand-mark {
    margin-bottom: 20px;
  }

  .intro {
    margin-bottom: 20px;
  }

  .text-card {
    min-height: 125px;
  }

  .question-area {
    padding-top: 14px;
  }

  .token-choice {
    min-height: 78px;
  }
}

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