:root {
  --bg: #f7f7f5;
  --surface: #ffffff;
  --ink: #1c1c1a;
  --muted: #73736b;
  --line: #e6e6e1;
  --fill: #f3f3f0;
  --accent: #1c1c1a;
  --blue: #3b82f6;
  --warn: #b42318;
  --shadow: 0 1px 2px rgba(28, 28, 26, 0.04), 0 8px 24px rgba(28, 28, 26, 0.04);
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
  --t: 180ms;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  height: 100dvh;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.45 Inter, ui-sans-serif, system-ui, sans-serif;
  letter-spacing: -0.011em;
}

body {
  display: flex;
  flex-direction: column;
  animation: page-in 0.5s var(--ease);
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 16px;
  min-height: 52px;
  height: auto;
  padding: 8px 18px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface) 86%, transparent);
  backdrop-filter: blur(16px);
  flex-shrink: 0;
  z-index: 5;
}

.brand {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.brand-name {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.02em;
  white-space: nowrap;
}

.credit {
  margin: 0;
  font-size: 11px;
  font-weight: 500;
  color: var(--muted);
}

.meta {
  margin: 0 0 0 auto;
  color: var(--muted);
  font-size: 12px;
  display: flex;
  gap: 12px;
  align-items: center;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 24px;
  padding: 0 9px;
  border-radius: 999px;
  background: var(--fill);
  color: var(--ink);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.02em;
  transition: background var(--t) var(--ease), color var(--t) var(--ease);
}
.chip::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #22c55e;
}
.chip[data-state="live"],
.chip[data-state="reading"] {
  background: #eff6ff;
  color: #1d4ed8;
}
.chip[data-state="live"]::before,
.chip[data-state="reading"]::before {
  background: var(--blue);
  animation: pulse 1.2s ease-in-out infinite;
}
.chip[data-state="error"] {
  background: #fef2f2;
  color: var(--warn);
}
.chip[data-state="error"]::before { background: var(--warn); }
.chip[data-state="done"]::before { background: #22c55e; }

.mode-toggle {
  display: flex;
  padding: 3px;
  border-radius: 8px;
  background: var(--fill);
  border: 1px solid var(--line);
}

.mode-opt {
  display: flex;
  align-items: center;
  padding: 5px 12px;
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
  border-radius: 6px;
  transition: background var(--t) var(--ease), color var(--t) var(--ease), box-shadow var(--t) var(--ease);
}
.mode-opt:hover { color: var(--ink); }
.mode-opt:has(input:checked) {
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 1px 2px rgba(28, 28, 26, 0.08);
}
.mode-opt input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

main {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 16px;
}

#composer,
#mcq-form {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.editor {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  overflow: hidden;
  animation: rise 0.45s var(--ease);
}

textarea {
  flex: 1;
  width: 100%;
  min-height: 0;
  margin: 0;
  padding: 22px 24px;
  border: 0;
  resize: none;
  background: transparent;
  color: var(--ink);
  font: 15px/1.6 Inter, ui-sans-serif, system-ui, sans-serif;
}
textarea::placeholder { color: #a3a39b; }
textarea:focus { outline: none; }

.statusbar {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 52px;
  padding: 8px 12px 8px 16px;
  border-top: 1px solid var(--line);
  background: linear-gradient(#fff, var(--fill));
  flex-shrink: 0;
}

.hint {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  transition: color var(--t) var(--ease);
}
.hint.error { color: var(--warn); }

.dropzone {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  color: var(--muted);
  font-size: 12px;
  font-weight: 500;
  padding: 6px 8px;
  border-radius: 8px;
  transition: background var(--t) var(--ease), color var(--t) var(--ease);
}
.dropzone:hover,
.dropzone.drag {
  background: var(--fill);
  color: var(--ink);
}
.dropzone-copy { color: inherit; }

.preview {
  display: flex;
  align-items: center;
  gap: 8px;
}
.preview img {
  width: 22px;
  height: 22px;
  object-fit: cover;
  border-radius: 4px;
  border: 1px solid var(--line);
}

.linkish {
  border: 0;
  background: none;
  color: var(--muted);
  padding: 0;
  font: inherit;
  cursor: pointer;
  transition: color var(--t) var(--ease);
}
.linkish:hover { color: var(--ink); }

button {
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  padding: 7px 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: background var(--t) var(--ease), transform 80ms var(--ease), box-shadow var(--t) var(--ease);
}
button:hover:not(:disabled) { background: var(--fill); }
button:active:not(:disabled) { transform: translateY(1px); }
button:disabled { opacity: 0.45; cursor: not-allowed; }
#ask-btn {
  background: var(--ink);
  color: #fff;
  border-color: var(--ink);
  margin-left: auto;
  padding: 8px 14px;
}
#ask-btn:hover:not(:disabled) {
  background: #2e2e2b;
  box-shadow: 0 6px 16px rgba(28, 28, 26, 0.18);
}

.session {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(280px, 0.9fr) 1.2fr;
  grid-template-rows: minmax(0, 1fr) auto;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  overflow: hidden;
  animation: rise 0.4s var(--ease);
}

.pane {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
}
.pane + .pane { border-left: 1px solid var(--line); }

.pane-h {
  min-height: 40px;
  height: auto;
  display: flex;
  align-items: center;
  padding: 6px 12px 6px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--fill);
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  letter-spacing: 0.02em;
  flex-shrink: 0;
  gap: 8px;
}

.question-head {
  flex-wrap: wrap;
  justify-content: space-between;
}

.q-nav {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
  justify-content: flex-end;
  margin-left: auto;
  max-width: 100%;
}
.q-tab {
  background: var(--surface);
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 4px 9px;
  font-size: 12px;
  font-weight: 600;
}
.q-tab:hover { background: #fff; color: var(--ink); }
.q-tab.active {
  color: #fff;
  background: var(--ink);
  border-color: var(--ink);
}

#question-view {
  margin: 0;
  padding: 16px 18px;
  white-space: normal;
  font-size: 14px;
  line-height: 1.65;
  flex: 1;
  min-height: 0;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}

.katex {
  font-size: 1.08em;
  white-space: nowrap;
}
.katex-mathml {
  display: none !important;
}
.katex-display {
  display: block;
  margin: 0.65em 0;
  overflow-x: auto;
  overflow-y: hidden;
  white-space: normal;
}

.consensus-box {
  border-top: 1px solid var(--line);
  padding: 16px 18px;
  background: var(--fill);
  animation: rise 0.35s var(--ease);
  flex-shrink: 0;
  max-height: 42%;
  overflow: auto;
}
.agree {
  font-size: 28px;
  font-weight: 600;
  letter-spacing: -0.03em;
  word-break: break-word;
}
.host-solution {
  margin-top: 10px;
  white-space: normal;
  font-size: 13px;
  line-height: 1.6;
}
.muted, .detail { color: var(--muted); font-size: 12px; }
.again-inline {
  grid-column: 1 / -1;
  margin: 0;
  border: 0;
  border-top: 1px solid var(--line);
  border-radius: 0;
  width: 100%;
  min-height: 44px;
  background: var(--surface);
}

.chat {
  padding: 0;
  overflow: auto;
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  -webkit-overflow-scrolling: touch;
}

.msg {
  border-bottom: 1px solid var(--line);
  padding: 14px 16px;
  animation: rise 0.35s var(--ease);
}
.msg-h {
  display: flex;
  justify-content: space-between;
  color: var(--muted);
  font-size: 11px;
  font-weight: 500;
}
.handle { color: var(--ink); font-weight: 600; }
.answer {
  margin: 6px 0 0;
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -0.03em;
  word-break: break-word;
}
.why, .solution {
  margin-top: 6px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.55;
}
.solution { white-space: normal; color: var(--ink); }
.msg.thinking {
  background: linear-gradient(90deg, #fff 0%, var(--fill) 50%, #fff 100%);
  background-size: 200% 100%;
  animation: shimmer 1.4s ease-in-out infinite;
}
.msg.error { background: #fef7f6; }
.msg.error .answer { color: var(--warn); font-size: 14px; }
.msg.skipped .answer { color: var(--muted); font-size: 16px; }

.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(20, 20, 18, 0.4);
  display: grid;
  place-items: center;
  animation: fade 0.2s var(--ease);
}
.modal-card {
  width: min(520px, calc(100% - 32px));
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 16px;
  box-shadow: 0 24px 60px rgba(20, 20, 18, 0.18);
  animation: rise 0.3s var(--ease);
}
.modal-h {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
.modal-h h2 {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
}
#camera-video {
  width: 100%;
  max-height: 360px;
  border-radius: 10px;
  background: #111;
  object-fit: cover;
}
.modal-actions { display: flex; justify-content: flex-end; margin-top: 10px; }

/* ---------- account & credits ---------- */
.account {
  display: flex;
  align-items: center;
  min-height: 32px;
}
.account-in {
  display: flex;
  align-items: center;
  gap: 10px;
}
.balance {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 30px;
  padding: 0 6px 0 11px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.balance .plus {
  display: grid;
  place-items: center;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 13px;
  line-height: 1;
}
.balance.low { border-color: #f5c2bd; color: var(--warn); }
.avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 1px solid var(--line);
  object-fit: cover;
}
.credits-card { width: min(440px, calc(100% - 32px)); }
.credits-balance { margin: 0 0 6px; font-size: 13px; color: var(--muted); }
.credits-balance strong { color: var(--ink); font-size: 18px; margin-left: 6px; font-variant-numeric: tabular-nums; }
.packs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin: 14px 0 10px;
}
.pack { height: 44px; font-size: 14px; font-weight: 600; }
.custom-amount { display: flex; gap: 8px; }
.custom-amount input {
  flex: 1;
  min-width: 0;
  height: 38px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font: inherit;
  font-size: 16px;
  background: var(--surface);
  color: var(--ink);
}
.history { margin-top: 12px; font-size: 12px; }
.history summary { cursor: pointer; color: var(--muted); }
.history ul { list-style: none; margin: 8px 0 0; padding: 0; max-height: 180px; overflow: auto; }
.history li {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 6px 0;
  border-bottom: 1px solid var(--line);
}
.history li.plus span:first-child { color: #15803d; }
.legal-links a, .site-footer a { color: var(--muted); }
.cost-line { margin-top: 10px; font-size: 11px; color: var(--muted); }
.site-footer {
  flex-shrink: 0;
  padding: 6px 18px;
  font-size: 11px;
  color: var(--muted);
  text-align: center;
}
@media (max-width: 420px) {
  .packs { grid-template-columns: repeat(2, 1fr); }
}

.hidden { display: none !important; }

@keyframes page-in {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes rise {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}
@keyframes fade {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes pulse {
  50% { opacity: 0.35; transform: scale(0.85); }
}
@keyframes shimmer {
  0% { background-position: 100% 0; }
  100% { background-position: -100% 0; }
}

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

@media (max-width: 860px) {
  .toolbar {
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-areas:
      "brand account"
      "mode meta";
    height: auto;
    gap: 8px 10px;
    padding: 10px 12px;
    padding-top: max(10px, env(safe-area-inset-top));
  }
  .brand { grid-area: brand; }
  .mode-toggle {
    grid-area: mode;
    width: 100%;
  }
  .mode-opt {
    flex: 1;
    justify-content: center;
    padding: 8px 10px;
  }
  .meta {
    grid-area: meta;
    margin: 0;
  }
  .account { grid-area: account; justify-self: end; }
  .models { display: none; }
  .site-footer { display: none; }

  main {
    padding: 10px;
    padding-bottom: max(10px, env(safe-area-inset-bottom));
  }

  textarea {
    padding: 16px;
    font-size: 16px;
  }

  .statusbar {
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-areas:
      "photo camera"
      "hint hint"
      "run run";
    gap: 8px;
    height: auto;
    min-height: 0;
    padding: 10px;
    padding-bottom: max(10px, env(safe-area-inset-bottom));
  }
  .dropzone { grid-area: photo; min-height: 40px; }
  #camera-btn { grid-area: camera; min-height: 40px; }
  .statusbar .hint {
    grid-area: hint;
    white-space: normal;
    overflow: visible;
  }
  #ask-btn {
    grid-area: run;
    margin-left: 0;
    width: 100%;
    min-height: 44px;
  }

  .preview {
    max-width: 100%;
  }
  #preview-name {
    max-width: 42vw;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  html:has(#session:not(.hidden)),
  html:has(#session:not(.hidden)) body {
    height: auto;
    min-height: 100dvh;
    overflow: auto;
  }
  body:has(#session:not(.hidden)) main {
    flex: 1 0 auto;
    min-height: auto;
    overflow: visible;
  }

  .session {
    display: flex;
    flex-direction: column;
    flex: 0 0 auto;
    min-height: 0;
    height: auto;
    overflow: visible;
  }
  .pane {
    flex: 0 0 auto;
    min-height: 0;
    overflow: visible;
  }
  .pane + .pane { border-left: 0; border-top: 1px solid var(--line); }
  body:has(#session:not(.hidden)) .toolbar {
    position: sticky;
    top: 0;
    z-index: 4;
  }
  .pane:first-child,
  .chat-wrap {
    max-height: none;
    min-height: 0;
  }
  #question-view {
    flex: none;
    min-height: 0;
    overflow: visible;
    font-size: 16px;
    padding: 16px 14px;
  }
  .consensus-box {
    max-height: none;
    overflow: visible;
    padding: 16px 14px 18px;
  }
  .chat {
    flex: none;
    overflow: visible;
  }
  .again-inline {
    position: sticky;
    bottom: 0;
    z-index: 3;
    padding-bottom: max(10px, env(safe-area-inset-bottom));
    box-shadow: 0 -8px 20px rgba(28, 28, 26, 0.06);
  }

  .agree { font-size: 26px; }
  .answer { font-size: 22px; }
  .host-solution,
  .solution,
  .why {
    font-size: 15px;
    line-height: 1.6;
  }

  .modal {
    padding: 12px;
    padding-top: max(12px, env(safe-area-inset-top));
    padding-bottom: max(12px, env(safe-area-inset-bottom));
    align-items: end;
  }
  .modal-card {
    width: 100%;
    max-height: min(92dvh, 100%);
    overflow: auto;
  }
  #camera-video {
    max-height: 52dvh;
  }
  #shutter-btn {
    width: 100%;
    min-height: 44px;
  }
}

@media (max-width: 420px) {
  .brand-name { font-size: 13px; }
  .mode-opt { padding: 8px 6px; font-size: 12px; }
}
