/* ── Base reset ── */
*, ::before, ::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
h1, h2, h3, h4, h5, h6 { font-size: inherit; font-weight: inherit; margin: 0; }
p { margin: 0; }
ul, ol { list-style: none; margin: 0; padding: 0; }
button {
  background: transparent; border: 0; padding: 0;
  color: inherit; font: inherit; cursor: pointer;
}
svg { display: block; }

/* ── Utility classes ── */
.font-sans { font-family: 'Inter', 'Noto Sans TC', sans-serif; }
.font-mono { font-family: 'Fira Code', monospace; }
.font-medium { font-weight: 500; }
.font-semibold { font-weight: 600; }
.min-h-screen { min-height: 100vh; }
.flex { display: flex; }
.flex-col { flex-direction: column; }
.flex-1 { flex: 1 1 0%; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.justify-center { justify-content: center; }
.hidden { display: none; }
.cursor-pointer { cursor: pointer; }
.p-1 { padding: 0.25rem; }
.px-8 { padding-left: 2rem; padding-right: 2rem; }
.py-8 { padding-top: 2rem; padding-bottom: 2rem; }
.py-6 { padding-top: 1.5rem; padding-bottom: 1.5rem; }
.mx-auto { margin-left: auto; margin-right: auto; }
.mt-4 { margin-top: 1rem; }
.gap-2 { gap: 0.5rem; }
.gap-2\.5 { gap: 0.625rem; }
.text-sm { font-size: 0.875rem; line-height: 1.25rem; }
.text-xs { font-size: 0.75rem; line-height: 1rem; }
.max-w-5xl { max-width: 64rem; }
.w-full { width: 100%; }
.w-7 { width: 1.75rem; }
.w-4 { width: 1rem; }
.w-64 { width: 16rem; }
.w-3\/4 { width: 75%; }
.w-5\/6 { width: 83.333333%; }
.w-2\/3 { width: 66.666667%; }
.h-8 { height: 2rem; }
.h-7 { height: 1.75rem; }
.h-4 { height: 1rem; }
.h-32 { height: 8rem; }
.rounded-lg { border-radius: 0.5rem; }
.space-y-4 > * + * { margin-top: 1rem; }

/* ── Theme Variables ── */
:root,
[data-theme="dark"] {
  --bg-body: #0f1117;
  --bg-sidebar: #0c0e14;
  --bg-surface: #1e2433;
  --bg-surface-alt: #13161e;
  --bg-code: #1a1f2e;
  --bg-inline-code: #1e293b;
  --bg-blockquote: #161a24;
  --bg-table-head: #1e2433;
  --bg-table-odd: #13161e;
  --bg-table-hover: #1e2433;
  --bg-skeleton-a: #1e2433;
  --bg-skeleton-b: #2a3142;
  --border-primary: #334155;
  --border-secondary: #2a3142;
  --border-sidebar: #1e293b;
  --text-primary: #F0EDE6;
  --text-body: #d4d0c8;
  --text-muted: #A8A29E;
  --text-dim: #6B6560;
  --gold: #D4AF37;
  --gold-light: #F5D680;
  --gold-dim: #8B7325;
  --gold-bg: rgba(212,175,55,0.15);
  --scrollbar-track: #1e2433;
  --scrollbar-thumb: #8B7325;
  --backdrop: rgba(0,0,0,0.6);
  --tab-active-bg: #1e2433;
  --tab-inactive: #A8A29E;
  --text-code: #d4d0c8;
  --gold-code: #D4AF37;
}

[data-theme="light"] {
  --bg-body: #ffffff;
  --bg-sidebar: #f6f7fb;
  --bg-surface: #e2e6f0;
  --bg-surface-alt: #eef0f6;
  --bg-code: #282c34;
  --bg-inline-code: #eef0f6;
  --bg-blockquote: #eef0f6;
  --bg-table-head: #e2e6f0;
  --bg-table-odd: #f6f7fb;
  --bg-table-hover: #eaecf3;
  --bg-skeleton-a: #e2e6f0;
  --bg-skeleton-b: #d0d5e2;
  --border-primary: #b0b8cc;
  --border-secondary: #d0d5e2;
  --border-sidebar: #d0d5e2;
  --text-primary: #111318;
  --text-body: #1f2937;
  --text-muted: #4b5563;
  --text-dim: #6b7280;
  --gold: #7c6210;
  --gold-light: #5c4a0c;
  --gold-dim: #a07e14;
  --gold-bg: rgba(124,98,16,0.08);
  --scrollbar-track: #e2e6f0;
  --scrollbar-thumb: #a07e14;
  --backdrop: rgba(0,0,0,0.3);
  --tab-active-bg: #e2e6f0;
  --tab-inactive: #4b5563;
  --text-code: #e8ecf4;
  --gold-code: #e5c07b;
}

/* ── Base ── */
html, body {
  background-color: var(--bg-body) !important;
  color: var(--text-primary) !important;
  overflow-x: hidden;
  transition: background-color 0.3s, color 0.3s;
}
* { scrollbar-width: thin; scrollbar-color: var(--scrollbar-thumb) var(--scrollbar-track); }
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: var(--scrollbar-track); }
::-webkit-scrollbar-thumb { background: var(--scrollbar-thumb); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--gold); }

/* ── Markdown content styles ── */
.md-content { overflow-wrap: break-word; word-break: break-word; }
.md-content h1 {
  font-size: 1.75rem; font-weight: 700; color: var(--gold);
  border-bottom: 1px solid var(--border-primary);
  padding-bottom: 0.75rem; margin-bottom: 1.5rem; margin-top: 0;
}
.md-content h2 {
  font-size: 1.35rem; font-weight: 600; color: var(--gold-light);
  margin-top: 2.5rem; margin-bottom: 1rem; padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--border-secondary);
}
.md-content h3 {
  font-size: 1.05rem; font-weight: 600; color: var(--text-primary);
  margin-top: 1.75rem; margin-bottom: 0.75rem;
}
.md-content p { color: var(--text-body); line-height: 1.75; margin-bottom: 0.75rem; }
.md-content strong { color: var(--text-primary); font-weight: 600; }
.md-content ul, .md-content ol { color: var(--text-body); padding-left: 1.5rem; margin-bottom: 1rem; }
.md-content li { margin-bottom: 0.35rem; line-height: 1.7; }
.md-content ul { list-style-type: disc; }
.md-content ol { list-style-type: decimal; }
.md-content code:not(pre code) {
  font-family: 'Fira Code', monospace; font-size: 0.85em;
  background: var(--bg-inline-code); color: var(--gold);
  padding: 0.15em 0.4em; border-radius: 4px;
  border: 1px solid var(--border-primary);
  word-break: break-all; overflow-wrap: anywhere;
  font-weight: 600;
}
.md-content pre {
  background: var(--bg-code) !important;
  border: 1px solid var(--border-primary);
  border-left: 3px solid var(--gold-code);
  border-radius: 8px; padding: 1rem 1.25rem;
  margin: 1rem 0 1.25rem; overflow-x: auto; position: relative;
}
.md-content pre code {
  font-family: 'Fira Code', monospace; font-size: 0.85rem; line-height: 1.6;
  background: transparent !important; padding: 0; border: none; color: var(--text-code);
}
.md-content pre code .hljs-comment,
.md-content pre code .hljs-quote {
  color: #6abf69 !important;
}
.copy-btn {
  position: absolute; top: 0.5rem; right: 0.5rem;
  padding: 0.25rem 0.5rem; border-radius: 4px;
  font-size: 0.7rem; font-family: 'Fira Code', monospace;
  background: var(--bg-surface); color: var(--text-muted);
  border: 1px solid var(--border-primary);
  cursor: pointer; opacity: 0; transition: opacity 0.2s, background 0.2s, color 0.2s;
  z-index: 1;
}
.md-content pre:hover .copy-btn { opacity: 1; }
.copy-btn:hover { background: var(--gold); color: #000; border-color: var(--gold); }
.copy-btn.copied { background: #22c55e; color: #fff; border-color: #22c55e; opacity: 1; }
.md-content table { width: 100%; border-collapse: collapse; margin: 1rem 0 1.5rem; font-size: 0.9rem; }
.md-content thead th {
  background: var(--bg-table-head); color: var(--gold); font-weight: 600;
  text-align: left; padding: 0.65rem 1rem; border-bottom: 2px solid var(--gold);
}
.md-content tbody td { padding: 0.55rem 1rem; border-bottom: 1px solid var(--border-secondary); color: var(--text-body); }
.md-content tbody tr:nth-child(odd) { background: var(--bg-table-odd); }
.md-content tbody tr:hover { background: var(--bg-table-hover); }
.md-content blockquote {
  border-left: 3px solid var(--gold); background: var(--bg-blockquote);
  padding: 0.75rem 1rem; margin: 1rem 0; color: var(--text-muted); border-radius: 0 6px 6px 0;
}
.md-content hr { border: none; border-top: 1px solid var(--border-primary); margin: 2rem 0; }
.md-content a { color: var(--gold); text-decoration: underline; text-underline-offset: 2px; }
.md-content a:hover { color: var(--gold-light); }

/* ── Mobile header ── */
.mobile-header {
  display: none; position: sticky; top: 0; z-index: 40;
  background: var(--bg-sidebar); border-bottom: 1px solid var(--border-sidebar);
  padding: 0.75rem 1rem; align-items: center; justify-content: space-between;
}

/* ── Sidebar ── */
.sidebar {
  position: fixed; top: 0; left: 0; width: 300px; height: 100vh;
  background: var(--bg-sidebar); border-right: 1px solid var(--border-sidebar);
  display: flex; flex-direction: column; z-index: 50; transition: transform 0.25s ease;
}
.sidebar-header { padding: 1.25rem 1rem; border-bottom: 1px solid var(--border-sidebar); }
.sidebar-nav { flex: 1; overflow-y: auto; padding: 0.5rem 0; }
.main-area { margin-left: 300px; }

.sidebar-backdrop {
  display: none; position: fixed; inset: 0;
  background: var(--backdrop); z-index: 45;
}
.sidebar-backdrop.open { display: block; }

/* ── Tab buttons ── */
.tab-btn {
  display: block; width: 100%; text-align: left;
  padding: 0.6rem 1rem; transition: color 0.2s, background-color 0.2s;
  border-left: 3px solid transparent;
  height: 42px; box-sizing: border-box;
}
.tab-btn:hover { background: var(--tab-active-bg); }
.tab-btn.active { color: var(--gold); background: var(--tab-active-bg); border-left-color: var(--gold); }

/* ── Score badge ── */
.score-badge {
  display: inline-flex; align-items: center; gap: 0.2rem;
  font-size: 0.65rem; font-family: 'Fira Code', monospace; font-weight: 600;
  padding: 0.1rem 0.4rem; border-radius: 4px; line-height: 1.4;
  white-space: nowrap; flex-shrink: 0;
}
.score-badge.grade-s { background: rgba(34,197,94,0.15); color: #22c55e; }
.score-badge.grade-a { background: rgba(59,130,246,0.15); color: #3b82f6; }
.score-badge.grade-b { background: rgba(245,158,11,0.15); color: #f59e0b; }
.score-badge.grade-c { background: rgba(239,68,68,0.15); color: #ef4444; }
.score-badge.grade-d { background: rgba(239,68,68,0.2); color: #ef4444; }
.score-badge.grade-na { background: rgba(107,114,128,0.15); color: var(--text-muted); }

/* ── Theme toggle button ── */
.theme-toggle {
  cursor: pointer; padding: 0.35rem;
  border-radius: 6px; border: 1px solid var(--border-sidebar);
  background: transparent; color: var(--text-muted);
  transition: background 0.2s, color 0.2s, border-color 0.2s;
  display: flex; align-items: center; justify-content: center;
}
.theme-toggle:hover { background: var(--tab-active-bg); color: var(--gold); border-color: var(--gold); }
.theme-toggle .icon-sun, .theme-toggle .icon-moon { width: 16px; height: 16px; }
[data-theme="dark"] .theme-toggle .icon-sun { display: block; }
[data-theme="dark"] .theme-toggle .icon-moon { display: none; }
[data-theme="light"] .theme-toggle .icon-sun { display: none; }
[data-theme="light"] .theme-toggle .icon-moon { display: block; }

/* ── Sidebar footer ── */
.sidebar-footer {
  padding: 0.75rem 1rem; border-top: 1px solid var(--border-sidebar);
  display: flex; align-items: center; justify-content: space-between;
}

/* ── Mobile RWD (< 768px) ── */
@media (max-width: 767px) {
  .mobile-header { display: flex; }
  .sidebar { transform: translateX(-100%); width: 260px; }
  .sidebar.open { transform: translateX(0); }
  .main-area { margin-left: 0; max-width: 100vw; overflow-x: hidden; }
  .main-area main { padding: 1rem 0.75rem !important; max-width: 100% !important; }
  .main-area footer > div {
    padding-left: 0.75rem !important; padding-right: 0.75rem !important;
    flex-direction: column; gap: 0.25rem;
  }
  .md-content { max-width: 100%; overflow-x: hidden; }
  .md-content h1 { font-size: 1.2rem; word-break: break-word; }
  .md-content h2 { font-size: 1.05rem; margin-top: 1.5rem; word-break: break-word; }
  .md-content h3 { font-size: 0.92rem; word-break: break-word; }
  .md-content p, .md-content li { font-size: 0.88rem; line-height: 1.6; }
  .md-content ul, .md-content ol { padding-left: 1.2rem; }
  .md-content pre {
    padding: 0.65rem; margin-left: -0.5rem; margin-right: -0.5rem;
    border-radius: 6px; max-width: calc(100vw - 1rem);
  }
  .md-content pre code { font-size: 0.72rem; white-space: pre; }
  .md-content code:not(pre code) { font-size: 0.75em; }
  .md-content table {
    font-size: 0.78rem; display: block; overflow-x: auto;
    -webkit-overflow-scrolling: touch; max-width: calc(100vw - 1.5rem);
  }
  .md-content thead th { padding: 0.4rem 0.5rem; white-space: nowrap; }
  .md-content tbody td { padding: 0.35rem 0.5rem; white-space: nowrap; }
  .sidebar-close-btn { display: block !important; }
}

/* ── Loading skeleton ── */
@keyframes shimmer {
  0% { background-position: -200% 0; }
  100% { background-position: 200% 0; }
}
.skeleton {
  background: linear-gradient(90deg, var(--bg-skeleton-a) 25%, var(--bg-skeleton-b) 50%, var(--bg-skeleton-a) 75%);
  background-size: 200% 100%; animation: shimmer 1.5s infinite; border-radius: 6px;
}

/* ── Fade in ── */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}
.fade-in { animation: fadeIn 0.3s ease forwards; }
