/* ===== Design tokens ===== */
:root {
  color-scheme: light dark;

  /* sizes (rem) */
  --font: 1rem;
  --msg: 0.975rem;
  --title: 1.0625rem;
  --small: 0.8125rem;
  --radius: 1.5rem;
  --radius-sm: 0.75rem;
  --gap: 1rem;
  --thread-max: 46rem;
  --avatar: 1.75rem;

  /* light palette (default) */
  --bg: oklch(0.98 0.008 85);
  --surface: oklch(1 0 0);
  --text: oklch(0.28 0.012 60);
  --muted: oklch(0.58 0.012 65);
  --user-bubble: oklch(0.94 0.006 75);
  --border: oklch(0.9 0.005 80);
  --accent: oklch(0.63 0.16 40);
  --accent-contrast: oklch(0.99 0 0);
  --error: oklch(0.55 0.2 25);
  --error-bg: oklch(0.95 0.04 25);
  --scrim: oklch(0 0 0 / 0.5);
  --shadow: 0 0.5rem 1.5rem oklch(0 0 0 / 0.06);
}

/* dark when system prefers it (and user hasn't forced light) */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: oklch(0.2 0.004 60);
    --surface: oklch(0.24 0.005 60);
    --text: oklch(0.92 0.008 80);
    --muted: oklch(0.68 0.01 75);
    --user-bubble: oklch(0.29 0.007 65);
    --border: oklch(0.33 0.005 60);
    --accent: oklch(0.7 0.14 42);
    --accent-contrast: oklch(0.18 0.004 60);
    --error: oklch(0.72 0.17 25);
    --error-bg: oklch(0.3 0.06 25);
    --shadow: 0 0.5rem 1.5rem oklch(0 0 0 / 0.35);
  }
}

/* explicit dark toggle wins regardless of system */
:root[data-theme="dark"] {
  --bg: oklch(0.2 0.004 60);
  --surface: oklch(0.24 0.005 60);
  --text: oklch(0.92 0.008 80);
  --muted: oklch(0.68 0.01 75);
  --user-bubble: oklch(0.29 0.007 65);
  --border: oklch(0.33 0.005 60);
  --accent: oklch(0.7 0.14 42);
  --accent-contrast: oklch(0.18 0.004 60);
  --error: oklch(0.72 0.17 25);
  --error-bg: oklch(0.3 0.06 25);
  --shadow: 0 0.5rem 1.5rem oklch(0 0 0 / 0.35);
}

/* ===== Base ===== */
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: var(--font)/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

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

/* ===== Top bar ===== */
.topbar {
  display: grid;
  grid-template-columns: 2.5rem 1fr auto;
  align-items: center;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.topbar-actions {
  display: flex;
  align-items: center;
}
.icon-btn {
  display: grid;
  place-items: center;
  width: 2.5rem;
  height: 2.5rem;
  border: none;
  border-radius: 50%;
  background: transparent;
}
.icon-btn:hover { background: var(--user-bubble); }
.ico {
  width: 1.375rem;
  height: 1.375rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.title {
  border: none;
  background: transparent;
  text-align: center;
  padding: 0.25rem;
  line-height: 1.15;
  -webkit-user-select: none;
  user-select: none;
}
.title-row {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}
.title-name {
  font-size: var(--title);
  font-weight: 600;
}
.subtitle {
  display: block;
  font-size: var(--small);
  color: var(--muted);
  margin-top: 0.1rem;
}

/* ===== Thread ===== */
.thread {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem 1rem 1.5rem;
  scroll-behavior: smooth;
}
.msg {
  width: 100%;
  max-width: var(--thread-max);
  margin: 0 auto 1.25rem;
  display: flex;
  gap: 0.75rem;
}
.msg.user { justify-content: flex-end; }
.msg.assistant { justify-content: flex-start; }

.avatar {
  flex: 0 0 var(--avatar);
  width: var(--avatar);
  height: var(--avatar);
  color: var(--accent);
  margin-top: 0.1rem;
}
.avatar svg { width: 100%; height: 100%; display: block; }

.bubble {
  font-size: var(--msg);
  line-height: 1.65;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg.user .bubble {
  background: var(--user-bubble);
  padding: 0.625rem 0.95rem;
  border-radius: var(--radius-sm);
  max-width: 80%;
}
.msg.assistant .bubble {
  padding-top: 0.15rem;
  max-width: calc(100% - var(--avatar) - 0.75rem);
}

/* typing indicator (reuses assistant layout) */
.dots {
  display: inline-flex;
  gap: 0.28rem;
  padding: 0.5rem 0.15rem;
}
.dots i {
  width: 0.45rem;
  height: 0.45rem;
  border-radius: 50%;
  background: var(--muted);
  animation: blink 1.3s infinite ease-in-out;
}
.dots i:nth-child(2) { animation-delay: 0.18s; }
.dots i:nth-child(3) { animation-delay: 0.36s; }
@keyframes blink {
  0%, 60%, 100% { opacity: 0.25; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-0.15rem); }
}

/* decoy "thinking" row + fake error (shown for non-phrase input) */
.think-word {
  font-size: var(--msg);
  color: var(--muted);
  padding-top: 0.15rem;
}
/* Claude-style "thinking": rays retract to the center and collapse to a point, then grow back
   out, turning as they go. */
.avatar.spin svg { animation: suncollapse 2.8s ease-in-out infinite; transform-origin: 50% 50%; }
@keyframes suncollapse {
  0%   { transform: rotate(0deg) scale(1); }
  50%  { transform: rotate(180deg) scale(0.12); }
  100% { transform: rotate(360deg) scale(1); }
}

.error-card {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  max-width: calc(100% - var(--avatar) - 0.75rem);
  padding: 0.6rem 0.85rem;
  background: var(--error-bg);
  color: var(--error);
  border: 1px solid var(--error);
  border-radius: var(--radius-sm);
  font-size: var(--msg);
}
.error-card svg { width: 1.1rem; height: 1.1rem; flex: 0 0 auto; }
.retry-btn {
  margin-left: auto;
  padding: 0.15rem 0.65rem;
  background: transparent;
  color: var(--error);
  border: 1px solid var(--error);
  border-radius: 0.5rem;
  font-size: var(--small);
}

@media (prefers-reduced-motion: reduce) {
  .avatar.spin svg { animation: sunpulse 1.6s ease-in-out infinite; }
}
@keyframes sunpulse { 0%, 100% { opacity: 0.4; } 50% { opacity: 1; } }

/* ===== Composer ===== */
.composer {
  padding: 0.5rem 1rem 0.75rem;
  background: var(--bg);
}
.composer-inner {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  max-width: var(--thread-max);
  margin: 0 auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.4rem 0.4rem 0.4rem 1rem;
  box-shadow: var(--shadow);
}
#input {
  flex: 1;
  border: none;
  outline: none;
  resize: none;
  background: transparent;
  color: var(--text);
  font: var(--msg)/1.5 inherit;
  max-height: 40vh;
  padding: 0.45rem 0;
}
#input::placeholder { color: var(--muted); }
.send-btn {
  flex: 0 0 auto;
  width: 2.25rem;
  height: 2.25rem;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-contrast);
  display: grid;
  place-items: center;
  transition: opacity 0.2s;
}
.send-btn:disabled { opacity: 0.35; cursor: default; }
.disclaimer {
  text-align: center;
  font-size: var(--small);
  color: var(--muted);
  margin: 0.5rem 0 0;
}

/* ===== Help card (type "help" while unlocked) ===== */
.modal {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 1rem;
  background: var(--scrim);
}
.modal-card {
  position: relative;
  width: 100%;
  max-width: 26rem;
  max-height: 85vh;
  overflow-y: auto;
  padding: 1.25rem 1.25rem 1.5rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
}
.modal-close {
  position: absolute;
  top: 0.6rem;
  right: 0.6rem;
  width: 2rem;
  height: 2rem;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
}
.modal-close:hover { background: var(--user-bubble); }
.modal-title {
  margin: 0 0 0.9rem;
  font-size: var(--title);
}
.help-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}
.help-list li {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}
.help-list strong { font-size: var(--msg); }
.help-list span {
  font-size: var(--small);
  line-height: 1.5;
  color: var(--muted);
  white-space: pre-line;
}

@media (max-width: 30rem) {
  :root { --thread-max: 100%; }
  .disclaimer { font-size: 0.75rem; }
}
