/* ---------- 색상 토큰 ---------- */

:root {
  color-scheme: light;
  --bg: #fbfaf7;
  --surface: #ffffff;
  --surface-2: #f3f1ec;
  --text: #1f2328;
  --muted: #5f6672;
  --border: #e4e1da;
  --accent: #4c5bd4;
  --accent-soft: #eceefc;
  --mark: #fff1a8;

  --def-bg: #eef4ff;  --def-line: #3d6fd9;
  --thm-bg: #f4effd;  --thm-line: #7b52d1;
  --idea-bg: #fff7e6; --idea-line: #d08a12;
  --warn-bg: #fdf0ef; --warn-line: #d1493f;
  --done-bg: #e6f5ec; --done-text: #1f7a45;
  --open-bg: #fff0d6; --open-text: #a3560b;

  --shadow: 0 1px 2px rgb(0 0 0 / 0.04), 0 2px 8px rgb(0 0 0 / 0.04);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #15171c;
    --surface: #1d2027;
    --surface-2: #252932;
    --text: #e6e6e3;
    --muted: #9aa0aa;
    --border: #2f343e;
    --accent: #95a1ff;
    --accent-soft: #262b45;
    --mark: #6b5a12;

    --def-bg: #1b2538;  --def-line: #6f9bf0;
    --thm-bg: #251f37;  --thm-line: #a687ec;
    --idea-bg: #2c2516; --idea-line: #e2a73f;
    --warn-bg: #301c1c; --warn-line: #ec7b72;
    --done-bg: #17301f; --done-text: #6fd497;
    --open-bg: #352713; --open-text: #f0b45d;

    --shadow: none;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #15171c;
  --surface: #1d2027;
  --surface-2: #252932;
  --text: #e6e6e3;
  --muted: #9aa0aa;
  --border: #2f343e;
  --accent: #95a1ff;
  --accent-soft: #262b45;
  --mark: #6b5a12;

  --def-bg: #1b2538;  --def-line: #6f9bf0;
  --thm-bg: #251f37;  --thm-line: #a687ec;
  --idea-bg: #2c2516; --idea-line: #e2a73f;
  --warn-bg: #301c1c; --warn-line: #ec7b72;
  --done-bg: #17301f; --done-text: #6fd497;
  --open-bg: #352713; --open-text: #f0b45d;

  --shadow: none;
}

/* ---------- 기본 ---------- */

:root { font-size: 17px; }
:root[data-fs="s"] { font-size: 15px; }
:root[data-fs="l"] { font-size: 19px; }
:root[data-fs="xl"] { font-size: 21px; }

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; scroll-padding-top: 4.5rem; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Pretendard", "Noto Sans KR", "Malgun Gothic", "Segoe UI", sans-serif;
  line-height: 1.75;
  word-break: keep-all;
  overflow-wrap: anywhere;
  -webkit-tap-highlight-color: transparent;
}

main {
  max-width: 46rem;
  margin: 0 auto;
  padding: 1rem max(16px, env(safe-area-inset-left)) calc(4rem + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-right));
}

a { color: var(--accent); }

/* ---------- 상단 바 ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  min-height: 3.25rem;
  padding: env(safe-area-inset-top) max(8px, env(safe-area-inset-right)) 0 max(8px, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.5) blur(12px);
  -webkit-backdrop-filter: saturate(1.5) blur(12px);
  border-bottom: 1px solid var(--border);
}

.brand { font-weight: 700; color: var(--text); text-decoration: none; padding: 0 0.5rem; }

.topbar-actions { display: flex; gap: 0.25rem; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.15rem;
  min-width: 2.75rem;
  min-height: 2.75rem;
  justify-content: center;
  padding: 0 0.4rem;
  border: 0;
  border-radius: 0.75rem;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 0.95rem;
  text-decoration: none;
  cursor: pointer;
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn svg { width: 1.35rem; height: 1.35rem; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn.back span { max-width: 50vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- 목록 화면 ---------- */

.page-title { font-size: 1.6rem; line-height: 1.35; margin: 0.5rem 0 1rem; }
.section-title { font-size: 0.85rem; font-weight: 600; color: var(--muted); margin: 1.75rem 0 0.6rem; letter-spacing: 0.02em; }

.card-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.6rem; }
.subject-grid { grid-template-columns: repeat(auto-fill, minmax(min(100%, 10rem), 1fr)); }

.card {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  padding: 0.9rem 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 1rem;
  box-shadow: var(--shadow);
  color: var(--text);
  text-decoration: none;
}
.card:active { background: var(--surface-2); }
.card.empty { opacity: 0.6; }
.card-title { font-weight: 650; line-height: 1.45; }
.card-summary { color: var(--muted); font-size: 0.9rem; line-height: 1.6; }
.card-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 0.35rem 0.75rem; color: var(--muted); font-size: 0.8rem; }

.badge { display: inline-block; padding: 0.05rem 0.5rem; border-radius: 999px; background: var(--accent-soft); color: var(--accent); font-size: 0.75rem; font-weight: 600; }
.badge-open { background: var(--open-bg); color: var(--open-text); }

.chip { display: inline-block; padding: 0.05rem 0.55rem; border-radius: 999px; background: var(--surface-2); color: var(--muted); font-size: 0.78rem; }

.empty-state { color: var(--muted); padding: 1rem 0; }

.search input {
  width: 100%;
  padding: 0.75rem 1rem;
  border: 1px solid var(--border);
  border-radius: 0.9rem;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 1rem;
  outline: none;
}
.search input:focus { border-color: var(--accent); }
.search .card-list { margin-top: 0.75rem; }
mark { background: var(--mark); color: inherit; border-radius: 0.2em; }

/* ---------- 노트 본문 ---------- */

.prose h1 { font-size: 1.6rem; line-height: 1.35; margin: 0.5rem 0 0.5rem; }
.prose h2 { font-size: 1.3rem; line-height: 1.4; margin: 2.5rem 0 0.75rem; padding-bottom: 0.35rem; border-bottom: 1px solid var(--border); }
.prose h3 { font-size: 1.1rem; line-height: 1.45; margin: 1.75rem 0 0.5rem; }
.prose h4 { font-size: 1rem; margin: 1.25rem 0 0.4rem; }
.prose p, .prose ul, .prose ol { margin: 0.6rem 0; }
.prose ul, .prose ol { padding-left: 1.4rem; }
.prose li { margin: 0.2rem 0; }
.prose hr { border: 0; border-top: 1px solid var(--border); margin: 2rem 0; }

.note-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 0.4rem; color: var(--muted); font-size: 0.82rem; margin-bottom: 1rem; }
.note-meta > span:first-child { margin-right: 0.35rem; }

.prose blockquote {
  margin: 1rem 0;
  padding: 0.6rem 1rem;
  border-left: 4px solid var(--border);
  border-radius: 0 0.75rem 0.75rem 0;
  background: var(--surface-2);
  color: var(--text);
}
.prose blockquote > :first-child { margin-top: 0; }
.prose blockquote > :last-child { margin-bottom: 0; }
.prose blockquote.callout > p:first-child > strong:first-child { display: block; margin-bottom: 0.15rem; }
.callout-def  { background: var(--def-bg) !important;  border-left-color: var(--def-line) !important; }
.callout-thm  { background: var(--thm-bg) !important;  border-left-color: var(--thm-line) !important; }
.callout-idea { background: var(--idea-bg) !important; border-left-color: var(--idea-line) !important; }
.callout-warn { background: var(--warn-bg) !important; border-left-color: var(--warn-line) !important; }

.prose code { font-family: ui-monospace, "SF Mono", "Cascadia Code", Consolas, monospace; font-size: 0.88em; background: var(--surface-2); padding: 0.1em 0.35em; border-radius: 0.35em; }
.prose pre { background: var(--surface-2); border: 1px solid var(--border); border-radius: 0.75rem; padding: 0.85rem 1rem; overflow-x: auto; line-height: 1.55; }
.prose pre code { background: none; padding: 0; font-size: 0.85rem; }
.prose pre.mermaid { background: var(--surface); text-align: center; }
.mermaid-failed { white-space: pre-wrap; text-align: left !important; }

.table-wrap { overflow-x: auto; margin: 1rem 0; border: 1px solid var(--border); border-radius: 0.75rem; }
.prose table { border-collapse: collapse; width: 100%; font-size: 0.92rem; }
.prose th, .prose td { padding: 0.5rem 0.75rem; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
.prose th { background: var(--surface-2); font-weight: 650; white-space: nowrap; }
.prose th[align="right"], .prose td[align="right"] { text-align: right; }
.prose th[align="center"], .prose td[align="center"] { text-align: center; }
.prose tr:last-child td { border-bottom: 0; }

/* 수식: 긴 블록 수식은 가로로 스크롤 */
.katex { font-size: 1.08em; }
.katex-display { overflow-x: auto; overflow-y: hidden; padding: 0.35rem 0; margin: 0.75rem 0; }
.katex-display > .katex { white-space: nowrap; }

/* 접는 블록: 답변, 정답, 목차 */
.prose details {
  margin: 0.9rem 0;
  border: 1px solid var(--border);
  border-radius: 0.85rem;
  background: var(--surface);
  overflow: hidden;
}
.prose details > summary {
  list-style: none;
  cursor: pointer;
  padding: 0.7rem 1rem;
  font-weight: 600;
  color: var(--accent);
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.prose details > summary::-webkit-details-marker { display: none; }
.prose details > summary::after {
  content: "";
  margin-left: auto;
  flex: none;
  width: 0.55rem;
  height: 0.55rem;
  border-right: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  transform: rotate(45deg) translateY(-2px);
  transition: transform 0.15s;
}
.prose details[open] > summary::after { transform: rotate(-135deg) translateX(-2px); }
.prose details[open] > summary { border-bottom: 1px solid var(--border); }
.prose details > :not(summary) { margin-left: 1rem; margin-right: 1rem; }
.prose details > :last-child { margin-bottom: 0.9rem; }

.toc { margin: 0 0 1.5rem !important; }
.toc ul { list-style: none; padding: 0.4rem 0 0 !important; }
.toc li { margin: 0; }
.toc a { display: block; padding: 0.35rem 0; color: var(--text); text-decoration: none; }
.toc .toc-3 a { padding-left: 1rem; color: var(--muted); font-size: 0.92rem; }

/* 질문 태그 */
.tag { display: inline-block; padding: 0 0.45rem; border-radius: 0.4rem; font-size: 0.8rem; font-weight: 700; line-height: 1.6; vertical-align: 0.08em; }
.tag-done { background: var(--done-bg); color: var(--done-text); }
.tag-open { background: var(--open-bg); color: var(--open-text); }

.broken-link { text-decoration: underline dotted; }

/* ---------- 이전/다음, 맨 위로 ---------- */

.pager { display: grid; grid-template-columns: 1fr 1fr; gap: 0.6rem; margin-top: 3rem; }
.pager .next { text-align: right; }
.pager-label { color: var(--muted); font-size: 0.78rem; }

.to-top {
  position: fixed;
  right: max(16px, env(safe-area-inset-right));
  bottom: calc(16px + env(safe-area-inset-bottom));
  width: 2.9rem;
  height: 2.9rem;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  color: var(--text);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}
.to-top.show { opacity: 1; pointer-events: auto; }
.to-top svg { width: 1.3rem; height: 1.3rem; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- 하단 탭 바 ---------- */

main { padding-bottom: calc(5.5rem + env(safe-area-inset-bottom)); }

.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: 1fr 1fr;
  padding: 0.25rem max(8px, env(safe-area-inset-right)) env(safe-area-inset-bottom) max(8px, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--bg) 90%, transparent);
  backdrop-filter: saturate(1.5) blur(12px);
  -webkit-backdrop-filter: saturate(1.5) blur(12px);
  border-top: 1px solid var(--border);
}
.tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.1rem;
  min-height: 3.1rem;
  justify-content: center;
  color: var(--muted);
  text-decoration: none;
  font-size: 0.75rem;
  font-weight: 600;
}
.tab svg { width: 1.45rem; height: 1.45rem; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
.tab.active { color: var(--accent); }

.to-top { bottom: calc(4.6rem + env(safe-area-inset-bottom)); }

/* ---------- 브리핑 ---------- */

.brief-card.latest { border-color: var(--accent); }
.brief-highlights { margin: 0.25rem 0 0; padding-left: 1.1rem; color: var(--muted); font-size: 0.9rem; line-height: 1.6; }
.brief-highlights li { margin: 0.1rem 0; }

.brief .prose h4 { font-size: 1.02rem; line-height: 1.5; margin: 1.5rem 0 0.35rem; }
.brief .prose h3 { margin-top: 2.25rem; color: var(--accent); }
.brief .prose td { white-space: nowrap; }
.brief .prose td:first-child { white-space: normal; }

.up { color: #d6334a; }
.down { color: #2f6fd6; }
.flat { color: var(--muted); }
:root[data-theme="dark"] .up { color: #ff7a8a; }
:root[data-theme="dark"] .down { color: #7aa8ff; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .up { color: #ff7a8a; }
  :root:not([data-theme="light"]) .down { color: #7aa8ff; }
}

.brief-source { color: var(--muted); font-size: 0.8rem; line-height: 1.6; }

@media (min-width: 720px) {
  .page-title, .prose h1 { font-size: 1.9rem; }
}
