:root {
  --bg: #0b0b0c;
  --sidebar: #111113;
  --panel: #17171a;
  --card: #1d1d21;
  --line: #2c2c31;
  --text: #f2f2f3;
  --muted: #9a9aa3;
  --accent: #f2f2f3;
  --accent-fg: #111113;
  --user: #26262b;
  --bot: #1a1a1e;
  --danger: #e07a5f;
  --ok: #7dcea0;
  --shadow: 0 16px 50px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 ui-sans-serif, system-ui, "Segoe UI", sans-serif;
}
button, input, textarea, select { font: inherit; color: inherit; }
.hidden { display: none !important; }
a { color: inherit; }

.screen { min-height: 100dvh; }
.login-card {
  width: min(400px, 92vw);
  margin: 16vh auto 0;
  padding: 32px 28px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: var(--shadow);
}
.brand-mark {
  display: inline-flex;
  align-items: center;
  height: 28px;
  padding: 0 10px;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-fg);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.login-card h1 { margin: 18px 0 6px; font-size: 28px; letter-spacing: -0.03em; }
.login-card p { margin: 0 0 22px; color: var(--muted); }
#login-form input {
  width: 100%;
  background: #0f0f12;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 14px;
}
#login-form button {
  width: 100%;
  margin-top: 12px;
  border: 0;
  border-radius: 12px;
  padding: 12px;
  background: var(--accent);
  color: var(--accent-fg);
  font-weight: 650;
  cursor: pointer;
}
.error { color: var(--danger); }

.workspace {
  display: flex !important;
  flex-direction: row;
  min-height: 100dvh;
  background:
    radial-gradient(1200px 500px at 70% -10%, rgba(255,255,255,0.04), transparent 50%),
    var(--bg);
}
.sidebar {
  width: 272px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 18px 14px 14px;
  background: var(--sidebar);
  border-right: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; padding: 0 6px 4px; }
.brand-sub { color: var(--muted); font-size: 12px; }
.new-chat, .logout, .text-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 12px;
  padding: 10px 12px;
  cursor: pointer;
}
.new-chat:hover, .logout:hover, .icon-btn:hover, .chat-item:hover { background: #242428; }
.logout { color: var(--muted); }
.chat-list {
  flex: 1;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.chat-item {
  text-align: left;
  width: 100%;
  border: 0;
  background: transparent;
  border-radius: 12px;
  padding: 10px 12px;
  cursor: pointer;
}
.chatgpt-btn {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 12px;
  padding: 10px 12px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 650;
}
.chatgpt-btn.on { border-color: #3d5c48; color: var(--ok); }
.chat-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 2px;
  align-items: stretch;
  border-radius: 12px;
}
.chat-row.active { background: var(--card); }
.chat-row .chat-item { padding-right: 6px; }
.chat-act {
  width: 28px;
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  border-radius: 8px;
}
.chat-act:hover { background: #242428; color: var(--text); }

.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(0,0,0,0.55);
  display: grid;
  place-items: center;
  padding: 18px;
}
.modal-card {
  width: min(420px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 22px;
  box-shadow: var(--shadow);
}
.modal-card h2 { margin: 0 0 8px; font-size: 20px; }
.modal-card p { margin: 0 0 12px; color: var(--muted); font-size: 14px; }
.auth-status { min-height: 1.2em; }
.modal .auth-code {
  margin: 8px 0 14px;
  padding: 14px;
  text-align: center;
  background: #0f0f12;
  border: 1px solid var(--line);
  border-radius: 12px;
}
.chat-item small { display: block; margin-top: 3px; color: var(--muted); font-size: 11px; }

.stage {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  position: relative;
}
.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 22px 10px;
}
.top-meta strong { display: block; font-size: 15px; }
.top-meta span { color: var(--muted); font-size: 12px; }
.icon-btn {
  width: 36px;
  height: 36px;
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 10px;
  display: grid;
  place-items: center;
  cursor: pointer;
}
.icon-btn.rec { background: var(--danger); border-color: transparent; color: white; }
#toggle-side { display: none; }

.banner a { color: #ffd7c8; }
.auth-code {
  margin: 8px 0;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 0.12em;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.thread {
  flex: 1;
  overflow: auto;
  padding: 8px 22px 24px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-width: 860px;
  width: 100%;
  margin: 0 auto;
}
.bubble {
  max-width: min(100%, 720px);
  padding: 12px 14px;
  border-radius: 16px;
  white-space: pre-wrap;
  word-break: break-word;
}
.bubble.user {
  align-self: flex-end;
  background: var(--user);
}
.bubble.assistant {
  align-self: flex-start;
  background: var(--bot);
  border: 1px solid var(--line);
  white-space: normal;
}
.bubble.user { white-space: pre-wrap; }
.md p { margin: 0 0 0.7em; }
.md p:last-child { margin-bottom: 0; }
.md ul, .md ol { margin: 0 0 0.7em; padding-left: 1.2em; }
.md h1, .md h2, .md h3 { margin: 0 0 0.45em; font-size: 1.05em; }
.md code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.86em;
  background: #121215;
  padding: 0.1em 0.35em;
  border-radius: 6px;
}
.md pre {
  background: #121215;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  overflow: auto;
  margin: 0 0 0.7em;
}
.md pre code { background: none; padding: 0; }
.md a { color: #d7d7de; text-decoration: underline; text-underline-offset: 3px; }
.md-table { overflow: auto; margin: 0 0 0.7em; }
.md table { width: 100%; border-collapse: collapse; font-size: 13px; }
.md th, .md td { border-bottom: 1px solid var(--line); padding: 7px 8px; text-align: left; vertical-align: top; }
.md th { color: var(--muted); font-weight: 600; }

.usage-box { margin-top: auto; display: flex; flex-direction: column; gap: 8px; padding-top: 12px; }
.usage-card {
  background: #0f0f12;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px;
}
.usage-head { font-size: 12px; font-weight: 650; margin-bottom: 6px; }
.usage-card small { color: var(--muted); font-size: 11px; line-height: 1.35; display: block; }
.usage-card a { color: var(--muted); }
.usage-bar { display: grid; grid-template-columns: 64px 1fr auto; gap: 6px; align-items: center; font-size: 11px; color: var(--muted); margin-top: 6px; position: relative; }
.usage-bar span { color: #c8c8d0; }
.usage-bar i {
  display: block;
  height: 6px;
  border-radius: 99px;
  background: var(--ok);
  min-width: 2px;
}
.usage-bar.mid i { background: #e0b15c; }
.usage-bar.low i { background: var(--danger); }
.usage-bar b { color: var(--text); font-weight: 650; }
.usage-bar em { display: none; }
.bubble.error { border-color: var(--danger); }
.meta { display: block; margin-top: 6px; color: var(--muted); font-size: 11px; }
.tools { color: #c8c8d0; font-size: 12px; margin-top: 8px; }
.thumbs, .previews { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.thumbs img, .previews img { width: 96px; height: 96px; object-fit: cover; border-radius: 10px; }
audio { width: 220px; margin-top: 8px; }

.composer {
  padding: 0 22px calc(18px + env(safe-area-inset-bottom));
}
.composer-card {
  max-width: 860px;
  margin: 0 auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 10px 10px 8px;
  box-shadow: var(--shadow);
}
#text {
  width: 100%;
  resize: none;
  min-height: 48px;
  max-height: 160px;
  background: transparent;
  border: 0;
  outline: none;
  padding: 8px 10px 4px;
}
.composer-bar {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: center;
  padding: 4px 2px 2px;
}
.bar-left, .bar-right { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.select-wrap {
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-size: 10px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.select-wrap select {
  background: #121215;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 7px 10px;
  min-width: 148px;
}
.select-wrap.is-disabled { opacity: 0.45; pointer-events: none; }
.send {
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-fg);
  display: grid;
  place-items: center;
  cursor: pointer;
}
.text-btn {
  width: auto;
  padding: 8px 10px;
  font-size: 12px;
}
.backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 4;
}

@media (max-width: 860px) {
  #toggle-side { display: grid; }
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 5;
    transform: translateX(-105%);
    transition: transform 0.2s ease;
  }
  .sidebar.open { transform: none; }
  .backdrop.open { display: block; }
  .thread, .composer { padding-left: 14px; padding-right: 14px; }
  .select-wrap select { min-width: 120px; }
}
