:root {
  color-scheme: light dark;
  --bg: #ffffff;
  --panel: #f3f4f6;
  --text: #14171c;
  --muted: #6b7280;
  --line: rgba(20, 23, 28, 0.1);
  --accent: #2f6df6;
  --danger: #d9373c;
  --radius: 14px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111318;
    --panel: #1b1e25;
    --text: #e8eaee;
    --muted: #9aa2af;
    --line: rgba(232, 234, 238, 0.12);
  }
}

:root[data-theme="light"] {
  --bg: #ffffff;
  --panel: #f3f4f6;
  --text: #14171c;
  --muted: #6b7280;
  --line: rgba(20, 23, 28, 0.1);
}

:root[data-theme="dark"] {
  --bg: #111318;
  --panel: #1b1e25;
  --text: #e8eaee;
  --muted: #9aa2af;
  --line: rgba(232, 234, 238, 0.12);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

/* Must outrank class rules that set `display` (e.g. .gate), or `hidden` is ignored. */
[hidden] {
  display: none !important;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, system-ui, "Segoe UI", sans-serif;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

button,
input,
select {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
  border: 0;
  border-radius: 10px;
  padding: 10px 14px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}

button.secondary {
  background: var(--panel);
  color: var(--text);
}

button.danger {
  background: transparent;
  color: var(--danger);
  font-weight: 500;
}

button:disabled {
  opacity: 0.45;
}

input,
select {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  -webkit-appearance: none;
}

input[type="search"] {
  background: var(--panel);
}

/* --- shell --- */

.screen {
  max-width: 680px;
  margin: 0 auto;
  padding: 8px 16px 40px;
}

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

.topbar h1 {
  flex: 1;
  min-width: 0;
  margin: 0;
  font-size: 17px;
  font-weight: 600;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar .icon {
  background: var(--panel);
  color: var(--text);
  padding: 8px 12px;
  font-weight: 500;
}

.toolbar {
  display: flex;
  gap: 8px;
  margin: 14px 0;
}

.toolbar button {
  flex-shrink: 0;
}

.toolbar input,
.toolbar select {
  flex: 1 1 auto;
  min-width: 0;
  width: auto;
}

.toolbar.wrap {
  flex-wrap: wrap;
}

.toolbar.wrap button {
  flex: 0 0 auto;
}

.section {
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  margin: 28px 0 8px;
}

.nudge {
  background: var(--panel);
  border-radius: var(--radius);
  padding: 12px 14px;
  font-size: 14px;
  color: var(--muted);
}

.nudge a {
  color: var(--accent);
}

.nudge.warn {
  border-left: 3px solid var(--danger);
  color: var(--text);
}

.hint {
  color: var(--muted);
  font-size: 14px;
}

.error {
  color: var(--danger);
  font-size: 14px;
  white-space: pre-wrap;
}

/* --- chat list --- */

.chat-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.chat-row {
  display: flex;
  align-items: center;
  gap: 8px;
  border-bottom: 1px solid var(--line);
}

.chat-row .open {
  flex: 1;
  min-width: 0;
  text-align: left;
  background: none;
  color: inherit;
  font-weight: 400;
  padding: 14px 4px;
}

.chat-row .title {
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-row .meta {
  display: block;
  color: var(--muted);
  font-size: 13px;
  margin-top: 2px;
}

.chat-row .actions {
  display: flex;
  gap: 2px;
}

.chat-row .actions button {
  background: none;
  color: var(--muted);
  font-size: 14px;
  padding: 8px;
}

.empty {
  color: var(--muted);
  text-align: center;
  padding: 48px 0;
}

/* --- chat --- */

.transcript {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 14px 0 8px;
}

.msg {
  max-width: 88%;
  padding: 10px 12px;
  border-radius: 14px;
}

.msg .content {
  white-space: pre-wrap;
  word-break: break-word;
}

.msg.user {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
}

.msg.assistant {
  align-self: flex-start;
  background: var(--panel);
}

.msg-error {
  margin-top: 6px;
  font-size: 13px;
  white-space: pre-wrap;
  color: var(--danger);
}

.msg.user .msg-error {
  color: #ffdada;
}

.msg-actions {
  display: flex;
  gap: 14px;
  margin-top: 6px;
}

.msg-actions button.link {
  background: none;
  padding: 0;
  font-size: 13px;
  font-weight: 500;
  color: inherit;
  opacity: 0.75;
}

.composer {
  position: sticky;
  bottom: 0;
  display: flex;
  gap: 8px;
  align-items: flex-end;
  padding: 10px 0 calc(10px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
}

.composer textarea {
  flex: 1;
  min-height: 44px;
  max-height: 140px;
  resize: none;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: inherit;
  font: inherit;
}

.composer button {
  flex-shrink: 0;
  height: 44px;
}

.chat-meta {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 0;
}

.chat-meta .hint {
  flex: 1;
  min-width: 0;
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-meta select {
  flex: 0 0 auto;
  width: auto;
  max-width: 45%;
  padding: 6px 10px;
  font-size: 13px;
}

/* markdown-lite (render.js) */

.msg .content > *:first-child {
  margin-top: 0;
}

.msg .content > *:last-child {
  margin-bottom: 0;
}

.msg .content p {
  margin: 0 0 8px;
}

.msg .content h1,
.msg .content h2,
.msg .content h3 {
  margin: 10px 0 6px;
  line-height: 1.25;
}

.msg .content h1 {
  font-size: 20px;
}

.msg .content h2 {
  font-size: 17px;
}

.msg .content h3 {
  font-size: 15px;
}

.msg .content ul,
.msg .content ol {
  margin: 0 0 8px;
  padding-left: 22px;
}

.msg .content li {
  margin: 2px 0;
}

.msg .content code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.9em;
  background: rgba(127, 127, 127, 0.18);
  padding: 1px 5px;
  border-radius: 5px;
}

.msg.user .content code {
  background: rgba(255, 255, 255, 0.22);
}

.code-block {
  margin: 8px 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
}

.code-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 6px 4px 10px;
  background: rgba(127, 127, 127, 0.14);
  font-size: 12px;
  color: var(--muted);
}

.code-head .copy {
  background: none;
  color: inherit;
  font-size: 12px;
  font-weight: 500;
  padding: 4px 8px;
}

.code-block pre {
  margin: 0;
  padding: 10px;
  overflow-x: auto;
}

.code-block code {
  background: none;
  padding: 0;
  font-size: 13px;
  white-space: pre;
}

/* --- new-chat presets --- */

.presets {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0 0 12px;
}

.chip {
  background: var(--panel);
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  padding: 7px 12px;
  border-radius: 999px;
}

/* --- settings --- */

.field {
  margin-bottom: 18px;
}

.field label {
  display: block;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 6px;
}

.field .help {
  font-size: 13px;
  color: var(--muted);
  margin-top: 6px;
}

/* --- install gate --- */

.gate {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
}

.gate-card {
  max-width: 420px;
  background: var(--bg);
  border-radius: var(--radius);
  padding: 24px;
}

.gate-card h1 {
  margin-top: 0;
  font-size: 20px;
}

.gate-card ol {
  padding-left: 20px;
}

.gate-card button {
  width: 100%;
  margin-top: 12px;
}
