:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #1c1f24;
  --muted: #6b7280;
  --border: #e3e6ea;
  --accent: #3b5bdb;
  --accent-text: #ffffff;
  --user-bubble: #3b5bdb;
  --user-text: #ffffff;
  --claude-bubble: #ffffff;
  --code-bg: #f0f2f5;
  --danger: #c92a2a;
  --danger-bg: #fff5f5;
  --warn: #e8a317;
  --warn-bg: #fff9db;
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --panel: #1c1f24;
    --text: #e8eaed;
    --muted: #9aa0a6;
    --border: #2c3036;
    --accent: #5c7cfa;
    --accent-text: #ffffff;
    --user-bubble: #4263eb;
    --user-text: #ffffff;
    --claude-bubble: #23272e;
    --code-bg: #2a2f37;
    --danger: #ff8787;
    --danger-bg: #2b1d1d;
    --warn: #f0b429;
    --warn-bg: #2b2616;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

button, input, textarea { font: inherit; color: inherit; }
button {
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: 10px;
  padding: 8px 14px;
  cursor: pointer;
}
button:disabled { opacity: 0.6; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
button.small { padding: 4px 10px; font-size: 13px; }
button.link { background: none; border: none; color: var(--muted); padding: 6px; }
button.link:hover { color: var(--text); text-decoration: underline; }
button.danger:hover { color: var(--danger); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.2em; margin: 6px 0 0; font-size: 14px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* ---- sign in ---- */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px;
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 28px 24px; display: flex; flex-direction: column; gap: 8px;
}
.login-card h1 { margin: 4px 0 0; font-size: 24px; }
.login-card p { margin: 0 0 12px; }
.login-icon { width: 44px; height: 44px; }
.login-card input {
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--bg);
}
.login-card button { margin-top: 8px; padding: 10px; }

/* ---- layout ---- */
.app { display: flex; height: 100%; }
.sidebar {
  width: 280px; flex-shrink: 0;
  background: var(--panel); border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 14px 10px; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; }
.brand img { width: 26px; height: 26px; }
.conv-list { list-style: none; margin: 0; padding: 4px 8px; overflow-y: auto; flex: 1; }
.conv-item {
  width: 100%; text-align: left; border: none; background: none; border-radius: 10px;
  padding: 9px 10px; display: flex; flex-direction: column; gap: 1px;
}
.conv-item:hover { background: var(--bg); }
.conv-item.active { background: var(--code-bg); }
.conv-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-time { font-size: 12px; }
.empty-list { padding: 10px; font-size: 14px; }
.sidebar > .link { margin: 8px; align-self: flex-start; }
.usage:not(:empty) { margin: 8px 12px 0; padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; font-size: 12px; color: var(--muted); }
.usage table { width: 100%; border-collapse: collapse; }
.usage th, .usage td { text-align: right; padding: 1px 0; font-weight: normal; }
.usage th:first-child { text-align: left; }
.usage thead th { font-weight: 600; color: var(--text); }
.usage-note { margin: 6px 0 0; }
.usage.blocked { border-color: var(--warn); color: var(--text); background: var(--warn-bg); }
.bubble.paused { background: var(--warn-bg) !important; border-color: var(--warn) !important; display: flex; flex-direction: column; gap: 6px; align-items: flex-start; }

.chat { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.chat-head {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px; border-bottom: 1px solid var(--border); background: var(--panel);
}
.chat-head h2 { flex: 1; margin: 0; font-size: 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-btn { border: none; background: none; font-size: 20px; padding: 4px 8px; cursor: pointer; line-height: 1; border-radius: 8px; }
.icon-btn:hover { background: var(--bg); }
#toggle-sidebar { display: none; }

/* ---- messages ---- */
.messages { flex: 1; overflow-y: auto; padding: 20px 16px; display: flex; flex-direction: column; gap: 14px; }
.messages > .empty { margin: auto; text-align: center; max-width: 420px; }
.msg { display: flex; flex-direction: column; max-width: min(760px, 92%); gap: 4px; }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.claude { align-self: flex-start; align-items: flex-start; }
.bubble { padding: 10px 14px; border-radius: var(--radius); overflow-wrap: anywhere; }
.msg.user .bubble { background: var(--user-bubble); color: var(--user-text); border-bottom-right-radius: 4px; }
.msg.claude .bubble { background: var(--claude-bubble); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.bubble.plain { white-space: pre-wrap; }
.meta { font-size: 12px; padding: 0 4px; }

.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol, .md pre, .md blockquote, .md .table-wrap { margin: 0 0 10px; }
.md h3, .md h4, .md h5, .md h6 { margin: 14px 0 6px; }
.md ul, .md ol { padding-left: 22px; }
.md code { background: var(--code-bg); padding: 1px 5px; border-radius: 5px; font: 13px/1.45 ui-monospace, Consolas, monospace; }
.md pre { background: var(--code-bg); padding: 10px 12px; border-radius: 10px; overflow-x: auto; }
.md pre code { background: none; padding: 0; }
.md blockquote { border-left: 3px solid var(--border); padding-left: 10px; color: var(--muted); }
.md a { color: var(--accent); }
.table-wrap { overflow-x: auto; }
.md table { border-collapse: collapse; font-size: 14px; }
.md th, .md td { border: 1px solid var(--border); padding: 5px 9px; text-align: left; }

.bubble.thinking { display: flex; align-items: center; gap: 10px; }
.dots { display: inline-flex; gap: 4px; }
.dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite ease-in-out; }
.dots i:nth-child(2) { animation-delay: 0.2s; }
.dots i:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.25; } 40% { opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .dots i { animation: none; opacity: 0.6; } }

.bubble.failed { background: var(--danger-bg) !important; border-color: var(--danger) !important; display: flex; flex-direction: column; gap: 6px; align-items: flex-start; }
.error-detail { margin: 0; white-space: pre-wrap; font: 12px/1.4 ui-monospace, Consolas, monospace; color: var(--muted); max-height: 160px; overflow: auto; }

.files { display: flex; flex-wrap: wrap; gap: 6px; }
.file-chip {
  display: inline-flex; align-items: center; gap: 6px; max-width: 100%;
  padding: 5px 10px; border: 1px solid var(--border); border-radius: 999px;
  background: var(--panel); color: var(--text); text-decoration: none; font-size: 13px;
  overflow-wrap: anywhere;
}
a.file-chip:hover { border-color: var(--accent); color: var(--accent); }
.chip-remove { border: none; background: none; padding: 0 2px; font-size: 16px; line-height: 1; color: var(--muted); }

/* ---- composer ---- */
.composer { border-top: 1px solid var(--border); background: var(--panel); padding: 10px 16px 12px; }
.attachments { display: flex; flex-wrap: wrap; gap: 6px; }
.attachments:not(:empty) { margin-bottom: 8px; }
.composer-row { display: flex; align-items: flex-end; gap: 8px; }
.composer textarea {
  flex: 1; resize: none; min-height: 42px; max-height: 220px; overflow-y: hidden;
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 12px; background: var(--bg);
}
.composer .attach { display: inline-flex; align-items: center; height: 42px; }
.composer #send { height: 42px; }
.composer .error:empty { display: none; }

/* ---- phones ---- */
@media (max-width: 720px) {
  #toggle-sidebar { display: inline-block; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 10; width: min(300px, 85vw);
    transform: translateX(-100%); transition: transform 0.2s ease; box-shadow: 0 0 24px rgba(0, 0, 0, 0.25);
  }
  body.sidebar-open .sidebar { transform: none; }
  .messages { padding: 14px 12px; }
  .composer { padding: 8px 12px 10px; }
  .msg { max-width: 96%; }
}
