/* ==========================================================================
   Math Book Layout — Screen & Print Stylesheet
   Compatible with MathJax 3 SVG and Thai Typography (Sarabun)
   ========================================================================== */

:root {
  --bg: #f7f8fc;
  --panel: #ffffff;
  --ink: #1c2140;
  --muted: #5e6483;
  --line: #dfe3f0;
  --accent: #3b4fd8;
  --accent2: #ff7a1a;
  --c1: #8fb3ff;
  --c2: #ffc074;
  --c3: #9fe0b2;
  --c4: #ff9ecb;
  --code: #eef1fa;
  --good: #1c9a5b;
  --bad: #d64545;
  --shadow: 0 6px 24px rgba(30, 40, 90, 0.07);
  --sidebar-w: 290px;
}

:root[data-theme="night"] {
  --bg: #0b0a1c;
  --panel: #161433;
  --ink: #eeeaff;
  --muted: #b3acd6;
  --line: #2b2750;
  --accent: #b49cff;
  --accent2: #ffb15c;
  --c1: #5f7fe0;
  --c2: #e0964a;
  --c3: #4fb37a;
  --c4: #e06ea6;
  --code: #1e1b40;
  --good: #5fd39a;
  --bad: #ff7b7b;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.4);
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: Sarabun, "Noto Sans Thai", "Leelawadee UI", Tahoma, sans-serif;
  font-size: 17.5px;
  line-height: 1.7;
  transition: background 0.2s ease, color 0.2s ease;
}

a {
  color: var(--accent);
  text-decoration: none;
}

/* MathJax SVG styling & font synchronization */
mjx-container[jax="SVG"] {
  font-family: inherit !important;
  font-size: 105%;
  margin: 0.1em 0.2em;
  vertical-align: -0.15em;
}

mjx-container[jax="SVG"][display="true"] {
  margin: 0.8em 0;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 4px 0;
}

/* --------------------------------------------------------------------------
   Layout & Universal Collapsible Sidebar
   -------------------------------------------------------------------------- */
.layout {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: 100vh;
  transition: grid-template-columns 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

body.sidebar-collapsed {
  --sidebar-w: 0px;
}

nav.side {
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  background: var(--panel);
  border-right: 1px solid var(--line);
  padding: 18px 14px 40px;
  width: var(--sidebar-w);
  transition: width 0.25s ease, opacity 0.2s ease, padding 0.25s ease;
  white-space: nowrap;
}

body.sidebar-collapsed nav.side {
  padding: 0;
  border-right: none;
  opacity: 0;
  pointer-events: none;
  overflow: hidden;
}

nav.side .side-head {
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

nav.side .brand {
  flex: 1;
  font-weight: 800;
  font-size: 1.05em;
  color: var(--accent);
  margin: 4px 8px 14px;
  line-height: 1.3;
  white-space: normal;
}

.side-close {
  display: none;
  font: inherit;
  font-size: 1em;
  line-height: 1;
  cursor: pointer;
  background: var(--code);
  border: 1px solid var(--line);
  border-radius: 999px;
  width: 32px;
  height: 32px;
  flex: 0 0 auto;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  margin: 4px 0 0;
}

.side-close:hover,
.side-close:active {
  background: color-mix(in srgb, var(--accent) 14%, transparent);
  border-color: var(--accent);
  color: var(--accent);
}

nav.side a {
  display: block;
  color: var(--ink);
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 0.92em;
  margin: 2px 0;
  text-overflow: ellipsis;
  overflow: hidden;
}

nav.side a.l2 {
  padding-left: 24px;
  font-size: 0.86em;
  color: var(--muted);
}

nav.side a:hover {
  background: var(--code);
  color: var(--accent);
}

nav.side a.active {
  background: color-mix(in srgb, var(--accent) 14%, transparent);
  color: var(--accent);
  font-weight: 700;
}

nav.side .grp {
  margin-top: 14px;
  margin-bottom: 4px;
  font-size: 0.74em;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--muted);
  padding: 0 10px;
}

/* --------------------------------------------------------------------------
   Main Content Container & Topbar
   -------------------------------------------------------------------------- */
main {
  padding: 24px 48px 120px;
  max-width: 1020px;
  min-width: 0;
  width: 100%;
  margin: 0 auto;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: sticky;
  top: 0;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  padding: 10px 0;
  z-index: 50;
  border-bottom: 1px solid var(--line);
  margin-bottom: 24px;
}

.topbar-left, .topbar-right {
  display: flex;
  gap: 10px;
  align-items: center;
}

button.tbtn {
  font: inherit;
  font-size: 0.88em;
  cursor: pointer;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 14px;
  color: var(--ink);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.03);
  transition: background 0.15s ease, border-color 0.15s ease;
}

button.tbtn:hover {
  background: var(--code);
  border-color: var(--accent);
  color: var(--accent);
}

/* --------------------------------------------------------------------------
   Document Header / Hero
   -------------------------------------------------------------------------- */
.hero {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 28px 34px;
  margin-bottom: 32px;
  box-shadow: var(--shadow);
}

.hero .badge-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}

.kicker {
  display: inline-block;
  background: var(--accent);
  color: #ffffff;
  border-radius: 999px;
  padding: 2px 14px;
  font-size: 0.82em;
  font-weight: 700;
}

.kicker.subtle {
  background: var(--code);
  color: var(--muted);
  border: 1px solid var(--line);
}

.hero h1 {
  font-size: 2em;
  line-height: 1.3;
  margin: 0.2em 0 0.4em;
  color: var(--accent);
}

.hero .subject {
  font-size: 1.1em;
  font-weight: 600;
  color: var(--muted);
  margin: 0;
}

/* --------------------------------------------------------------------------
   Core Concepts Card
   -------------------------------------------------------------------------- */
.topic-head {
  margin-top: 48px;
  padding-top: 20px;
}

h2 {
  font-size: 1.7em;
  margin: 0.3em 0 0.5em;
  line-height: 1.3;
  color: var(--ink);
}

h3 {
  font-size: 1.3em;
  margin: 1.4em 0 0.6em;
  color: var(--accent);
}

.summary.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 24px 30px;
  box-shadow: var(--shadow);
  margin-bottom: 30px;
}

.summary-block {
  margin-bottom: 20px;
}

.summary-block:last-child {
  margin-bottom: 0;
}

.summary-block h4 {
  font-size: 1.05em;
  font-weight: 700;
  color: var(--accent2);
  margin: 0 0 8px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.summary-block ul {
  margin: 0;
  padding-left: 22px;
}

.summary-block li {
  margin: 6px 0;
}

/* --------------------------------------------------------------------------
   Guided Examples & Solution Workspace
   -------------------------------------------------------------------------- */
.example {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 22px 28px;
  margin: 20px 0;
  box-shadow: var(--shadow);
}

.ex-head {
  font-weight: 800;
  font-size: 1.05em;
  color: var(--accent2);
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.problem {
  font-weight: 500;
  font-size: 1.02em;
  line-height: 1.7;
}

.workspace-box {
  margin-top: 16px;
  border: 1.5px dashed var(--line);
  border-radius: 14px;
  padding: 14px 18px;
  background: color-mix(in srgb, var(--code) 40%, transparent);
}

.workspace-label {
  font-size: 0.85em;
  font-weight: 700;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 6px;
  user-select: none;
}

.workspace-inner {
  min-height: 90px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 0.9em;
  font-style: italic;
}

/* --------------------------------------------------------------------------
   Practice Problems & Tiered Categories
   -------------------------------------------------------------------------- */
.level-section {
  margin: 40px 0 24px;
}

.lvl-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
}

.tag.lvl {
  display: inline-block;
  font-size: 0.82em;
  font-weight: 800;
  padding: 4px 14px;
  border-radius: 999px;
  text-transform: uppercase;
}

.tag.lvl-medium {
  background: color-mix(in srgb, var(--accent) 18%, transparent);
  color: var(--accent);
  border: 1px solid color-mix(in srgb, var(--accent) 30%, transparent);
}

.tag.lvl-hard {
  background: color-mix(in srgb, var(--accent2) 18%, transparent);
  color: var(--accent2);
  border: 1px solid color-mix(in srgb, var(--accent2) 30%, transparent);
}

.tag.lvl-challenging {
  background: color-mix(in srgb, var(--bad) 18%, transparent);
  color: var(--bad);
  border: 1px solid color-mix(in srgb, var(--bad) 30%, transparent);
}

.item {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 18px 24px;
  margin: 14px 0;
  box-shadow: var(--shadow);
}

.item-head {
  display: flex;
  gap: 8px;
  align-items: flex-start;
}

.inum {
  font-weight: 800;
  color: var(--accent);
  font-size: 1.05em;
  min-width: 28px;
}

.item-body {
  flex: 1;
}

/* --------------------------------------------------------------------------
   Responsive Web Media Queries
   -------------------------------------------------------------------------- */
@media (max-width: 900px) {
  :root {
    --sidebar-w: 260px;
  }
  body:not(.sidebar-collapsed) nav.side {
    position: fixed;
    z-index: 100;
    box-shadow: 0 0 30px rgba(0, 0, 0, 0.2);
  }
  .side-close {
    display: inline-flex;
  }
  main {
    padding: 16px 20px 80px;
  }
}

/* --------------------------------------------------------------------------
   Book Print Media Queries (@media print)
   Conforming to Invariant 2 & CSS Paged Media standards
   -------------------------------------------------------------------------- */
@page {
  size: A4 portrait;
  margin: 18mm 15mm 20mm 15mm;
  @bottom-center {
    content: "— " counter(page) " —";
    font-family: Sarabun, sans-serif;
    font-size: 9pt;
    color: #444444;
  }
  @top-left {
    content: "เอกสารติวเข้มคณิตศาสตร์ สอบเข้า ม.1 (Gifted / GATE)";
    font-family: Sarabun, sans-serif;
    font-size: 8pt;
    color: #777777;
  }
}

@media print {
  /* Hide all interactive UI chrome */
  nav.side,
  .topbar,
  button,
  .tbtn,
  .web-only {
    display: none !important;
  }

  body {
    background: #ffffff !important;
    color: #000000 !important;
    font-size: 13.5pt;
    line-height: 1.55;
  }

  .layout {
    display: block !important;
    width: 100% !important;
  }

  main {
    padding: 0 !important;
    max-width: 100% !important;
    margin: 0 !important;
  }

  .hero,
  .summary.card,
  .example,
  .item {
    background: #ffffff !important;
    border: 1px solid #c8ccd6 !important;
    box-shadow: none !important;
    border-radius: 10px !important;
    break-inside: avoid;
    page-break-inside: avoid;
  }

  .hero {
    padding: 16px 20px !important;
    margin-bottom: 18px !important;
  }

  .hero h1 {
    font-size: 1.6em !important;
    color: #000000 !important;
  }

  .topic-head {
    break-before: page;
    page-break-before: always;
    margin-top: 0 !important;
    padding-top: 0 !important;
  }

  h2, h3, h4 {
    color: #000000 !important;
    break-after: avoid;
    page-break-after: avoid;
  }

  /* Physical ruled solution box for handwriting */
  .workspace-box {
    display: block !important;
    min-height: 140px !important;
    border: 1px dashed #888888 !important;
    background: repeating-linear-gradient(
      transparent,
      transparent 25px,
      #e8eaee 26px
    ) !important;
    margin-top: 12px !important;
    padding: 10px 14px !important;
  }

  .workspace-inner {
    display: none !important;
  }

  .workspace-label {
    color: #555555 !important;
    font-size: 9pt !important;
  }

  mjx-container[jax="SVG"] {
    color: #000000 !important;
  }
}
