/* ── 기본 ──────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --blue:    #1a56db;
  --blue-lt: #e8f0fe;
  --gray-1:  #f8f9fa;
  --gray-2:  #e9ecef;
  --gray-3:  #adb5bd;
  --gray-4:  #495057;
  --text:    #212529;
  --sidebar-w: 260px;
  --topbar-h:  52px;
  --radius:    6px;
  font-size: 15px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  color: var(--text);
  background: #fff;
  line-height: 1.6;
}

a { color: var(--blue); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ── 상단 바 ────────────────────────────────────── */
#topbar {
  position: fixed; top: 0; left: 0; right: 0; height: var(--topbar-h);
  display: flex; align-items: center; gap: 10px;
  padding: 0 12px;
  background: #fff;
  border-bottom: 1px solid var(--gray-2);
  z-index: 100;
}

#menu-btn {
  font-size: 20px; background: none; border: none;
  cursor: pointer; padding: 4px 8px; color: var(--gray-4);
  display: none;
}

#hub-link {
  display: inline-flex; align-items: center; gap: 3px;
  font-weight: 600; font-size: 12px; color: var(--gray-4);
  white-space: nowrap; text-decoration: none;
  border: 1px solid var(--gray-2); border-radius: 14px;
  padding: 3px 10px; background: var(--gray-1);
}
#hub-link::before { content: '←'; font-size: 11px; }
#hub-link:hover { color: var(--blue); border-color: var(--blue); background: var(--blue-lt); }
#logo {
  font-weight: 700; font-size: 16px; color: var(--blue);
  white-space: nowrap; text-decoration: none;
}

#search-box {
  flex: 1; display: flex; max-width: 480px;
  border: 1px solid var(--gray-2); border-radius: var(--radius);
  overflow: hidden;
}
#search-input {
  flex: 1; border: none; outline: none;
  padding: 6px 10px; font-size: 14px;
}
#search-btn {
  border: none; background: var(--gray-1); padding: 0 12px;
  cursor: pointer; font-size: 15px;
}
#search-btn:hover { background: var(--gray-2); }

#auth-area { margin-left: auto; display: flex; align-items: center; gap: 6px; }
.btn-outline {
  border: 1px solid var(--blue); color: var(--blue); background: none;
  padding: 5px 14px; border-radius: 20px; cursor: pointer; font-size: 13px; font-weight: 600;
}
.btn-outline:hover { background: var(--blue-lt); }
.btn-ghost {
  border: 1px solid var(--gray-2); background: none; color: var(--gray-4);
  cursor: pointer; font-size: 13px; padding: 5px 10px; border-radius: 20px;
}
.btn-ghost:hover { border-color: var(--gray-3); color: var(--text); }

#user-info:not([hidden]) { display: flex; align-items: center; gap: 6px; }
#user-name { display: flex; align-items: center; }

/* ── 레이아웃 ────────────────────────────────────── */
#layout {
  display: flex;
  margin-top: var(--topbar-h);
  min-height: calc(100vh - var(--topbar-h));
}

/* ── 사이드바 ────────────────────────────────────── */
#sidebar {
  width: var(--sidebar-w); flex-shrink: 0;
  position: fixed; top: var(--topbar-h); bottom: 0; left: 0;
  overflow-y: auto; overflow-x: hidden;
  background: var(--gray-1);
  border-right: 1px solid var(--gray-2);
  transition: transform 0.2s;
}

#sidebar-inner { padding: 10px 0; }

#series-tabs {
  display: flex; flex-wrap: wrap; gap: 4px;
  padding: 8px 10px 6px;
  border-bottom: 1px solid var(--gray-2);
  margin-bottom: 4px;
}
.series-tab {
  font-size: 12px; padding: 3px 8px;
  border: 1px solid var(--gray-3); border-radius: 12px;
  background: none; cursor: pointer; color: var(--gray-4);
}
.series-tab.active {
  background: var(--blue); border-color: var(--blue); color: #fff;
}

#doc-list { list-style: none; }
#doc-list li a {
  display: block; padding: 6px 14px;
  font-size: 13px; color: var(--gray-4);
  border-left: 3px solid transparent;
  text-decoration: none;
}
#doc-list li a:hover { background: var(--gray-2); color: var(--text); }
#doc-list li a.active {
  color: var(--blue); border-left-color: var(--blue);
  background: var(--blue-lt); font-weight: 600;
}
.doc-code-sm { font-size: 11px; color: var(--gray-3); display: block; }
.doc-title-sm {
  display: block; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis;
}

/* TOC 링크 */
.toc-link { display: flex !important; gap: 6px; align-items: baseline; }
.toc-link.level-1 { font-weight: 600; color: var(--text) !important; }
.toc-link.level-2 { padding-left: 18px !important; font-size: 12px; }
.toc-link.level-3 { padding-left: 30px !important; font-size: 11px; color: var(--gray-3) !important; }
.sec-num-sm { white-space: nowrap; font-size: 11px; color: var(--gray-3); }

/* ── 메인 콘텐츠 ─────────────────────────────────── */
#content {
  margin-left: var(--sidebar-w);
  flex: 1; min-width: 0;
  padding: 28px 32px;
  max-width: 860px;
}

/* ── 홈 페이지 ───────────────────────────────────── */
.home-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 12px; margin-top: 20px;
}
.doc-card {
  border: 1px solid var(--gray-2); border-radius: var(--radius);
  padding: 14px 16px; cursor: pointer;
  text-decoration: none; color: inherit;
  display: block; transition: box-shadow 0.15s;
}
.doc-card:hover { box-shadow: 0 2px 8px rgba(0,0,0,0.1); text-decoration: none; }
.doc-card-code { font-size: 11px; color: var(--gray-3); margin-bottom: 4px; }
.doc-card-title { font-size: 13px; font-weight: 600; line-height: 1.4; }

/* ── 문서 뷰 ─────────────────────────────────────── */
.doc-header { margin-bottom: 0; padding-bottom: 16px; border-bottom: 2px solid var(--gray-2); }
.doc-header h1 { font-size: 20px; font-weight: 700; margin-bottom: 10px; }
.doc-header-top { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.doc-code { font-size: 12px; color: var(--gray-3); }

/* 즐겨찾기 버튼 */
.fav-btn {
  background: none; border: none; cursor: pointer;
  font-size: 18px; color: var(--gray-3); padding: 0 2px; line-height: 1;
  transition: color 0.15s;
}
.fav-btn:hover { color: #f5a623; }
.fav-btn.fav-active { color: #f5a623; }

/* 즐겨찾기 페이지 */
.favs-heading { font-size: 18px; font-weight: 700; margin-bottom: 16px; }
.fav-list { display: flex; flex-direction: column; gap: 8px; }
.fav-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px; border: 1px solid var(--gray-2); border-radius: 8px;
  background: #fff;
}
.fav-item-link { flex: 1; text-decoration: none; color: inherit; display: flex; flex-direction: column; gap: 3px; }
.fav-item-link:hover .fav-item-main { color: var(--blue); }
.fav-item-meta { font-size: 11px; color: var(--gray-3); }
.fav-item-main { font-size: 14px; font-weight: 600; line-height: 1.4; }
.fav-remove-btn {
  background: none; border: none; cursor: pointer;
  color: var(--gray-3); font-size: 14px; padding: 4px 6px; border-radius: 4px;
}
.fav-remove-btn:hover { background: var(--gray-1); color: var(--text); }
.fav-group-label { font-size: 12px; color: var(--gray-3); font-weight: 600; margin: 16px 0 6px; text-transform: uppercase; letter-spacing: 0.05em; }
.fav-sec-num { font-weight: 700; color: var(--blue); margin-left: 4px; }

/* topbar 즐겨찾기 링크 */
#favs-btn {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 13px; color: var(--gray-4); text-decoration: none; white-space: nowrap;
  padding: 5px 12px; border-radius: 20px; border: 1px solid var(--gray-2); background: #fff;
}
#favs-btn:hover { border-color: #f5a623; color: #f5a623; }

/* 섹션 헤딩 즐겨찾기 버튼 */
.sec-heading { display: flex; align-items: baseline; gap: 6px; }
.sec-fav-btn {
  flex-shrink: 0; background: none; border: none; cursor: pointer;
  font-size: 14px; color: var(--gray-3); padding: 0; line-height: 1;
  opacity: 0; transition: opacity 0.15s, color 0.15s;
}
.sec-block:hover .sec-fav-btn { opacity: 0.7; }
.sec-fav-btn:hover { opacity: 1 !important; color: #f5a623; }
.sec-fav-btn.fav-active { opacity: 1; color: #f5a623; }

/* 사용자 아바타 */
#user-avatar {
  width: 30px; height: 30px; border-radius: 50%;
  object-fit: cover; border: 1px solid var(--gray-2);
  cursor: default; display: block;
}
#user-avatar-fallback {
  width: 30px; height: 30px; border-radius: 50%;
  background: var(--blue); color: #fff;
  font-size: 13px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}

/* 탭 (본문 / 신구대비표) */
.doc-tabs {
  display: flex; gap: 0; margin-top: 14px;
  border-bottom: 2px solid var(--gray-2);
}
.doc-tab {
  padding: 7px 18px; border: none; background: none;
  font-size: 14px; cursor: pointer; color: var(--gray-4);
  border-bottom: 2px solid transparent; margin-bottom: -2px;
}
.doc-tab.active { color: var(--blue); border-bottom-color: var(--blue); font-weight: 600; }

/* 섹션 블록 */
.sec-block { margin-top: 28px; scroll-margin-top: calc(var(--topbar-h) + 12px); }
.sec-block.level-1 { margin-top: 40px; }

.sec-heading {
  scroll-margin-top: calc(var(--topbar-h) + 12px);
  font-weight: 700; line-height: 1.4;
}
h2.sec-heading { font-size: 18px; padding-bottom: 6px; border-bottom: 1px solid var(--gray-2); margin-bottom: 12px; }
h3.sec-heading { font-size: 15px; margin-bottom: 8px; color: var(--text); }
h4.sec-heading { font-size: 14px; margin-bottom: 6px; color: var(--gray-4); }

.para { margin-bottom: 8px; font-size: 14px; line-height: 1.85; }
.para-eq { padding: 6px 0; overflow-x: auto; -webkit-overflow-scrolling: touch; }
.para-eq-def { overflow-x: auto; -webkit-overflow-scrolling: touch; padding: 4px 0; }
.eq-row { display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 8px; min-width: max-content; width: 100%; }
.eq-body { text-align: center; }
.eq-num { white-space: nowrap; font-size: 0.92em; color: #333; padding-right: 4px; align-self: center; }
.para-table { overflow-x: auto; margin: 10px 0; }
.contributor-heading { font-weight: 700; font-size: 1em; margin: 1.4em 0 0.3em; padding-bottom: 4px; border-bottom: 2px solid var(--gray-2); }
.para-table table { border-collapse: collapse; font-size: 13px; width: 100%; }
.para-table th, .para-table td {
  border: 1px solid var(--gray-2); padding: 6px 10px; text-align: left;
}
.para-table th { background: var(--gray-1); font-weight: 600; }
.tbl-caption {
  caption-side: top; text-align: left;
  font-size: 13px; font-weight: 600; padding: 0 0 4px;
  color: var(--text);
}

/* 홈 시리즈 */
.series-summary {
  font-weight: 700; font-size: 15px; cursor: pointer; padding: 4px 0;
}
.series-summary .cnt {
  font-weight: 400; font-size: 12px; color: var(--gray-3); margin-left: 4px;
}

/* ── 검색 결과 ───────────────────────────────────── */
.search-header { margin-bottom: 20px; }
.search-header h2 { font-size: 18px; }
.search-header p { color: var(--gray-3); font-size: 13px; margin-top: 4px; }

.search-tabs {
  display: flex; gap: 0; margin-bottom: 16px;
  border-bottom: 2px solid var(--gray-2);
}
.search-tab {
  padding: 7px 16px; font-size: 13px; cursor: pointer;
  border: none; background: none; color: var(--gray-4);
  border-bottom: 2px solid transparent; margin-bottom: -2px;
}
.search-tab.active { color: var(--blue); border-bottom-color: var(--blue); font-weight: 600; }

.result-item {
  padding: 12px 0; border-bottom: 1px solid var(--gray-2);
}
.result-item:last-child { border-bottom: none; }
.result-doc { font-size: 11px; color: var(--gray-3); margin-bottom: 2px; }
.result-title { font-size: 14px; font-weight: 600; margin-bottom: 4px; }
.result-title a { color: var(--text); }
.result-title a:hover { color: var(--blue); text-decoration: none; }
.result-text { font-size: 13px; color: var(--gray-4); line-height: 1.6; }
.result-text em { color: var(--blue); font-style: normal; font-weight: 600; }

/* ── 드롭다운 섹션 (용어/기호의 정의) ─────────────────── */
.sec-collapsible { border: none; }
.sec-collapsible-summary {
  list-style: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
}
.sec-collapsible-summary::-webkit-details-marker { display: none; }
.sec-collapsible-summary::before {
  content: '▶';
  font-size: 0.7em;
  color: var(--blue);
  transition: transform 0.2s;
  flex-shrink: 0;
}
.sec-collapsible[open] > .sec-collapsible-summary::before {
  transform: rotate(90deg);
}
.sec-collapsible-summary .sec-heading {
  margin: 0;
  display: inline;
}

/* ── 용어 목록 ───────────────────────────────────── */
.terms-list { margin: 4px 0 16px; padding: 0; list-style: none; }
.term-item {
  display: grid;
  grid-template-columns: minmax(100px, 200px) 1fr;
  gap: 4px 16px;
  align-items: baseline;
  padding: 8px 0;
  border-bottom: 1px solid var(--gray-2);
}
.term-item:last-child { border-bottom: none; }
.term-name {
  font-size: 14px; font-weight: 600; color: var(--blue);
  line-height: 1.5;
}
.term-en { font-weight: 400; color: var(--gray-3); font-size: 12px; display: block; }
.term-def { font-size: 13px; color: var(--gray-4); line-height: 1.65; margin: 0; }

/* ── 기호 목록 ───────────────────────────────────── */
.symbols-list { margin: 4px 0 16px; padding: 0; list-style: none; }
.symbol-item {
  display: grid;
  grid-template-columns: 140px 1fr;
  gap: 4px 16px;
  align-items: baseline;
  padding: 6px 0;
  border-bottom: 1px solid var(--gray-2);
}
.symbol-item:last-child { border-bottom: none; }
.symbol-notation {
  font-size: 14px; font-weight: 500; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.symbol-def { font-size: 13px; color: var(--gray-4); line-height: 1.6; margin: 0; }

/* ── 신구대비표 ──────────────────────────────────── */
.changes-table { width: 100%; border-collapse: collapse; font-size: 13px; margin-top: 16px; }
.changes-table th {
  background: var(--gray-1); border: 1px solid var(--gray-2);
  padding: 8px 10px; font-weight: 600; white-space: nowrap;
}
.changes-table td {
  border: 1px solid var(--gray-2); padding: 8px 10px;
  vertical-align: top; line-height: 1.6;
}
.changes-table tr:hover td { background: var(--blue-lt); }

/* ── 북마크 버튼 ─────────────────────────────────── */
.bookmark-btn {
  float: right; font-size: 16px; background: none; border: none;
  cursor: pointer; color: var(--gray-3); padding: 0 4px;
  transition: color 0.15s;
}
.bookmark-btn:hover, .bookmark-btn.active { color: #f59e0b; }

/* ── 참조 팝업 ───────────────────────────────────── */
.kds-ref {
  color: var(--blue); cursor: pointer; text-decoration: underline;
  text-decoration-style: dotted; text-underline-offset: 2px;
}
.kds-ref:hover { text-decoration-style: solid; }

.tbl-ref, .fig-ref {
  color: var(--blue); cursor: pointer; text-decoration: underline;
  text-decoration-style: dotted; text-underline-offset: 2px;
}
.tbl-ref:hover, .fig-ref:hover { text-decoration-style: solid; }

/* 윗첨자 */
.para sup { font-size: 0.75em; vertical-align: super; line-height: 0; }
.para-table th sup,
.para-table td sup { font-size: 0.7em; vertical-align: super; line-height: 1; }

#ref-popup {
  position: absolute; z-index: 200;
  background: #fff; border: 1px solid var(--gray-2);
  border-radius: var(--radius); box-shadow: 0 4px 24px rgba(0,0,0,0.18);
  width: 380px; max-width: calc(100vw - 16px); max-height: min(75vh, 560px); overflow-y: auto;
}
#ref-popup-header {
  display: flex; align-items: flex-start; justify-content: space-between;
  padding: 10px 12px 8px; border-bottom: 1px solid var(--gray-2);
  position: sticky; top: 0; background: #fff; gap: 8px;
}
#ref-popup-header-left { min-width: 0; flex: 1; }
#ref-popup-label { font-size: 11px; color: var(--gray-3); margin-bottom: 2px; }
#ref-popup-title { font-size: 13px; font-weight: 600; line-height: 1.4; }
#ref-popup-actions { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
#ref-popup-open {
  font-size: 12px; padding: 3px 10px;
  border: 1px solid var(--blue); border-radius: var(--radius);
  color: var(--blue); background: none; cursor: pointer; white-space: nowrap;
  text-decoration: none;
}
#ref-popup-open:hover { background: var(--blue-lt); text-decoration: none; }
#ref-popup-close {
  background: none; border: none; cursor: pointer;
  font-size: 16px; color: var(--gray-3); padding: 0 2px; line-height: 1;
}
#ref-popup-close:hover { color: var(--text); }
#ref-popup-body { padding: 10px 12px; font-size: 13px; line-height: 1.75; }
#ref-popup-body .para { margin-bottom: 6px; }
#ref-popup-body .para-eq { overflow-x: auto; text-align: center; padding: 6px 0; }

/* 팝업 배경 오버레이 */
#ref-popup-overlay {
  position: fixed; inset: 0; z-index: 199;
  display: none;
}

/* ── 표 팝업 모달 ────────────────────────────────── */
#tbl-modal-overlay {
  position: fixed; inset: 0; z-index: 198;
  background: rgba(0,0,0,0.25); display: none;
}
#tbl-modal {
  position: fixed; z-index: 199;
  background: #fff; border: 1px solid var(--gray-2);
  border-radius: var(--radius); box-shadow: 0 4px 24px rgba(0,0,0,0.18);
  width: min(92vw, 820px); max-height: 80vh;
  overflow: auto;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
}
#tbl-modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px; gap: 8px;
  border-bottom: 1px solid var(--gray-2);
  position: sticky; top: 0; background: #fff;
}
#tbl-modal-title { font-size: 13px; font-weight: 600; flex: 1; }
#tbl-modal-close {
  background: none; border: none; cursor: pointer;
  font-size: 18px; color: var(--gray-3); padding: 0 4px; line-height: 1;
}
#tbl-modal-close:hover { color: var(--text); }
#tbl-modal-body { padding: 12px 14px; }
#tbl-modal-body .para-table { overflow-x: auto; margin: 0; }
#tbl-modal-body .para-table table { width: auto; min-width: 100%; }
#tbl-modal-body .para-table-subtitle { font-size: 13px; font-weight: 600; margin: 12px 0 4px; }

/* 동일 번호 서브표 그룹 */
.table-group { margin: 10px 0; }
.table-group .para-table { margin: 0 0 4px; }
.para-table-subtitle { font-size: 13px; font-weight: 600; margin: 12px 0 4px; color: var(--text); }

/* ── 로딩 / 빈 상태 ──────────────────────────────── */
.loading { color: var(--gray-3); font-size: 14px; padding: 40px 0; text-align: center; }
.empty { color: var(--gray-3); font-size: 14px; padding: 24px 0; }

/* ── 모바일 ──────────────────────────────────────── */
@media (max-width: 767px) {
  #menu-btn { display: block; }
  #search-box { max-width: none; flex: 1; }

  #sidebar {
    transform: translateX(-100%);
    z-index: 90;
    width: min(var(--sidebar-w), 80vw);
  }
  #sidebar.open { transform: translateX(0); }

  #content { margin-left: 0; padding: 16px; }
  #content.sidebar-open { opacity: 0.4; pointer-events: none; }

  .home-grid { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 400px) {
  .home-grid { grid-template-columns: 1fr; }
}

/* ── 그림 (이미지) ───────────────────────────────── */
.para-img {
  margin: 12px 0;
  text-align: center;
}
.para-img figure {
  display: inline-block;
  max-width: 100%;
}
.doc-img {
  max-width: 100%;
  height: auto;
  border: 1px solid var(--gray-2);
  border-radius: 4px;
  display: block;
  margin: 0 auto;
}

/* ── 용어 하이라이팅 & 팝업 ─────────────────────────── */
.term-ref {
  color: #0f7a4f;
  cursor: pointer;
  border-bottom: 1px dotted #0f7a4f;
  text-underline-offset: 2px;
}
.term-ref:hover { border-bottom-style: solid; background: #f0faf5; }

.term-popup {
  position: absolute;
  z-index: 201;
  background: #fff;
  border: 1px solid #c3e6d8;
  border-radius: var(--radius);
  box-shadow: 0 4px 20px rgba(0,0,0,0.15);
  width: 340px;
  max-width: calc(100vw - 16px);
  padding: 0;
  font-size: 13px;
}
.term-popup.hidden { display: none; }
.term-popup-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 10px; background: #f0faf5;
  border-bottom: 1px solid #c3e6d8; border-radius: var(--radius) var(--radius) 0 0;
}
.term-popup-source { font-size: 11px; color: #0f7a4f; font-weight: 500; }
.term-popup-close {
  background: none; border: none; cursor: pointer;
  font-size: 14px; color: #6b7280; padding: 0 2px; line-height: 1;
}
.term-popup-close:hover { color: var(--text); }
.term-popup-word {
  padding: 8px 12px 4px; font-weight: 700; font-size: 14px; color: var(--text);
}
.term-popup-def {
  padding: 0 12px 10px; color: #374151; line-height: 1.6;
}
