:root {
  --bg: #0f1623;
  --bg-elev: #161f30;
  --bg-card: #1b2740;
  --border: #283856;
  --text: #e6edf6;
  --text-dim: #9fb0c8;
  --accent: #3b82f6;
  --accent-2: #38bdf8;
  --accent-soft: rgba(59, 130, 246, 0.15);
  --warn: #f59e0b;
  --good: #34d399;
  --sidebar-w: 290px;
  --header-h: 58px;
  --mono: ui-monospace, "Cascadia Code", Consolas, monospace;
  --sans: "Segoe UI", system-ui, "Noto Sans Thai", sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--sans);
  background: var(--bg);
  color: var(--text);
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
}

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 30;
  height: var(--header-h);
  display: flex; align-items: center; gap: 14px;
  padding: 0 18px;
  background: linear-gradient(90deg, #101a2e, #16233c);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: baseline; gap: 10px; }
.brand h1 { font-size: 1.25rem; margin: 0; letter-spacing: .3px; }
.brand .subtitle { color: var(--text-dim); font-size: .85rem; }
.badge {
  background: var(--accent-soft); color: var(--accent-2);
  border: 1px solid var(--accent); border-radius: 999px;
  font-size: .7rem; font-weight: 700; padding: 3px 10px; letter-spacing: .5px;
}
.menu-btn, .repo-link {
  background: none; border: none; color: var(--text);
  font-size: 1.4rem; cursor: pointer; text-decoration: none; line-height: 1;
}
.menu-btn { display: none; }
.repo-link { margin-left: auto; opacity: .8; }
.repo-link:hover { opacity: 1; }

/* ---------- Layout ---------- */
.layout { display: flex; min-height: calc(100vh - var(--header-h)); }

.sidebar {
  width: var(--sidebar-w); flex: 0 0 var(--sidebar-w);
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  position: sticky; top: var(--header-h);
  height: calc(100vh - var(--header-h));
  display: flex; flex-direction: column;
}
.search-wrap { padding: 14px; border-bottom: 1px solid var(--border); }
#search {
  width: 100%; padding: 9px 12px; border-radius: 8px;
  background: var(--bg); border: 1px solid var(--border); color: var(--text);
  font-size: .9rem; outline: none;
}
#search:focus { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }

.nav { overflow-y: auto; flex: 1; padding: 8px 8px 20px; }
.nav .group-title {
  font-size: .72rem; text-transform: uppercase; letter-spacing: .8px;
  color: var(--text-dim); padding: 14px 12px 6px; font-weight: 700;
}
.nav a {
  display: block; padding: 8px 12px; margin: 2px 4px;
  border-radius: 7px; color: var(--text); text-decoration: none;
  font-size: .92rem; border-left: 3px solid transparent;
}
.nav a:hover { background: var(--bg-card); }
.nav a.active {
  background: var(--accent-soft); border-left-color: var(--accent);
  color: #fff; font-weight: 600;
}
.nav a.hidden { display: none; }
.side-foot { padding: 12px 16px; border-top: 1px solid var(--border); color: var(--text-dim); }

.overlay {
  display: none; position: fixed; inset: var(--header-h) 0 0 0;
  background: rgba(0,0,0,.5); z-index: 19;
}

/* ---------- Content ---------- */
.content { flex: 1; min-width: 0; display: flex; justify-content: center; }
.article {
  max-width: 860px; width: 100%; padding: 38px 48px 90px;
}
.loading { color: var(--text-dim); padding: 40px 0; }

.article h1 {
  font-size: 2rem; margin: 0 0 6px; padding-bottom: 14px;
  border-bottom: 2px solid var(--border);
}
.article h2 {
  font-size: 1.4rem; margin: 36px 0 12px; color: var(--accent-2);
  padding-top: 8px;
}
.article h3 { font-size: 1.12rem; margin: 26px 0 10px; color: #cdd9ec; }
.article p { margin: 12px 0; }
.article ul, .article ol { padding-left: 24px; }
.article li { margin: 5px 0; }
.article strong { color: #fff; }
.article a { color: var(--accent-2); text-decoration: none; border-bottom: 1px dotted var(--accent-2); }
.article a:hover { border-bottom-style: solid; }
.article a.wikilink-dead { color: var(--text-dim); border-bottom: 1px dotted var(--text-dim); cursor: default; }

.article code {
  font-family: var(--mono); font-size: .88em;
  background: var(--bg-card); padding: 2px 6px; border-radius: 5px;
}
.article hr { border: none; border-top: 1px solid var(--border); margin: 30px 0; }
.article blockquote {
  margin: 16px 0; padding: 10px 18px; border-left: 4px solid var(--warn);
  background: var(--bg-elev); border-radius: 0 8px 8px 0; color: var(--text-dim);
}

/* Tables — important for ATPL */
.table-scroll { overflow-x: auto; margin: 16px 0; border-radius: 10px; border: 1px solid var(--border); }
.article table { border-collapse: collapse; width: 100%; font-size: .9rem; }
.article th, .article td { padding: 9px 14px; text-align: left; border-bottom: 1px solid var(--border); }
.article th { background: var(--bg-card); color: var(--accent-2); font-weight: 700; white-space: nowrap; }
.article tr:nth-child(even) td { background: rgba(255,255,255,.02); }
.article tbody tr:hover td { background: var(--accent-soft); }

/* Topic meta chip */
.topic-meta { display: flex; gap: 10px; flex-wrap: wrap; margin: 0 0 8px; }
.chip {
  font-size: .72rem; background: var(--bg-card); color: var(--text-dim);
  border: 1px solid var(--border); border-radius: 999px; padding: 3px 11px;
}

/* Home hero */
.hero-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px,1fr)); gap: 14px; margin-top: 22px; }
.hero-card {
  background: var(--bg-elev); border: 1px solid var(--border); border-radius: 12px;
  padding: 16px 18px; text-decoration: none; color: var(--text); display: block;
  transition: transform .12s, border-color .12s;
}
.hero-card:hover { transform: translateY(-2px); border-color: var(--accent); }
.hero-card h3 { margin: 0 0 6px; color: var(--accent-2); font-size: 1rem; }
.hero-card p { margin: 0; font-size: .85rem; color: var(--text-dim); line-height: 1.4; }

/* prev/next */
.pager { display: flex; justify-content: space-between; margin-top: 50px; gap: 12px; }
.pager a {
  flex: 1; background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: 10px; padding: 12px 16px; text-decoration: none; color: var(--text);
}
.pager a:hover { border-color: var(--accent); }
.pager .dir { font-size: .72rem; color: var(--text-dim); display: block; }
.pager .nxt { text-align: right; }

/* ---------- Mobile ---------- */
@media (max-width: 860px) {
  .menu-btn { display: block; }
  .brand .subtitle { display: none; }
  .sidebar {
    position: fixed; top: var(--header-h); left: 0; z-index: 20;
    transform: translateX(-100%); transition: transform .22s ease;
    box-shadow: 4px 0 24px rgba(0,0,0,.4);
  }
  .sidebar.open { transform: translateX(0); }
  .overlay.show { display: block; }
  .article { padding: 24px 18px 80px; }
  .article h1 { font-size: 1.6rem; }
}

@media print {
  .topbar, .sidebar, .overlay, .pager { display: none !important; }
  body { background: #fff; color: #000; }
  .article { max-width: none; padding: 0; }
  .article a { color: #000; border: none; }
  .article th { background: #eee; color: #000; }
}
