:root {
  --bg: #f7f7f8;
  --surface: #ffffff;
  --surface-2: #ececf0;
  --border: #d7d7de;
  --text: #1a1a1e;
  --muted: #5f5f6b;
  --primary: #4f46e5;
  --primary-strong: #4338ca;
  --primary-soft: #eef2ff;
  --on-primary: #ffffff;
  --accent: #e8590c;
  --ok: #198754;
  --danger: #c92a2a;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 8px 24px rgb(0 0 0 / 0.08);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121216;
    --surface: #1b1b21;
    --surface-2: #25252c;
    --border: #34343d;
    --text: #f0f0f2;
    --muted: #a2a2ad;
    --primary: #7c73ff;
    --primary-strong: #6b61f0;
    --primary-soft: #26224a;
    --on-primary: #ffffff;
    --accent: #ff7a45;
    --ok: #4cd18b;
    --danger: #ff6b6b;
    --shadow: 0 8px 24px rgb(0 0 0 / 0.32);
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, p { margin: 0; }

a { color: var(--primary); }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
  font-size: 18px;
  font-weight: 700;
  text-decoration: none;
  letter-spacing: -0.01em;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 9px;
  background: var(--primary);
  color: var(--on-primary);
  font-size: 18px;
  font-weight: 800;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--ok) 14%, transparent);
  color: var(--ok);
  font-size: 14px;
  font-weight: 600;
}

.main {
  display: flex;
  flex: 1;
  flex-direction: column;
  width: 100%;
  max-width: 760px;
  min-height: 0;
  margin: 0 auto;
  padding: 0 16px;
}

.chat {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 16px 0;
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.msg {
  display: flex;
}

.msg-user { justify-content: flex-end; }

.msg-body {
  max-width: min(88%, 620px);
  padding: 12px 16px;
  border-radius: var(--radius);
  overflow-wrap: anywhere;
}

.msg-user .msg-body {
  background: var(--primary);
  color: var(--on-primary);
  border-bottom-right-radius: 4px;
}

.msg-assistant .msg-body {
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

.msg-assistant .msg-body > :first-child { margin-top: 0; }
.msg-assistant .msg-body > :last-child { margin-bottom: 0; }
.msg-assistant p { margin: 0 0 8px; }
.msg-assistant ul, .msg-assistant ol { margin: 0 0 8px; padding-left: 24px; }
.msg-assistant pre {
  margin: 8px 0;
  padding: 12px;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  overflow-x: auto;
}
.msg-assistant code { font-family: var(--mono); font-size: 14px; }
.msg-assistant :not(pre) > code {
  padding: 2px 4px;
  border-radius: 4px;
  background: var(--surface-2);
}
.msg-assistant blockquote {
  margin: 8px 0;
  padding-left: 12px;
  border-left: 3px solid var(--border);
  color: var(--muted);
}
.msg-assistant a { color: var(--primary); }

.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  min-height: 100%;
  padding: 32px 16px;
  text-align: center;
}

.empty h1 {
  font-size: 32px;
  font-weight: 800;
  letter-spacing: -0.03em;
}

.empty p { color: var(--muted); font-size: 16px; }
.empty .btn { margin-top: 8px; min-width: 200px; }

.summary-pill {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 12px;
  padding: 8px 12px;
  border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--muted);
  font-size: 14px;
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px 0 8px;
  border-top: 1px solid var(--border);
}

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

.composer textarea:focus-visible,
.field select:focus-visible,
.btn:focus-visible,
.link-btn:focus-visible,
input[type="range"]:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

.underbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 24px;
  padding-bottom: 8px;
}

.status, .note {
  color: var(--muted);
  font-size: 14px;
}

.recommend {
  width: min(100%, 440px);
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  text-align: left;
}

.recommend .note { margin: 0 0 10px; }
.recommend .btn { min-width: 0; margin-top: 0; }

.error-detail {
  margin-top: 10px;
  color: var(--muted);
  font-size: 13px;
}

.error-detail summary { cursor: pointer; }

.error-detail pre {
  margin: 6px 0 0;
  font-family: var(--mono);
  font-size: 12px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 16px;
  font-weight: 600;
  line-height: 1;
  text-decoration: none;
  cursor: pointer;
}

.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn:disabled:hover { background: var(--surface); }

.btn-primary {
  border-color: transparent;
  background: var(--primary);
  color: var(--on-primary);
}
.btn-primary:hover { background: var(--primary-strong); }

.btn-ghost {
  border-color: transparent;
  background: transparent;
}
.btn-ghost:hover { background: var(--surface-2); }

.btn-donate {
  border-color: transparent;
  background: var(--accent);
  color: #ffffff;
  white-space: nowrap;
}
.btn-donate:hover { background: var(--accent); filter: brightness(0.94); }

.donate-short { display: none; }

.link-btn {
  padding: 0;
  border: 0;
  background: none;
  color: var(--primary);
  font: inherit;
  font-weight: 600;
  text-decoration: underline;
  cursor: pointer;
}

.sheet {
  width: min(560px, calc(100vw - 24px));
  max-height: min(760px, calc(100dvh - 24px));
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

.sheet::backdrop {
  background: rgb(0 0 0 / 0.4);
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 16px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--surface);
}

.sheet-head h2 { font-size: 20px; font-weight: 700; }

.sheet-body {
  padding: 16px;
  overflow-y: auto;
  max-height: calc(min(760px, calc(100dvh - 24px)) - 65px);
}

.group { padding: 16px 0; border-bottom: 1px solid var(--border); }
.group:first-child { padding-top: 0; }
.group:last-child { border-bottom: 0; padding-bottom: 0; }

.group h3 {
  margin-bottom: 8px;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.group .note { margin-top: 8px; }

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 8px;
  font-size: 14px;
  font-weight: 600;
  color: var(--muted);
}

.field select {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 16px;
}

.row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
}

.folder-name {
  flex: 1 0 180px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  font-size: 14px;
  overflow-wrap: anywhere;
}

.steer-list { display: flex; flex-direction: column; gap: 12px; }

.steer-row {
  display: grid;
  grid-template-columns: 96px 1fr 48px;
  align-items: center;
  gap: 8px;
  font-size: 14px;
}

.steer-row input[type="range"] { width: 100%; accent-color: var(--primary); }
.steer-row output { text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }

.apk-list { display: flex; flex-direction: column; gap: 8px; }

.apk-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.apk-row strong { display: block; font-size: 16px; }
.apk-row .note { margin: 0; }

.chat-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 12px;
}

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

.chat-open {
  flex: 1;
  padding: 4px;
  border: 0;
  background: none;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.chat-open small { display: block; color: var(--muted); font-size: 14px; }

.summary-text { white-space: pre-wrap; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  padding: 12px 16px;
  border-radius: var(--radius);
  background: var(--text);
  color: var(--bg);
  box-shadow: var(--shadow);
  font-size: 14px;
  font-weight: 600;
  z-index: 10;
}

@media (max-width: 640px) {
  .topbar-actions { gap: 4px; }
  .topbar-actions .btn { padding: 8px 10px; font-size: 14px; }
  .donate-full { display: none; }
  .donate-short { display: inline; }
}

@media (max-width: 520px) {
  .topbar { padding: 8px 12px; }
  .brand span:last-child { display: none; }
  .main { padding: 0 12px; }
  .steer-row { grid-template-columns: 80px 1fr 44px; }
  .msg-body { max-width: 92%; }
}

@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; transition: none !important; }
}
