/* === Reset & Base === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: #f0f2f5;
  color: #1f2937;
  min-height: 100vh;
}

.app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* === Top Nav === */
.top-nav {
  background: white;
  border-bottom: 1px solid #e5e7eb;
  height: 56px;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 1px 3px rgba(0,0,0,0.06);
  display: flex;
  align-items: center;
}

.nav-inner {
  display: flex;
  align-items: center;
  width: 100%;
  max-width: 1164px; /* 256px sidebar + 18px gap + 816px worksheet + 2×24px padding + 26px extra */
  margin: 0 auto;
  padding: 0 24px;
  height: 100%;
}

.nav-logo {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 1.2rem;
  font-weight: 700;
  color: #111827;
  width: 256px;
  flex-shrink: 0;
  letter-spacing: -0.01em;
}

.nav-logo img {
  width: 28px;
  height: 28px;
  object-fit: contain;
  flex-shrink: 0;
}

.nav-tabs {
  display: flex;
  flex: 1;
  justify-content: flex-start;
}

.nav-tab {
  padding: 0 20px;
  height: 56px;
  display: flex;
  align-items: center;
  font-size: 0.875rem;
  font-weight: 500;
  color: #6b7280;
  cursor: pointer;
  text-decoration: none;
  border-bottom: 2px solid transparent;
  transition: color 0.15s;
  user-select: none;
}
.nav-tab:hover { color: #374151; }
.nav-tab.active {
  color: #DC2626;
  border-bottom-color: #DC2626;
}

/* === Main Layout === */
.main-layout {
  display: flex;
  gap: 18px;
  padding: 20px 24px;
  flex: 1;
  align-items: flex-start;
  max-width: 1164px;
  width: 100%;
  margin: 0 auto;
}

/* === Sidebar === */
.sidebar {
  width: 256px;
  flex-shrink: 0;
  background: white;
  border-radius: 12px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.08);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 76px;
}

.config-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid #f3f4f6;
}

.config-icon {
  width: 34px;
  height: 34px;
  background: #f3f4f6;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  color: #6b7280;
  flex-shrink: 0;
}

.config-title {
  font-size: 0.9rem;
  font-weight: 600;
  color: #111827;
}

.config-subtitle {
  font-size: 0.72rem;
  color: #9ca3af;
  margin-top: 1px;
}

.sidebar-content {
  padding: 14px 16px;
  flex: 1;
}

.section-label {
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.07em;
  color: #9ca3af;
  text-transform: uppercase;
  margin-top: 18px;
  margin-bottom: 10px;
}

.sidebar-content > .section-label:first-child { margin-top: 0; }

.form-group {
  margin-bottom: 10px;
}

.form-group > label {
  display: block;
  font-size: 0.76rem;
  color: #6b7280;
  margin-bottom: 4px;
}

.form-input {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid #e5e7eb;
  border-radius: 6px;
  font-size: 0.875rem;
  color: #1f2937;
  background: #f9fafb;
  outline: none;
  font-family: inherit;
  transition: border-color 0.15s, background 0.15s;
}
.form-input:focus {
  border-color: #DC2626;
  background: white;
  box-shadow: 0 0 0 3px rgba(220,38,38,0.10);
}

.form-select { cursor: pointer; }

.toggle-row {
  display: flex;
  gap: 6px;
  margin-bottom: 4px;
}

.toggle-btn {
  flex: 1;
  padding: 6px;
  border: 1px solid #e5e7eb;
  background: white;
  border-radius: 6px;
  font-size: 0.78rem;
  font-weight: 500;
  cursor: pointer;
  color: #6b7280;
  font-family: inherit;
  transition: all 0.15s;
}
.toggle-btn:hover:not(.active) { border-color: #9ca3af; color: #374151; }
.toggle-btn.active {
  background: #1f2937;
  color: white;
  border-color: #1f2937;
}

.gap-controls {
  display: flex;
  gap: 6px;
}

.gap-btn {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #e5e7eb;
  border-radius: 6px;
  font-size: 1rem;
  cursor: pointer;
  color: #6b7280;
  background: white;
  user-select: none;
  transition: all 0.15s;
}
.gap-btn:hover { border-color: #9ca3af; color: #1f2937; background: #f9fafb; }
.gap-btn:active { background: #f3f4f6; }

.checkbox-group {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.checkbox-group input[type="checkbox"] {
  width: 15px;
  height: 15px;
  cursor: pointer;
  accent-color: #DC2626;
  flex-shrink: 0;
}

.checkbox-group label {
  font-size: 0.8rem;
  color: #4b5563;
  cursor: pointer;
}

.sidebar-footer {
  padding: 14px 16px;
  border-top: 1px solid #f3f4f6;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.btn-generate-sidebar {
  width: 100%;
  padding: 10px;
  background: #4F46E5;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s;
}
.btn-generate-sidebar:hover { background: #4338CA; }
.btn-generate-sidebar:active { background: #3730A3; }

.btn-print-sidebar {
  width: 100%;
  padding: 10px;
  background: #0D9488;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s;
}
.btn-print-sidebar:hover { background: #0F766E; }
.btn-print-sidebar:active { background: #115E59; }

/* === Worksheet Area === */
.worksheet-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0;
}

/* Visual gap between pages on screen */
.page-gap {
  height: 24px;
  flex-shrink: 0;
}

/* === Worksheet Card (paper page) === */
.worksheet-card {
  background: white;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.10);
  padding: 40px 44px;
  position: relative;
  overflow: hidden;
  flex-shrink: 0;
}

/* A4: 210mm × 297mm at 96dpi */
.worksheet-card.paper-a4 {
  width: 794px;
  min-height: 1123px;
}

/* Letter: 8.5in × 11in at 96dpi */
.worksheet-card.paper-letter {
  width: 816px;
  min-height: 1056px;
}

.worksheet-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 32px;
}

#worksheet-title {
  font-size: 1.75rem;
  font-weight: 700;
  color: #111827;
  letter-spacing: -0.02em;
  margin-bottom: 5px;
}

#worksheet-subtitle {
  font-size: 0.78rem;
  color: #9ca3af;
  letter-spacing: 0.01em;
}

.worksheet-meta {
  display: flex;
  flex-direction: column;
  gap: 10px;
  text-align: right;
}

.meta-field {
  font-size: 0.7rem;
  font-weight: 600;
  color: #9ca3af;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.meta-line {
  width: 150px;
  height: 1px;
  background: #d1d5db;
  margin-top: 5px;
  margin-left: auto;
}

/* === Questions Grid === */
#questions-container,
.questions-page {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

.question-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  border-bottom: 1px solid #f3f4f6;
}

.q-num {
  font-size: 0.78rem;
  color: #d1d5db;
  min-width: 28px;
  flex-shrink: 0;
}

.q-expr-katex {
  font-size: 1.3rem;
  color: #111827;
  flex: 1;
}

/* === Page number indicator on subsequent pages === */
.page-indicator {
  font-size: 0.75rem;
  color: #d1d5db;
  text-align: right;
  margin-bottom: 16px;
  font-weight: 500;
  letter-spacing: 0.04em;
}

/* === Watermark === */
.watermark {
  position: absolute;
  bottom: 36px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 2.2rem;
  font-weight: 900;
  color: rgba(0,0,0,0.04);
  letter-spacing: 0.18em;
  pointer-events: none;
  user-select: none;
  white-space: nowrap;
}

/* === Shortcuts Bar === */
.shortcuts-bar {
  display: flex;
  justify-content: center;
  gap: 48px;
  padding: 14px;
  font-size: 0.78rem;
  color: #9ca3af;
}

kbd {
  background: #f3f4f6;
  border: 1px solid #d1d5db;
  border-radius: 4px;
  padding: 1px 6px;
  font-size: 0.72rem;
  font-family: monospace;
  color: #6b7280;
}

/* === Modal === */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 1000;
  align-items: center;
  justify-content: center;
}

.modal-box {
  background: white;
  border-radius: 12px;
  width: 90%;
  max-width: 540px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.2);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 22px;
  border-bottom: 1px solid #f3f4f6;
}

.modal-title {
  font-size: 1rem;
  font-weight: 600;
  color: #111827;
}

.modal-close {
  background: none;
  border: none;
  font-size: 1.4rem;
  color: #9ca3af;
  cursor: pointer;
  line-height: 1;
  padding: 2px 6px;
  border-radius: 4px;
}
.modal-close:hover { color: #374151; background: #f3f4f6; }

.modal-body {
  padding: 20px 22px;
  font-size: 0.875rem;
  color: #4b5563;
  line-height: 1.6;
}
.modal-body h4 { margin: 14px 0 6px; font-size: 0.9rem; color: #1f2937; }
.modal-body h4:first-child { margin-top: 0; }
.modal-body p { margin-bottom: 8px; }
.modal-body a { color: #2563eb; }

.modal-footer {
  padding: 14px 22px;
  border-top: 1px solid #f3f4f6;
  display: flex;
  justify-content: flex-end;
}

.btn-modal-close {
  padding: 7px 18px;
  background: #f3f4f6;
  border: none;
  border-radius: 6px;
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  font-family: inherit;
  color: #374151;
}
.btn-modal-close:hover { background: #e5e7eb; }

/* === Answer Sheet (print only) === */
.hideFromScreen { display: none; }

.print-answer {
  padding: 24px;
}

.answer-logo {
  font-size: 1.5rem;
  font-weight: 700;
  color: #DC2626;
  margin-bottom: 8px;
}

.print-answer h3 { margin: 8px 0; }

#answers-container {
  display: flex;
  flex-wrap: wrap;
}

#answers-container > div {
  width: 140px;
  font-size: 1em;
}

/* === Algebra Questions: 2-column Grid Layout === */
.algebra-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  column-gap: 0;
  row-gap: 0;
}

/* === PEMDAS2: Workspace Layout === */
.pemdas2-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  border-top: 1px solid #e5e7eb;
  border-left: 1px solid #e5e7eb;
}

.pemdas2-question {
  padding: 16px 20px 12px;
  border-right: 1px solid #e5e7eb;
  border-bottom: 1px solid #e5e7eb;
  min-height: 150px;
  display: flex;
  flex-direction: column;
}

.pemdas2-header {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 6px;
}

.pemdas2-expr {
  font-family: 'Roboto Mono', monospace;
  font-size: 1.05rem;
  color: #111827;
  line-height: 1.6;
}

.pemdas2-work {
  flex: 1;
}

.pemdas2-answer-line {
  width: 130px;
  height: 1.5px;
  background: #374151;
  margin-top: 8px;
}

.algebra-question {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 4px;
  border-bottom: 1px solid #f3f4f6;
  font-family: 'Roboto Mono', monospace;
  flex-wrap: nowrap;
  min-width: 0;
}

.alg-num {
  font-size: 0.78rem;
  color: #d1d5db;
  min-width: 28px;
  flex-shrink: 0;
}

.alg-expr {
  font-size: 1rem;
  color: #111827;
  flex: 1;
}

.alg-blank {
  display: inline-block;
  width: 80px;
  border-bottom: 1.5px solid #374151;
  margin-left: 6px;
  flex-shrink: 0;
}

/* === Systems of Equations Layout === */
.systems-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  border-top: 1px solid #e5e7eb;
  border-left: 1px solid #e5e7eb;
}

.systems-question {
  padding: 12px 16px 10px;
  border-right: 1px solid #e5e7eb;
  border-bottom: 1px solid #e5e7eb;
  min-height: 90px;
  display: flex;
  flex-direction: column;
}

.systems-header {
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

.systems-equations {
  font-family: 'Roboto Mono', monospace;
  font-size: 0.95rem;
  color: #111827;
  line-height: 1.8;
  flex: 1;
}

.systems-answer-line {
  width: 160px;
  height: 1.5px;
  background: #374151;
  margin-top: 10px;
}

/* === KaTeX math rendering === */
.katex { font-size: 1em; }
.alg-expr .katex-display { display: inline; margin: 0; }

/* === Print Styles === */
@page { size: letter; margin: 0; }

@media print {
  .dontprint { display: none !important; }
  .shortcuts-bar { display: none !important; }
  .hideFromScreen { display: block !important; }

  body { background: white; }

  .app {
    display: block;
    min-height: 0;
  }

  .main-layout {
    display: block;
    padding: 0;
  }

  .worksheet-wrapper {
    display: block;
  }

  .page-gap { display: none; }

  .worksheet-card {
    box-shadow: none;
    border-radius: 0;
    padding: 45px 57px;
    overflow: visible;
  }

  .worksheet-page-break {
    page-break-before: always;
    break-before: page;
  }

  .worksheet-card.paper-a4,
  .worksheet-card.paper-letter {
    width: 100%;
    min-height: unset;
  }

  .questions-page {
    grid-template-columns: 1fr 1fr;
  }

  .question-card,
  .algebra-question,
  .pemdas2-question {
    break-inside: avoid;
    page-break-inside: avoid;
  }

  .print-answer {
    page-break-before: always;
    break-before: page;
  }
}
