/* main.css — playground UI for while.hassiba.cc.
   Light theme + system fonts + monospace for code panes.
   Kept intentionally simple; v1.5 adds dark mode. */

:root {
  --bg: #fafafa;
  --bg-card: #ffffff;
  --bg-soft: #f0f0f0;
  --fg: #1a1a1a;
  --fg-muted: #555;
  --fg-faint: #888;
  --accent: #1565c0;
  --accent-hover: #0d47a1;
  --border: #d8d8d8;
  --border-strong: #888;
  --warn: #b85c00;
  --warn-bg: #fff4e0;
  --error: #b71c1c;
  --error-bg: #ffebee;
  --good: #1b5e20;
  --code-font: "Cascadia Mono", "JetBrains Mono", "Fira Code", "Consolas", "Menlo", monospace;
  --formal-font: "Cambria Math", "STIX Two Math", "Latin Modern Math", Georgia, serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  line-height: 1.4;
}

a { color: var(--accent); }
a:hover { color: var(--accent-hover); }

.page-header {
  padding: 18px 24px 8px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-card);
}
.page-header .header-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}
.page-header h1 {
  margin: 0;
  font-size: 22px;
  font-weight: 600;
  font-family: var(--code-font);
}
.page-header h1 a { color: var(--fg); text-decoration: none; }
.page-header .header-disclaimer {
  font-size: 12px;
  color: var(--fg-faint);
}
.page-header .header-disclaimer abbr {
  text-decoration: none;
  border-bottom: 1px dotted var(--fg-faint);
  cursor: help;
}
.page-header .header-disclaimer a { color: var(--accent); }
.page-header .tagline {
  margin: 4px 0 0;
  color: var(--fg-muted);
  font-size: 14px;
}

/* ── Top-level tabs (Playground / Cheatsheets) ── */
.tabs {
  margin-top: 12px;
  display: flex;
  gap: 4px;
  border-bottom: 1px solid transparent;
}
.tab {
  background: transparent;
  border: 1px solid transparent;
  border-bottom: none;
  padding: 6px 14px;
  font-size: 14px;
  cursor: pointer;
  color: var(--fg-muted);
  border-radius: 6px 6px 0 0;
  font-family: inherit;
  margin-bottom: -1px;
}
.tab:hover { color: var(--fg); background: var(--bg-soft); }
.tab.active {
  background: var(--bg);
  border-color: var(--border);
  color: var(--fg);
  font-weight: 600;
}
.tab.tab-link {
  text-decoration: none;
  display: inline-block;
}
.tab.tab-link:hover { color: var(--accent); }

/* Cheatsheets page sets its own body background so the page doesn't feel
   like an extension of the playground card-grid. */
body.cheatsheets-page { background: var(--bg); }

/* ── Cheatsheets view ── */
.cheatsheets-view {
  padding: 16px 24px 32px;
  max-width: 1100px;
  margin: 0 auto;
}
.cheatsheet-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-bottom: 16px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}
.sheet-tab {
  background: var(--bg-card);
  border: 1px solid var(--border);
  padding: 5px 12px;
  font-size: 13px;
  cursor: pointer;
  color: var(--fg-muted);
  border-radius: 4px;
  font-family: inherit;
}
.sheet-tab:hover { border-color: var(--border-strong); color: var(--fg); }
.sheet-tab.active {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
  font-weight: 500;
}

.cheatsheet-body {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 28px 36px;
  font-family: -apple-system, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  line-height: 1.65;
  color: var(--fg);
  overflow-x: auto;
}
.cheatsheet-loading { color: var(--fg-faint); font-style: italic; text-align: center; }

/* Cheatsheet typography */
.cheatsheet-body h1 { font-size: 26px; margin: 0 0 12px; line-height: 1.25; }
.cheatsheet-body h2 { font-size: 20px; margin: 28px 0 8px; padding-bottom: 4px; border-bottom: 1px solid var(--border); line-height: 1.3; }
.cheatsheet-body h3 { font-size: 17px; margin: 20px 0 6px; line-height: 1.3; }
.cheatsheet-body h4 { font-size: 15px; margin: 16px 0 4px; }
.cheatsheet-body p { margin: 0 0 12px; }
.cheatsheet-body ul, .cheatsheet-body ol { margin: 0 0 12px; padding-left: 26px; }
.cheatsheet-body li { margin: 3px 0; }
.cheatsheet-body code {
  font-family: var(--code-font);
  background: var(--bg-soft);
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 0.9em;
}
.cheatsheet-body pre {
  background: var(--bg-soft);
  padding: 12px 14px;
  border-radius: 4px;
  font-family: var(--code-font);
  font-size: 13px;
  overflow-x: auto;
  margin: 0 0 14px;
}
.cheatsheet-body pre code { background: transparent; padding: 0; }
.cheatsheet-body blockquote {
  border-left: 3px solid var(--accent);
  margin: 12px 0;
  padding: 6px 16px;
  background: rgba(21, 101, 192, 0.04);
  color: var(--fg-muted);
}
.cheatsheet-body hr { border: 0; border-top: 1px solid var(--border); margin: 24px 0; }
.cheatsheet-body table {
  border-collapse: collapse;
  margin: 0 0 14px;
  font-size: 13px;
  width: 100%;
}
.cheatsheet-body th, .cheatsheet-body td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
  vertical-align: top;
}
.cheatsheet-body th { background: var(--bg-soft); font-weight: 600; }
.cheatsheet-body strong { font-weight: 600; }

/* KaTeX display-math: bigger margin and centered */
.cheatsheet-body .katex-display {
  margin: 14px 0;
  overflow-x: auto;
  overflow-y: hidden;
}

.cheatsheet-note {
  margin-top: 20px;
  font-size: 12px;
  color: var(--fg-faint);
  text-align: center;
}

@media (max-width: 720px) {
  .cheatsheet-body { padding: 18px 16px; }
}

.layout {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  padding: 16px;
  min-height: calc(100vh - 200px);
}

.editor-pane,
.result-pane-wrap {
  display: flex;
  flex-direction: column;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
}

.editor-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-soft);
  flex-wrap: wrap;
  gap: 8px;
}
.editor-header label {
  font-size: 13px;
  font-weight: 600;
  color: var(--fg-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.examples { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.examples-label { font-size: 12px; color: var(--fg-faint); margin-right: 2px; }
#examples-select {
  font-size: 12px;
  padding: 3px 8px;
  border: 1px solid var(--border);
  background: var(--bg-card);
  border-radius: 4px;
  cursor: pointer;
  font-family: -apple-system, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  min-width: 240px;
  max-width: 360px;
  color: var(--fg);
}
#examples-select:hover { border-color: var(--border-strong); }
#examples-select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

/* CodeMirror 6 mount: gives the editor a fixed visible area + scroll. */
.editor-mount {
  flex: 1 1 240px;
  min-height: 240px;
  width: 100%;
  background: var(--bg-card);
  overflow: hidden;
  border: none;
  position: relative;
}
.editor-mount .cm-editor {
  height: 100%;
}
.editor-mount .cm-editor.cm-focused {
  outline: none;
}

.theory-pane {
  border-top: 1px solid var(--border);
  background: #fcfaf3;
  border-left: 3px solid var(--accent);
}
.theory-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px 4px;
}
.theory-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.theory-dismiss {
  background: transparent;
  border: none;
  font-size: 18px;
  line-height: 1;
  color: var(--fg-faint);
  cursor: pointer;
  padding: 0 4px;
}
.theory-dismiss:hover { color: var(--fg); }
.theory-body {
  padding: 4px 14px 12px;
  font-size: 13px;
  line-height: 1.55;
  color: var(--fg);
}
.theory-body p { margin: 0 0 8px; }
.theory-body p:last-child { margin-bottom: 0; }
.theory-body code {
  font-family: var(--code-font);
  background: rgba(21, 101, 192, 0.08);
  padding: 1px 4px;
  border-radius: 3px;
  font-size: 0.92em;
}
.theory-body strong { font-weight: 600; }
.theory-body em { font-style: italic; }
.theory-body ul {
  margin: 4px 0 8px;
  padding-left: 20px;
}
.theory-body li { margin: 2px 0; }

.state-pane,
.hoare-pane {
  border-top: 1px solid var(--border);
  padding: 8px 12px;
}
.state-pane summary,
.hoare-pane summary {
  font-size: 13px;
  font-weight: 600;
  color: var(--fg-muted);
  cursor: pointer;
  padding: 4px 0;
}
.state-pane[open] summary,
.hoare-pane[open] summary { margin-bottom: 8px; }

#state-input {
  width: 100%;
  min-height: 28px;
  padding: 8px 10px;
  font-family: var(--code-font);
  font-size: 13px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg);
  /* Native auto-grow (Chrome 123+, FF 124+, Safari 17.4+);
     manual drag-resize still available via the bottom-right handle. */
  field-sizing: content;
  resize: vertical;
  overflow: auto;
}

.hoare-pane label {
  display: block;
  margin: 8px 0;
  font-size: 13px;
  color: var(--fg-muted);
}
.hoare-pane label code { font-size: 12px; color: var(--fg); }
.hoare-pane input,
.hoare-pane textarea {
  width: 100%;
  margin-top: 4px;
  padding: 6px 10px;
  font-family: var(--code-font);
  font-size: 13px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg);
}
.hoare-pane textarea {
  min-height: 28px;
  field-sizing: content;
  resize: vertical;
  overflow: auto;
}
.hoare-pane .hint {
  margin: 8px 0 0;
  font-size: 12px;
  color: var(--fg-faint);
}

/* Hide hoare pane unless tool=hoare; show it when active */
.hoare-pane { display: none; }
body.tool-hoare .hoare-pane { display: block; }

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-soft);
  flex-wrap: wrap;
  gap: 8px;
}
.tool-group, .action-group { display: flex; gap: 4px; flex-wrap: wrap; }
.toolbar button {
  font-size: 13px;
  padding: 4px 12px;
  border: 1px solid var(--border);
  background: var(--bg-card);
  border-radius: 4px;
  cursor: pointer;
  font-family: inherit;
}
.toolbar button:hover { border-color: var(--border-strong); background: var(--bg-soft); }
.toolbar button.active {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}
.toolbar button.primary {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
  font-weight: 600;
}
.toolbar button.primary:hover { background: var(--accent-hover); }

.theme-select-label {
  font-size: 12px;
  color: var(--fg-faint);
  margin-right: 2px;
}
#theme-select {
  font-size: 12px;
  padding: 3px 8px;
  border: 1px solid var(--border);
  background: var(--bg-card);
  border-radius: 4px;
  cursor: pointer;
  font-family: -apple-system, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  color: var(--fg);
}
#theme-select:hover { border-color: var(--border-strong); }
#theme-select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

.result-pane {
  flex: 1;
  padding: 16px;
  overflow: auto;
  font-family: var(--formal-font);
  font-size: 14px;
  line-height: 1.6;
  background: var(--bg-card);
}

.result-pane .status {
  color: var(--fg-faint);
  font-style: italic;
  font-family: inherit;
}

.result-pane pre.trace {
  margin: 0;
  font-family: var(--formal-font);
  font-size: 14px;
  line-height: 1.55;
  white-space: pre;
  color: var(--fg);
  overflow-x: auto;
}

/* ─── Table-view trace ───
   Shown when tool=table. One row per small-step transition; columns are
   step number, rule label, and one column per variable. Cells where a
   value changed from the previous row get a subtle highlight. */
.trace-table-wrap { overflow-x: auto; margin: 0; }
table.trace-table {
  border-collapse: collapse;
  font-family: var(--code-font);
  font-size: 13px;
  line-height: 1.4;
}
table.trace-table thead th {
  position: sticky;
  top: 0;
  background: var(--bg-soft);
  border-bottom: 2px solid var(--border-strong);
  padding: 6px 12px;
  text-align: left;
  font-weight: 600;
  color: var(--fg-muted);
  white-space: nowrap;
}
table.trace-table tbody td {
  padding: 4px 12px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
table.trace-table .tt-step  { color: var(--fg-faint); text-align: right; font-variant-numeric: tabular-nums; }
table.trace-table .tt-rule  { color: var(--fg-muted); font-style: italic; }
table.trace-table .tt-val   { font-variant-numeric: tabular-nums; }
table.trace-table .tt-steps {
  color: var(--fg-muted);
  text-align: right;
  font-variant-numeric: tabular-nums;
  border-left: 1px dashed var(--border);
  padding-left: 14px;
}
table.trace-table .tt-steps-th {
  border-left: 1px dashed var(--border);
  padding-left: 14px;
  text-align: right;
}

/* Direction-aware change highlighting:
   - tt-up    : value increased     (dark blue text on light blue bg)
   - tt-down  : value decreased     (dark red text on light red bg)
   - tt-changed: value changed but not numerically comparable (fallback) */
table.trace-table .tt-up {
  color: #0d47a1;
  background: #e3f2fd;
  font-weight: 600;
}
table.trace-table .tt-down {
  color: #b71c1c;
  background: #ffebee;
  font-weight: 600;
}
table.trace-table .tt-changed {
  background: rgba(0, 0, 0, 0.05);
  font-weight: 600;
}
table.trace-table tr.truncated td {
  text-align: center;
  font-style: italic;
  color: var(--fg-faint);
  padding: 8px 12px;
  background: var(--bg-soft);
}
table.trace-table tbody tr:hover { background: rgba(0, 0, 0, 0.02); }

/* ─── Syntax-highlight tokens inside the trace ───
   Default: plain (everything inherits the surrounding color).
   Overrides for `body.theme-two` and `body.theme-ide` further down. */
.tw-bracket,
.tw-state-brace,
.tw-paren,
.tw-punct,
.tw-kw,
.tw-atom,
.tw-num,
.tw-var,
.tw-op,
.tw-arrow,
.tw-rule,
.tw-rule-name,
.tw-legend-header,
.tw-legend {
  color: inherit;
  font-weight: inherit;
  font-style: inherit;
}
/* The rule label suffix is always slightly muted regardless of theme so it
   reads as metadata, not as part of the configuration. */
.tw-rule { font-size: 0.9em; opacity: 0.75; }

/* ── theme-two: black + blue for keywords + bold := ── */
body.theme-two .tw-kw       { color: #1565c0; font-weight: 600; }
body.theme-two .tw-arrow    { font-weight: 600; }
body.theme-two .tw-rule-name { font-weight: 500; }

/* ── theme-ide: full palette (the original IDE look) ── */
body.theme-ide .tw-bracket     { color: #888; }
body.theme-ide .tw-state-brace { color: #5e35b1; font-weight: 500; }
body.theme-ide .tw-paren       { color: #888; }
body.theme-ide .tw-punct       { color: #888; }
body.theme-ide .tw-kw          { color: #1565c0; font-weight: 600; }
body.theme-ide .tw-atom        { color: #5e35b1; font-weight: 600; }
body.theme-ide .tw-num         { color: #00838f; }
body.theme-ide .tw-var         { color: #1a1a1a; font-style: italic; }
body.theme-ide .tw-op          { color: #b85c00; }
body.theme-ide .tw-arrow       { color: #d32f2f; font-weight: 600; }
body.theme-ide .tw-rule        { color: #888; font-style: italic; }
body.theme-ide .tw-rule-name   { color: #2e7d32; font-weight: 600; font-style: normal; }
body.theme-ide .tw-legend-header { color: var(--fg-muted); font-weight: 600; }
body.theme-ide .tw-legend      { color: var(--fg-muted); }

.show-all { margin-top: 16px; }
.show-all summary {
  cursor: pointer;
  color: var(--accent);
  font-family: inherit;
}
.show-all pre.trace.full { margin-top: 8px; }

.count-result {
  display: flex;
  align-items: baseline;
  gap: 16px;
}
.count-result .label {
  text-transform: uppercase;
  letter-spacing: 0.6px;
  font-size: 12px;
  color: var(--fg-faint);
  font-family: -apple-system, sans-serif;
}
.count-result .number {
  font-family: var(--code-font);
  font-size: 32px;
  font-weight: 600;
  color: var(--accent);
}

table.hoare {
  border-collapse: collapse;
  margin-bottom: 12px;
  font-family: -apple-system, sans-serif;
  font-size: 14px;
}
table.hoare th,
table.hoare td {
  padding: 6px 14px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}
table.hoare th {
  text-align: right;
  color: var(--fg-muted);
  font-weight: 500;
  width: 160px;
}

.warn {
  background: var(--warn-bg);
  color: var(--warn);
  border-left: 3px solid var(--warn);
  padding: 8px 12px;
  margin: 12px 0;
  border-radius: 0 4px 4px 0;
  font-size: 14px;
  font-family: -apple-system, sans-serif;
}

.error-block {
  background: var(--error-bg);
  border-left: 3px solid var(--error);
  padding: 12px 16px;
  border-radius: 0 4px 4px 0;
  font-family: -apple-system, sans-serif;
}
.error-block h3 {
  margin: 0 0 6px;
  color: var(--error);
  font-size: 16px;
}
.error-block p { margin: 0 0 8px; }
.error-block details { margin-top: 8px; font-size: 13px; color: var(--fg-muted); }
.error-block details pre {
  background: var(--bg-soft);
  padding: 8px;
  border-radius: 4px;
  overflow: auto;
  font-family: var(--code-font);
  font-size: 12px;
}

.page-footer {
  padding: 16px 24px;
  text-align: center;
  border-top: 1px solid var(--border);
  background: var(--bg-card);
}
.page-footer .footer-line {
  margin: 0 0 8px;
  font-size: 13px;
  color: var(--fg-muted);
}
.page-footer .footer-disclaimer {
  margin: 0 auto;
  max-width: 760px;
  font-size: 12px;
  line-height: 1.5;
  color: var(--fg-faint);
}
.page-footer a { color: var(--accent); }

#toast {
  position: fixed;
  bottom: 32px;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: #222;
  color: #fff;
  padding: 8px 16px;
  border-radius: 6px;
  font-size: 14px;
  opacity: 0;
  transition: transform 0.2s, opacity 0.2s;
  z-index: 9999;
  pointer-events: none;
  max-width: 90vw;
}
#toast.show { transform: translateX(-50%) translateY(0); opacity: 1; }
#toast.warn { background: var(--warn); }

/* Mobile-ish: stack at narrow widths. Not v1 polished but not broken. */
@media (max-width: 720px) {
  .layout {
    grid-template-columns: 1fr;
  }
}
