@import url("https://fonts.googleapis.com/css2?family=Be+Vietnam+Pro:ital,wght@0,300;0,400;0,500;0,600;1,300;1,400&family=Cormorant+Garamond:ital,wght@0,400;0,500;0,600;1,400;1,500&family=JetBrains+Mono:wght@400;500&display=swap");

/* ====================================================================
   Design tokens
   ==================================================================== */
:root {
  --bg: #fff8fa;
  --sidebar-bg: #ffffff;
  --sidebar-text: #2d0f15;
  --sidebar-muted: #9a6a75;
  --sidebar-border: #f1d2d8;
  --accent: #d21f43;
  --accent-hover: #b81839;
  --header-bg: #6f1128;
  --panel-header-bg: #ffffff;
  --editor-bg: #fffdfd;
  --preview-bg: #ffffff;
  --border: #f1d2d8;
  --text: #2d0f15;
  --text-muted: #8f4b58;
  --q-accent: #cd2447;
  --correct-bg: #fff2f5;
  --correct-border: #c31f41;

  --font-serif: "Times New Roman", serif;
  --font-display: "Cormorant Garamond", Georgia, "Times New Roman", serif;
  --font-ui:
    "Be Vietnam Pro", system-ui, -apple-system, "Segoe UI", Arial, sans-serif;
  --font-mono: "JetBrains Mono", "Fira Code", "Courier New", monospace;

  --radius: 6px;
  --tab-h: 38px;
}

/* ====================================================================
   Reset
   ==================================================================== */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}
[x-cloak] {
  display: none !important;
}

/* ====================================================================
   Body grid  (1 : 2 : 2)
   ==================================================================== */
html,
body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: var(--font-ui);
  font-size: 13.5px;
  background: var(--bg);
  color: var(--text);
  display: grid;
  grid-template-columns: 1fr 2fr 2fr;
  grid-template-rows: 46px 1fr;
  grid-template-areas:
    "header  header  header"
    "sidebar editor  preview";
  height: 100vh;
}

/* ====================================================================
   Header
   ==================================================================== */
#header {
  grid-area: header;
  background: var(--header-bg);
  display: flex;
  align-items: center;
  padding: 0 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

#header h1 {
  font-family: var(--font-display);
  font-size: 1.3rem;
  font-weight: 500;
  color: #fff2f4;
  letter-spacing: 0.015em;
}

#header h1 em {
  color: #ffd9e1;
  font-style: italic;
}

/* ====================================================================
   Sidebar
   ==================================================================== */
aside#sidebar {
  grid-area: sidebar;
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  border-right: 1px solid var(--border);
}

.sb-section {
  padding: 16px 15px;
  border-bottom: 1px solid var(--sidebar-border);
}
.sb-section:last-child {
  border-bottom: none;
}

.sb-heading {
  font-size: 0.63rem;
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--sidebar-muted);
  margin-bottom: 13px;
}

.sb-field {
  margin-bottom: 9px;
}

.sb-label {
  display: block;
  font-size: 0.72rem;
  font-weight: 500;
  color: var(--text-muted);
  margin-bottom: 4px;
  letter-spacing: 0.01em;
}

.sb-select,
.sb-input {
  display: block;
  width: 100%;
  background: #ffffff;
  border: 1px solid #e5c5cd;
  color: var(--sidebar-text);
  border-radius: var(--radius);
  padding: 7px 10px;
  font-family: var(--font-ui);
  font-size: 0.8rem;
  transition:
    border-color 0.15s,
    background 0.15s;
  appearance: none;
  -webkit-appearance: none;
}
.sb-select:focus,
.sb-input:focus {
  outline: none;
  border-color: var(--accent);
  background: #fff6f8;
}
.sb-select option {
  background: #ffffff;
  color: var(--text);
}

/* Remove number input spin arrows */
.sb-input[type="number"] {
  -moz-appearance: textfield;
}
.sb-input[type="number"]::-webkit-outer-spin-button,
.sb-input[type="number"]::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

/* Inline seed row */
.sb-seed-row {
  display: flex;
  align-items: center;
  gap: 8px;
}
.sb-seed-row .sb-label {
  margin: 0;
  white-space: nowrap;
  flex-shrink: 0;
}
.sb-seed-row .sb-input {
  flex: 1;
}

/* Buttons */
.sb-btn {
  display: block;
  width: 100%;
  border: none;
  border-radius: var(--radius);
  padding: 8px 14px;
  font-family: var(--font-ui);
  font-size: 0.82rem;
  font-weight: 500;
  cursor: pointer;
  transition:
    background 0.15s,
    opacity 0.15s,
    transform 0.08s;
  text-align: center;
  letter-spacing: 0.01em;
}
.sb-btn:active {
  transform: scale(0.98);
}
.sb-btn:disabled {
  opacity: 0.42;
  cursor: not-allowed;
  transform: none;
}

.sb-btn-primary {
  background: var(--accent);
  color: #fff;
}
.sb-btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
}

.sb-btn-secondary {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
}
.sb-btn-secondary:hover:not(:disabled) {
  color: var(--accent);
  border-color: #e6a9b8;
  background: #fff4f7;
}

/* Sidebar: stack buttons with a small gap between them */
.sb-section .sb-btn + .sb-btn {
  margin-top: 6px;
}

/* Sidebar divider */
.sb-divider {
  height: 1px;
  background: var(--border);
  margin: 14px 0 12px;
}

/* Status indicator */
.sb-status {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 10px;
  min-height: 16px;
}
.sb-status-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  flex-shrink: 0;
  transition: background 0.2s;
}
.sb-status-dot.ok {
  background: #4f9f65;
}
.sb-status-dot.err {
  background: #d94f4f;
}
.sb-status-text {
  font-size: 0.69rem;
  color: var(--text-muted);
  line-height: 1.3;
}

/* ====================================================================
   Shared panel header
   ==================================================================== */
.panel-header {
  display: flex;
  align-items: center;
  height: var(--tab-h);
  padding: 0 13px;
  background: var(--panel-header-bg);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  gap: 8px;
}
.panel-label {
  font-size: 0.67rem;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.panel-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 8px;
}

.panel-action-btn {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 3px 10px;
  font-family: var(--font-ui);
  font-size: 0.72rem;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.15s;
}

.panel-action-btn:hover:not(:disabled) {
  border-color: var(--accent);
  color: var(--accent);
  background: #fff6f8;
}

.panel-action-btn:disabled {
  opacity: 0.48;
  cursor: not-allowed;
}

/* ====================================================================
   Editor panel
   ==================================================================== */
#editor-panel {
  grid-area: editor;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border-right: 1px solid var(--border);
  background: var(--editor-bg);
}

#editor-panel,
#editor-container {
  min-width: 0;
  min-height: 0;
}

#editor-container {
  flex: 1;
  overflow: hidden;
}
#editor {
  width: 100%;
  height: 100%;
}

#editor .ace_content,
#editor .ace_line,
#editor .ace_text-layer,
#editor textarea.ace_text-input {
  font-variant-ligatures: none;
  font-feature-settings:
    "liga" 0,
    "calt" 0;
  font-kerning: none;
}

/* ====================================================================
   Preview panel
   ==================================================================== */
#preview-panel {
  grid-area: preview;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--preview-bg);
}

/* Tabs */
.preview-tabs {
  display: flex;
  gap: 2px;
  flex: 1;
}

.preview-tab {
  background: none;
  border: none;
  padding: 0 14px;
  height: calc(var(--tab-h) - 1px);
  font-family: var(--font-ui);
  font-size: 0.72rem;
  font-weight: 500;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
  cursor: pointer;
  border-radius: 4px 4px 0 0;
  transition:
    color 0.15s,
    background 0.15s;
  position: relative;
}
.preview-tab:hover {
  color: var(--text);
  background: rgba(210, 31, 67, 0.08);
}
.preview-tab.active {
  color: var(--accent);
}
.preview-tab.active::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 2px;
  background: var(--accent);
  border-radius: 2px 2px 0 0;
}

/* Content area: raw and math views overlap, shown/hidden by Alpine x-show */
#preview-content {
  flex: 1;
  overflow: hidden;
  position: relative;
}

#raw-view,
#math-view {
  position: absolute;
  inset: 0;
  overflow-y: auto;
  overflow-x: hidden;
}

/* ====================================================================
   Raw mode
   ==================================================================== */
#raw-view pre {
  font-family: var(--font-mono);
  font-size: 12px;
  line-height: 1.65;
  white-space: pre-wrap;
  word-break: break-word;
  padding: 14px 16px;
  min-height: 100%;
  color: var(--text);
}
#raw-view pre.ok {
  color: var(--text);
}
#raw-view pre.error {
  color: #bf3030;
  background: #fff8f8;
  border-left: 3px solid #d94f4f;
}
#raw-view pre.idle {
  color: var(--text-muted);
  font-style: italic;
}

/* ====================================================================
   Math mode — container
   ==================================================================== */
#math-view {
  padding: 18px 22px 32px;
}

/* ====================================================================
   Math mode — exam structure
   ==================================================================== */
.exam-content {
  max-width: 800px;
}

.exam-section {
  margin-bottom: 26px;
}

.exam-section-title {
  font-family: var(--font-display);
  font-size: 0.92rem;
  font-weight: 600;
  font-style: italic;
  color: var(--text-muted);
  padding: 7px 12px;
  background: #fff1f3;
  border-left: 3px solid var(--q-accent);
  border-radius: 0 4px 4px 0;
  margin-bottom: 14px;
}

.question {
  margin-bottom: 12px;
  padding: 13px 15px;
  background: #fff;
  border: 1px solid #f2d8de;
  border-left: 3px solid var(--q-accent);
  border-radius: 0 var(--radius) var(--radius) 0;
  box-shadow: 0 1px 4px rgba(111, 17, 40, 0.08);
}

.question-body {
  font-family: var(--font-serif);
  line-height: 1.75;
  font-size: 0.875rem;
  margin-bottom: 11px;
  color: var(--text);
}

.q-num {
  font-weight: 700;
  color: var(--q-accent);
  font-family: var(--font-serif);
  font-size: 0.9rem;
  margin-right: 3px;
}

/* Choices grid — column count driven by CSS custom property from JS */
.choices {
  display: grid;
  grid-template-columns: repeat(var(--cols, 2), 1fr);
  gap: 5px;
}

.choice {
  font-family: var(--font-serif);
  display: flex;
  align-items: flex-start;
  gap: 7px;
  padding: 6px 10px;
  border: 1px solid #f0dbe0;
  border-radius: 4px;
  background: #fff9fa;
  font-size: 0.845rem;
  line-height: 1.55;
}
.choice.correct {
  background: var(--correct-bg);
  border-color: var(--correct-border);
}

.choice-label {
  font-weight: 700;
  font-size: 0.75rem;
  color: var(--text-muted);
  flex-shrink: 0;
  min-width: 16px;
  padding-top: 1px;
}
.choice.correct .choice-label {
  color: var(--correct-border);
}

.choice-content {
  font-family: var(--font-serif);
  flex: 1;
  min-width: 0;
}

/* Solution block */
.solution {
  margin-top: 10px;
  border-top: 1px dashed #f0dbe0;
  padding-top: 8px;
}

.solution-title {
  font-size: 0.82rem;
  font-weight: 600;
  font-family: var(--font-serif);
  color: var(--q-accent);
  cursor: pointer;
  user-select: none;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 5px;
}

.solution-title::before {
  content: "▶";
  font-size: 0.6rem;
  transition: transform 0.15s ease;
  display: inline-block;
}

.solution[open] .solution-title::before {
  transform: rotate(90deg);
}

.solution-title:hover {
  opacity: 0.75;
}

.solution-body {
  font-family: var(--font-serif);
  font-size: 0.845rem;
  line-height: 1.75;
  color: var(--text);
  margin-top: 7px;
  padding: 8px 12px;
  background: #fffbf4;
  border-left: 2px solid #e8a855;
  border-radius: 0 4px 4px 0;
}

/* Variable config block — shown in latex (Biến tổng quát) mode */
.var-config {
  margin-top: 10px;
  padding: 6px 10px 6px 12px;
  background: #f4f7ff;
  border-left: 2px solid #8ca4d4;
  border-radius: 0 4px 4px 0;
}

.var-config-label {
  display: block;
  font-size: 0.62rem;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: #5a78b8;
  margin-bottom: 5px;
}

.var-config-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.var-config-list li {
  font-size: 0.82rem;
  line-height: 1.6;
  color: var(--text);
}

/* Rendered \begin{enumerate} / \begin{itemize} lists */
.env-list {
  padding-left: 22px;
  margin: 6px 0;
}

.env-list li {
  font-size: 0.845rem;
  line-height: 1.65;
  margin-bottom: 3px;
}

/* Preview info bar — shown below the tab row, above content */
.preview-info-bar {
  padding: 5px 14px;
  font-size: 0.74rem;
  font-weight: 500;
  border-bottom: 1px solid transparent;
  display: flex;
  align-items: center;
  gap: 6px;
}
.preview-info-bar.ok {
  background: #f0faf3;
  color: #3a7d50;
  border-bottom-color: #c8e6d1;
}
.preview-info-bar.ok::before {
  content: "✓";
  font-weight: 700;
}
.preview-info-bar.err {
  background: #fff4f4;
  color: #c0392b;
  border-bottom-color: #f5c6c6;
}
.preview-info-bar.err::before {
  content: "✕";
  font-weight: 700;
}

/* Syntax warnings block — shown when client-side checks fire */
.syntax-warnings {
  border-bottom: 1px solid #f5e3c6;
  background: #fffaf3;
}
.syntax-warning-item {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 6px 14px;
  font-size: 0.775rem;
  line-height: 1.5;
}
.syntax-warning-item + .syntax-warning-item {
  border-top: 1px solid #f5e3c6;
}
.syntax-warning-item.error {
  background: #fff4f4;
  color: #b53a2f;
  border-left: 3px solid #e05a4e;
}
.syntax-warning-item.warn {
  color: #7a5b1a;
  border-left: 3px solid #e8a855;
}
.syntax-warning-icon {
  font-size: 0.7rem;
  padding-top: 2px;
  flex-shrink: 0;
}

/* Notice / empty state */
.preview-notice {
  color: var(--text-muted);
  font-size: 0.84rem;
  font-style: italic;
  line-height: 1.65;
}

/* Export modal body */
.export-modal {
  max-width: 440px;
}
.export-modal-body {
  padding: 20px 24px 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.export-modal-actions {
  display: flex;
  align-items: stretch;
  gap: 10px;
  margin-top: 6px;
}
.export-modal-actions .sb-btn {
  width: auto;
  flex: 1;
  height: 40px;
  padding: 0 14px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ====================================================================
   Math mode — Markdown content
   ==================================================================== */
.md-content {
  font-size: 0.875rem;
  line-height: 1.78;
  color: var(--text);
  max-width: 720px;
}
.md-content p {
  margin-bottom: 12px;
}
.md-content h1 {
  font-family: var(--font-display);
  font-size: 1.5rem;
  font-weight: 600;
  margin: 20px 0 10px;
}
.md-content h2 {
  font-family: var(--font-display);
  font-size: 1.2rem;
  font-weight: 600;
  margin: 16px 0 8px;
}
.md-content h3 {
  font-family: var(--font-display);
  font-size: 1rem;
  font-weight: 600;
  margin: 12px 0 6px;
}
.md-content ul {
  padding-left: 20px;
  margin-bottom: 12px;
}
.md-content li {
  margin-bottom: 4px;
}
.md-content code {
  font-family: var(--font-mono);
  font-size: 0.85em;
  background: #fbeef1;
  padding: 1px 5px;
  border-radius: 3px;
}

/* ====================================================================
   Reference formula modal
   ==================================================================== */
.ref-modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
  background: rgba(0, 0, 0, 0.67);
}

.ref-modal {
  width: min(1080px, 100%);
  max-height: calc(100vh - 36px);
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 22px 54px rgba(43, 9, 18, 0.28);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.ref-modal-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  background: #fff3f6;
}

.ref-modal-header h2 {
  font-family: var(--font-display);
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 3px;
}

.ref-modal-subtitle {
  color: var(--text-muted);
  font-size: 0.78rem;
  line-height: 1.45;
}

.ref-close-btn {
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text-muted);
  border-radius: 6px;
  font-family: var(--font-ui);
  font-size: 0.76rem;
  font-weight: 500;
  padding: 5px 12px;
  cursor: pointer;
  transition: all 0.15s;
}

.ref-close-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: #fff7f9;
}

.ref-modal-body {
  display: grid;
  grid-template-columns: 270px 1fr;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.ref-list {
  background: #fffbfc;
  border-right: 1px solid var(--border);
  padding: 10px;
  min-height: 0;
  overflow-y: auto;
}

.ref-card {
  width: 100%;
  text-align: left;
  border: 1px solid transparent;
  background: #fff;
  border-radius: 7px;
  padding: 9px 10px;
  margin-bottom: 8px;
  cursor: pointer;
  transition: all 0.14s;
}

.ref-card:last-child {
  margin-bottom: 0;
}

.ref-card:hover {
  border-color: #f0c5cf;
  background: #fff6f8;
}

.ref-card.active {
  border-color: #e69bab;
  background: #fff1f5;
  box-shadow: inset 0 0 0 1px rgba(210, 31, 67, 0.1);
}

.ref-card-title {
  display: block;
  font-size: 0.8rem;
  line-height: 1.35;
  color: var(--text);
  font-weight: 600;
  margin-bottom: 4px;
}

.ref-card-category {
  display: inline-block;
  font-size: 0.66rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.ref-detail {
  padding: 14px 16px 16px;
  min-height: 0;
  overflow-y: auto;
}

.ref-detail-title {
  font-family: var(--font-display);
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 2px;
}

.ref-detail-category {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 10px;
}

.ref-detail-desc {
  color: var(--text);
  font-size: 0.83rem;
  line-height: 1.6;
  margin-bottom: 10px;
}

.ref-snippet {
  border: 1px solid var(--border);
  background: #fff9fb;
  border-radius: 7px;
  padding: 12px;
  font-family: var(--font-mono);
  font-size: 0.76rem;
  line-height: 1.55;
  white-space: pre-wrap;
  color: var(--text);
}

.ref-detail-actions {
  margin-top: 12px;
  display: flex;
  justify-content: flex-end;
}

.ref-copy-btn {
  width: auto;
  min-width: 94px;
  padding: 8px 14px;
}

@media (max-width: 980px) {
  .ref-modal-overlay {
    padding: 10px;
  }

  .ref-modal {
    max-height: calc(100vh - 20px);
  }

  .ref-modal-body {
    grid-template-columns: 1fr;
    grid-template-rows: 190px 1fr;
    min-height: 0;
  }

  .ref-list {
    border-right: none;
    border-bottom: 1px solid var(--border);
  }
}

/* ====================================================================
   KaTeX overrides
   ==================================================================== */
.katex-display {
  overflow-x: auto;
  overflow-y: hidden;
  padding: 3px 0;
}
.katex-display > .katex {
  max-width: 100%;
}

/* ====================================================================
   Scrollbars (WebKit)
   ==================================================================== */
::-webkit-scrollbar {
  width: 5px;
  height: 5px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: #d9a1ad;
  border-radius: 3px;
}
::-webkit-scrollbar-thumb:hover {
  background: #c06f82;
}

.error-text {
  color: #bf3030;
  background: #fff8f8;
  font-size: 9pt;
}
