/* ==========================================================================
   KISA 법령 AI - 사용자용 채팅 화면 (ChatGPT 스타일)
   ========================================================================== */
[hidden] { display: none !important; }

:root {
  --bg: #ffffff;
  --bg-side: #f9f9f9;
  --bg-hover: #ececec;
  --bg-active: #e3e3e3;
  --bg-user: #f1f1f1;
  --bg-composer: #ffffff;
  --border: #e5e5e5;
  --text: #0d0d0d;
  --text-2: #5d5d5d;
  --text-3: #8f8f8f;
  --accent: #0d0d0d;
  --accent-text: #ffffff;
  --cite-bg: #ececec;
  --cite-text: #5d5d5d;
  --danger: #d93025;
  --warn-bg: #fff8e6;
  --warn-text: #8a5a00;
  --shadow: 0 0 0 1px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.08);
  --radius-composer: 26px;
  --sidebar-w: 260px;
  --thread-w: 768px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #212121;
    --bg-side: #171717;
    --bg-hover: #2a2a2a;
    --bg-active: #303030;
    --bg-user: #303030;
    --bg-composer: #303030;
    --border: #3a3a3a;
    --text: #ececec;
    --text-2: #b4b4b4;
    --text-3: #8f8f8f;
    --accent: #ffffff;
    --accent-text: #0d0d0d;
    --cite-bg: #3a3a3a;
    --cite-text: #cdcdcd;
    --danger: #f28b82;
    --warn-bg: #3a2f14;
    --warn-text: #f5d38a;
    --shadow: 0 0 0 1px rgba(255,255,255,.06), 0 4px 16px rgba(0,0,0,.3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans KR", "Apple SD Gothic Neo", "Malgun Gothic", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; background: none; border: none; cursor: pointer; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; }

.app { display: flex; height: 100vh; height: 100dvh; overflow: hidden; }

/* ── 사이드바 ─────────────────────────────── */
.sidebar {
  width: var(--sidebar-w);
  background: var(--bg-side);
  display: flex;
  flex-direction: column;
  padding: 8px;
  flex-shrink: 0;
  transition: margin-left .2s ease;
}
.app.sb-collapsed .sidebar { margin-left: calc(-1 * var(--sidebar-w)); }
.sb-top { display: flex; justify-content: space-between; padding: 4px 4px 8px; }
.icon-btn {
  width: 36px; height: 36px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 8px;
  color: var(--text-2);
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }

.sb-item {
  display: flex; align-items: center; gap: 10px;
  width: 100%;
  padding: 8px 10px;
  border-radius: 10px;
  font-size: 14px;
  text-align: left;
}
.sb-item:hover { background: var(--bg-hover); }
.sb-new { font-weight: 500; }
.sb-logo {
  width: 26px; height: 26px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--bg); box-shadow: 0 0 0 1px var(--border); font-size: 14px;
}
.sb-label { font-size: 12px; color: var(--text-3); padding: 18px 10px 6px; font-weight: 500; }
.sb-group { font-size: 12px; color: var(--text-3); padding: 14px 10px 4px; font-weight: 500; }
.sb-history { flex: 1; overflow-y: auto; min-height: 0; }
.h-item {
  position: relative;
  display: flex; align-items: center;
  width: 100%;
  padding: 8px 10px;
  border-radius: 10px;
  font-size: 14px;
  text-align: left;
  color: var(--text);
}
.h-item:hover, .h-item.active { background: var(--bg-hover); }
.h-item.active { background: var(--bg-active); }
.h-title { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding-right: 4px; }
.h-del {
  opacity: 0; width: 26px; height: 26px; border-radius: 6px;
  display: inline-flex; align-items: center; justify-content: center; color: var(--text-3);
}
.h-del svg { width: 16px; height: 16px; }
.h-item:hover .h-del, .h-item.active .h-del { opacity: 1; }
.h-del:hover { color: var(--danger); }
.h-empty { font-size: 13px; color: var(--text-3); padding: 6px 10px; }

.sb-bottom { padding-top: 8px; border-top: 1px solid var(--border); }
.sb-profile { display: flex; align-items: center; gap: 10px; padding: 8px 10px; font-size: 14px; }
.sb-avatar {
  width: 30px; height: 30px; border-radius: 50%;
  background: #10a37f; color: #fff; font-weight: 700; font-size: 13px;
  display: inline-flex; align-items: center; justify-content: center;
}
.sb-dev { font-size: 12px; color: var(--text-3); text-decoration: none; }
.sb-dev:hover { text-decoration: underline; }
.sb-backdrop { display: none; }

/* ── 메인 ─────────────────────────────────── */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }
.topbar { display: flex; align-items: center; gap: 4px; height: 56px; padding: 0 12px; flex-shrink: 0; }
.topbar-spacer { flex: 1; }
#btn-sb-open { display: none; }
.app.sb-collapsed #btn-sb-open { display: inline-flex; }
.mobile-only { display: none; }

.picker { position: relative; }
.picker-btn {
  display: flex; align-items: center; gap: 4px;
  padding: 6px 10px; border-radius: 10px;
  font-size: 18px; font-weight: 600; color: var(--text);
}
.picker-btn .chev { width: 16px; height: 16px; color: var(--text-3); }
.picker-btn:hover { background: var(--bg-hover); }
.picker-menu {
  position: absolute; top: 44px; left: 0; z-index: 30;
  width: 340px; max-width: calc(100vw - 24px);
  background: var(--bg-composer);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 6px;
}
.picker-opt {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  width: 100%; padding: 10px 12px; border-radius: 10px; text-align: left;
}
.picker-opt:hover { background: var(--bg-hover); }
.po-title { font-size: 14px; font-weight: 500; }
.po-tag { font-size: 11px; color: #10a37f; border: 1px solid #10a37f; border-radius: 6px; padding: 0 5px; margin-left: 4px; }
.po-desc { font-size: 13px; color: var(--text-3); margin-top: 2px; }
.po-check { visibility: hidden; font-weight: 700; }
.picker-opt.selected .po-check { visibility: visible; }

.banner {
  margin: 0 auto 8px; max-width: var(--thread-w); width: calc(100% - 32px);
  background: var(--warn-bg); color: var(--warn-text);
  font-size: 14px; border-radius: 12px; padding: 10px 14px;
}

.scroll { flex: 1; overflow-y: auto; min-height: 0; }
.thread { max-width: var(--thread-w); margin: 0 auto; padding: 16px 24px 32px; display: flex; flex-direction: column; gap: 28px; }

.to-bottom {
  position: absolute; left: 50%; transform: translateX(-50%);
  bottom: 150px; z-index: 5;
  width: 34px; height: 34px; border-radius: 50%;
  background: var(--bg); box-shadow: var(--shadow);
  display: inline-flex; align-items: center; justify-content: center; color: var(--text-2);
}
.to-bottom svg { width: 18px; height: 18px; }

/* 빈 화면 (첫 화면) */
.hero { display: none; text-align: center; padding: 0 16px 24px; }
.hero h1 { font-size: 28px; font-weight: 600; letter-spacing: -0.01em; }
.suggestions { display: none; }
.main.is-empty { justify-content: center; }
.main.is-empty .scroll { display: none; }
.main.is-empty .hero { display: block; margin-top: -8vh; }
.main.is-empty .topbar { position: absolute; top: 0; left: 0; right: 0; }
.main.is-empty .suggestions {
  display: flex; flex-wrap: wrap; justify-content: center; gap: 8px;
  max-width: var(--thread-w); margin: 16px auto 0; padding: 0 24px;
}
.main.is-empty .disclaimer { display: none; }
.sugg {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 9px 14px; border-radius: 999px;
  border: 1px solid var(--border); font-size: 14px; color: var(--text-2);
}
.sugg:hover { background: var(--bg-hover); color: var(--text); }
.sugg span { font-size: 15px; }

/* 입력창 */
.composer-wrap { width: 100%; max-width: var(--thread-w); margin: 0 auto; padding: 0 24px 10px; flex-shrink: 0; }
.composer {
  background: var(--bg-composer);
  border-radius: var(--radius-composer);
  box-shadow: var(--shadow);
  padding: 12px 12px 10px 20px;
  display: flex; flex-direction: column; gap: 4px;
}
.composer textarea {
  width: 100%; resize: none; border: none; outline: none; background: transparent;
  color: var(--text); font: inherit; font-size: 16px; line-height: 1.5;
  max-height: 200px; padding: 4px 0;
}
.composer textarea::placeholder { color: var(--text-3); }
.composer-row { display: flex; align-items: center; justify-content: flex-end; gap: 8px; }
.limit { font-size: 12px; color: var(--danger); }
.send {
  width: 36px; height: 36px; border-radius: 50%;
  background: var(--accent); color: var(--accent-text);
  display: inline-flex; align-items: center; justify-content: center;
  transition: opacity .15s;
}
.send:disabled { opacity: .25; cursor: default; }
.disclaimer { font-size: 12px; color: var(--text-3); text-align: center; padding: 8px 0 2px; }

/* ── 메시지 ─────────────────────────────── */
.msg-user { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.bubble {
  max-width: 70%;
  background: var(--bg-user);
  border-radius: 22px;
  padding: 10px 18px;
  white-space: pre-wrap; word-break: break-word;
}
.msg-bot { display: flex; flex-direction: column; gap: 6px; }

.md { word-break: break-word; line-height: 1.75; }
.md p { margin: 0 0 12px; }
.md p:last-child { margin-bottom: 0; }
.md h3 { font-size: 17px; font-weight: 600; margin: 20px 0 8px; }
.md h4 { font-size: 16px; font-weight: 600; margin: 16px 0 6px; }
.md > :first-child { margin-top: 0; }
.md ul, .md ol { margin: 0 0 12px; padding-left: 24px; }
.md li { margin: 4px 0; }
.md li.l2 { margin-left: 22px; list-style-type: circle; }
.md hr { border: none; border-top: 1px solid var(--border); margin: 18px 0; }
.md strong { font-weight: 600; }
.md code { font-family: ui-monospace, Consolas, monospace; font-size: 14px; background: var(--cite-bg); border-radius: 5px; padding: 1px 5px; }

.cite {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 20px; height: 20px; padding: 0 6px; margin: 0 2px;
  border-radius: 10px; font-size: 11px; font-weight: 600;
  background: var(--cite-bg); color: var(--cite-text);
  vertical-align: 2px; cursor: pointer; line-height: 1;
}
.cite:hover { background: var(--accent); color: var(--accent-text); }

.typing { display: inline-block; width: 10px; height: 10px; border-radius: 50%; background: var(--text); vertical-align: middle; margin-left: 2px; animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { transform: scale(.7); opacity: .6; } 50% { transform: scale(1); opacity: 1; } }

.thinking { display: flex; align-items: center; gap: 10px; color: var(--text-2); font-size: 15px; }
.shimmer {
  background: linear-gradient(90deg, var(--text-3) 0%, var(--text) 50%, var(--text-3) 100%);
  background-size: 200% 100%;
  -webkit-background-clip: text; background-clip: text; color: transparent;
  animation: shimmer 1.8s linear infinite;
}
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }
.thinking-sub { font-size: 13px; color: var(--text-3); margin-top: 2px; }

.note { font-size: 13px; color: var(--text-3); }
.err {
  display: flex; gap: 10px; align-items: flex-start;
  border: 1px solid color-mix(in srgb, var(--danger) 40%, transparent);
  color: var(--danger); border-radius: 14px; padding: 12px 14px; font-size: 14px;
}
.err .retry { margin-left: auto; white-space: nowrap; font-size: 13px; text-decoration: underline; color: inherit; }

/* 답변 하단 */
.bot-foot { display: flex; align-items: center; gap: 2px; flex-wrap: wrap; margin-top: 2px; }
.act {
  width: 32px; height: 32px; border-radius: 8px;
  display: inline-flex; align-items: center; justify-content: center; color: var(--text-3);
}
.act svg { width: 18px; height: 18px; }
.act:hover { background: var(--bg-hover); color: var(--text); }
.src-pill {
  display: inline-flex; align-items: center; gap: 8px;
  height: 32px; padding: 0 12px 0 6px; margin-left: 6px;
  border-radius: 999px; border: 1px solid var(--border);
  font-size: 13px; color: var(--text-2);
}
.src-pill:hover { background: var(--bg-hover); color: var(--text); }
.src-icons { display: inline-flex; }
.src-icons span {
  width: 20px; height: 20px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 10px; font-weight: 700; color: #fff; margin-left: -5px;
  box-shadow: 0 0 0 2px var(--bg);
}
.src-icons span:first-child { margin-left: 0; }
.law-network { background: #2563eb; }
.law-pipa { background: #10a37f; }
.law-industry { background: #9333ea; }
.law-other { background: #6b7280; }

/* ── 출처 패널 ─────────────────────────────── */
.sources-panel {
  width: 400px; flex-shrink: 0;
  border-left: 1px solid var(--border);
  display: flex; flex-direction: column;
  background: var(--bg);
}
.sp-head { display: flex; align-items: center; justify-content: space-between; height: 56px; padding: 0 12px 0 20px; flex-shrink: 0; }
.sp-title { font-size: 16px; font-weight: 600; }
.sp-body { flex: 1; overflow-y: auto; padding: 4px 16px 24px; }
.sp-section { font-size: 13px; color: var(--text-3); font-weight: 500; margin: 14px 4px 8px; }
.src-card {
  display: block; width: 100%; text-align: left;
  border-radius: 14px; padding: 12px 14px; margin-bottom: 6px;
  scroll-margin-top: 40px;
}
.src-card:hover, .src-card.focus { background: var(--bg-hover); }
.src-card.focus { box-shadow: inset 0 0 0 1px var(--border); }
.sc-top { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--text-3); }
.sc-top .src-icons span { width: 18px; height: 18px; font-size: 9px; }
.sc-title { font-size: 15px; font-weight: 600; margin: 4px 0 2px; color: var(--text); }
.sc-meta { font-size: 12px; color: var(--text-3); }
.sc-preview { font-size: 13px; color: var(--text-2); margin-top: 6px; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; white-space: pre-line; }
.sc-full {
  margin-top: 8px; font-size: 13.5px; line-height: 1.75; color: var(--text);
  white-space: pre-wrap; word-break: break-word;
  background: var(--bg-side); border-radius: 10px; padding: 12px;
}
.sc-actions { display: flex; gap: 14px; margin-top: 8px; font-size: 13px; }
.sc-actions button, .sc-actions a { color: var(--text-2); text-decoration: none; }
.sc-actions button:hover, .sc-actions a:hover { color: var(--text); text-decoration: underline; }

.toast {
  position: fixed; top: 64px; left: 50%; transform: translateX(-50%);
  white-space: nowrap; max-width: calc(100vw - 32px); overflow: hidden; text-overflow: ellipsis;
  background: var(--accent); color: var(--accent-text);
  padding: 8px 16px; border-radius: 10px; font-size: 14px; z-index: 100;
}

::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
::-webkit-scrollbar-track { background: transparent; }

/* ── 반응형 ─────────────────────────────── */
@media (max-width: 1100px) {
  .sources-panel { position: fixed; right: 0; top: 0; bottom: 0; z-index: 40; box-shadow: var(--shadow); }
}
@media (max-width: 768px) {
  .sidebar { position: fixed; left: 0; top: 0; bottom: 0; z-index: 50; margin-left: calc(-1 * var(--sidebar-w)); }
  .app.sb-open .sidebar { margin-left: 0; }
  .app.sb-open .sb-backdrop { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 45; }
  #btn-sb-open { display: inline-flex; }
  .mobile-only { display: inline-flex; }
  .thread { padding: 12px 16px 24px; }
  .composer-wrap { padding: 0 12px 8px; }
  .bubble { max-width: 85%; }
  .hero h1 { font-size: 24px; }
  .sources-panel { width: 100%; }
  .picker-btn { font-size: 17px; }
}
