/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:        #1e1e2e;
  --surface:   #282a36;
  --surface2:  #313244;
  --border:    #44475a;
  --text:      #f8f8f2;
  --text-dim:  #6272a4;
  --accent:    #bd93f9;
  --green:     #50fa7b;
  --red:       #ff5555;
  --yellow:    #f1fa8c;
  --orange:    #ffb86c;
  --cyan:      #8be9fd;
  --pink:      #ff79c6;
  --tab-active:#bd93f9;
  --radius:    6px;
}

html { font-size: 15px; }
body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ===== Header ===== */
.header {
  background: var(--surface);
  border-bottom: 2px solid var(--accent);
  padding: 0.75rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.5rem;
}
.header h1 {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--accent);
}
.header h1 span { color: var(--text-dim); font-weight: 400; font-size: 0.85rem; }
.header .paper-ref {
  font-size: 0.8rem;
  color: var(--text-dim);
}
#pyodide-status {
  font-size: 0.78rem;
  padding: 0.25rem 0.7rem;
  border-radius: 20px;
  background: var(--surface2);
  display: flex;
  align-items: center;
  gap: 0.4rem;
}
#pyodide-status .dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--yellow);
  animation: pulse 1.2s ease-in-out infinite;
}
#pyodide-status.ready .dot {
  background: var(--green);
  animation: none;
}
#pyodide-status.error .dot {
  background: var(--red);
  animation: none;
}
@keyframes pulse {
  0%, 100% { opacity: 0.4; }
  50% { opacity: 1; }
}

/* ===== Tab Bar ===== */
.tab-bar {
  display: flex;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  padding: 0 0.5rem;
}
.tab-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  padding: 0.65rem 1.2rem;
  font-size: 0.88rem;
  cursor: pointer;
  border-bottom: 3px solid transparent;
  white-space: nowrap;
  transition: color 0.2s, border-color 0.2s;
  font-family: inherit;
}
.tab-btn:hover {
  color: var(--text);
}
.tab-btn.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
  font-weight: 600;
}
.tab-btn .marks {
  font-size: 0.72rem;
  color: var(--text-dim);
  margin-left: 0.3rem;
}

/* ===== Main Content ===== */
.main-content {
  flex: 1;
  overflow: hidden;
}
.tab-panel {
  display: none;
  height: calc(100vh - 105px);
}
.tab-panel.active {
  display: flex;
}

/* ===== Split Pane ===== */
.question-pane {
  width: 40%;
  min-width: 300px;
  overflow-y: auto;
  padding: 1.25rem 1.5rem;
  border-right: 2px solid var(--border);
  background: var(--surface);
}
.editor-pane {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
}

/* ===== Question Text ===== */
.question-pane h2 {
  font-size: 1.05rem;
  color: var(--accent);
  margin-bottom: 0.3rem;
}
.question-pane .q-meta {
  font-size: 0.78rem;
  color: var(--text-dim);
  margin-bottom: 1rem;
  display: flex;
  gap: 1rem;
}
.question-pane .q-meta .tag {
  background: var(--surface2);
  padding: 0.15rem 0.5rem;
  border-radius: 3px;
}
.question-pane p {
  margin-bottom: 0.7rem;
  line-height: 1.55;
  font-size: 0.92rem;
}
.question-pane ul, .question-pane ol {
  margin: 0.4rem 0 0.8rem 1.5rem;
  font-size: 0.92rem;
  line-height: 1.55;
}
.question-pane li { margin-bottom: 0.3rem; }
.question-pane code {
  background: var(--bg);
  padding: 0.1rem 0.4rem;
  border-radius: 3px;
  font-family: 'Fira Code', 'Consolas', monospace;
  font-size: 0.85rem;
  color: var(--green);
}
.question-pane .code-block {
  background: var(--bg);
  padding: 0.6rem 0.8rem;
  border-radius: var(--radius);
  font-family: 'Fira Code', 'Consolas', monospace;
  font-size: 0.82rem;
  line-height: 1.5;
  margin: 0.5rem 0 0.8rem;
  overflow-x: auto;
  white-space: pre;
  color: var(--cyan);
}
.question-pane .test-table {
  width: 100%;
  border-collapse: collapse;
  margin: 0.5rem 0 1rem;
  font-size: 0.85rem;
}
.question-pane .test-table th {
  background: var(--accent);
  color: var(--bg);
  padding: 0.4rem 0.6rem;
  text-align: left;
  font-weight: 600;
}
.question-pane .test-table td {
  padding: 0.35rem 0.6rem;
  border-bottom: 1px solid var(--border);
}
.question-pane .test-table tr:nth-child(even) td {
  background: var(--surface2);
}
.question-pane .save-note {
  margin-top: 0.8rem;
  padding: 0.5rem 0.7rem;
  background: var(--surface2);
  border-left: 3px solid var(--accent);
  border-radius: 0 var(--radius) var(--radius) 0;
  font-size: 0.82rem;
}
.question-pane .save-note strong { color: var(--accent); }

.question-pane .sub-list {
  list-style: disc;
  margin-left: 1rem;
}
.question-pane .sub-list li { margin-bottom: 0.15rem; }

.question-pane h3 {
  font-size: 0.95rem;
  color: var(--pink);
  margin: 1rem 0 0.4rem;
}

/* ===== Toolbar ===== */
.toolbar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.toolbar button {
  border: none;
  padding: 0.4rem 0.9rem;
  border-radius: var(--radius);
  font-size: 0.82rem;
  font-family: inherit;
  cursor: pointer;
  font-weight: 600;
  transition: opacity 0.2s, transform 0.1s;
}
.toolbar button:hover { opacity: 0.85; }
.toolbar button:active { transform: scale(0.97); }

.btn-run {
  background: var(--green);
  color: var(--bg);
}
.btn-run:disabled {
  background: var(--text-dim);
  cursor: not-allowed;
  opacity: 0.5 !important;
}
.btn-stop {
  background: var(--red);
  color: var(--text);
  display: none;
}
.btn-reset {
  background: var(--orange);
  color: var(--bg);
}
.btn-download {
  background: var(--cyan);
  color: var(--bg);
}
.btn-download.highlight {
  animation: glow 1.5s ease-in-out infinite alternate;
}
@keyframes glow {
  from { box-shadow: 0 0 4px var(--cyan); }
  to   { box-shadow: 0 0 12px var(--cyan), 0 0 24px rgba(139,233,253,0.3); }
}
.toolbar .spacer { flex: 1; }
.toolbar .file-label {
  font-size: 0.75rem;
  color: var(--text-dim);
}

/* ===== Warning Banner (Q5) ===== */
.warning-banner {
  background: rgba(241,250,140,0.12);
  border: 1px solid var(--yellow);
  color: var(--yellow);
  padding: 0.5rem 0.8rem;
  font-size: 0.8rem;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

/* ===== Note Banner (Q2) ===== */
.note-banner {
  background: rgba(139,233,253,0.08);
  border: 1px solid var(--cyan);
  color: var(--cyan);
  padding: 0.4rem 0.8rem;
  font-size: 0.78rem;
}

/* ===== Editor Area ===== */
.editor-wrapper {
  flex: 1;
  overflow: hidden;
  position: relative;
  min-height: 0;
}
.editor-wrapper .CodeMirror {
  height: 100%;
  font-size: 0.88rem;
  font-family: 'Fira Code', 'Consolas', 'Courier New', monospace;
}

/* ===== Output Panel ===== */
.output-panel {
  height: 180px;
  min-height: 80px;
  border-top: 2px solid var(--border);
  display: flex;
  flex-direction: column;
  background: var(--bg);
}
.output-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.3rem 0.75rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  font-size: 0.78rem;
  color: var(--text-dim);
}
.output-header button {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 0.75rem;
  font-family: inherit;
}
.output-header button:hover { color: var(--text); }
.output-body {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem 0.75rem;
  font-family: 'Fira Code', 'Consolas', monospace;
  font-size: 0.82rem;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.5;
}
.output-body .stdout { color: var(--text); }
.output-body .stderr { color: var(--red); }
.output-body .info   { color: var(--cyan); }

/* ===== File Output Panel (Q4) ===== */
.file-output-panel {
  border-top: 1px solid var(--border);
  background: var(--surface2);
  max-height: 150px;
  overflow-y: auto;
  display: none;
}
.file-output-panel .file-header {
  padding: 0.3rem 0.75rem;
  font-size: 0.75rem;
  color: var(--orange);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.file-output-panel .file-body {
  padding: 0.4rem 0.75rem;
  font-family: 'Fira Code', 'Consolas', monospace;
  font-size: 0.8rem;
  white-space: pre;
  color: var(--text);
}

/* ===== Turtle Canvas Panel (Q5) ===== */
.turtle-canvas-panel {
  border-top: 1px solid var(--border);
  background: var(--surface2);
  display: none;
}
.turtle-canvas-panel .turtle-canvas-header {
  padding: 0.3rem 0.75rem;
  font-size: 0.75rem;
  color: var(--green);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  font-weight: 600;
}
.turtle-canvas-panel .turtle-canvas-body {
  padding: 0.4rem;
  text-align: center;
  background: white;
}
.turtle-canvas-panel canvas {
  width: 100%;
  height: auto;
  display: block;
}

/* ===== Loading Overlay ===== */
.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(30,30,46,0.92);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  transition: opacity 0.4s;
}
.loading-overlay.hidden {
  opacity: 0;
  pointer-events: none;
}
.loading-overlay .spinner {
  width: 48px; height: 48px;
  border: 4px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-bottom: 1rem;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-overlay p {
  color: var(--text-dim);
  font-size: 0.9rem;
}

/* ===== Responsive ===== */
@media (max-width: 900px) {
  .tab-panel.active {
    flex-direction: column;
    height: auto;
    min-height: calc(100vh - 105px);
  }
  .question-pane {
    width: 100%;
    min-width: unset;
    border-right: none;
    border-bottom: 2px solid var(--border);
    max-height: 40vh;
  }
  .editor-pane {
    min-height: 50vh;
  }
  .editor-wrapper .CodeMirror {
    height: 350px;
  }
}

/* ===== Show Answer Button ===== */
.btn-answer {
  background: var(--pink);
  color: var(--bg);
}
.btn-answer.active {
  background: var(--text-dim);
}

/* ===== Model Answer Panel ===== */
.model-answer-panel {
  border-top: 2px solid var(--pink);
  background: var(--surface2);
  display: none;
  flex-direction: column;
  flex-shrink: 0;
  height: 250px;
  overflow: hidden;
}
.model-answer-panel.show {
  display: flex;
}
.model-answer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.35rem 0.75rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  font-size: 0.78rem;
  color: var(--pink);
  font-weight: 600;
  flex-shrink: 0;
}
.model-answer-body {
  flex: 1;
  overflow-y: auto;
  padding: 0;
  min-height: 0;
}
.model-answer-body pre {
  margin: 0;
  padding: 0.6rem 0.75rem;
  font-family: 'Fira Code', 'Consolas', monospace;
  font-size: 0.82rem;
  white-space: pre;
  color: var(--green);
  line-height: 1.5;
  background: var(--bg);
}

/* ===== Scrollbar ===== */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-dim); }

/* ===== Input Modal ===== */
.input-modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(30,30,46,0.8);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 500;
}
.input-modal-overlay.show { display: flex; }
.input-modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  min-width: 340px;
  max-width: 90vw;
}
.input-modal h3 {
  color: var(--accent);
  margin-bottom: 0.8rem;
  font-size: 0.95rem;
}
.input-modal label {
  display: block;
  font-size: 0.82rem;
  color: var(--text-dim);
  margin-bottom: 0.25rem;
}
.input-modal input[type="text"] {
  width: 100%;
  padding: 0.45rem 0.6rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: 'Fira Code', 'Consolas', monospace;
  font-size: 0.85rem;
  margin-bottom: 0.6rem;
}
.input-modal input[type="text"]:focus {
  outline: none;
  border-color: var(--accent);
}
.input-modal .modal-buttons {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 0.5rem;
}
.input-modal .modal-buttons button {
  padding: 0.4rem 1rem;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  font-family: inherit;
  font-size: 0.82rem;
  font-weight: 600;
}
.input-modal .btn-modal-run {
  background: var(--green);
  color: var(--bg);
}
.input-modal .btn-modal-cancel {
  background: var(--surface2);
  color: var(--text);
}
