:root {
  --bg: #0f1115;
  --panel: #181b22;
  --panel-2: #1f232c;
  --border: #2a2f3a;
  --text: #e7e9ee;
  --muted: #8a93a6;
  --accent: #5b8def;
  --accent-hover: #7aa3ff;
  --green: #4cb782;
  --red: #e26d6d;
  --left: #3b82f6;   /* blue   — A */
  --right: #f97316;  /* orange — B */
  --judge: #10b981;  /* green  — judge */
  --radius: 10px;
  --mono: ui-monospace, SFMono-Regular, Menlo, monospace;
  --tap: 44px;
  --topbar-h: 52px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #fafbfc;
    --panel: #ffffff;
    --panel-2: #f3f5f8;
    --border: #e2e5ea;
    --text: #1a1d23;
    --muted: #6a7280;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}

.muted { color: var(--muted); }
.small { font-size: 12px; }
.spacer { flex: 1; }
.block { display: block; width: 100%; text-align: left; }

/* Tap targets */
button, .ghost, select, input[type="checkbox"], a.ghost {
  min-height: var(--tap);
}
button, .ghost {
  font: inherit;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
button.compact { padding: 6px 10px; min-height: 36px; }
button:hover, .ghost:hover { border-color: var(--accent); }
button:active { transform: scale(0.98); }
button:disabled { opacity: 0.5; cursor: not-allowed; }
.iconbtn {
  background: transparent;
  border: none;
  font-size: 20px;
  padding: 0 10px;
  min-width: var(--tap);
  min-height: var(--tap);
}
a.ghost { text-decoration: none; }

select, input[type="text"], input[type="email"], input[type="url"], input[type="number"], input[type="search"], textarea {
  font: inherit;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  width: 100%;
}
textarea { font-family: var(--mono); font-size: 14px; resize: vertical; }
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent);
}

.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; vertical-align: middle; margin-right: 4px; }
.left-dot { background: var(--left); }
.right-dot { background: var(--right); }
.judge-dot { background: var(--judge); }

/* ===== Login ===== */
.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 24px;
}
.login-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
  max-width: 420px;
  width: 100%;
}
.login-card h1 { margin: 0 0 4px; }
.login-card form { margin-top: 24px; display: grid; gap: 10px; }
.login-card label { font-size: 12px; color: var(--muted); }
.login-card button { margin-top: 8px; padding: 12px; background: var(--accent); border-color: var(--accent); color: white; font-weight: 600; }
.msg { margin-top: 12px; padding: 10px 12px; border-radius: 8px; font-size: 13px; }
.msg.ok { background: rgba(76,183,130,0.15); color: var(--green); }
.msg.err { background: rgba(226,109,109,0.15); color: var(--red); }

/* ===== App layout ===== */
.app-body { display: flex; flex-direction: column; height: 100dvh; }
.topbar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
  height: var(--topbar-h);
  flex: 0 0 auto;
}
.topbar .brand { margin: 0; font-size: 16px; font-weight: 600; flex: 0 0 auto; }
.topbar #userEmail {
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.layout {
  display: grid;
  grid-template-columns: 280px 1fr;
  flex: 1;
  min-height: 0;
  position: relative;
}

/* ===== Drawer / sidebar ===== */
.drawer {
  border-right: 1px solid var(--border);
  background: var(--panel);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}
.drawer-head { padding: 12px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--panel); z-index: 1; }
.drawer-foot { padding: 12px; border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: 6px; }
.drawer-email { margin-top: 8px; word-break: break-all; }
.history-list { list-style: none; margin: 0; padding: 0; flex: 1; }
.history-list li {
  padding: 12px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  font-size: 13px;
}
.history-list li:hover { background: var(--panel-2); }
.history-list li.active { background: var(--panel-2); border-left: 3px solid var(--accent); padding-left: 9px; }
.h-prompt { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.h-meta { font-size: 11px; color: var(--muted); margin-top: 4px; }
.drawer-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 8;
}

/* ===== Main ===== */
.main {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
}

/* Settings card (collapsible model picker) */
.settings-card {
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
.settings-card > summary {
  padding: 10px 14px;
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 12px;
  user-select: none;
}
.settings-card > summary::-webkit-details-marker { display: none; }
.settings-card > summary::after {
  content: "▾";
  margin-left: auto;
  color: var(--muted);
  transition: transform 0.15s;
}
.settings-card[open] > summary::after { transform: rotate(180deg); }
.settings-summary { display: flex; align-items: center; gap: 6px; font-size: 13px; font-weight: 500; }
.vs-small { color: var(--muted); margin: 0 4px; }
.settings-body {
  padding: 0 14px 14px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.settings-body .judge-row { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 16px; padding-top: 8px; border-top: 1px solid var(--border); }
.judge-row label { display: flex; align-items: center; gap: 6px; }
.judge-row select { width: auto; min-width: 160px; }
.use-profile { cursor: pointer; }
.use-profile input { min-height: 0; width: auto; margin: 0; }

.model-cell { display: flex; flex-direction: column; gap: 6px; }
.cell-label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }
.adv { font-size: 12px; }
.adv summary { cursor: pointer; color: var(--muted); padding: 4px 0; list-style: revert; }
.adv label { display: block; margin-top: 6px; color: var(--muted); }
.adv .temp { display: flex; gap: 8px; align-items: center; }
.adv .temp input { width: 100px; }

/* Pane tabs (mobile only by default) */
.pane-tabs {
  display: none;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
  flex: 0 0 auto;
}
.pane-tabs .tab {
  flex: 1;
  background: transparent;
  border: none;
  border-bottom: 3px solid transparent;
  border-radius: 0;
  padding: 12px 0;
  min-height: var(--tap);
  font-weight: 500;
  color: var(--muted);
}
.pane-tabs .tab.active { color: var(--text); }
.pane-tabs .tab[data-tab="left"]  { color: var(--text); }
.pane-tabs .tab[data-tab="right"] { color: var(--text); }
.pane-tabs .tab[data-tab="judge"] { color: var(--text); }
.pane-tabs .tab[data-tab="left"].active  { border-bottom-color: var(--left); }
.pane-tabs .tab[data-tab="right"].active { border-bottom-color: var(--right); }
.pane-tabs .tab[data-tab="judge"].active { border-bottom-color: var(--judge); }
/* "has content" indicator dot, colored per tab */
.pane-tabs .tab.has-content::after { content: " ●"; font-size: 10px; vertical-align: middle; margin-left: 4px; }
.pane-tabs .tab[data-tab="left"].has-content::after  { color: var(--left); }
.pane-tabs .tab[data-tab="right"].has-content::after { color: var(--right); }
.pane-tabs .tab[data-tab="judge"].has-content::after { color: var(--judge); }

/* Panes: desktop = side-by-side + judge below; mobile = tabs.
   A/B row gets 2fr, judge row gets 1fr — A/B is twice as tall as judge. */
.panes {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 2fr 1fr;
  gap: 10px;
  padding: 10px;
  flex: 1;
  overflow: hidden;
  min-height: 0;
}
.pane {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
}
#paneLeft { border-top: 3px solid var(--left); }
#paneRight { border-top: 3px solid var(--right); }
#judgePane { grid-column: 1 / -1; border-top: 3px solid var(--judge); min-height: 0; }
.pane[hidden], #judgePane[hidden] { display: none !important; }
.pane-head {
  padding: 6px 10px 6px 12px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 12px;
  color: var(--muted);
  flex: 0 0 auto;
}
.pane-model { font-weight: 600; color: var(--text); }
.pane-stats { margin-left: auto; }
.copy-btn {
  font-size: 11px;
  padding: 4px 10px;
  min-height: 28px;
  border-radius: 6px;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
}
.copy-btn:hover { color: var(--text); border-color: var(--accent); }
.copy-btn.copied { background: var(--green); color: white; border-color: var(--green); }
.pane-body {
  padding: 12px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: var(--mono);
  font-size: 13.5px;
  flex: 1;
  min-height: 0;
  -webkit-overflow-scrolling: touch;
}
.pane-body.error { color: var(--red); }
.pane-body.markdown { font-family: inherit; font-size: 14px; white-space: normal; }

/* Animated "thinking" loader (per pane + judge) */
.pane-loader {
  color: var(--muted);
  font-family: inherit;
  font-size: 13px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.pane-loader .spinner {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
  flex: 0 0 auto;
}
.pane-loader .dots span {
  opacity: 0.25;
  animation: blink 1.4s infinite;
}
.pane-loader .dots span:nth-child(1) { animation-delay: 0s; }
.pane-loader .dots span:nth-child(2) { animation-delay: 0.2s; }
.pane-loader .dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes blink {
  0%, 60%, 100% { opacity: 0.25; }
  30% { opacity: 1; }
}
.pane-stats.waiting { color: var(--accent); }

/* Memory strips (above panes) */
.mem-strip {
  background: var(--panel-2);
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  flex: 0 0 auto;
}
.mem-strip[hidden] { display: none; }
.mem-strip details summary {
  padding: 6px 14px;
  cursor: pointer;
  list-style: none;
  display: flex;
  gap: 8px;
  align-items: center;
  user-select: none;
}
.mem-strip details summary::-webkit-details-marker { display: none; }
.mem-strip details summary::before {
  content: "▸";
  color: var(--muted);
  font-size: 10px;
  transition: transform 0.15s;
  display: inline-block;
}
.mem-strip details[open] summary::before { transform: rotate(90deg); }
.mem-strip-label { font-weight: 500; }
.mem-strip.learned .mem-strip-label::before { content: "✨ "; }
.mem-strip.docs .mem-strip-label::before { content: "📄 "; }
.mem-list {
  list-style: none;
  margin: 0;
  padding: 4px 14px 10px 32px;
  font-size: 12.5px;
  display: grid;
  gap: 4px;
}
.mem-list li { display: flex; gap: 8px; }
.mem-list .mem-kind {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  flex: 0 0 auto;
  min-width: 70px;
}

/* Memories dialog */
.memories-toolbar { display: flex; gap: 8px; margin: 12px 0; }
.memories-toolbar input { flex: 1; }
.memories-list { list-style: none; padding: 0; margin: 0; }
.memories-list li {
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  display: grid;
  grid-template-columns: 80px 1fr auto;
  gap: 10px;
  align-items: start;
  font-size: 13px;
}
.memories-list li .mem-kind { padding-top: 2px; }
.memories-list .mem-meta { font-size: 11px; color: var(--muted); margin-top: 4px; }
.memories-list .mem-del {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  font-size: 11px;
  padding: 4px 8px;
  min-height: 28px;
  border-radius: 6px;
}
.memories-list .mem-del:hover { color: var(--red); border-color: var(--red); }

/* Files dialog */
.file-add { display: grid; gap: 10px; margin: 12px 0 20px; }
.file-add-row { display: grid; gap: 4px; }
.file-tabs { display: flex; gap: 4px; }
.file-tab {
  padding: 6px 12px;
  min-height: 32px;
  background: transparent;
  border: 1px solid var(--border);
  font-size: 12px;
}
.file-tab.active { background: var(--accent); border-color: var(--accent); color: white; }
.file-pane[hidden] { display: none; }
#fileAddBtn { background: var(--accent); border-color: var(--accent); color: white; font-weight: 600; }
.files-list-head { font-size: 13px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); margin: 0 0 6px; }
.files-list { list-style: none; padding: 0; margin: 0; }
.files-list li {
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: center;
}
.files-list .file-title { font-weight: 500; font-size: 13px; }
.files-list .file-meta { font-size: 11px; color: var(--muted); margin-top: 2px; }
.files-list .file-meta a { color: var(--muted); }
.files-list .file-del {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  font-size: 11px;
  padding: 4px 8px;
  min-height: 28px;
  border-radius: 6px;
}
.files-list .file-del:hover { color: var(--red); border-color: var(--red); }

/* Vote bar */
.vote-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
  border-top: 1px solid var(--border);
  background: var(--panel);
  flex: 0 0 auto;
  flex-wrap: wrap;
}
.vote { font-size: 13px; padding: 8px 14px; min-height: 36px; }
.vote.selected { background: var(--accent); border-color: var(--accent); color: white; }

/* Prompt bar — sticky at bottom */
.prompt-bar {
  display: flex;
  gap: 8px;
  padding: 10px;
  padding-bottom: calc(10px + var(--safe-bottom));
  border-top: 1px solid var(--border);
  background: var(--panel);
  flex: 0 0 auto;
}
.prompt-bar textarea {
  flex: 1;
  min-height: 44px;
  max-height: 200px;
  resize: none;
}
.prompt-bar #runBtn {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
  font-weight: 600;
  padding: 0 22px;
}
.prompt-bar #runBtn:hover { background: var(--accent-hover); border-color: var(--accent-hover); }

/* Markdown rendering (judge) */
.markdown p { margin: 0 0 8px; }
.markdown strong { color: var(--accent); }
.markdown ul { margin: 4px 0 8px; padding-left: 20px; }
.markdown li { margin: 2px 0; }
.markdown code { font-family: var(--mono); background: var(--panel-2); padding: 1px 4px; border-radius: 3px; font-size: 0.9em; }

/* ===== Dialogs (bottom sheet on mobile, centered on desktop) ===== */
dialog.sheet {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0;
  width: 90vw;
  max-width: 560px;
  max-height: 85vh;
  overflow: hidden;
}
/* Only apply flex layout when actually open — otherwise dialog would
   stay in the document flow and obscure the page. */
dialog.sheet[open] {
  display: flex;
  flex-direction: column;
}
dialog.sheet::backdrop { background: rgba(0,0,0,0.55); backdrop-filter: blur(2px); }
.sheet-head {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
.sheet-head h2 { margin: 0; font-size: 16px; flex: 1; }
.sheet-body {
  padding: 16px;
  overflow-y: auto;
  flex: 1 1 auto;
  min-height: 0;
  -webkit-overflow-scrolling: touch;
}
.sheet-foot {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  background: var(--panel-2);
  flex: 0 0 auto;
  padding-bottom: calc(12px + var(--safe-bottom));
}
.sheet-foot #profileSave {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
  font-weight: 600;
}

.suggest-row { display: flex; gap: 8px; margin: 12px 0 8px; }
.suggest-row input { flex: 1; }
.suggest-row button { white-space: nowrap; }
#deepSearchBtn {
  flex-direction: column;
  gap: 2px;
  text-align: center;
  line-height: 1.3;
  padding: 10px 14px;
}
#deepSearchBtn[disabled] { opacity: 0.7; }
#deepSearchBtn .small { color: var(--muted); font-weight: normal; }
.profile-label { display: block; margin-top: 12px; }
.profile-label textarea { margin-top: 6px; min-height: 200px; }

.stats-row { display: flex; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid var(--border); }
.stats-row:last-child { border-bottom: none; }
.stats-section { margin-top: 16px; }
.stats-section h3 { margin: 0 0 4px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }

/* ===== Mobile breakpoint ===== */
@media (max-width: 800px) {
  .hide-mobile { display: none !important; }

  /* Drawer becomes overlay */
  .layout { grid-template-columns: 1fr; }
  .drawer {
    position: fixed;
    inset: var(--topbar-h) auto 0 0;
    width: min(85vw, 320px);
    z-index: 9;
    transform: translateX(-100%);
    transition: transform 0.2s;
    border-right: 1px solid var(--border);
  }
  body.drawer-open .drawer { transform: translateX(0); box-shadow: 4px 0 16px rgba(0,0,0,0.3); }
  body.drawer-open .drawer-backdrop { display: block; }

  .settings-body { grid-template-columns: 1fr; }

  /* Tabs visible, panes stack to single via tab switching */
  .pane-tabs { display: flex; }
  .panes {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr;
    padding: 8px;
    gap: 0;
  }
  .pane { display: none; }
  .pane.tab-active { display: flex; }
  #judgePane { max-height: none; }

  /* Slightly larger prompt bar */
  .prompt-bar textarea { min-height: var(--tap); }
}

@media (min-width: 801px) {
  .show-mobile { display: none !important; }
  .pane-tabs { display: none; }
  /* On desktop, judge pane only shows when it has content; controlled via [hidden] */
}
