/* === Global Reset === */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  background: #f5f5f5;
  font-family: 'Patrick Hand', cursive;
  color: #333;
  line-height: 1.6;
  min-height: 100vh;
  padding: 16px 0;
  /* Base font size for rem units with responsive scaling */
  font-size: 16px;
}

/* Responsive base font size adjustments */
@media (max-width: 900px) {
  html, body {
    font-size: 15px;
  }
}

@media (max-width: 700px) {
  html, body {
    font-size: 14px;
  }
}

@media (max-width: 500px) {
  html, body {
    font-size: 13px;
  }
}

/* === Notebook Page Layout === */
.notebook-page {
  max-width: 900px;
  margin: 0 auto;
  background: #fff;
  position: relative;
  padding: 40px 30px 60px 50px; /* Extra left padding for margin line alignment */
  border-radius: 6px;
  box-shadow: 0 0 16px rgba(0, 0, 0, 0.15);
  background-image: linear-gradient(to bottom, #e3eafe 1px, transparent 1px);
  background-size: 100% 32px; /* Simulates ruled notebook lines */
}

/* === Red Margin Line (left edge) === */
.margin-line {
  position: absolute;
  left: 35px; /* Aligns with content despite 50px left padding */
  top: 0;
  bottom: 0;
  width: 5px;
  background: linear-gradient(to right, #ff6666 0% 35%, #ff9999 35% 80%, #ff6666 80% 100%);
  border-radius: 2px;
}

/* === Headings === */
h1 {
  font-family: 'Caveat', cursive;
  text-align: center;
  margin: 0 0 30px 0;
  font-size: 2.2rem;
  text-decoration: underline;
  font-weight: 700;
  color: #3e7708; /* Default green */
}

h1.blue {
  color: #0609c4; /* Alternate blue heading */
}

/* === Exercise Structure === */
.exercise {
  margin-bottom: 30px;
  padding-left: 10px;
}

/* Question styling */
.question {
  color: #850324; /* Dark maroon/red */
  font-weight: bold;
  font-size: 1.6rem;
  line-height: 1.4;
  margin-bottom: 12px;
}

/* Ensure inline elements inside .question inherit size */
.question p,
.question strong,
.question em,
.question .part {
  font-size: inherit;
  line-height: inherit;
}

/* Solution block */
.solution {
  color: #000080; /* Navy blue */
  font-size: 1.5rem;
  margin-left: 15px;
  margin-bottom: 20px;
  line-height: 1.6;
  background: linear-gradient(180deg, rgba(0, 0, 0, 0.01), transparent);
  padding: 10px 12px;
  border-radius: 6px;
}

/* Nested elements in solution inherit style */
.solution p,
.solution .step,
.solution li,
.solution div,
.solution table,
.solution td,
.solution th,
.solution pre {
  color: inherit;
  font-size: inherit;
}

/* Part labels (e.g., (a), (b)) */
.part {
  display: inline-block;
  color: #b80c0c; /* Red */
  font-size: 1.5rem;
}

/* Step-by-step working */
.step {
  margin: 10px 0;
  padding-left: 12px;
}

/* Highlight important terms */
.important-term {
  display: inline-block;
  background: #92e6263d; /* Semi-transparent light green */
  border-left: 4px solid #ff6f00; /* Orange accent */
  padding: 4px 8px;
  margin: 2px 0;
  border-radius: 3px;
  font-weight: 700;
}

/* Formula/code block */
.formula {
  display: block;
  font-family: 'Courier New', monospace;
  background: #f4f7fb;
  padding: 8px 10px;
  margin: 10px 0;
  border-radius: 6px;
  border: 1px dashed #cfd8e3;
  overflow-x: auto;
  max-width: 100%;
  white-space: normal;
  overflow-wrap: anywhere;
  word-break: break-word;
}

/* Final answer highlight */
.final-answer {
  background-color: #e6f7ff;
  padding: 12px 14px;
  border-radius: 6px;
  border: 2px solid #000080;
  margin: 12px 0;
  font-weight: 700;
}

/* Note/hint box */
.note {
  background: #fff8e1;
  border-left: 4px solid #ffb300;
  padding: 8px 12px;
  border-radius: 4px;
  margin: 8px 0;
}

/* Multiple solution options */
.mult-solutions {
  margin-top: 8px;
}

.mult-solutions .option {
  margin: 8px 0;
  padding: 50px;
  border-radius: 6px;
  border: 1px solid #e0e7ff;
  background: #fafcffde; /* Light translucent blue */
}

.mult-solutions .option .label {
  font-weight: 700;
  margin-bottom: 6px;
  color: #0b333d; /* Dark teal */
}

/* Figure/diagram wrapper */
.figure {
  display: block;
  margin: 12px auto;
  text-align: center;
}

.figure caption {
  font-style: italic;
  margin-top: 6px;
  color: #666;
}

/* Math container with horizontal scroll */
.math {
  overflow-x: auto;
  padding: 10px 0;
}

.math::-webkit-scrollbar {
  height: 5px;
}

.math::-webkit-scrollbar-thumb {
  background: #3498db;
  border-radius: 10px;
}

/* Table styling */
table {
  border-collapse: collapse;
  margin: 12px 0;
  width: 100%;
}

table.factor,
table.step-table {
  margin: 8px 0;
}

table td,
table th,
table.step-table td {
  border: 1px solid #d6d6d6;
  padding: 8px 10px;
  text-align: center;
  vertical-align: middle;
}

table th {
  background-color: #f0f8ff; /* Light blue header */
}

/* Utility classes */
.small-muted {
  color: #666;
  font-size: 0.95rem;
}

.small-step {
  margin: 6px 0;
}

.image-placeholder {
  width: 100%;
  height: 200px;
  border: 1px dashed #f8f8f805;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 15px;
  color: #eee9e97a;
  font-family: sans-serif;
  font-size: 14px;
  text-align: center;
}

/* === MathJax Overflow Protection === */
mjx-container,
.mjx-itable,
.mjx-mrow,
.mjx-mn,
.mjx-mi,
.mjx-mo,
.mjx-mfrac,
.mjx-msqrt {
  max-width: 100% !important;
  white-space: normal !important;
  overflow-wrap: anywhere !important;
  word-break: break-word !important;
  display: inline-block;
}

/* Responsive images */
.solution img,
.figure img {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 0 auto;
}

/* Print-friendly styles */
@media print {
  body {
    background: #fff;
    color: #000;
  }
  .notebook-page {
    box-shadow: none;
    background: transparent;
  }
  .margin-line {
    display: none;
  }
  .final-answer {
    page-break-inside: avoid;
  }
}

/* === Responsive Adjustments === */
@media (max-width: 700px) {
  .notebook-page {
    padding: 22px 16px 36px 36px;
  }

  .question,
  .solution,
  .part {
    font-size: 1.3rem;
  }

  .formula {
    font-size: 0.95rem;
    padding: 6px 8px;
  }
}

@media (max-width: 520px) {
  .question {
    font-size: 1.1rem;
  }
  .solution {
    font-size: 1.05rem;
  }
}

@media (max-width: 500px) {
  .notebook-page {
    padding: 18px 12px 28px 28px;
  }

  h1 {
    font-size: 1.8rem;
  }

  /* Override 520px rule for consistency on smallest screens */
  .question,
  .solution,
  .part {
    font-size: 1.2rem;
  }
}