/* ── Reset & Tokens ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg: #0d1117; --surface: #161b27; --surface2: #1f2535; --border: #2a3050;
  --accent: #1de9b6; --accent-lo: rgba(29,233,182,.18);
  --ionic: #ff6e40;  --ionic-lo:  rgba(255,110,64,.16);
  --green: #3ddc84;  --red: #ff5555; --gold: #f5c842;
  --text: #dde3f0;   --text-dim: #8b9dc3;  /* #8b9dc3 on #0d1117 = 5.1:1 — WCAG AA ✅ */
  --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: 980px; 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%, #a7ffeb 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-ghost.btn-active { border-color: var(--accent); color: var(--accent); }
.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.1); box-shadow: 0 0 12px var(--accent-lo); }
  .btn-ghost:hover { background: var(--border); }
}

.hidden { display: none !important; }

/* ══════════ SIMULATE LAYOUT ══════════ */
.sim-layout {
  display: grid;
  grid-template-columns: 300px 1fr;
  gap: 14px;
  align-items: start;
}
.sim-left {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  position: sticky;
  top: 14px;
}
.sim-right {
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-width: 0;
}

/* Compound pills inside controls-bar */
.cmpd-tabs { flex-wrap: wrap; }
.cmpd-tabs .pill { padding: 5px 10px; font-size: 13px; font-weight: 700; }
.cmpd-group { flex: 1; min-width: 0; }

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

/* ══════════ READOUT PANEL ══════════ */
.readout-panel {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
}
.readout-cell {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 10px 12px;
}
.rc-label { font-size: 10px; color: var(--text-dim); text-transform: uppercase; letter-spacing: .07em; margin-bottom: 4px; }
.rc-val   { font-size: 12px; color: var(--text); font-weight: 600; word-break: break-word; line-height: 1.4; }

/* ══════════ INFO BOX ══════════ */
.info-box {
  background: var(--surface); border: 1px solid var(--accent);
  border-radius: var(--radius); padding: 16px 20px;
  animation: cbFadeUp .4s ease both;
}
.info-box-title {
  font-weight: 700; font-size: 16px; color: var(--accent);
  margin-bottom: 12px; padding-bottom: 8px; border-bottom: 1px solid var(--border);
}
.info-grid   { display: flex; flex-direction: column; gap: 7px; }
.info-row    { display: grid; grid-template-columns: 170px 1fr; gap: 10px; font-size: 13px; align-items: start; }
.info-label  { color: var(--text-dim); font-weight: 600; }
.info-val    { color: var(--text); line-height: 1.5; }
.info-fact   { display: flex; gap: 6px; }
.info-fact .info-label { color: var(--accent); flex-shrink: 0; }

@keyframes cbFadeUp {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: none; }
}

/* ══════════ 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); }
}
.explore-card h3 { margin: 0 0 8px; font-size: 15px; color: var(--accent); }
.explore-card p  { margin: 0 0 10px; font-size: 13px; color: var(--text); line-height: 1.65; }
.ec-formula {
  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); font-family: var(--mono); line-height: 1.5;
}

/* ══════════ PRACTICE MODE ══════════ */
.practice-layout {
  display: grid; grid-template-columns: 360px 1fr;
  gap: 14px; align-items: start;
}
.practice-panel, .result-panel {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px;
}
.panel-title { font-weight: 700; font-size: 15px; color: var(--accent); margin-bottom: 6px; }
.panel-hint  { font-size: 12px; color: var(--text-dim); margin-bottom: 12px; line-height: 1.55; }

.el-legend { display: flex; gap: 12px; flex-wrap: wrap; margin-bottom: 10px; }
.el-legend-item { display: flex; align-items: center; gap: 5px; font-size: 11px; color: var(--text-dim); }
.el-legend-dot  { width: 10px; height: 10px; border-radius: 2px; }

.element-selector {
  display: grid; grid-template-columns: repeat(5, 1fr);
  gap: 4px; margin-bottom: 14px;
  max-height: 296px; overflow-y: auto;
  padding-right: 2px;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}
.el-btn {
  position: relative;
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 6px; padding: 6px 3px 5px;
  min-height: 52px;
  cursor: pointer; touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  text-align: center; transition: border-color .15s, transform .1s; overflow: hidden;
}
.el-btn::before {
  content: ''; position: absolute; inset: 0;
  background: var(--el-color, #fff); opacity: .06;
  transition: opacity .15s; pointer-events: none;
}
.el-btn:active { transform: scale(.94); }
@media (hover: hover) {
  .el-btn:hover            { border-color: var(--el-color, var(--accent)); }
  .el-btn:hover::before    { opacity: .14; }
}
.el-z   { display:block; font-size:9px;  color:var(--text-dim); line-height:1.1; }
.el-sym { display:block; font-size:15px; font-weight:700; color:var(--el-color,var(--text)); line-height:1.1; }
.el-name{ display:block; font-size:8px;  color:var(--text-dim); line-height:1.2; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }

.el-sel1 { border-color: var(--accent) !important; background: var(--accent-lo) !important; }
.el-sel2 { border-color: var(--ionic)  !important; background: var(--ionic-lo)  !important; }
.el-sel1::before, .el-sel2::before { opacity: 0 !important; }

.selected-elements { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.sel-slot {
  flex: 1; min-height: 78px; border: 2px dashed var(--border);
  border-radius: 10px; display: flex; align-items: center; justify-content: center;
}
.sel-placeholder { color: var(--text-dim); font-size: 12px; text-align: center; padding: 10px; }
.sel-el      { text-align: center; padding: 8px 10px; border: 2px solid; border-radius: 8px; width: 100%; }
.sel-el-sym  { font-size: 22px; font-weight: 700; }
.sel-el-name { font-size: 12px; color: var(--text-dim); }
.sel-el-info { font-size: 11px; color: var(--text-dim); }
.sel-plus    { font-size: 22px; color: var(--text-dim); flex-shrink: 0; }
.practice-actions { display: flex; gap: 8px; }

.result-placeholder { text-align: center; color: var(--text-dim); padding: 36px 16px; }
.result-icon { font-size: 42px; margin-bottom: 12px; }

.result-success, .result-invalid { animation: cbFadeUp .3s ease both; }

.rs-header {
  border: 2px solid; border-radius: 10px; padding: 16px; margin-bottom: 14px; text-align: center;
}
.rs-formula  { font-size: 30px; font-weight: 700; margin-bottom: 4px; }
.rs-name     { font-size: 14px; color: var(--text-dim); margin-bottom: 10px; }
.rs-type     { display: inline-block; padding: 4px 14px; border-radius: 20px; font-size: 13px; font-weight: 700; }

.rs-atoms    { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; }
.rs-atom     { flex: 1; border: 1px solid; border-radius: 10px; padding: 12px; text-align: center; }
.rs-asym     { font-size: 24px; font-weight: 700; }
.rs-aname    { font-size: 12px; color: var(--text); font-weight: 600; }
.rs-ainfo    { font-size: 11px; color: var(--text-dim); }
.rs-plus     { font-size: 22px; color: var(--text-dim); flex-shrink: 0; }

.rs-explanation  { background: var(--surface2); border-radius: 8px; padding: 12px 14px; }
.rs-expl-title   { font-size: 12px; font-weight: 700; color: var(--text-dim); margin-bottom: 6px; }
.rs-explanation p{ font-size: 13px; color: var(--text); line-height: 1.65; margin: 0; }

.result-invalid { text-align: center; padding: 24px 16px; }
.ri-icon  { font-size: 44px; color: var(--red); margin-bottom: 8px; }
.ri-title { font-size: 18px; font-weight: 700; color: var(--red); margin-bottom: 8px; }
.ri-detail{ font-size: 13px; color: var(--text); line-height: 1.65; margin-bottom: 12px; max-width: 360px; margin-left: auto; margin-right: auto; }
.ri-atoms { font-size: 13px; color: var(--text-dim); }

/* ══════════ QUIZ MODE ══════════ */
.quiz-container { display: flex; flex-direction: column; gap: 14px; }

/* Score + progress bar — rendered as a controls-bar strip */
.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: 48px 32px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
.qs-icon       { font-size: 56px; margin-bottom: 16px; }
.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 28px; }

.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: 10px; }
.qz-text     { font-size: 16px; color: var(--text); font-weight: 600; margin-bottom: 20px; line-height: 1.55; }
.qz-options  { display: flex; flex-direction: column; gap: 8px; margin-bottom: 16px; }

.quiz-opt {
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 8px; padding: 14px 16px; text-align: left;
  min-height: 52px;
  cursor: pointer; color: var(--text); font-size: 14px; line-height: 1.45;
  touch-action: manipulation; -webkit-tap-highlight-color: transparent;
  transition: border-color .15s, background .15s;
}
@media (hover: hover) {
  .quiz-opt:hover:not(:disabled) { border-color: var(--accent); background: var(--accent-lo); }
}
.opt-correct { background: rgba(61,220,132,.15) !important; border-color: var(--green) !important; color: var(--green) !important; font-weight: 600; }
.opt-wrong   { background: rgba(255,85,85,.15)  !important; border-color: var(--red)   !important; color: var(--red)   !important; }

.qz-feedback {
  background: var(--surface2); border-radius: 8px;
  padding: 12px 16px; font-size: 13px; line-height: 1.65; color: var(--text);
  margin-bottom: 16px; border-left: 3px solid var(--accent);
}
.fb-ok    { color: var(--green); font-weight: 700; }
.fb-wrong { color: var(--red);   font-weight: 700; }

.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; }
.qr-stars.gold   { color: var(--gold); }
.qr-stars.silver { color: #b0bec5; }
.qr-stars.bronze { color: #a1887f; }
.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: 28px 1fr 30px;
  gap: 8px; align-items: center;
  padding: 7px 0; border-bottom: 1px solid var(--border); font-size: 13px;
}
.qr-num    { color: var(--text-dim); }
.qr-q      { color: var(--text); line-height: 1.4; }
.qr-status { color: var(--green); font-weight: 700; text-align: center; }
.qr-wrong .qr-status { color: var(--red); }
.qr-wrong .qr-q      { color: var(--text-dim); }

/* ══════════ ELEMENT TYPE BADGE COLOURS (legend) ══════════ */
.el-metal     { --el-type-color: #ffd740; }
.el-nonmetal  { --el-type-color: #1de9b6; }
.el-noble     { --el-type-color: #ea80fc; }
.el-metalloid { --el-type-color: #90a4ae; }

/* ══════════ CANVAS RIGHT-CLICK CONTEXT MENU ══════════ */
.cb-ctx-menu {
  position: fixed; z-index: 9999;
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 10px; padding: 5px;
  min-width: 192px;
  box-shadow: 0 8px 32px rgba(0,0,0,.55);
  animation: cbFadeUp .12s ease both;
}
.cb-ctx-item {
  display: block; width: 100%; text-align: left;
  padding: 9px 14px; border: none; background: transparent;
  color: var(--text); font-size: 13px; border-radius: 6px;
  cursor: pointer; touch-action: manipulation;
}
@media (hover: hover) {
  .cb-ctx-item:hover { background: var(--border); }
}
.cb-ctx-item.cb-ctx-danger { color: var(--red); }

/* ══════════ SOUND / EXPORT ICON BUTTONS ══════════ */
.btn-icon {
  padding: 0; width: 40px; min-height: 40px;
  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); }
}

/* ══════════ RESPONSIVE ══════════ */

/* iPad — 6-column element grid, full-width mode tabs */
@media (min-width: 721px) and (max-width: 1024px) {
  .element-selector { grid-template-columns: repeat(6, 1fr); }
  .sim-layout { grid-template-columns: 320px 1fr; }
  .cmpd-tabs .pill { min-height: 40px; padding: 5px 8px; }
}

@media (max-width: 720px) {
  .sim-layout { grid-template-columns: 1fr; }
  .sim-left   { position: static; }
}

@media (max-width: 768px) {
  .readout-panel   { grid-template-columns: repeat(3, 1fr); }
  .explore-cards   { grid-template-columns: 1fr; }
  .info-row        { grid-template-columns: 130px 1fr; }
  .cmpd-group      { width: 100%; }
  .cmpd-tabs       { max-height: 90px; overflow-y: auto; }
  .controls-bar    { gap: 10px; }
}

@media (max-width: 500px) {
  header h1         { font-size: 1.4rem; }
  .subtitle         { font-size: 0.78rem; }
  .readout-panel    { grid-template-columns: repeat(2, 1fr); }
  .pill             { padding: 5px 10px; font-size: 0.76rem; }
  .element-selector { grid-template-columns: repeat(4, 1fr); }
  .rs-atoms         { flex-direction: column; }
  .rs-plus          { transform: rotate(90deg); }
  .info-row         { grid-template-columns: 1fr; gap: 2px; }
  .info-label       { font-size: 11px; }
  .controls-bar     { padding: 10px 12px; gap: 8px; }
}
