:root {
  --bg: #f7f6f3;
  --panel: #ffffff;
  --side: #efede8;
  --text: #1f1e1c;
  --muted: #6d6a63;
  --line: #e2dfd8;
  --accent: #c2602f;
  --accent-ink: #ffffff;
  --accent-soft: #f6e6dc;
  --user-bubble: #ebe8e1;
  --code-bg: #f3f1ec;
  --danger: #b3261e;
  --ok: #2e7d4f;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.06);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1d1c1a; --panel: #262523; --side: #171614; --text: #ecebe7; --muted: #a29f97;
    --line: #3a3835; --accent: #e0814f; --accent-ink: #1d1c1a; --accent-soft: #3b2a20;
    --user-bubble: #33312e; --code-bg: #1f1e1c; --danger: #f08b84; --ok: #7fcf9d;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.3);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1d1c1a; --panel: #262523; --side: #171614; --text: #ecebe7; --muted: #a29f97;
  --line: #3a3835; --accent: #e0814f; --accent-ink: #1d1c1a; --accent-soft: #3b2a20;
  --user-bubble: #33312e; --code-bg: #1f1e1c; --danger: #f08b84; --ok: #7fcf9d;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.3);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font-family: "Pretendard", "Apple SD Gothic Neo", "Malgun Gothic", system-ui, -apple-system, sans-serif;
  font-size: 15px; line-height: 1.6; -webkit-font-smoothing: antialiased;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }
.hidden { display: none !important; }

.btn {
  border: 1px solid var(--line); background: var(--panel); border-radius: 10px;
  padding: 8px 14px; transition: background .15s, border-color .15s;
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.05); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 4px 10px; font-size: 13px; border-radius: 8px; }
.icon-btn {
  border: none; background: transparent; border-radius: 8px; padding: 6px 8px; color: var(--muted);
  display: inline-flex; align-items: center; gap: 6px;
}
.icon-btn:hover { background: var(--line); color: var(--text); }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field label { font-size: 13px; color: var(--muted); }
.field input, .field textarea, .field select {
  border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 10px 12px; outline: none;
}
.field input:focus, .field textarea:focus { border-color: var(--accent); }

/* ── 로그인 ── */
.login-wrap { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.login-card {
  width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 18px; padding: 32px 28px; box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 20px; margin-bottom: 4px; }
.brand-mark {
  width: 30px; height: 30px; border-radius: 9px; background: var(--accent); color: var(--accent-ink);
  display: grid; place-items: center; font-size: 16px;
}
.login-card p.sub { color: var(--muted); margin: 0 0 22px; font-size: 14px; }
.login-card .btn.primary { width: 100%; padding: 11px; }
.form-error { color: var(--danger); font-size: 13px; min-height: 20px; margin-top: 8px; }
.privacy-note { font-size: 12px; color: var(--muted); margin-top: 18px; line-height: 1.5; }

/* ── 앱 레이아웃 ── */
.app { display: flex; height: 100%; }
.sidebar {
  width: 272px; flex-shrink: 0; background: var(--side); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; min-height: 0;
}
.side-top { padding: 14px 12px 8px; display: flex; flex-direction: column; gap: 10px; }
.side-top .brand { font-size: 16px; padding: 2px 4px; }
.side-top .brand-mark { width: 26px; height: 26px; font-size: 14px; }
.new-chat { width: 100%; text-align: left; display: flex; align-items: center; gap: 8px; }
.search { border: 1px solid var(--line); background: var(--panel); border-radius: 10px; padding: 7px 10px; outline: none; font-size: 14px; }
.conv-list { flex: 1; overflow-y: auto; padding: 4px 8px 12px; }
.conv-group { font-size: 12px; color: var(--muted); padding: 12px 8px 4px; }
.conv-item {
  display: flex; align-items: center; gap: 4px; border-radius: 9px; padding: 7px 8px; cursor: pointer;
  font-size: 14px; color: var(--text);
}
.conv-item:hover { background: var(--line); }
.conv-item.active { background: var(--panel); box-shadow: var(--shadow); }
.conv-item .title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-item .acts { display: none; }
.conv-item:hover .acts, .conv-item.active .acts { display: flex; }
.conv-item .acts button { border: none; background: none; color: var(--muted); padding: 2px 4px; border-radius: 6px; font-size: 13px; }
.conv-item .acts button:hover { color: var(--text); background: var(--bg); }
.side-bottom { border-top: 1px solid var(--line); padding: 12px; font-size: 13px; }
.usage-line { display: flex; justify-content: space-between; color: var(--muted); margin-bottom: 6px; }
.bar { height: 6px; background: var(--line); border-radius: 99px; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--accent); border-radius: 99px; }
.bar.warn > i { background: var(--danger); }
.user-row { display: flex; align-items: center; justify-content: space-between; margin-top: 10px; gap: 6px; }
.user-row .who { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar {
  height: 52px; display: flex; align-items: center; gap: 8px; padding: 0 14px; border-bottom: 1px solid var(--line);
  background: var(--bg);
}
.topbar .conv-title { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.menu-btn { display: none; }

.chat { flex: 1; overflow-y: auto; }
.chat-inner { max-width: 820px; margin: 0 auto; padding: 24px 20px 40px; }
.empty-state { text-align: center; margin-top: 12vh; color: var(--muted); }
.empty-state h1 { color: var(--text); font-size: 28px; font-weight: 650; margin: 0 0 8px; letter-spacing: -0.02em; }
.suggests { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 10px; max-width: 620px; margin: 28px auto 0; }
.suggests button {
  text-align: left; border: 1px solid var(--line); background: var(--panel); border-radius: 12px; padding: 12px 14px;
  font-size: 14px; color: var(--text);
}
.suggests button:hover { border-color: var(--accent); }
.suggests button small { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; }

.msg { margin: 0 0 26px; }
.msg.user { display: flex; flex-direction: column; align-items: flex-end; }
.msg.user .bubble {
  background: var(--user-bubble); border-radius: 18px 18px 4px 18px; padding: 10px 16px; max-width: 85%;
  white-space: pre-wrap; word-break: break-word;
}
.files { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; margin-bottom: 6px; }
.file-chip {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); background: var(--panel);
  border-radius: 10px; padding: 5px 10px; font-size: 13px; max-width: 260px;
}
.file-chip span.n { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-chip .x { border: none; background: none; padding: 0 2px; color: var(--muted); }
.file-chip img { width: 26px; height: 26px; object-fit: cover; border-radius: 5px; }
.msg-actions { display: flex; gap: 2px; margin-top: 4px; opacity: .0; transition: opacity .15s; }
.msg:hover .msg-actions, .msg.last .msg-actions { opacity: 1; }
.msg-actions .icon-btn { font-size: 12.5px; padding: 4px 7px; }

.msg.assistant .content { word-break: break-word; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 .8em; }
.md h1, .md h2, .md h3, .md h4 { line-height: 1.35; margin: 1.3em 0 .5em; }
.md h1 { font-size: 1.35em; } .md h2 { font-size: 1.2em; } .md h3 { font-size: 1.07em; }
.md ul, .md ol { padding-left: 1.4em; margin: 0 0 .8em; }
.md li { margin: .2em 0; }
.md blockquote { margin: 0 0 .8em; padding: 2px 14px; border-left: 3px solid var(--line); color: var(--muted); }
.md code { background: var(--code-bg); border: 1px solid var(--line); border-radius: 5px; padding: .08em .35em; font-size: .9em; font-family: "D2Coding", Consolas, "SFMono-Regular", monospace; }
.md pre { position: relative; background: var(--code-bg); border: 1px solid var(--line); border-radius: 10px; padding: 14px; overflow-x: auto; margin: 0 0 1em; }
.md pre code { background: none; border: none; padding: 0; font-size: 13px; line-height: 1.55; }
.md pre .copy-code { position: absolute; top: 6px; right: 6px; font-size: 12px; opacity: .0; }
.md pre:hover .copy-code { opacity: 1; }
.md .table-wrap { overflow-x: auto; margin: 0 0 1em; }
.md table { border-collapse: collapse; font-size: 14px; min-width: 50%; }
.md th, .md td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; vertical-align: top; }
.md th { background: var(--code-bg); }
.md hr { border: none; border-top: 1px solid var(--line); margin: 1.2em 0; }
.hljs { background: transparent !important; }

details.thinking { margin: 0 0 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel); }
details.thinking summary { cursor: pointer; padding: 7px 12px; font-size: 13px; color: var(--muted); list-style: none; }
details.thinking summary::-webkit-details-marker { display: none; }
details.thinking summary::before { content: "▸ "; }
details.thinking[open] summary::before { content: "▾ "; }
details.thinking .md { padding: 0 14px 10px; font-size: 13.5px; color: var(--muted); }
.tool-chip { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); background: var(--panel); border: 1px solid var(--line); border-radius: 99px; padding: 3px 11px; margin: 0 6px 10px 0; }
.sources { margin-top: 10px; display: flex; flex-wrap: wrap; gap: 6px; }
.sources a { font-size: 12.5px; text-decoration: none; border: 1px solid var(--line); border-radius: 8px; padding: 3px 9px; color: var(--muted); max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sources a:hover { color: var(--text); border-color: var(--muted); }
.notice-line { font-size: 12.5px; color: var(--muted); margin: 0 0 8px; }
.typing { display: inline-flex; gap: 4px; padding: 6px 0; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite ease-in-out; }
.typing i:nth-child(2) { animation-delay: .15s; } .typing i:nth-child(3) { animation-delay: .3s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
.status-text { font-size: 13px; color: var(--muted); }

/* ── 입력창 ── */
.composer-wrap { padding: 0 20px 14px; }
.composer {
  max-width: 820px; margin: 0 auto; background: var(--panel); border: 1px solid var(--line); border-radius: 18px;
  box-shadow: var(--shadow); padding: 10px 12px 8px;
}
.composer.drag { border-color: var(--accent); background: var(--accent-soft); }
.composer .files { justify-content: flex-start; margin-bottom: 8px; }
.composer textarea {
  width: 100%; border: none; outline: none; resize: none; background: transparent; padding: 4px 4px; max-height: 260px;
  min-height: 26px; line-height: 1.55;
}
.composer-bar { display: flex; align-items: center; gap: 6px; margin-top: 4px; }
.composer-bar .spacer { flex: 1; }
.toggle { border: 1px solid var(--line); background: transparent; border-radius: 99px; padding: 4px 11px; font-size: 13px; color: var(--muted); }
.toggle.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.composer select { border: 1px solid var(--line); background: transparent; border-radius: 99px; padding: 4px 8px; font-size: 13px; color: var(--muted); }
.send-btn {
  width: 34px; height: 34px; border-radius: 10px; border: none; background: var(--accent); color: var(--accent-ink);
  display: grid; place-items: center; font-size: 16px;
}
.send-btn:disabled { opacity: .4; cursor: default; }
.composer-foot { max-width: 820px; margin: 6px auto 0; font-size: 11.5px; color: var(--muted); text-align: center; }
.long-warn { max-width: 820px; margin: 0 auto 8px; font-size: 13px; background: var(--accent-soft); border-radius: 10px; padding: 7px 12px; }

/* ── 모달·토스트 ── */
.modal-back { position: fixed; inset: 0; background: rgba(0,0,0,.35); display: grid; place-items: center; padding: 16px; z-index: 50; }
.modal { width: 100%; max-width: 520px; background: var(--panel); border-radius: 16px; padding: 22px; box-shadow: var(--shadow); max-height: 90vh; overflow-y: auto; }
.modal h2 { margin: 0 0 14px; font-size: 18px; }
.modal h3 { font-size: 14px; margin: 18px 0 8px; }
.modal .row { display: flex; gap: 8px; justify-content: flex-end; margin-top: 8px; }
.toast {
  position: fixed; left: 50%; bottom: 96px; transform: translateX(-50%); background: var(--text); color: var(--bg);
  padding: 10px 16px; border-radius: 10px; font-size: 14px; z-index: 60; max-width: calc(100% - 32px); box-shadow: var(--shadow);
}

/* ── 관리자 ── */
.admin { max-width: 1080px; margin: 0 auto; padding: 24px 16px 60px; }
.admin header { display: flex; align-items: center; gap: 12px; margin-bottom: 20px; flex-wrap: wrap; }
.admin header .brand { flex: 1; }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; margin-bottom: 18px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.card .k { font-size: 13px; color: var(--muted); }
.card .v { font-size: 26px; font-weight: 700; margin: 2px 0 8px; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.section { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 18px; }
.section h2 { font-size: 16px; margin: 0 0 12px; display: flex; align-items: center; gap: 8px; }
.tbl-wrap { overflow-x: auto; }
.tbl { width: 100%; border-collapse: collapse; font-size: 14px; }
.tbl th, .tbl td { padding: 9px 8px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
.tbl th { font-size: 12.5px; color: var(--muted); font-weight: 600; }
.tbl td.num { text-align: right; font-variant-numeric: tabular-nums; }
.tbl .bar { width: 120px; display: inline-block; vertical-align: middle; margin-left: 8px; }
.badge { font-size: 12px; border-radius: 99px; padding: 2px 8px; background: var(--line); }
.badge.off { background: var(--accent-soft); color: var(--danger); }
.add-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; align-items: end; }
.add-form .field { margin: 0; }
.chart { display: flex; align-items: flex-end; gap: 3px; height: 140px; padding-top: 8px; }
.chart .col { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; min-width: 0; }
.chart .col i { width: 100%; max-width: 22px; background: var(--accent); border-radius: 4px 4px 0 0; min-height: 1px; }
.chart .col span { font-size: 10px; color: var(--muted); margin-top: 4px; }
.hint { font-size: 12.5px; color: var(--muted); }

@media (max-width: 760px) {
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 40; transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow); }
  .sidebar.open { transform: none; }
  .menu-btn { display: inline-flex; }
  .chat-inner { padding: 16px 16px 30px; }
  .composer-wrap { padding: 0 10px 10px; }
  .suggests { grid-template-columns: 1fr; }
  .msg.user .bubble { max-width: 92%; }
  .side-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.3); z-index: 39; }
}
