:root {
  color-scheme: light;
  --bg: #f6f5f2;
  --surface: #fcfcfb;
  --surface-2: #f0efeb;
  --border: #e3e1db;
  --text: #1b1b1a;
  --text-2: #52514e;
  --muted: #7a7873;
  --accent: #f7931a;
  --accent-ink: #1b1b1a;
  --primary: #1b1b1a;
  --primary-ink: #ffffff;
  --user-bubble: #1b1b1a;
  --user-ink: #ffffff;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --good: #1a7f37;
  --bad: #c62828;
  --flat: #6b6a66;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 16px rgba(0,0,0,.04);
  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --bg: #121211; --surface: #1a1a19; --surface-2: #232321; --border: #2f2f2c;
    --text: #f0efec; --text-2: #c3c2b7; --muted: #8f8d86;
    --primary: #f0efec; --primary-ink: #121211; --user-bubble: #2b3a52; --user-ink: #f0efec;
    --series-1: #3987e5; --series-2: #d95926;
    --good: #4cc26b; --bad: #ef6b6b; --flat: #a3a19a;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface: #1a1a19; --surface-2: #232321; --border: #2f2f2c;
  --text: #f0efec; --text-2: #c3c2b7; --muted: #8f8d86;
  --primary: #f0efec; --primary-ink: #121211; --user-bubble: #2b3a52; --user-ink: #f0efec;
  --series-1: #3987e5; --series-2: #d95926;
  --good: #4cc26b; --bad: #ef6b6b; --flat: #a3a19a;
  --shadow: none;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  font-family: "Pretendard Variable", Pretendard, -apple-system, system-ui, sans-serif;
  background: var(--bg); color: var(--text); font-size: 15px; line-height: 1.55;
  min-height: 100vh;
}
h1, h2, h3 { margin: 0; letter-spacing: -0.01em; }
button, input, textarea { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.small { font-size: 13px; font-weight: 400; }

/* ---------- top ---------- */
.topbar {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px 24px; border-bottom: 1px solid var(--border); background: var(--surface);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; gap: 12px; align-items: center; }
.logo {
  width: 38px; height: 38px; border-radius: 11px; display: grid; place-items: center;
  background: var(--accent); color: #fff; font-weight: 800; font-size: 20px;
}
.brand h1 { font-size: 17px; }
.sub { margin: 0; font-size: 12.5px; color: var(--muted); }
.top-actions { display: flex; gap: 10px; align-items: center; }
.pill { font-size: 12.5px; padding: 4px 10px; border-radius: 999px; border: 1px solid var(--border); white-space: nowrap; }
.pill::before { content: "●"; margin-right: 6px; font-size: 10px; }
.pill-wait::before { color: #d29a00; }
.pill-ok::before { color: var(--good); }
.pill-err::before { color: var(--bad); }
.icon-btn {
  width: 36px; height: 36px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface-2); cursor: pointer; font-size: 16px;
}
.banner {
  margin: 12px 24px 0; padding: 10px 14px; border-radius: 10px;
  background: #fff4e0; color: #6a4300; border: 1px solid #f4d9a6; font-size: 14px;
}
:root[data-theme="dark"] .banner { background: #3a2c12; color: #f6dca8; border-color: #5b4520; }
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) .banner { background: #3a2c12; color: #f6dca8; border-color: #5b4520; }
}

/* ---------- layout ---------- */
.layout {
  display: grid; grid-template-columns: 260px minmax(0, 1fr); gap: 20px;
  padding: 20px 24px; max-width: 1280px; margin: 0 auto;
}
.sidebar {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; align-self: start; position: sticky; top: 86px;
  max-height: calc(100vh - 110px); overflow: auto;
}
.side-title { font-size: 12.5px; color: var(--muted); font-weight: 600; margin: 18px 4px 8px; }
.conv-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.conv-item {
  display: flex; align-items: center; gap: 4px; border-radius: 10px; padding: 8px 6px 8px 10px; cursor: pointer;
}
.conv-item:hover { background: var(--surface-2); }
.conv-item.active { background: var(--surface-2); box-shadow: inset 3px 0 0 var(--accent); }
.conv-text { flex: 1; min-width: 0; }
.conv-title { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-meta { font-size: 12px; color: var(--muted); }
.conv-del {
  border: 0; background: transparent; color: var(--muted); cursor: pointer; padding: 4px 6px;
  border-radius: 6px; opacity: 0;
}
.conv-item:hover .conv-del, .conv-del:focus { opacity: 1; }
.conv-del:hover { color: var(--bad); background: var(--surface); }

.main { display: flex; flex-direction: column; gap: 14px; min-width: 0; }

/* ---------- summary ---------- */
.summary {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px 18px; box-shadow: var(--shadow);
}
.summary-head { flex-wrap: wrap; display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 12px; }
.summary-head h2 { font-size: 15px; }
.summary-grid { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 10px; }
.stat { background: var(--surface-2); border-radius: 10px; padding: 10px 12px; min-height: 64px; }
.stat-label { font-size: 12px; color: var(--muted); }
.stat-value { font-size: 17px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-sub { font-size: 12px; color: var(--text-2); }
.stat-sub .up { color: var(--good); } .stat-sub .down { color: var(--bad); }
.skeleton { animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .5; } }
.badge { font-size: 13px; font-weight: 700; padding: 4px 10px; border-radius: 999px; background: var(--surface-2); white-space: nowrap; }
.badge.up { color: var(--good); }
.badge.down { color: var(--bad); }
.badge.flat { color: var(--flat); }
#trendDetail { margin: 10px 0 0; }

/* ---------- tabs ---------- */
.tabs { display: flex; gap: 6px; }
.tab {
  border: 1px solid var(--border); background: var(--surface); padding: 8px 16px; border-radius: 10px;
  cursor: pointer; font-weight: 600; color: var(--text-2);
}
.tab.active { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.panel { display: flex; flex-direction: column; gap: 14px; }

/* ---------- chat ---------- */
.chat {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); gap: 0; height: calc(100vh - 360px); min-height: 420px;
}
.messages { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 14px; }
.welcome { margin: auto; text-align: center; max-width: 520px; }
.welcome-title { font-size: 20px; font-weight: 700; margin: 0 0 4px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 16px; }
.chip {
  border: 1px solid var(--border); background: var(--surface-2); padding: 7px 12px; border-radius: 999px;
  cursor: pointer; font-size: 13.5px;
}
.chip:hover { border-color: var(--accent); }
.msg { display: flex; flex-direction: column; max-width: 78%; }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.assistant { align-self: flex-start; }
.bubble { padding: 10px 14px; border-radius: 14px; word-break: break-word; }
.msg.user .bubble { background: var(--user-bubble); color: var(--user-ink); border-bottom-right-radius: 4px; white-space: pre-wrap; }
.msg.assistant .bubble { background: var(--surface-2); border-bottom-left-radius: 4px; }
.bubble p { margin: 0 0 8px; } .bubble p:last-child { margin: 0; }
.bubble ul, .bubble ol { margin: 4px 0 8px; padding-left: 20px; }
.msg.error .bubble { background: transparent; border: 1px solid var(--bad); color: var(--bad); }
.tools { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.tool-tag { font-size: 11.5px; color: var(--muted); border: 1px dashed var(--border); padding: 2px 8px; border-radius: 999px; }
.typing { display: inline-flex; gap: 4px; align-items: center; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: bounce 1s infinite; }
.typing i:nth-child(2) { animation-delay: .15s; } .typing i:nth-child(3) { animation-delay: .3s; }
.typing-text { margin-left: 8px; font-size: 13px; color: var(--muted); }
@keyframes bounce { 0%, 60%, 100% { transform: translateY(0); opacity: .5; } 30% { transform: translateY(-4px); opacity: 1; } }

.chat-form { display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--border); }
.chat-form textarea {
  flex: 1; resize: none; border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px;
  background: var(--surface-2); max-height: 140px; outline: none;
}
.chat-form textarea:focus, input:focus { border-color: var(--accent); }

/* ---------- buttons & forms ---------- */
.btn {
  border: 1px solid var(--border); background: var(--surface-2); padding: 9px 16px; border-radius: 10px;
  cursor: pointer; font-weight: 600; white-space: nowrap;
}
.btn:hover { border-color: var(--text-2); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.btn-block { width: 100%; }
.btn-sm { padding: 5px 10px; font-size: 13px; }
.btn-link { border: 0; background: transparent; cursor: pointer; padding: 4px 6px; border-radius: 6px; font-size: 13px; color: var(--text-2); }
.btn-link:hover { background: var(--surface-2); }
.btn-link.danger:hover { color: var(--bad); }
.row-gap { display: flex; gap: 6px; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px 18px; box-shadow: var(--shadow);
}
.card h3 { font-size: 15px; margin-bottom: 12px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 8px; }
.card-head h3 { margin: 0; }
.data-form { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
.data-form label { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--muted); font-weight: 600; }
.data-form label.grow { flex: 1; min-width: 160px; }
.data-form input {
  border: 1px solid var(--border); background: var(--surface-2); border-radius: 9px; padding: 8px 10px;
  outline: none; color: var(--text); font-weight: 400; font-size: 14px;
}
.form-btns { display: flex; gap: 6px; }
.form-msg { margin: 8px 0 0; font-size: 13.5px; min-height: 1em; }
.form-msg.ok { color: var(--good); } .form-msg.err { color: var(--bad); }

.table-wrap { max-height: 420px; overflow: auto; border: 1px solid var(--border); border-radius: 10px; }
.data-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.data-table th {
  position: sticky; top: 0; background: var(--surface-2); text-align: left; font-size: 12.5px;
  color: var(--muted); padding: 8px 12px; font-weight: 600;
}
.data-table td { padding: 7px 12px; border-top: 1px solid var(--border); }
.data-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.data-table td:last-child { text-align: right; white-space: nowrap; }
.data-table tr.flash td { animation: flash 1.6s ease-out; }
@keyframes flash { from { background: rgba(247, 147, 26, .25); } }

/* ---------- chart ---------- */
.legend { display: flex; gap: 14px; font-size: 12.5px; color: var(--text-2); }
.swatch { display: inline-block; width: 14px; height: 3px; border-radius: 2px; vertical-align: middle; margin-right: 6px; }
.swatch.s1 { background: var(--series-1); } .swatch.s2 { background: var(--series-2); }
.chart { position: relative; width: 100%; height: 260px; }
.chart svg { width: 100%; height: 100%; display: block; overflow: visible; }
.chart .grid { stroke: var(--border); stroke-width: 1; }
.chart .axis-text { fill: var(--muted); font-size: 11px; }
.chart .line-1 { stroke: var(--series-1); }
.chart .line-2 { stroke: var(--series-2); }
.chart .crosshair { stroke: var(--text-2); stroke-dasharray: 3 3; }
.chart .dot { stroke: var(--surface); stroke-width: 2; }
.tooltip {
  position: absolute; pointer-events: none; background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; font-size: 12.5px; box-shadow: 0 4px 16px rgba(0,0,0,.12);
  white-space: nowrap; transform: translate(-50%, -110%);
}
.tooltip b { font-variant-numeric: tabular-nums; }
.extra-stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; margin-top: 14px; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: var(--primary);
  color: var(--primary-ink); padding: 10px 16px; border-radius: 10px; font-size: 14px; z-index: 20;
}

/* ---------- responsive ---------- */
@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; padding: 12px 16px; }
  /* 모바일: 요약·채팅이 먼저, 대화 목록은 아래로 */
  .main { order: 1; }
  .sidebar { order: 2; position: static; max-height: none; }
  .summary-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .extra-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .chat { height: 70vh; scroll-margin-top: 76px; }
  .msg { max-width: 92%; }
  .topbar { padding: 12px 16px; }
  .banner { margin: 10px 16px 0; }
  .sub { display: none; }
  .summary-head h2 .small { display: none; }
  .brand h1 { font-size: 15px; }
  .card-head { flex-wrap: wrap; }
  .data-form label { flex: 1 1 140px; }
}
