/* ---------- design tokens ---------- */
:root {
  --bg-paper:       #faf9f6;
  --bg-paper-soft:  #f1ede3;
  --bg-card:        #ffffff;
  --ink:            #1a1915;
  --ink-soft:       #383530;
  --ink-muted:      #8a857a;
  --ink-faint:      #b8b3a7;
  --rule:           #e8e2d2;
  --rule-soft:      #f0eadb;

  --sidebar-bg:     #1a1916;
  --sidebar-surface:#23211d;
  --sidebar-hover:  #2a2824;
  --sidebar-active: #34312c;
  --sidebar-ink:    #e9e4d6;
  --sidebar-muted:  #8a8578;
  --sidebar-rule:   #2c2a26;

  --accent:         #c2683a;
  --accent-soft:    #e09a6d;
  --accent-ink:     #ffffff;

  --user-bubble:    #ece4d2;
  --user-bubble-ink:#1a1915;

  --code-bg:        #f5f0e3;
  --code-ink:       #2a2620;

  --danger:         #b04a3f;
  --ok:             #6c8e5a;

  --r-sm: 6px;
  --r-md: 10px;
  --r-lg: 14px;
  --r-xl: 20px;

  --font-display: 'Fraunces', Georgia, serif;
  --font-body:    'Inter', system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-mono:    'JetBrains Mono', ui-monospace, Menlo, monospace;

  --max-col: 760px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font-body);
  font-size: 15.5px;
  color: var(--ink);
  background: var(--bg-paper);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button { font-family: inherit; cursor: pointer; }

/* ---------- login ---------- */
.login-body {
  display: grid;
  place-items: center;
  min-height: 100vh;
  background:
    radial-gradient(1200px 600px at 20% 0%, #ece4d2 0%, transparent 60%),
    radial-gradient(900px 500px at 90% 100%, #ebdfc4 0%, transparent 55%),
    var(--bg-paper);
}
.login-card {
  width: min(420px, 92vw);
  padding: 36px 36px 32px;
  border: 1px solid var(--rule);
  background: var(--bg-card);
  border-radius: var(--r-lg);
  box-shadow: 0 30px 80px -40px rgba(28,26,22,.25);
}
.login-mark { display: flex; align-items: center; gap: 10px; margin-bottom: 28px; }
.login-mark-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 4px rgba(194,104,58,.15);
}
.login-mark-text {
  font-family: var(--font-mono);
  font-size: 11.5px;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--ink-muted);
}
.login-title {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 36px;
  letter-spacing: -.02em;
  margin: 0 0 6px;
}
.login-sub { margin: 0 0 22px; color: var(--ink-muted); font-size: 14px; }
.login-form { display: grid; gap: 14px; }
.login-error {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--danger);
  padding: 8px 10px;
  border: 1px solid rgba(176,74,63,.3);
  background: rgba(176,74,63,.06);
  border-radius: var(--r-md);
}

/* ---------- fields / buttons ---------- */
.field { display: grid; gap: 6px; }
.field-label {
  font-family: var(--font-mono);
  font-size: 10.5px;
  letter-spacing: .12em;
  text-transform: uppercase;
  color: var(--ink-muted);
  display: flex; align-items: center; gap: 8px;
  font-weight: 500;
}
.field-value {
  margin-left: auto;
  font-family: var(--font-mono);
  font-size: 11.5px;
  color: var(--ink);
  background: var(--bg-paper-soft);
  padding: 2px 8px;
  border-radius: 999px;
  letter-spacing: 0;
  text-transform: none;
  font-weight: 500;
}
.muted { color: var(--ink-muted); font-weight: 400; }
.field-narrow { max-width: 140px; }

input[type=text], input[type=password], input[type=number], select, textarea {
  font-family: var(--font-body);
  font-size: 14.5px;
  color: var(--ink);
  background: var(--bg-card);
  border: 1px solid var(--rule);
  border-radius: var(--r-md);
  padding: 10px 12px;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(194,104,58,.15);
}
input[type=range] { accent-color: var(--accent); }

.checkbox { display: inline-flex; gap: 10px; align-items: center; font-size: 14px; color: var(--ink-soft); }
.checkbox input { accent-color: var(--accent); }

.btn-primary {
  background: var(--ink);
  color: var(--bg-paper);
  border: 1px solid var(--ink);
  padding: 11px 16px;
  border-radius: var(--r-md);
  font-weight: 500;
  font-size: 14.5px;
  transition: transform .05s ease, background .15s;
}
.btn-primary:hover { background: #2a2620; }
.btn-primary:active { transform: translateY(1px); }

.btn-ghost {
  background: transparent;
  border: 1px solid transparent;
  color: var(--sidebar-ink);
  padding: 9px 12px;
  border-radius: var(--r-md);
  font-size: 13.5px;
  display: flex; align-items: center; gap: 9px;
  transition: background .12s;
  width: 100%;
  text-align: left;
}
.btn-ghost:hover { background: var(--sidebar-hover); }
.btn-ghost-danger:hover { background: rgba(176,74,63,.15); color: #f3c5be; }
.btn-ghost .ico { font-family: var(--font-mono); width: 14px; text-align: center; opacity: .8; }

.btn-link { background: none; border: 0; color: inherit; padding: 0; }

/* ---------- app shell ---------- */
.app {
  display: grid;
  grid-template-columns: 268px 1fr;
  height: 100vh;
  overflow: hidden;
}

/* ---------- sidebar ---------- */
.sidebar {
  background: var(--sidebar-bg);
  color: var(--sidebar-ink);
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--sidebar-rule);
  min-height: 0;
  overflow: hidden;
}
.brand {
  display: flex; align-items: center; gap: 10px;
  padding: 18px 18px 14px;
  border-bottom: 1px solid var(--sidebar-rule);
  flex-shrink: 0;
}
.brand-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 3px rgba(194,104,58,.15);
}
.brand-text {
  font-family: var(--font-mono);
  font-size: 11.5px;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--sidebar-muted);
}

.btn-new {
  margin: 14px 12px 8px;
  padding: 10px 12px;
  background: var(--sidebar-surface);
  color: var(--sidebar-ink);
  border: 1px solid var(--sidebar-rule);
  border-radius: var(--r-md);
  font-size: 13.5px;
  font-weight: 500;
  text-align: left;
  transition: background .12s, border-color .12s;
  flex-shrink: 0;
}
.btn-new:hover { background: var(--sidebar-hover); border-color: #3a3832; }

.chats-section {
  flex: 1 1 0;
  overflow-y: auto;
  padding: 6px 6px 12px;
  min-height: 0;
}
.chats-label {
  font-family: var(--font-mono);
  font-size: 9.5px;
  letter-spacing: .16em;
  text-transform: uppercase;
  color: var(--sidebar-muted);
  padding: 12px 10px 6px;
  font-weight: 500;
}
.chats { list-style: none; padding: 0; margin: 0; display: grid; gap: 1px; }
.chat-item {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px;
  border-radius: var(--r-md);
  cursor: pointer;
  color: var(--sidebar-ink);
  font-size: 13.5px;
  transition: background .12s;
  position: relative;
}
.chat-item:hover { background: var(--sidebar-hover); }
.chat-item.active { background: var(--sidebar-active); }
.chat-item .chat-item-title {
  flex: 1; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chat-item .chat-item-actions { display: none; gap: 2px; }
.chat-item:hover .chat-item-actions,
.chat-item.active .chat-item-actions { display: flex; }
.chat-item-actions button {
  background: transparent; border: 0;
  color: var(--sidebar-muted);
  font-family: var(--font-mono);
  font-size: 12px;
  width: 22px; height: 22px;
  border-radius: 4px;
}
.chat-item-actions button:hover { background: rgba(255,255,255,.07); color: var(--sidebar-ink); }
.chat-item-actions .del:hover { color: #e88a82; }

.sidebar-footer {
  border-top: 1px solid var(--sidebar-rule);
  padding: 8px;
  display: grid;
  gap: 1px;
  flex-shrink: 0;
}
.user-line {
  margin-top: 4px;
  padding: 6px 12px 2px;
  font-family: var(--font-mono);
  font-size: 10.5px;
  color: var(--sidebar-muted);
  letter-spacing: .04em;
}

/* ---------- main panel ---------- */
.main {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}
.main-header {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px;
  padding: 12px 24px;
  border-bottom: 1px solid var(--rule);
  background: var(--bg-paper);
  flex-shrink: 0;
}
.chat-title {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 18px;
  letter-spacing: -.01em;
  color: var(--ink);
  cursor: pointer;
}
.chat-title:hover { color: var(--accent); }
.header-right { display: flex; align-items: center; gap: 14px; }

.model-picker { display: flex; align-items: center; gap: 8px; }
.model-picker-label {
  font-family: var(--font-mono);
  font-size: 10.5px;
  letter-spacing: .12em;
  text-transform: uppercase;
  color: var(--ink-muted);
  font-weight: 500;
}
.model-picker select {
  font-family: var(--font-mono);
  font-size: 12.5px;
  padding: 6px 10px;
  min-width: 200px;
}

.web-toggle {
  display: inline-flex; align-items: center; gap: 8px;
  cursor: pointer;
  font-family: var(--font-mono);
  font-size: 11.5px;
  letter-spacing: .12em;
  text-transform: uppercase;
  color: var(--ink-muted);
  font-weight: 500;
}
.web-toggle input { display: none; }
.web-toggle-track {
  position: relative;
  width: 32px; height: 18px;
  background: var(--bg-paper-soft);
  border: 1px solid var(--rule);
  border-radius: 999px;
  transition: background .15s, border-color .15s;
}
.web-toggle-track::after {
  content: ''; position: absolute;
  top: 1px; left: 1px;
  width: 14px; height: 14px;
  background: var(--bg-card);
  border-radius: 50%;
  transition: transform .18s ease;
  box-shadow: 0 1px 3px rgba(0,0,0,.15);
}
.web-toggle input:checked + .web-toggle-track {
  background: var(--accent);
  border-color: var(--accent);
}
.web-toggle input:checked + .web-toggle-track::after {
  transform: translateX(14px);
}
.web-toggle input:checked ~ .web-toggle-text { color: var(--accent); }

/* ---------- messages area ---------- */
.messages {
  overflow-y: auto;
  overflow-x: hidden;
  min-height: 0;
  scroll-behavior: smooth;
  padding: 24px 24px 8px;
}
.messages-inner {
  max-width: var(--max-col);
  margin: 0 auto;
  padding-bottom: 16px;
}

.empty-state {
  max-width: 440px;
  margin: 12vh auto 0;
  text-align: center;
  color: var(--ink-muted);
}
.empty-state .empty-mark {
  font-family: var(--font-mono);
  font-size: 30px;
  color: var(--accent);
  margin-bottom: 14px;
}
.empty-state h2 {
  font-family: var(--font-display);
  font-weight: 500;
  font-size: 26px;
  letter-spacing: -.01em;
  color: var(--ink);
  margin: 0 0 6px;
}
.empty-state p { font-size: 14.5px; line-height: 1.5; }

/* ---------- messages (Claude-like) ---------- */
.msg {
  margin: 0 0 22px;
  display: flex;
  flex-direction: column;
}
.msg.user { align-items: flex-end; }
.msg.assistant { align-items: stretch; }

.msg-body {
  font-family: var(--font-body);
  font-size: 15.5px;
  line-height: 1.65;
  color: var(--ink);
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.msg.user .msg-body {
  background: var(--user-bubble);
  color: var(--user-bubble-ink);
  padding: 10px 16px;
  border-radius: 18px 18px 4px 18px;
  max-width: 80%;
  white-space: pre-wrap;
}
.msg.assistant .msg-body { padding: 2px 0; }

.msg-body p { margin: 0 0 .85em; }
.msg-body p:last-child { margin-bottom: 0; }

.msg-body h1, .msg-body h2, .msg-body h3, .msg-body h4 {
  font-family: var(--font-display);
  font-weight: 600;
  line-height: 1.25;
  letter-spacing: -.01em;
  color: var(--ink);
  margin: 1.4em 0 .5em;
}
.msg-body h1:first-child, .msg-body h2:first-child, .msg-body h3:first-child { margin-top: 0; }
.msg-body h1 { font-size: 1.6em; }
.msg-body h2 { font-size: 1.35em; }
.msg-body h3 { font-size: 1.15em; }
.msg-body h4 { font-size: 1em; font-weight: 600; }

.msg-body strong { font-weight: 600; color: var(--ink); }
.msg-body em { font-style: italic; }

.msg-body ul, .msg-body ol { padding-left: 24px; margin: .3em 0 .9em; }
.msg-body li { margin-bottom: .25em; }
.msg-body li > p { margin-bottom: .35em; }

.msg-body a {
  color: var(--accent);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
}
.msg-body a:hover { color: #a8542c; }

.msg-body blockquote {
  border-left: 3px solid var(--rule);
  padding: 2px 0 2px 14px;
  color: var(--ink-soft);
  margin: .6em 0;
}

.msg-body hr {
  border: 0;
  border-top: 1px solid var(--rule);
  margin: 1.5em 0;
}

.msg-body code {
  font-family: var(--font-mono);
  font-size: .88em;
  background: var(--code-bg);
  color: var(--code-ink);
  padding: 1.5px 6px;
  border-radius: 4px;
  border: 1px solid var(--rule-soft);
}
.msg.user .msg-body code {
  background: rgba(255,255,255,.4);
  border-color: transparent;
}

.msg-body pre {
  font-family: var(--font-mono);
  background: var(--code-bg);
  color: var(--code-ink);
  padding: 14px 16px;
  border-radius: 10px;
  overflow-x: auto;
  font-size: 13px;
  line-height: 1.6;
  margin: .8em 0;
  border: 1px solid var(--rule);
}
.msg-body pre code {
  background: transparent;
  padding: 0;
  border: 0;
  color: inherit;
  font-size: 1em;
}

.msg-body table {
  border-collapse: collapse;
  width: 100%;
  margin: .8em 0;
  font-size: .95em;
  display: block;
  overflow-x: auto;
}
.msg-body thead { background: var(--bg-paper-soft); }
.msg-body th, .msg-body td {
  border: 1px solid var(--rule);
  padding: 8px 12px;
  text-align: left;
  vertical-align: top;
}
.msg-body th { font-weight: 600; color: var(--ink); font-size: .92em; }

.msg-body .katex { font-size: 1.05em; }
.msg-body .katex-display {
  margin: 1em 0;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 4px 0;
}

.msg-sources {
  margin: 12px 0 0;
  padding: 10px 14px;
  border: 1px dashed var(--rule);
  border-radius: 10px;
  background: rgba(0,0,0,.015);
}
.msg-sources-label {
  font-family: var(--font-mono);
  font-size: 9.5px;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--ink-muted);
  margin-bottom: 6px;
  font-weight: 500;
}
.msg-sources ol { margin: 0; padding-left: 22px; }
.msg-sources li { font-size: 13px; margin-bottom: 3px; line-height: 1.5; }
.msg-sources a { color: var(--ink-soft); }
.msg-sources a:hover { color: var(--accent); }

.msg-actions {
  display: flex;
  gap: 6px;
  margin-top: 8px;
  opacity: 0;
  transition: opacity .15s ease;
}
.msg.assistant:hover .msg-actions,
.msg-actions.show { opacity: 1; }

.msg-action-btn {
  background: transparent;
  border: 1px solid transparent;
  color: var(--ink-muted);
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: .04em;
  padding: 5px 10px;
  border-radius: 6px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: background .12s, color .12s, border-color .12s;
}
.msg-action-btn:hover {
  background: var(--bg-paper-soft);
  color: var(--ink);
  border-color: var(--rule);
}
.msg-action-btn.copied {
  color: var(--ok);
  border-color: rgba(108,142,90,.3);
  background: rgba(108,142,90,.07);
}
.msg-action-btn svg { width: 13px; height: 13px; stroke-width: 1.8; }

.msg-thinking {
  display: inline-flex; align-items: center; gap: 8px;
  color: var(--ink-muted);
  font-family: var(--font-mono);
  font-size: 12.5px;
  padding: 2px 0;
}
.dot-pulse {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--accent);
  animation: pulse 1.1s ease-in-out infinite;
}
.dot-pulse:nth-child(2) { animation-delay: .15s; }
.dot-pulse:nth-child(3) { animation-delay: .3s; }
@keyframes pulse {
  0%, 80%, 100% { opacity: .25; transform: scale(.9); }
  40% { opacity: 1; transform: scale(1.2); }
}

.msg-error {
  font-family: var(--font-mono);
  font-size: 12.5px;
  color: var(--danger);
  background: rgba(176,74,63,.06);
  border: 1px solid rgba(176,74,63,.2);
  padding: 8px 12px;
  border-radius: 8px;
}

/* ---------- composer ---------- */
.composer {
  background: var(--bg-paper);
  padding: 8px 24px 18px;
  flex-shrink: 0;
}
.composer-wrap {
  max-width: var(--max-col);
  margin: 0 auto;
  background: var(--bg-card);
  border: 1px solid var(--rule);
  border-radius: var(--r-xl);
  padding: 6px 6px 6px 16px;
  transition: border-color .15s, box-shadow .15s;
}
.composer-wrap:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(194,104,58,.10);
}
.composer-form {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: end;
}
#input {
  font-family: var(--font-body);
  font-size: 15.5px;
  resize: none;
  min-height: 36px;
  max-height: 220px;
  line-height: 1.5;
  padding: 10px 0;
  background: transparent;
  border: 0;
  outline: 0;
  width: 100%;
}
#input:focus { box-shadow: none; }
.btn-send {
  width: 36px; height: 36px;
  border-radius: 50%;
  background: var(--ink);
  color: var(--bg-paper);
  border: 1px solid var(--ink);
  font-size: 18px; line-height: 1;
  display: grid; place-items: center;
  margin-bottom: 4px;
  transition: background .12s, transform .05s;
}
.btn-send:disabled { opacity: .35; cursor: not-allowed; }
.btn-send:not(:disabled):hover { background: var(--accent); border-color: var(--accent); }
.btn-send:not(:disabled):active { transform: scale(.95); }

.composer-hint {
  max-width: var(--max-col); margin: 6px auto 0;
  font-family: var(--font-mono);
  font-size: 10.5px;
  color: var(--ink-muted);
  letter-spacing: .04em;
  min-height: 14px;
  padding-left: 6px;
}

/* ---------- modal ---------- */
.modal {
  position: fixed; inset: 0;
  display: grid; place-items: center;
  z-index: 100;
}
.modal[hidden] { display: none; }
.modal-backdrop {
  position: absolute; inset: 0;
  background: rgba(20,18,15,.45);
  backdrop-filter: blur(3px);
}
.modal-card {
  position: relative;
  width: min(560px, 92vw);
  max-height: 92vh;
  overflow-y: auto;
  background: var(--bg-card);
  border: 1px solid var(--rule);
  border-radius: var(--r-lg);
  box-shadow: 0 40px 100px -30px rgba(0,0,0,.4);
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 22px 12px;
  border-bottom: 1px solid var(--rule);
  position: sticky;
  top: 0;
  background: var(--bg-card);
  z-index: 1;
}
.modal-head h3 {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 21px;
  letter-spacing: -.01em;
  margin: 0;
}
.modal-x {
  background: transparent; border: 0;
  font-size: 24px; line-height: 1;
  color: var(--ink-muted); cursor: pointer;
  width: 32px; height: 32px;
  border-radius: 6px;
}
.modal-x:hover { background: var(--bg-paper-soft); color: var(--ink); }

.modal-body {
  padding: 18px 22px 8px;
  display: grid; gap: 16px;
}
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.modal-actions {
  display: flex; gap: 10px; justify-content: flex-end;
  padding: 14px 22px 18px;
  border-top: 1px solid var(--rule);
  position: sticky;
  bottom: 0;
  background: var(--bg-card);
}
.modal-actions .btn-ghost {
  color: var(--ink-soft);
  border: 1px solid var(--rule);
  background: transparent;
  width: auto;
}
.modal-actions .btn-ghost:hover { background: var(--bg-paper-soft); }

*::-webkit-scrollbar { width: 10px; height: 10px; }
*::-webkit-scrollbar-thumb { background: rgba(0,0,0,.12); border-radius: 6px; }
*::-webkit-scrollbar-thumb:hover { background: rgba(0,0,0,.22); }
.sidebar *::-webkit-scrollbar-thumb { background: rgba(255,255,255,.07); }
.sidebar *::-webkit-scrollbar-thumb:hover { background: rgba(255,255,255,.14); }

@media (max-width: 720px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .messages { padding: 16px 14px 8px; }
  .composer { padding: 8px 14px 14px; }
  .main-header { padding: 10px 14px; }
  .model-picker select { min-width: 140px; }
}
