/* ── Reset & Tokens ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg: #0d1117; --surface: #161b27; --surface2: #1f2535; --border: #2a3050;
  --accent: #cddc39; --accent-lo: rgba(205,220,57,.20);
  --green: #3ddc84; --red: #ff5555; --gold: #f5c842;
  --text: #dde3f0; --text-dim: #8b9dc3;
  --font: 'Segoe UI', system-ui, sans-serif; --mono: 'Courier New', monospace;
  --radius: 12px;
}

body { background: var(--bg); color: var(--text); font-family: var(--font); min-height: 100dvh; padding: 0 16px; overscroll-behavior-y: none; }
#app { max-width: 1040px; margin: 0 auto; display: flex; flex-direction: column; gap: 14px; }

/* ── Header ── */
header { text-align: center; padding: 4px 0; }
header h1 {
  font-size: 1.8rem; font-weight: 700;
  background: linear-gradient(120deg, var(--accent) 0%, #f0f4c3 100%);
  -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text;
}
.subtitle { color: var(--text-dim); font-size: 0.85rem; margin-top: 3px; }

/* ── Controls bar ── */
.controls-bar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 14px;
  padding: 12px 16px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
.ctrl-group { display: flex; align-items: center; gap: 8px; }
.ctrl-label { font-size: 0.72rem; text-transform: uppercase; letter-spacing: .06em; color: var(--text-dim); white-space: nowrap; }

/* ── Pill tabs ── */
.pill-tabs { display: flex; background: var(--bg); border-radius: 8px; padding: 3px; gap: 2px; flex-wrap: wrap; }
.pill {
  padding: 5px 13px; border: none; background: transparent; color: var(--text-dim);
  border-radius: 6px; cursor: pointer; font-size: 0.81rem;
  min-height: 44px; display: inline-flex; align-items: center;
  touch-action: manipulation; -webkit-tap-highlight-color: transparent;
  transition: background .18s, color .18s, box-shadow .18s;
}
.pill.active { background: var(--accent); color: #000; box-shadow: 0 0 12px var(--accent-lo); }
@media (hover: hover) { .pill:hover:not(.active) { background: var(--surface2); color: var(--text); } }

/* ── Buttons ── */
.btn {
  padding: 8px 18px; border: none; border-radius: 8px;
  cursor: pointer; font-size: 0.84rem; font-weight: 600;
  min-height: 44px; display: inline-flex; align-items: center; justify-content: center;
  transition: filter .18s, box-shadow .18s, opacity .18s;
  white-space: nowrap; touch-action: manipulation; -webkit-tap-highlight-color: transparent;
}
.btn:disabled { opacity: 0.38; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: #000; }
.btn-ghost { background: var(--surface2); color: var(--text); border: 1px solid var(--border); }
.btn-lg { padding: 14px 36px; font-size: 1rem; border-radius: 10px; min-height: 52px; }
@media (hover: hover) {
  .btn-primary:not(:disabled):hover { filter: brightness(1.08); box-shadow: 0 0 12px var(--accent-lo); }
  .btn-ghost:not(:disabled):hover { background: var(--border); }
}
.btn-icon {
  padding: 0; width: 44px; min-height: 44px; border-radius: 8px; font-size: 1.1rem;
  background: var(--surface2); border: 1px solid var(--border); color: var(--text);
  cursor: pointer; display: inline-flex; align-items: center; justify-content: center;
  touch-action: manipulation; -webkit-tap-highlight-color: transparent; transition: background .15s;
}
@media (hover: hover) { .btn-icon:hover { background: var(--border); } }

.hidden { display: none !important; }

/* ══════════ SIMULATE LAYOUT ══════════ */
.sim-layout { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; align-items: start; }
.sim-left {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px 14px 16px;
  display: flex; flex-direction: column; gap: 12px;
}
.sim-right { display: flex; flex-direction: column; gap: 12px; min-width: 0; }
.panel-title { font-weight: 700; font-size: 15px; color: var(--accent); }
.panel-hint  { font-size: 11.5px; color: var(--text-dim); line-height: 1.55; }

/* ── Interactive equation line ── */
.eq-stage {
  background: var(--bg); border: 1px solid var(--border); border-radius: 10px;
  padding: 14px 12px; display: flex; flex-direction: column; gap: 10px; align-items: center;
}
.eq-line {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: center;
  gap: 6px 8px; font-family: var(--mono); width: 100%;
}
.eq-line-lg { font-size: 1.15rem; gap: 8px 12px; }
.eq-term { display: inline-flex; align-items: center; gap: 4px; }
.eq-formula { font-size: 1.15rem; font-weight: 700; color: var(--text); white-space: nowrap; letter-spacing: .3px; }
.eq-formula sub { font-size: 0.68em; }
.eq-line-lg .eq-formula { font-size: 1.4rem; }
.eq-op    { color: var(--text-dim); font-size: 1.1rem; font-weight: 700; }
.eq-arrow { color: var(--accent); font-size: 1.35rem; font-weight: 700; padding: 0 2px; }

/* coefficient stepper (Simulate) */
.coeff-stepper {
  display: inline-flex; align-items: center; background: var(--surface2);
  border: 1px solid var(--border); border-radius: 7px; overflow: hidden; height: 30px;
}
.coeff-btn {
  width: 26px; height: 30px; border: none; background: transparent; color: var(--accent);
  font-size: 1.05rem; font-weight: 700; cursor: pointer; line-height: 1;
  display: flex; align-items: center; justify-content: center; transition: background .12s;
}
@media (hover: hover) { .coeff-btn:hover { background: var(--accent-lo); } }
.coeff-btn:active { background: var(--accent); color: #000; }
.coeff-val {
  min-width: 22px; text-align: center; font-family: var(--mono); font-weight: 700;
  font-size: 0.95rem; color: var(--accent); padding: 0 2px;
}
.coeff-fixed { font-family: var(--mono); font-weight: 700; color: var(--accent); font-size: 1.05em; margin-right: 1px; }

/* coefficient input box (Practice / Quiz) */
.coeff-input {
  width: 46px; height: 44px; border: 2px solid var(--border); border-radius: 8px;
  background: var(--surface2); color: var(--accent); font-family: var(--mono);
  font-size: 1.15rem; font-weight: 700; text-align: center; outline: none;
  transition: border-color .15s, box-shadow .15s; -moz-appearance: textfield;
}
.coeff-input::placeholder { color: var(--text-dim); font-weight: 400; }
.coeff-input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-lo); }

.eq-status-row { display: flex; justify-content: center; }
.sim-status {
  font-size: 0.82rem; font-weight: 700; padding: 5px 16px; border-radius: 20px;
  border: 1px solid; letter-spacing: .02em;
}
.sim-status.ok  { color: var(--accent); border-color: var(--accent); background: var(--accent-lo); }
.sim-status.bad { color: var(--red); border-color: rgba(255,85,85,.4); background: rgba(255,85,85,.08); }

.sim-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* ── Reaction picker chips ── */
.eq-picker { display: flex; flex-direction: column; gap: 7px; }
.picker-label { font-size: 0.72rem; text-transform: uppercase; letter-spacing: .06em; color: var(--text-dim); }
.level-pills { display: flex; gap: 4px; background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 3px; flex-wrap: wrap; }
.level-pill {
  flex: 1; min-width: 60px; padding: 6px 8px; border: none; background: transparent; color: var(--text-dim);
  border-radius: 6px; cursor: pointer; font-size: 0.76rem; font-weight: 700; min-height: 34px;
  touch-action: manipulation; -webkit-tap-highlight-color: transparent; transition: background .15s, color .15s;
}
@media (hover: hover) { .level-pill:hover:not(.active) { background: var(--surface2); color: var(--text); } }
.level-pill.active { background: var(--accent); color: #000; }
.eq-chips { display: flex; flex-wrap: wrap; gap: 6px; max-height: 148px; overflow-y: auto; padding-right: 2px;
  scrollbar-width: thin; scrollbar-color: var(--border) transparent; }
.eq-chip {
  padding: 6px 10px; border: 1px solid var(--border); border-radius: 7px; background: var(--bg);
  color: var(--text-dim); font-family: var(--mono); font-size: 0.78rem; cursor: pointer;
  white-space: nowrap; transition: border-color .15s, color .15s, background .15s;
}
.eq-chip sub { font-size: 0.7em; }
@media (hover: hover) { .eq-chip:hover { border-color: var(--accent); color: var(--text); } }
.eq-chip.active { border-color: var(--accent); color: var(--accent); background: var(--accent-lo); }

/* ── Custom equation solver ── */
.custom-box { display: flex; flex-direction: column; gap: 7px; padding-top: 4px; border-top: 1px solid var(--border); }
.custom-input-row { display: flex; gap: 6px; }
.custom-input {
  flex: 1; min-width: 0; height: 42px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg); color: var(--text); font-family: var(--mono); font-size: 0.85rem;
  padding: 0 12px; outline: none; transition: border-color .15s;
}
.custom-input:focus { border-color: var(--accent); }
.custom-input-row .btn { min-height: 42px; padding: 8px 16px; }
.custom-result { font-size: 0.85rem; line-height: 1.5; min-height: 0; }
.custom-result.err { color: var(--red); background: rgba(255,85,85,.08); border: 1px solid rgba(255,85,85,.3); border-radius: 8px; padding: 10px 12px; }
.custom-result.ok  { color: var(--text); background: var(--bg); border: 1px solid var(--accent); border-radius: 8px; padding: 12px; display: flex; flex-direction: column; align-items: flex-start; }
.custom-eq { display: flex; flex-wrap: wrap; align-items: center; gap: 5px 8px; font-family: var(--mono); }
.custom-eq .eq-formula { font-size: 1.05rem; }

/* ══════════ CANVAS ══════════ */
.canvas-card {
  position: relative; background: #080c14; border: 1px solid var(--border);
  border-radius: var(--radius); overflow-x: clip;
}
.canvas-card canvas { display: block; width: 100%; height: auto; touch-action: pan-y; }

/* right-click context menu */
.ctx-menu {
  position: absolute; z-index: 100; min-width: 190px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 4px 0; box-shadow: 0 8px 24px rgba(0,0,0,.5);
}
.ctx-item {
  display: block; width: 100%; padding: 9px 16px; border: none; background: transparent;
  color: var(--text); font-size: 0.82rem; text-align: left; cursor: pointer; transition: background .12s;
}
.ctx-item:hover { background: var(--accent-lo); color: var(--accent); }

/* ══════════ ELEMENT BALANCE TABLE ══════════ */
.balance-table {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  overflow: hidden;
}
.bt-row {
  display: grid; grid-template-columns: 1.4fr 1fr 1fr 0.6fr; align-items: center;
  padding: 8px 14px; font-size: 0.86rem; border-bottom: 1px solid var(--border);
}
.bt-row:last-child { border-bottom: none; }
.bt-head { background: var(--surface2); color: var(--text-dim); font-size: 0.7rem;
  text-transform: uppercase; letter-spacing: .05em; font-weight: 700; }
.bt-cell { text-align: center; font-family: var(--mono); font-weight: 700; }
.bt-head .bt-cell { font-family: var(--font); }
.bt-el { display: flex; align-items: center; gap: 7px; justify-content: flex-start; text-align: left; }
.bt-dot { width: 11px; height: 11px; border-radius: 50%; flex: none; box-shadow: 0 0 5px rgba(0,0,0,.4); }
.bt-mark { font-size: 1rem; }
.bt-row.ok  .bt-mark { color: var(--accent); }
.bt-row.bad .bt-mark { color: var(--red); }
.bt-row.bad { background: rgba(255,85,85,.05); }
.bt-row.bad .bt-cell:nth-child(2), .bt-row.bad .bt-cell:nth-child(3) { color: #ffb3b3; }

/* ══════════ EXPLORE MODE ══════════ */
.explore-cards { display: grid; grid-template-columns: repeat(2, 1fr); gap: 14px; margin-top: 14px; }
.explore-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px 20px; transition: border-color .2s;
}
@media (hover: hover) { .explore-card:hover { border-color: var(--accent); } }
.ec-title { margin: 0 0 8px; font-size: 15px; color: var(--accent); }
.ec-body  { margin: 0; font-size: 13px; color: var(--text); line-height: 1.65; }
.ec-body sub { font-size: 0.72em; }
.ec-note {
  margin-top: 10px; background: var(--surface2); border-left: 3px solid var(--accent);
  padding: 8px 12px; border-radius: 0 6px 6px 0; font-size: 12px; color: var(--text-dim); line-height: 1.5;
}

/* ══════════ PRACTICE MODE ══════════ */
.prac-container {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 20px 22px; display: flex; flex-direction: column; gap: 14px;
}
.prac-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.prac-badge {
  font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .07em;
  color: var(--accent); background: var(--accent-lo); border: 1px solid var(--accent);
  padding: 4px 12px; border-radius: 20px;
}
.prac-scoreline { font-size: 13px; font-weight: 600; color: var(--text-dim); }
.prac-scoreline span { color: var(--accent); }
.prac-stage { padding: 22px 12px; }
.prac-feedback { font-size: 13px; line-height: 1.6; min-height: 0; padding: 0; }
.prac-feedback.ok   { color: var(--green); background: rgba(61,220,132,.1); border: 1px solid rgba(61,220,132,.35); border-radius: 8px; padding: 10px 14px; }
.prac-feedback.bad  { color: #ffb3b3; background: rgba(255,85,85,.08); border: 1px solid rgba(255,85,85,.3); border-radius: 8px; padding: 10px 14px; }
.prac-feedback.bad strong { color: #fff; }
.prac-feedback.hint { color: var(--gold); background: rgba(245,200,66,.08); border: 1px solid rgba(245,200,66,.3); border-radius: 8px; padding: 10px 14px; }
.prac-feedback sub  { font-size: 0.72em; }
.prac-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ══════════ QUIZ MODE ══════════ */
.quiz-container { display: flex; flex-direction: column; gap: 14px; }
.quiz-header {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 16px; display: flex; align-items: center; gap: 14px;
}
.quiz-score-display { font-size: 13px; font-weight: 600; color: var(--text-dim); white-space: nowrap; flex-shrink: 0; }
.quiz-score-display span { color: var(--accent); }
.quiz-progress { flex: 1; height: 6px; background: var(--surface2); border-radius: 3px; overflow: hidden; }
.quiz-progress-bar { height: 100%; background: var(--accent); border-radius: 3px; transition: width .45s ease; }
.quiz-start { text-align: center; padding: 44px 30px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.qs-icon { font-size: 52px; margin-bottom: 14px; }
.quiz-start h2 { margin: 0 0 12px; color: var(--accent); }
.quiz-start p { color: var(--text-dim); font-size: 14px; line-height: 1.65; margin: 0 0 26px; }
.quiz-question { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 24px; }
.qz-category { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .08em; color: var(--accent); margin-bottom: 16px; }
.quiz-question .eq-stage { margin-bottom: 16px; padding: 26px 12px; }
.qz-feedback { background: var(--surface2); border-radius: 8px; padding: 12px 16px; font-size: 13px; line-height: 1.6; color: var(--text); margin-bottom: 16px; border-left: 3px solid var(--accent); }
.qz-feedback.ok  { border-left-color: var(--green); color: var(--green); }
.qz-feedback.bad { border-left-color: var(--red); color: #ffb3b3; }
.qz-feedback strong, .qz-feedback sub { color: inherit; }
.qz-feedback sub { font-size: 0.72em; }
.quiz-btn-row { display: flex; gap: 8px; }
.quiz-result { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 36px 24px; text-align: center; }
.qr-stars { font-size: 44px; margin-bottom: 12px; letter-spacing: 4px; color: var(--gold); }
.qr-score { font-size: 20px; font-weight: 700; color: var(--text); margin-bottom: 24px; }
.qr-rows { text-align: left; margin-bottom: 28px; }
.qr-row { display: grid; grid-template-columns: 26px 1fr; gap: 10px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.qr-row:last-child { border-bottom: none; }
.qr-mark { text-align: center; font-weight: 700; }
.qr-row.ok  .qr-mark { color: var(--green); }
.qr-row.bad .qr-mark { color: var(--red); }
.qr-eq { font-family: var(--mono); color: var(--text); }
.qr-eq sub { font-size: 0.72em; }
.qr-row.bad .qr-eq { color: var(--text-dim); }

/* ── header quick-links ── */
.head-actions { display: flex; align-items: center; gap: 8px; margin-left: auto; flex-wrap: wrap; justify-content: flex-end; }
.btn-link { display: inline-flex; align-items: center; gap: 7px; padding: 5px 12px; border-radius: 8px; font-size: .82rem; font-weight: 700; text-decoration: none; cursor: pointer; background: var(--surface2); color: var(--text); border: 1.5px solid var(--border); white-space: nowrap; transition: border-color .18s, background .18s, color .18s; }
.btn-link:hover { border-color: var(--accent); background: var(--accent-lo); color: #fff; }
.btn-link img { width: 20px; height: 20px; border-radius: 4px; flex: none; }
.btn-link .lbl { line-height: 1; }

/* ══════════ RESPONSIVE ══════════ */
@media (max-width: 900px) {
  .sim-layout { grid-template-columns: 1fr; }
}
@media (max-width: 768px) {
  .explore-cards { grid-template-columns: 1fr; }
  .controls-bar { gap: 10px; }
  .eq-formula { font-size: 1.05rem; }
}
@media (max-width: 560px) { .btn-link .lbl { display: none; } .btn-link { padding: 5px 9px; } }
@media (max-width: 500px) {
  header h1 { font-size: 1.4rem; }
  .subtitle { font-size: 0.78rem; }
  .pill { padding: 5px 10px; font-size: 0.76rem; }
  .controls-bar { padding: 10px 12px; gap: 8px; }
  .prac-container { padding: 16px 14px; }
  .quiz-question { padding: 18px 14px; }
  .eq-line-lg .eq-formula { font-size: 1.2rem; }
  .coeff-input { width: 42px; height: 42px; font-size: 1.05rem; }
  .bt-row { padding: 7px 10px; font-size: 0.8rem; }
}
