:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --border: #e4e7ec;
  --text: #1f2937;
  --muted: #6b7280;
  --accent: #4f46e5;
  --accent-hover: #4338ca;
  --accent-soft: #eef2ff;
  --danger: #b91c1c;
  --danger-soft: #fef2f2;
  --ok: #047857;
  --header-bg: #1e293b;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16,24,40,.06), 0 1px 3px rgba(16,24,40,.05);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Apple SD Gothic Neo", "Malgun Gothic",
    "Noto Sans KR", sans-serif;
  font-size: 15px;
  line-height: 1.6;
  color: var(--text);
  background: var(--bg);
}

/* ---------- 헤더 / 내비 ---------- */
.site-header {
  background: var(--header-bg);
  color: #e2e8f0;
  padding: 0 24px;
  display: flex;
  align-items: center;
  gap: 20px;
  min-height: 56px;
  flex-wrap: wrap;
  box-shadow: var(--shadow);
}
.site-header .brand {
  font-weight: 700;
  color: #fff;
  text-decoration: none;
  font-size: 16px;
  white-space: nowrap;
}
.site-header nav {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  flex: 1;
}
.site-header nav a {
  color: #cbd5e1;
  text-decoration: none;
  padding: 6px 10px;
  border-radius: 7px;
  font-size: 14px;
  white-space: nowrap;
}
.site-header nav a:hover { background: rgba(255,255,255,.08); color: #fff; }
.site-header nav a.active { background: var(--accent); color: #fff; }
.site-header .user {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  white-space: nowrap;
}
.site-header .user form { margin: 0; }
.site-header .nav-db {
  color: #cbd5e1;
  text-decoration: none;
  padding: 6px 10px;
  border-radius: 7px;
  font-size: 14px;
  white-space: nowrap;
}
.site-header .nav-db:hover { background: rgba(255,255,255,.08); color: #fff; }
.site-header .nav-db.active { background: var(--accent); color: #fff; }
.nav-subtabs {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  padding: 6px 24px;
  background: rgba(0,0,0,.04);
  border-bottom: 1px solid var(--border, #e2e8f0);
}
.nav-subtabs a {
  color: #475569;
  text-decoration: none;
  padding: 5px 10px;
  border-radius: 6px;
  font-size: 13px;
  white-space: nowrap;
}
.nav-subtabs a:hover { background: rgba(0,0,0,.06); }
.nav-subtabs a.active { background: var(--accent); color: #fff; }

/* ---------- 레이아웃 ---------- */
main {
  max-width: 1040px;
  margin: 0 auto;
  padding: 28px 24px 64px;
}
h1 { font-size: 22px; margin: 4px 0 20px; }
h2 { font-size: 16px; margin: 0 0 12px; }
h3 { font-size: 14px; color: var(--muted); margin: 18px 0 8px; }
a { color: var(--accent); }
a:hover { color: var(--accent-hover); }
p { margin: 8px 0; }

/* section을 카드로 */
section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px 22px;
  margin-bottom: 20px;
  box-shadow: var(--shadow);
}

/* ---------- 표 ---------- */
table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  font-size: 14px;
  margin: 8px 0;
}
th, td {
  border: 1px solid var(--border);
  padding: 9px 12px;
  text-align: left;
  vertical-align: top;
}
thead th, table tr:first-child th {
  background: #f8fafc;
  font-weight: 600;
  color: #374151;
}
tbody tr:nth-child(even), table tr:nth-child(even) { background: #fbfcfd; }

/* ---------- 폼 ---------- */
label { display: block; font-size: 14px; font-weight: 500; margin-bottom: 4px; }
input, textarea, select {
  font: inherit;
  color: var(--text);
  width: 100%;
  max-width: 640px;
  padding: 8px 10px;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  background: #fff;
}
input[type="checkbox"], input[type="radio"] { width: auto; }
input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
textarea { min-height: 90px; resize: vertical; }
form p { margin: 12px 0; }

/* 긴 프롬프트 편집기: 넓고 높게, 모노스페이스, 세로 줄바꿈(가로 스크롤 없음) */
.prompt-editor {
  max-width: none;
  width: 100%;
  min-height: 58vh;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
  line-height: 1.55;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  resize: vertical;
}
.prompt-editor.tall { min-height: 86vh; }

/* ---------- 버튼 ---------- */
button, .button {
  display: inline-block;
  font: inherit;
  font-weight: 600;
  font-size: 14px;
  padding: 8px 16px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  text-decoration: none;
  line-height: 1.4;
}
button:hover, .button:hover { background: var(--accent-hover); color: #fff; }
button.secondary, .button.secondary {
  background: #fff;
  color: var(--text);
  border-color: #cbd5e1;
}
button.secondary:hover, .button.secondary:hover { background: #f8fafc; }

/* ---------- 유틸 ---------- */
.error {
  color: var(--danger);
  background: var(--danger-soft);
  border: 1px solid #fecaca;
  border-radius: 8px;
  padding: 10px 14px;
  margin: 12px 0;
}
.muted { color: var(--muted); }
pre {
  background: #0f172a;
  color: #e2e8f0;
  padding: 14px 16px;
  border-radius: 8px;
  overflow-x: auto;
  font-size: 13px;
  line-height: 1.5;
}
ul { padding-left: 20px; }

/* 인라인 border 속성(table border="1") 잔재 정리 */
table[border] { border: 1px solid var(--border); }

/* ---------- 로그인 화면 ---------- */
body.auth {
  display: flex;
  min-height: 100vh;
  align-items: center;
  justify-content: center;
  background: linear-gradient(160deg, #eef2ff 0%, #f5f6f8 60%);
}
body.auth main { padding: 0; width: 100%; }
.auth-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 10px 30px rgba(16,24,40,.10);
  padding: 32px 30px;
  max-width: 380px;
  margin: 0 auto;
}
.auth-card h1 { text-align: center; font-size: 20px; margin-bottom: 6px; }
.auth-card .sub { text-align: center; color: var(--muted); font-size: 13px; margin-bottom: 22px; }
.auth-card input { max-width: none; }
.auth-card button { width: 100%; margin-top: 8px; padding: 10px; }

/* ---------- 골드셋 예시 카드 ---------- */
.example-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
}
.example-card .card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}
.example-card .head-fields { display: flex; gap: 18px; flex-wrap: wrap; }
label.inline {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin: 0 18px 0 0;
  font-weight: 600;
}
label.inline select, label.inline input { width: auto; }
.example-card > label { margin-top: 10px; }
.example-card textarea,
.example-card > input { max-width: none; width: 100%; }
/* 카드 본문은 항상 세로 줄바꿈(가로 스크롤 금지) */
.example-card textarea {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  overflow-x: hidden;
}
.example-card .struct { margin: 12px 0; }
.example-card .struct-title {
  margin: 0 0 6px;
  font-weight: 600;
  color: var(--muted);
}
.example-card .struct-rows {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 8px;
}
.example-card .struct-row {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 8px;
  align-items: center;
}
.example-card .struct-row input { max-width: none; font-weight: 400; color: var(--text); }
@media (max-width: 640px) {
  .example-card .struct-row { grid-template-columns: 1fr; }
}

/* ---------- 승인 pool(골드셋 편집)·정석화 원천 — 세로 리스트 + 스크롤(C1) ---------- */
.pool-grid {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 12px 0;
  max-height: 420px;
  overflow-y: auto;
  padding-right: 4px;
}
.pool-card {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 12px;
  box-shadow: var(--shadow);
}
.pool-card-head {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 0;
  flex: 0 0 auto;
}
.badge {
  display: inline-block;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 12px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  white-space: nowrap;
}
.pool-summary { font-size: 14px; margin: 0; color: var(--text); flex: 1 1 auto; }
.pool-card-actions { display: flex; gap: 8px; flex: 0 0 auto; }

/* ---------- 모달(경량, 라이브러리 없음) ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, .45);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 40px 16px;
  z-index: 50;
  overflow-y: auto;
}
.modal {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 22px 24px;
  max-width: 640px;
  width: 100%;
  max-height: calc(100vh - 80px);
  overflow-y: auto;
  box-shadow: 0 20px 40px rgba(16,24,40,.25);
}
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
.modal-head h2 { margin: 0; }
/* 모달 본문(pre)은 가로 스크롤 말고 세로 줄바꿈 */
.modal pre {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  overflow-x: hidden;
}

/* ---------- 화법 채팅 ---------- */
body.chat main { max-width: 1280px; }

.chat-topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: end;
  gap: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}
.chat-topbar label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
  color: var(--muted);
}
.chat-topbar button { margin-left: auto; }

.chat-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.15fr);
  /* row를 minmax(0,1fr)로 고정해야 콘텐츠가 길어도 row가 커지지 않고(=definite height),
     chat-log가 flex:1+min-height:0으로 자체 스크롤한다. auto row면 컨테이너 height 무시됨. */
  grid-template-rows: minmax(0, 1fr);
  gap: 16px;
  align-items: stretch;
  height: max(420px, calc(100vh - 230px));
}
.chat-prompt,
.chat-main,
.chat-sessions {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}
.chat-prompt { display: flex; flex-direction: column; }
.chat-prompt-help { font-size: 12px; color: var(--muted); margin: 0 0 12px; line-height: 1.5; }
.chat-prompt-actions { display: flex; gap: 8px; align-self: flex-start; }
.chat-prompt label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.chat-prompt textarea {
  width: 100%;
  flex: 1;
  min-height: 260px;
  margin: 10px 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
  line-height: 1.5;
  resize: vertical;
}

/* 채팅 본문: 로그(내부 스크롤) + 하단 고정 입력창 */
.chat-main { display: flex; flex-direction: column; }
.chat-log {
  display: flex;
  flex-direction: column;
  gap: 14px;
  flex: 1;
  min-height: 0;  /* flex 자식이 콘텐츠보다 줄어들어 내부 스크롤이 생기게 한다 */
  overflow-y: auto;
  padding: 14px;
  margin-bottom: 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.chat-msg {
  max-width: 82%;
  border-radius: 14px;
  padding: 10px 14px;
  box-shadow: var(--shadow);
}
.chat-user {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.chat-user a { color: #fff; text-decoration: underline; }
.chat-assistant {
  align-self: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.chat-role {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  margin-bottom: 5px;
  padding: 1px 8px;
  border-radius: 999px;
}
.chat-user .chat-role { background: rgba(255,255,255,.22); color: #fff; }
.chat-assistant .chat-role { background: var(--accent-soft); color: var(--accent); }
.chat-plain { white-space: pre-wrap; overflow-wrap: anywhere; }
.chat-md { overflow-wrap: anywhere; }
.chat-md > :first-child { margin-top: 0; }
.chat-md > :last-child { margin-bottom: 0; }
.chat-md p { margin: 8px 0; }
.chat-md ul, .chat-md ol { margin: 8px 0; padding-left: 20px; }
.chat-md li { margin: 2px 0; }
.chat-md h1, .chat-md h2, .chat-md h3 { margin: 12px 0 6px; color: var(--text); }
.chat-md code { background: var(--bg); padding: 1px 5px; border-radius: 5px; font-size: 13px; }
.chat-md pre { background: var(--bg); padding: 10px; border-radius: 8px; overflow-x: auto; }
.chat-md pre code { background: none; padding: 0; }
.chat-usage { margin-top: 6px; color: var(--muted); }

/* 입력창 */
.chat-composer { display: flex; gap: 10px; align-items: flex-end; }
.chat-composer textarea { flex: 1; resize: vertical; }
.chat-empty { color: var(--muted); }

.chat-sessions { margin-top: 16px; }
.chat-sessions ul { margin: 0; padding-left: 18px; }

@media (max-width: 860px) {
  /* 세로 1단에서는 높이 고정을 풀고, 로그만 자체 스크롤한다. */
  .chat-layout { grid-template-columns: 1fr; height: auto; }
  .chat-log { max-height: 60vh; }
}

/* ---------- 푸터 ---------- */
.site-footer {
  max-width: 1040px;
  margin: 0 auto;
  padding: 0 24px 40px;
  color: var(--muted);
  font-size: 12px;
}
