:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --border: #e2e5ea;
  --text: #16191f;
  --text-dim: #5d6573;
  --accent: #2f5bd3;
  --accent-soft: #e8eefc;
  --ok: #1f9d55;
  --warn: #b7791f;
  --danger: #d03b3b;
  --code-bg: #0f1420;
  --code-text: #dbe4f3;
  --radius: 10px;
  --series-1: #2a78d6; --series-2: #eb6834; --series-3: #1baf7a; --series-4: #eda100;
  --series-5: #e87ba4; --series-6: #008300; --series-7: #4a3aa7; --series-8: #898781;
  --grid: #ecebe7;
  --axis: #c3c2b7;
  --mono: 'SF Mono', 'JetBrains Mono', Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, 'Apple SD Gothic Neo', 'Pretendard', 'Segoe UI', sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1016;
    --surface: #151922;
    --surface-2: #1c212c;
    --border: #2a303c;
    --text: #e6e9ef;
    --text-dim: #9099a8;
    --accent: #6d8ff0;
    --accent-soft: #1d2640;
    --code-bg: #0a0d14;
    --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70; --series-4: #c98500;
    --series-5: #d55181; --series-6: #008300; --series-7: #9085e9; --series-8: #898781;
    --grid: #262a33;
    --axis: #383835;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.5;
}
button, input, select, textarea { font: inherit; color: inherit; }
code, pre { font-family: var(--mono); }

/* ---------- 공통 ---------- */
.btn {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  padding: 7px 14px;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 4px 10px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: default; }

input[type=text], input[type=password], textarea, select {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  padding: 8px 10px;
}
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }

.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--text-dim); flex: none; }
.dot.on { background: var(--ok); }
.dot.off { background: var(--danger); }

.chip {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 12px; padding: 2px 8px; border-radius: 999px;
  background: var(--surface-2); color: var(--text-dim); border: 1px solid var(--border);
}
.chip.accent { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.chip.ok { color: var(--ok); }
.muted { color: var(--text-dim); }
.hidden { display: none !important; }

/* ---------- 로그인 ---------- */
.login-wrap { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  padding: 28px;
}
.login-card h1 { margin: 0 0 4px; font-size: 20px; }
.login-card p { margin: 0 0 20px; color: var(--text-dim); }
.login-card label { display: block; font-size: 13px; color: var(--text-dim); margin: 12px 0 4px; }
.login-card .btn { width: 100%; margin-top: 20px; padding: 10px; }
.error { color: var(--danger); font-size: 13px; min-height: 20px; margin-top: 10px; }

/* ---------- 레이아웃 ---------- */
.topbar {
  height: 56px; display: flex; align-items: center; gap: 16px;
  padding: 0 20px; border-bottom: 1px solid var(--border); background: var(--surface);
}
.topbar .brand { font-weight: 700; font-size: 16px; }
.topbar .base { font-family: var(--mono); font-size: 12px; color: var(--text-dim); }
.topbar .spacer { flex: 1; }
.tabs { display: flex; gap: 4px; }
.tabs button {
  border: 0; background: none; padding: 6px 12px; border-radius: 8px; cursor: pointer; color: var(--text-dim);
}
.tabs button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.layout { display: grid; grid-template-columns: 300px 1fr; height: calc(100% - 56px); }
.sidebar { border-right: 1px solid var(--border); overflow-y: auto; padding: 14px; background: var(--surface); }
.main { overflow-y: auto; padding: 24px 28px 60px; }
.main-inner { max-width: 980px; }

.side-head { display: flex; gap: 8px; margin-bottom: 12px; }
.group-title {
  display: flex; align-items: center; gap: 8px;
  font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--text-dim);
  margin: 16px 4px 6px;
}
.model-item {
  display: flex; align-items: center; gap: 8px; width: 100%;
  border: 0; background: none; text-align: left; padding: 8px 10px; border-radius: 8px; cursor: pointer;
}
.model-item:hover { background: var(--surface-2); }
.model-item.active { background: var(--accent-soft); color: var(--accent); }
.model-item .name { flex: 1; font-family: var(--mono); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.model-item .meta { font-size: 11px; color: var(--text-dim); }

/* ---------- 카드 ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 20px; margin-bottom: 18px; }
.card h2 { margin: 0 0 12px; font-size: 15px; }
.card h2 .muted { font-weight: 400; font-size: 13px; }
.model-title { font-family: var(--mono); font-size: 20px; margin: 0 0 8px; word-break: break-all; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }

.kv { display: grid; grid-template-columns: 120px 1fr; gap: 8px 12px; align-items: center; }
.kv .k { color: var(--text-dim); font-size: 13px; }
.inline-code {
  display: flex; align-items: center; gap: 8px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 6px 6px 6px 10px;
  font-family: var(--mono); font-size: 13px; min-width: 0;
}
.inline-code span { flex: 1; overflow-x: auto; white-space: nowrap; scrollbar-width: none; }

.code-tabs { display: flex; gap: 4px; margin-bottom: 8px; flex-wrap: wrap; }
.code-tabs button {
  border: 1px solid var(--border); background: var(--surface); border-radius: 6px; padding: 4px 10px; cursor: pointer; font-size: 12px;
}
.code-tabs button.active { background: var(--text); color: var(--surface); border-color: var(--text); }
.code-box { position: relative; }
.code-box pre {
  margin: 0; background: var(--code-bg); color: var(--code-text);
  border-radius: 8px; padding: 14px 16px; overflow-x: auto; font-size: 12.5px; line-height: 1.6;
}
.code-box .copy {
  position: absolute; top: 8px; right: 8px;
  background: rgba(255,255,255,.08); color: var(--code-text); border: 1px solid rgba(255,255,255,.15);
}
.code-box .copy:hover { background: rgba(255,255,255,.16); }

.key-pick { display: flex; gap: 8px; align-items: center; margin-bottom: 10px; }
.key-pick input { font-family: var(--mono); font-size: 12.5px; }

/* ---------- 플레이그라운드 ---------- */
.chat-log {
  border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2);
  min-height: 160px; max-height: 420px; overflow-y: auto; padding: 12px; margin-bottom: 10px;
}
.msg { margin-bottom: 10px; white-space: pre-wrap; word-break: break-word; }
.msg .role { font-size: 11px; color: var(--text-dim); text-transform: uppercase; display: block; }
.msg.user .body { color: var(--accent); }
.msg.err .body { color: var(--danger); }
.msg .think { color: var(--text-dim); font-size: 12px; border-left: 2px solid var(--border); padding-left: 8px; margin: 4px 0; }
.chat-form { display: flex; gap: 8px; }
.chat-form textarea { resize: vertical; min-height: 42px; height: 42px; }
.stats { font-size: 12px; color: var(--text-dim); margin-top: 6px; }

/* ---------- 키 / 로그 ---------- */
.form-row { display: grid; grid-template-columns: 1fr auto auto; gap: 8px; }
.form-row select { width: auto; }
.model-checks { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 4px 12px; margin-top: 10px; max-height: 220px; overflow-y: auto; }
.model-checks label { display: flex; gap: 6px; align-items: center; font-family: var(--mono); font-size: 12.5px; }
.new-key {
  border: 1px solid var(--ok); background: color-mix(in srgb, var(--ok) 8%, var(--surface));
  border-radius: 8px; padding: 12px; margin-top: 14px;
}
.new-key p { margin: 0 0 8px; font-size: 13px; }

table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-weight: 600; color: var(--text-dim); font-size: 12px; }
td.mono { font-family: var(--mono); font-size: 12px; }
tr.revoked td { color: var(--text-dim); text-decoration: line-through; }
tr.revoked td:last-child { text-decoration: none; }
.table-wrap { overflow-x: auto; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--surface); padding: 8px 16px; border-radius: 8px; font-size: 13px;
  opacity: 0; transition: opacity .2s; pointer-events: none;
}
.toast.show { opacity: 1; }

/* ---------- 대시보드 ---------- */
.dash-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.page-title { margin: 0; font-size: 20px; }
.small { font-size: 12.5px; margin: -6px 0 10px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--surface); }
.seg button { border: 0; background: none; padding: 6px 14px; cursor: pointer; color: var(--text-dim); }
.seg button + button { border-left: 1px solid var(--border); }
.seg button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 18px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.tile .label { font-size: 12px; color: var(--text-dim); }
.tile .value { font-size: 24px; font-weight: 650; margin-top: 2px; font-variant-numeric: tabular-nums; }
.tile .sub { font-size: 12px; color: var(--text-dim); margin-top: 2px; }

.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 12.5px; color: var(--text-dim); margin-bottom: 10px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; overflow: visible; }
.chart text { fill: var(--text-dim); font-size: 11px; font-family: var(--sans); }
.chart .grid line { stroke: var(--grid); }
.chart .baseline { stroke: var(--axis); }
.chart .hit { fill: transparent; cursor: default; }
.chart .hit:hover { fill: var(--text); fill-opacity: .04; }
.chart .empty { fill: var(--text-dim); font-size: 13px; }

.tooltip {
  position: fixed; z-index: 10; pointer-events: none;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 6px 20px rgba(0,0,0,.12); padding: 8px 10px; font-size: 12.5px; min-width: 160px;
}
.tooltip .t-title { font-weight: 600; margin-bottom: 4px; }
.tooltip .t-row { display: flex; align-items: center; gap: 6px; }
.tooltip .t-row i { width: 8px; height: 8px; border-radius: 2px; flex: none; }
.tooltip .t-row span:last-child { margin-left: auto; padding-left: 12px; font-variant-numeric: tabular-nums; }

.num-table td:not(:first-child), .num-table th:not(:first-child) { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.num-table td:first-child { font-family: var(--mono); font-size: 12.5px; }

.user-form { display: grid; grid-template-columns: 1fr 1fr auto auto; gap: 8px; }
.user-form select { width: auto; }
.role-admin { color: var(--accent); font-weight: 600; }

@media (max-width: 760px) {
  .user-form { grid-template-columns: 1fr; }
  .layout { grid-template-columns: 1fr; height: auto; }
  .sidebar { border-right: 0; border-bottom: 1px solid var(--border); max-height: 260px; }
  .main { padding: 16px; }
  .topbar { padding: 0 16px; gap: 10px; }
  .topbar .base { display: none; }
  .kv { grid-template-columns: 1fr; }
}
