/* ===== 테마 토큰 ===== */
:root {
  color-scheme: light;
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #f0efe9;
  --border: #e3e1d9;
  --text: #22211d;
  --text-2: #5f5d55;
  --muted: #8f8c82;
  --accent: #5b5bd6;
  --accent-soft: rgba(91, 91, 214, 0.12);
  --mark: rgba(255, 196, 0, 0.35);
  --code-bg: #f0efe9;

  --graph-bg: #fbfaf7;
  --graph-node: #8b8fa8;
  --graph-node-hub: #5b5bd6;
  --graph-ghost: #c9c6bc;
  --graph-link: rgba(60, 60, 80, 0.16);
  --graph-link-hi: rgba(91, 91, 214, 0.75);
  --graph-label: #3b3a35;
  --graph-dot: rgba(0, 0, 0, 0.05);

  --hm0: #ebe9e2;
  --hm1: #c9c9f2;
  --hm2: #9c9ce8;
  --hm3: #6e6edb;
  --hm4: #4343b8;

  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 4px 16px rgba(0, 0, 0, 0.05);
  --radius: 10px;
  --left-w: 300px;
  --right-w: 380px;
}

[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121316;
  --surface: #1a1b20;
  --surface-2: #23242b;
  --border: #2c2e36;
  --text: #e7e6e3;
  --text-2: #b4b2ab;
  --muted: #7d7b75;
  --accent: #8f8ff5;
  --accent-soft: rgba(143, 143, 245, 0.16);
  --mark: rgba(255, 196, 0, 0.3);
  --code-bg: #23242b;

  --graph-bg: #15161a;
  --graph-node: #7c8099;
  --graph-node-hub: #8f8ff5;
  --graph-ghost: #45464f;
  --graph-link: rgba(200, 200, 230, 0.13);
  --graph-link-hi: rgba(143, 143, 245, 0.85);
  --graph-label: #d6d5d0;
  --graph-dot: rgba(255, 255, 255, 0.04);

  --hm0: #24252c;
  --hm1: #33356b;
  --hm2: #4a4ca3;
  --hm3: #6c6ed8;
  --hm4: #a0a2ff;

  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 16px rgba(0, 0, 0, 0.3);
}

/* ===== 기본 ===== */
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: "Pretendard", -apple-system, BlinkMacSystemFont, "Segoe UI", "Apple SD Gothic Neo", "Malgun Gothic", sans-serif;
  font-size: 14px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
[hidden] { display: none !important; }

.app {
  display: grid;
  grid-template-columns: var(--left-w) 1fr var(--right-w);
  height: 100vh;
}

/* ===== 사이드바 공통 ===== */
.sidebar {
  background: var(--surface);
  overflow-y: auto;
  min-height: 0;
}
.sidebar-left {
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}
.sidebar-right { border-left: 1px solid var(--border); }

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-2);
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
  transition: background .15s, color .15s, border-color .15s;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ===== 로고 ===== */
.brand {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 20px 12px;
}
.logo {
  margin: 0;
  font-size: 22px;
  font-weight: 800;
  letter-spacing: -0.03em;
}
.logo span { color: var(--accent); margin: 0 1px; }
.icon-sun { display: none; }
[data-theme="dark"] .icon-sun { display: block; }
[data-theme="dark"] .icon-moon { display: none; }

/* ===== 패널 ===== */
.panel { padding: 12px 20px 16px; border-top: 1px solid var(--border); }
.panel-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 10px;
}
.panel h2 {
  margin: 0;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-2);
}

/* ===== 히트맵 ===== */
.heatmap {
  display: grid;
  grid-auto-flow: column;
  grid-template-rows: 14px repeat(7, 1fr);
  max-width: 380px;
  gap: 3px;
}
.heatmap .month {
  font-size: 10px;
  color: var(--muted);
  white-space: nowrap;
  overflow: visible;
  line-height: 14px;
}
.heatmap .cell {
  aspect-ratio: 1;
  border-radius: 2px;
  background: var(--hm0);
  cursor: pointer;
  border: 0;
  padding: 0;
}
.heatmap .cell.future { visibility: hidden; cursor: default; }
.heatmap .cell.today { box-shadow: inset 0 0 0 1px var(--text-2); }
.heatmap .cell.selected { box-shadow: 0 0 0 2px var(--accent); }
.heatmap .cell:hover:not(.future) { filter: brightness(0.92); }
.lv0 { background: var(--hm0) !important; }
.lv1 { background: var(--hm1) !important; }
.lv2 { background: var(--hm2) !important; }
.lv3 { background: var(--hm3) !important; }
.lv4 { background: var(--hm4) !important; }
.heatmap-legend {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 3px;
  margin-top: 8px;
}
.heatmap-legend i { width: 10px; height: 10px; border-radius: 2px; display: inline-block; }
.heatmap-legend span { margin: 0 4px; }

/* ===== 검색 ===== */
.panel-search { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.search-box {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  height: 38px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  color: var(--muted);
  margin-bottom: 12px;
}
.search-box:focus-within { border-color: var(--accent); background: var(--surface); }
.search-box input {
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--text);
  font: inherit;
  outline: none;
  min-width: 0;
}
.active-filter {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 10px;
  margin-bottom: 12px;
  border-radius: 8px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 12px;
  font-weight: 600;
}
.active-filter button {
  border: 0;
  background: none;
  cursor: pointer;
  color: inherit;
  padding: 0 2px;
}

.results { list-style: none; margin: 0 -8px; padding: 0; overflow-y: auto; }
.results li {
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
  transition: background .12s;
}
.results li:hover, .results li.active { background: var(--surface-2); }
.results li.active { box-shadow: inset 3px 0 0 var(--accent); }
.results .r-title { font-weight: 600; font-size: 13.5px; }
.results .r-meta { font-size: 11px; color: var(--muted); }
.results .r-snippet {
  font-size: 12px;
  color: var(--text-2);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  margin-top: 2px;
}
.results mark { background: var(--mark); color: inherit; border-radius: 2px; padding: 0 1px; }
.results .empty { color: var(--muted); cursor: default; text-align: center; padding: 20px 0; }
.results .empty:hover { background: none; }

/* ===== 메인 그래프 ===== */
.main {
  position: relative;
  background: var(--graph-bg);
  overflow: hidden;
  min-width: 0;
}
#graph { display: block; width: 100%; height: 100%; cursor: grab; touch-action: none; }
#graph.dragging { cursor: grabbing; }
#graph.hovering { cursor: pointer; }
.graph-toolbar {
  position: absolute;
  top: 16px;
  right: 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.graph-toolbar .icon-btn { box-shadow: var(--shadow); }
.graph-stats { position: absolute; top: 16px; left: 16px; }
.graph-hint { position: absolute; bottom: 14px; left: 16px; pointer-events: none; }
.tooltip {
  position: absolute;
  pointer-events: none;
  padding: 6px 10px;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  font-size: 12px;
  white-space: nowrap;
  transform: translate(-50%, calc(-100% - 14px));
}
.tooltip b { display: block; font-size: 13px; }

/* ===== 우측 상세 ===== */
.sidebar-right { padding: 24px; }
.empty-state {
  height: 100%;
  display: grid;
  place-content: center;
  text-align: center;
  color: var(--muted);
}
.empty-icon { font-size: 40px; line-height: 1; margin-bottom: 12px; opacity: .5; }
.detail-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.detail-head h2 { margin: 0; font-size: 22px; line-height: 1.3; letter-spacing: -0.02em; word-break: keep-all; }
.meta {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 12px;
  margin: 14px 0 18px;
  padding: 12px 14px;
  background: var(--surface-2);
  border-radius: var(--radius);
  font-size: 12px;
}
.meta dt { color: var(--muted); }
.meta dd { margin: 0; color: var(--text-2); word-break: break-word; }

/* 마크다운 */
.markdown { font-size: 14px; color: var(--text); word-break: break-word; }
.markdown h1, .markdown h2, .markdown h3, .markdown h4 { line-height: 1.35; margin: 1.4em 0 .5em; letter-spacing: -0.01em; }
.markdown h2 { font-size: 16px; padding-bottom: 4px; border-bottom: 1px solid var(--border); }
.markdown h3 { font-size: 14.5px; }
.markdown h4 { font-size: 13.5px; }
.markdown > :first-child { margin-top: 0; }
.markdown p { margin: .6em 0; }
.markdown ul, .markdown ol { padding-left: 1.3em; margin: .5em 0; }
.markdown li { margin: .2em 0; }
.markdown code {
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: 12.5px;
  background: var(--code-bg);
  padding: 1px 5px;
  border-radius: 4px;
}
.markdown pre {
  background: var(--code-bg);
  padding: 12px 14px;
  border-radius: 8px;
  overflow-x: auto;
  font-size: 12.5px;
  line-height: 1.5;
}
.markdown pre code { background: none; padding: 0; }
.markdown blockquote {
  margin: .8em 0;
  padding: 4px 12px;
  border-left: 3px solid var(--accent);
  color: var(--text-2);
  background: var(--accent-soft);
  border-radius: 0 6px 6px 0;
}
.markdown hr { border: 0; border-top: 1px solid var(--border); margin: 1.2em 0; }
.markdown table { border-collapse: collapse; width: 100%; font-size: 12.5px; margin: .8em 0; display: block; overflow-x: auto; }
.markdown th, .markdown td { border: 1px solid var(--border); padding: 5px 8px; text-align: left; }
.markdown th { background: var(--surface-2); }
.markdown a { color: var(--accent); }
.wikilink {
  color: var(--accent);
  text-decoration: none;
  background: var(--accent-soft);
  padding: 0 4px;
  border-radius: 4px;
  cursor: pointer;
  font-weight: 500;
}
.wikilink:hover { text-decoration: underline; }
.wikilink.ghost { color: var(--muted); background: var(--surface-2); text-decoration: line-through dotted; }

.links-section { margin-top: 24px; padding-top: 16px; border-top: 1px solid var(--border); }
.links-section h3 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-2); margin: 0 0 10px; }
.count { color: var(--muted); font-weight: 500; }
.chips { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 6px; }
.chips li button {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 12px;
  cursor: pointer;
}
.chips li button:hover { border-color: var(--accent); color: var(--accent); }
.chips .none { color: var(--muted); font-size: 12px; }

/* 스크롤바 */
.sidebar::-webkit-scrollbar, .results::-webkit-scrollbar { width: 8px; }
.sidebar::-webkit-scrollbar-thumb, .results::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

/* ===== 반응형 ===== */
@media (max-width: 1100px) {
  :root { --left-w: 260px; --right-w: 320px; }
}
@media (max-width: 820px) {
  body { overflow: auto; }
  .app { grid-template-columns: 1fr; grid-template-rows: auto 60vh auto; height: auto; }
  .sidebar-left { border-right: 0; border-bottom: 1px solid var(--border); }
  .results { max-height: 280px; }
  .sidebar-right { border-left: 0; border-top: 1px solid var(--border); padding: 20px 16px; }
  .brand, .panel { padding-left: 16px; padding-right: 16px; }
  .empty-state { padding: 40px 0; }
}
