:root {
  --bg: #0f1216;
  --panel: #161b22;
  --panel-2: #1c232c;
  --line: #262d38;
  --text: #e6edf3;
  --muted: #8b98a5;
  --accent: #3b82f6;
  --accent-2: #2563eb;
  --danger: #ef4444;
  --ok: #22c55e;
  --radius: 10px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.muted { color: var(--muted); }
.small { font-size: 12px; }

/* ---------------------------------------------------------------- auth */

.auth-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 24px;
}

.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 28px;
}

.auth-card h1 { margin: 0 0 4px; font-size: 20px; }
.auth-card form { margin-top: 18px; display: grid; gap: 14px; }

label {
  display: grid;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
}

input, select, textarea {
  width: 100%;
  padding: 10px 12px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  font: inherit;
  outline: none;
}

input:focus, select:focus, textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, .15);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-2);
  color: var(--text);
  font: inherit;
  cursor: pointer;
  transition: background .15s, border-color .15s;
}

.btn:hover { background: #232c37; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-2); }
.btn.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn.danger:hover { background: rgba(239, 68, 68, .12); }
.btn.ghost { background: transparent; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.sm { padding: 5px 10px; font-size: 12px; }

.alert {
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 13px;
  margin-bottom: 14px;
}
.alert.error { background: rgba(239, 68, 68, .12); border: 1px solid rgba(239, 68, 68, .4); color: #fca5a5; }
.alert.ok { background: rgba(34, 197, 94, .12); border: 1px solid rgba(34, 197, 94, .4); color: #86efac; }
.alert.warn {
  background: rgba(245, 158, 11, .1);
  border: 1px solid rgba(245, 158, 11, .45);
  color: #fcd34d;
  line-height: 1.45;
}
.alert.warn strong { color: #fde68a; }

/* ------------------------------------------------------ security banner */

.warn-banner {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 9px 14px;
  background: rgba(245, 158, 11, .1);
  border-bottom: 1px solid rgba(245, 158, 11, .35);
  color: #fcd34d;
  font-size: 12.5px;
  line-height: 1.45;
}
.warn-banner .txt { flex: 1; }
.warn-banner strong { color: #fde68a; }
.warn-banner.hidden { display: none; }

/* ---------------------------------------------------------------- shell */

.shell {
  display: grid;
  grid-template-columns: 260px 1fr;
  height: 100vh;
  height: 100dvh;
}

/* Elements that only exist for the mobile layout. */
.only-mobile { display: none !important; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  flex: 0 0 auto;
  padding: 0;
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
}
.icon-btn:hover { background: var(--panel-2); }

.bars { display: grid; gap: 4px; width: 16px; }
.bars i { display: block; height: 2px; border-radius: 2px; background: var(--text); }

.nav-overlay {
  position: fixed;
  inset: 0;
  z-index: 55;
  background: rgba(0, 0, 0, .55);
  opacity: 0;
  pointer-events: none;
  transition: opacity .22s ease;
}
.nav-overlay.show { opacity: 1; pointer-events: auto; }

.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.sidebar-head {
  padding: 14px;
  border-bottom: 1px solid var(--line);
  display: grid;
  gap: 10px;
}

.brand { font-weight: 600; letter-spacing: .2px; }

.brand-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.conv-list { flex: 1; overflow-y: auto; padding: 8px; }

.conv-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 10px;
  border-radius: 8px;
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
}

.conv-item:hover { background: var(--panel-2); }
.conv-item.active { background: var(--panel-2); box-shadow: inset 2px 0 0 var(--accent); }
.conv-item .t { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-item .x { color: var(--muted); opacity: 0; font-size: 16px; line-height: 1; }
.conv-item:hover .x { opacity: 1; }
.conv-item .x:hover { color: var(--danger); }

.sidebar-foot {
  border-top: 1px solid var(--line);
  padding: 12px 14px;
  font-size: 12px;
  display: grid;
  gap: 8px;
}

/* ---------------------------------------------------------------- main */

.main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}

.topbar .spacer { flex: 1; }
.topbar select { width: auto; min-width: 200px; padding: 7px 10px; }

.thread {
  flex: 1;
  overflow-y: auto;
  padding: 22px 16px 8px;
}

.thread-inner { max-width: 820px; margin: 0 auto; display: grid; gap: 14px; }

.msg {
  display: grid;
  gap: 6px;
  max-width: 100%;
}

.msg .who { font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }

.bubble {
  padding: 12px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: var(--panel);
  overflow-wrap: anywhere;
}

.msg.user .bubble { background: #1b2a3f; border-color: #24405f; }
.msg.assistant .bubble { background: var(--panel); }
.msg.tool .bubble {
  background: #12171d;
  border-style: dashed;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  color: var(--muted);
  max-height: 320px;
  overflow: auto;
}

.bubble p { margin: 0 0 8px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble ul, .bubble ol { margin: 6px 0; padding-left: 20px; }
.bubble code {
  background: #0b0f14;
  padding: 1px 5px;
  border-radius: 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12.5px;
}
.bubble pre {
  background: #0b0f14;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  overflow: auto;
  margin: 8px 0;
}
.bubble pre code { background: none; padding: 0; }

.pending-card {
  border: 1px solid rgba(239, 68, 68, .45);
  background: rgba(239, 68, 68, .07);
  border-radius: var(--radius);
  padding: 12px 14px;
  display: grid;
  gap: 10px;
}
.pending-card .row { display: flex; gap: 8px; }
.pending-card.resolved { opacity: .55; }

.composer {
  border-top: 1px solid var(--line);
  background: var(--panel);
  padding: 12px 16px 16px;
}

.composer-inner { max-width: 820px; margin: 0 auto; display: flex; gap: 10px; align-items: flex-end; }

.composer textarea {
  resize: none;
  min-height: 46px;
  max-height: 200px;
  line-height: 1.5;
}

.composer .btn { height: 46px; padding: 0 18px; }

.empty {
  text-align: center;
  color: var(--muted);
  padding: 60px 20px;
  font-size: 14px;
}

.typing { display: inline-flex; gap: 4px; align-items: center; color: var(--muted); font-size: 12px; }
.typing i {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--muted);
  animation: blink 1.2s infinite;
}
.typing i:nth-child(2) { animation-delay: .2s; }
.typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 60%, 100% { opacity: .25; } 30% { opacity: 1; } }

dialog {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 22px;
  width: min(440px, 92vw);
}
dialog::backdrop { background: rgba(0, 0, 0, .6); }
dialog h2 { margin: 0 0 14px; font-size: 16px; }
dialog form { display: grid; gap: 12px; }
dialog .row { display: flex; gap: 8px; justify-content: flex-end; }

@media (max-width: 820px) {
  .only-mobile { display: inline-flex !important; }

  .shell {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr;
  }

  /* Sidebar becomes an off-canvas drawer instead of disappearing. */
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 60;
    width: min(300px, 86vw);
    transform: translateX(-102%);
    transition: transform .24s ease;
    box-shadow: 8px 0 28px rgba(0, 0, 0, .45);
    padding-bottom: env(safe-area-inset-bottom, 0);
  }
  .sidebar.open { transform: translateX(0); }

  .sidebar-head { padding: 12px 14px; }
  .sidebar-foot { padding-bottom: calc(12px + env(safe-area-inset-bottom, 0)); }

  /* Touch screens have no hover: keep the delete affordance visible. */
  .conv-item { padding: 12px 10px; font-size: 14px; }
  .conv-item .x { opacity: .55; padding: 0 4px; }
  .conv-item .x:hover { opacity: 1; }

  .topbar {
    gap: 8px;
    padding: 8px 10px;
    padding-top: calc(8px + env(safe-area-inset-top, 0));
  }
  .topbar select {
    min-width: 0;
    flex: 1 1 auto;
    max-width: 100%;
    padding: 9px 10px;
    font-size: 13px;
  }
  .topbar #status-line { display: none; }

  .warn-banner { padding: 8px 10px; font-size: 12px; }

  .thread { padding: 14px 10px 6px; }
  .thread-inner { gap: 12px; }
  .bubble { padding: 10px 12px; }
  .msg.tool .bubble { max-height: 220px; }

  .composer {
    padding: 8px 10px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom, 0));
  }
  .composer-inner { gap: 8px; }
  .composer .btn { height: 44px; padding: 0 16px; }
  .composer textarea { min-height: 44px; font-size: 16px; } /* 16px avoids iOS zoom-on-focus */

  dialog {
    width: calc(100vw - 20px);
    max-width: 440px;
    max-height: 88vh;
    overflow-y: auto;
    padding: 18px;
  }
  dialog input, dialog select { font-size: 16px; }
  dialog .row { flex-wrap: wrap; }
  dialog .row .btn { flex: 1 1 auto; }
}

@media (max-width: 380px) {
  .brand { font-size: 14px; }
  .composer .btn { padding: 0 12px; }
}
