:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #1c1f24;
  --muted: #5d6573;
  --border: #d9dde3;
  --accent: #4b4bc8;
  --accent-text: #ffffff;
  --user: #e9ecff;
  --danger: #b42318;
  --notice-bg: #fff4e5;
  --notice-text: #7a4100;
  --focus: #1f6feb;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111317;
    --surface: #1b1e24;
    --text: #e8eaee;
    --muted: #a3abb8;
    --border: #333844;
    --accent: #8f8ff5;
    --accent-text: #0f1020;
    --user: #262a45;
    --danger: #ff8a80;
    --notice-bg: #3a2a12;
    --notice-text: #ffd699;
    --focus: #79b8ff;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #111317;
  --surface: #1b1e24;
  --text: #e8eaee;
  --muted: #a3abb8;
  --border: #333844;
  --accent: #8f8ff5;
  --accent-text: #0f1020;
  --user: #262a45;
  --danger: #ff8a80;
  --notice-bg: #3a2a12;
  --notice-text: #ffd699;
  --focus: #79b8ff;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
  overflow-x: hidden;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.55 -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Noto Sans KR", "Segoe UI", sans-serif;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

[hidden] { display: none !important; }

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

:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

.topbar {
  position: sticky; top: 0; z-index: 2;
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px;
  padding: calc(8px + env(safe-area-inset-top)) 14px 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; }
.brand h1 { font-size: 1.05rem; margin: 0; }
.meta { display: flex; flex-wrap: wrap; gap: 6px; order: 3; width: 100%; min-width: 0; }
.badge {
  font-size: 0.78rem; color: var(--muted);
  border: 1px solid var(--border); border-radius: 999px; padding: 2px 10px;
  max-width: 100%; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.badge b { color: var(--text); font-weight: 600; }
.actions { margin-left: auto; display: flex; gap: 6px; }

main {
  min-width: 0;
  width: 100%; max-width: 860px; margin: 0 auto; padding: 12px;
  flex: 1; display: flex; flex-direction: column;
}

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 14px 16px; margin-bottom: 12px;
}
.card h2 { margin: 0 0 8px; font-size: 1.15rem; }

.notice {
  background: var(--notice-bg); color: var(--notice-text);
  border-radius: 10px; padding: 10px 14px; margin: 0 0 12px;
}

button {
  font: inherit; border-radius: 10px; padding: 9px 16px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  min-height: 44px;
}
button:disabled { opacity: 0.55; cursor: not-allowed; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
button.danger { color: var(--danger); border-color: var(--danger); }
button.ghost { background: transparent; }
button.wide { width: 100%; margin-top: 8px; }
.icon-btn { min-width: 44px; padding: 6px 10px; }

label { display: block; font-size: 0.85rem; color: var(--muted); margin: 8px 0 4px; }
input[type="text"], input[type="number"], textarea {
  width: 100%; font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 10px; padding: 9px 11px;
}
input[type="range"] { width: 100%; min-height: 44px; accent-color: var(--accent); }
textarea { resize: vertical; }
.row { display: flex; flex-wrap: wrap; gap: 12px; }
.field { flex: 1 1 200px; }

.settings summary { cursor: pointer; font-weight: 600; min-height: 28px; }
.summary-hint { font-weight: 400; color: var(--muted); font-size: 0.85rem; margin-left: 6px; }

.conversation { flex: 1; display: flex; flex-direction: column; gap: 10px; padding-bottom: 12px; }
.empty { color: var(--muted); text-align: center; margin: 32px 8px; }

.message {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 10px 14px; max-width: 100%;
}
.message.user { background: var(--user); align-self: flex-end; max-width: 92%; }
.message .role { font-size: 0.75rem; color: var(--muted); margin-bottom: 2px; }
.message .content { white-space: pre-wrap; overflow-wrap: anywhere; }
.message .content.error { color: var(--danger); }
.message .stats { font-size: 0.75rem; color: var(--muted); margin-top: 6px; }
.message .stats:empty { display: none; }
.reasoning { margin: 2px 0 6px; color: var(--muted); font-size: 0.85rem; }
.reasoning pre { white-space: pre-wrap; overflow-wrap: anywhere; margin: 4px 0 0; font: inherit; }
.message.streaming .content::after { content: "▍"; animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .message.streaming .content::after { animation: none; } }

.composer {
  position: sticky; bottom: 0;
  background: var(--bg); padding: 8px 0 calc(8px + env(safe-area-inset-bottom));
}
.composer textarea { min-height: 64px; max-height: 40vh; }
.composer-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 8px; }
.composer-actions .primary { min-width: 96px; }

.rag-controls {
  display: flex; align-items: center; gap: .6rem; flex-wrap: wrap;
  margin-bottom: 6px;
}
.rag-controls label { margin: 0; }
.rag-controls select {
  max-width: 16rem; min-height: 36px; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 5px 8px;
}
.toggle { display: inline-flex; align-items: center; gap: .35rem; }
.sources { margin-top: .55rem; color: var(--muted); font-size: .86rem; }
.sources summary { cursor: pointer; }
.sources ul { margin: .4rem 0 0; padding-left: 1.2rem; }

code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.9em; }
a { color: var(--accent); }

@media (min-width: 720px) {
  .meta { order: 0; width: auto; }
}
