:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #1c2430;
  --muted: #6b7684;
  --line: #e3e7ec;
  --brand: #1f4e79;
  --brand-ink: #ffffff;
  --brand-soft: #e8f0f8;
  --user-bubble: #1f4e79;
  --user-ink: #ffffff;
  --danger: #c0392b;
  --warn-bg: #fff6e0;
  --warn-ink: #7a5a00;
  --radius: 14px;
  --font: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Malgun Gothic", "Noto Sans KR", "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12161c; --panel: #1a2029; --text: #e6eaf0; --muted: #98a2b3; --line: #2a323d;
    --brand: #5b9bd5; --brand-ink: #0d1520; --brand-soft: #1d2b3a;
    --user-bubble: #2d5f8f; --user-ink: #ffffff; --warn-bg: #3a2f10; --warn-ink: #f3d68a;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #12161c; --panel: #1a2029; --text: #e6eaf0; --muted: #98a2b3; --line: #2a323d;
  --brand: #5b9bd5; --brand-ink: #0d1520; --brand-soft: #1d2b3a;
  --user-bubble: #2d5f8f; --user-ink: #ffffff; --warn-bg: #3a2f10; --warn-ink: #f3d68a;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font-family: var(--font); font-size: 16px; line-height: 1.6; -webkit-text-size-adjust: 100%; }
[hidden] { display: none !important; }
button { font: inherit; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.error { color: var(--danger); font-size: 14px; margin: 4px 0; }

button.primary { background: var(--brand); color: var(--brand-ink); border: 0; border-radius: 10px; padding: 10px 16px; font-weight: 600; }
button.primary:disabled { opacity: .5; cursor: default; }
button.ghost { background: transparent; border: 1px solid var(--line); color: var(--text); border-radius: 10px; padding: 9px 16px; }
button.link { background: none; border: 0; color: var(--muted); padding: 6px 0; text-align: left; }
button.link:hover { color: var(--text); }
button.block { width: 100%; }
.icon-btn { background: none; border: 0; font-size: 22px; color: var(--text); padding: 6px 10px; border-radius: 8px; }

/* 로그인 */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.login-card { width: 100%; max-width: 360px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 28px 24px; display: flex; flex-direction: column; gap: 12px; }
.login-card h1 { font-size: 22px; margin: 0; text-align: center; }
.login-card p { margin: 0; text-align: center; }
.login-logo { width: 56px; height: 56px; margin: 0 auto; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 14px; color: var(--muted); }
label input { padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); font-size: 16px; }
label.check { flex-direction: row; align-items: center; gap: 8px; }

/* 레이아웃 */
.app { display: flex; height: 100dvh; }
.drawer { width: 280px; flex-shrink: 0; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; }
.drawer-head { padding: 14px; }
.history { flex: 1; overflow-y: auto; padding: 0 8px; }
.history .item { display: flex; align-items: center; gap: 4px; border-radius: 8px; }
.history .item.active, .history .item:hover { background: var(--brand-soft); }
.history .item button.open { flex: 1; min-width: 0; text-align: left; background: none; border: 0; color: var(--text); padding: 9px 10px; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.history .item button.del { background: none; border: 0; color: var(--muted); padding: 6px 8px; font-size: 14px; visibility: hidden; }
.history .item:hover button.del, .history .item.active button.del { visibility: visible; }
.history .group { font-size: 12px; color: var(--muted); padding: 12px 10px 4px; }
.drawer-foot { border-top: 1px solid var(--line); padding: 12px 16px calc(12px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; }
.me-name { font-weight: 600; margin-bottom: 4px; }
.scrim { display: none; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 6px; padding: 8px 12px; padding-top: calc(8px + env(safe-area-inset-top)); border-bottom: 1px solid var(--line); background: var(--panel); }
.topbar h1 { font-size: 17px; margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#menu { display: none; }
.banner { background: var(--warn-bg); color: var(--warn-ink); padding: 10px 16px; font-size: 14px; }

.chat { flex: 1; overflow-y: auto; padding: 20px 16px; }
.chat > * { max-width: 820px; margin-left: auto; margin-right: auto; }
.welcome { text-align: center; padding-top: 6vh; }
.welcome h2 { margin: 0 0 6px; font-size: 24px; }
.examples { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 10px; margin-top: 24px; text-align: left; }
.example { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; color: var(--text); text-align: left; font-size: 14px; }
.example:hover { border-color: var(--brand); }

.msg { margin-bottom: 20px; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble { background: var(--user-bubble); color: var(--user-ink); border-radius: 16px 16px 4px 16px; padding: 10px 14px; max-width: 85%; white-space: pre-wrap; word-break: break-word; }
.msg.bot .card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.steps { list-style: none; margin: 0 0 8px; padding: 0; font-size: 13px; color: var(--muted); }
.steps li { padding: 2px 0 2px 20px; position: relative; }
.steps li::before { content: "✓"; position: absolute; left: 2px; color: var(--brand); }
.steps li.running::before { content: ""; width: 10px; height: 10px; top: 8px; border: 2px solid var(--brand); border-right-color: transparent; border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
details.process { margin-bottom: 8px; }
details.process summary { font-size: 13px; color: var(--muted); cursor: pointer; }

.answer { word-break: break-word; }
.answer h1, .answer h2, .answer h3, .answer h4 { font-size: 16px; margin: 14px 0 6px; }
.answer p { margin: 6px 0; }
.answer ul, .answer ol { margin: 6px 0; padding-left: 22px; }
.answer li { margin: 2px 0; }
.answer blockquote { margin: 8px 0; padding: 6px 12px; border-left: 3px solid var(--brand); background: var(--brand-soft); border-radius: 0 8px 8px 0; }
.answer code { background: var(--brand-soft); padding: 1px 5px; border-radius: 4px; font-size: 14px; }
.answer table { border-collapse: collapse; margin: 8px 0; font-size: 14px; display: block; overflow-x: auto; }
.answer th, .answer td { border: 1px solid var(--line); padding: 6px 8px; text-align: left; }
.answer a { color: var(--brand); }
.answer .cursor { display: inline-block; width: 8px; height: 16px; background: var(--brand); vertical-align: text-bottom; animation: blink 1s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0; } }

.sources { margin-top: 12px; padding-top: 10px; border-top: 1px dashed var(--line); }
.sources h4 { margin: 0 0 6px; font-size: 13px; color: var(--muted); font-weight: 600; }
.sources .chips { display: flex; flex-wrap: wrap; gap: 6px; }
.sources a { font-size: 13px; text-decoration: none; color: var(--brand); background: var(--brand-soft); border-radius: 999px; padding: 4px 10px; }
.msg-actions { display: flex; gap: 12px; margin-top: 8px; }
.msg-actions button { background: none; border: 0; color: var(--muted); font-size: 13px; padding: 2px 0; }
.msg .err { color: var(--danger); }

.composer { border-top: 1px solid var(--line); background: var(--panel); padding: 10px 12px calc(8px + env(safe-area-inset-bottom)); }
.composer > * { max-width: 820px; margin-left: auto; margin-right: auto; }
.areas { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 8px; scrollbar-width: none; }
.areas button { flex-shrink: 0; border: 1px solid var(--line); background: var(--bg); color: var(--text); border-radius: 999px; padding: 4px 12px; font-size: 13px; }
.areas button.on { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { flex: 1; resize: none; border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; background: var(--bg); max-height: 160px; font-size: 16px; line-height: 1.5; }
.send { width: 44px; height: 44px; border-radius: 12px; border: 0; background: var(--brand); color: var(--brand-ink); font-size: 18px; flex-shrink: 0; }
.send:disabled { opacity: .45; cursor: default; }
.disclaimer { font-size: 12px; color: var(--muted); text-align: center; margin: 6px auto 0; }

/* 대화상자 */
dialog { border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); color: var(--text); padding: 0; width: min(420px, calc(100vw - 32px)); }
dialog.wide { width: min(760px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(0,0,0,.4); }
.dialog-body { padding: 20px; display: flex; flex-direction: column; gap: 12px; }
.dialog-body h3 { margin: 0; }
.dialog-body h4 { margin: 8px 0 4px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }
.table-wrap { overflow-x: auto; }
#user-table { width: 100%; border-collapse: collapse; font-size: 14px; }
#user-table th, #user-table td { border-bottom: 1px solid var(--line); padding: 8px 6px; text-align: left; white-space: nowrap; }
#user-table td button { background: none; border: 1px solid var(--line); border-radius: 6px; color: var(--text); font-size: 12px; padding: 3px 8px; margin-right: 4px; }
.small { font-size: 13px; margin: 0; }
.zone-list { display: flex; flex-direction: column; gap: 10px; }
.zone { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; font-size: 14px; }
.zone h5 { margin: 0 0 4px; font-size: 15px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.zone .tag { font-size: 12px; border-radius: 999px; padding: 1px 8px; background: var(--brand-soft); color: var(--brand); font-weight: 500; }
.zone .tag.off { background: var(--bg); color: var(--muted); }
.zone dl { margin: 0; display: grid; grid-template-columns: 70px 1fr; gap: 2px 8px; }
.zone dt { color: var(--muted); }
.zone dd { margin: 0; word-break: break-word; }
.zone .del-zone { margin-top: 6px; background: none; border: 1px solid var(--line); border-radius: 6px; color: var(--danger); font-size: 12px; padding: 3px 8px; }
textarea { border: 1px solid var(--line); border-radius: 10px; background: var(--bg); padding: 10px 12px; font-size: 15px; resize: vertical; }
.new-user .grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; align-items: end; }

/* 휴대폰 */
@media (max-width: 760px) {
  .drawer { position: fixed; inset: 0 auto 0 0; z-index: 20; transform: translateX(-100%); transition: transform .2s; width: 82vw; max-width: 300px; }
  .app.drawer-open .drawer { transform: none; }
  .app.drawer-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 10; }
  #menu { display: block; }
  .chat { padding: 16px 12px; }
  .welcome { padding-top: 2vh; }
  .welcome h2 { font-size: 20px; }
  .examples { grid-template-columns: 1fr; }
  .history .item button.del { visibility: visible; }
}

/* 인용 검증 배지 (korean-law-mcp) */
.verify { margin-top: 12px; font-size: 13px; border-radius: 10px; padding: 6px 10px; background: var(--brand-soft); }
.verify summary { cursor: pointer; font-weight: 600; color: var(--brand); }
.verify ul { margin: 6px 0 2px; padding-left: 4px; list-style: none; }
.verify li { margin: 2px 0; color: var(--muted); word-break: keep-all; overflow-wrap: anywhere; }
.verify li.bad { color: var(--danger); }
.verify-bad, .verify-check { background: var(--warn-bg); }
.verify-bad summary, .verify-check summary { color: var(--warn-ink); }
