/*
 * The Code Crunch browser IDE.
 *
 * Two panes on a desktop, stacked on a phone. The editor and the output are
 * each their own scroll region inside a bounded shell, so a long traceback
 * cannot push the Run button off screen — and the shell is bounded rather
 * than 100dvh because the page carries the global nav and the org footer.
 *
 * Every class is prefixed `ccide-` and every custom property `--ccide-`. This
 * page sits inside the shared site chrome, which already owns generic names
 * like .page, .actions and .output; unprefixed rules here would style the nav
 * and the footer as well as the editor.
 */

.ccide {
  padding: 18px 14px 26px;
}

/*
 * The palette lives on the editor surface, not on the section.
 *
 * It used to sit on .ccide, which also wraps the intro paragraph -- so picking
 * the dark theme repainted that paragraph's text light while it still sat on
 * the page's light background, and it all but disappeared. A theme is a
 * property of the editor, and nothing outside .ccide-page should answer to it.
 */
.ccide-page {
  --ccide-ink: #1c2430;
  --ccide-ink-soft: #3a4452;
  --ccide-muted: #6a6157;
  --ccide-rule: #e6e8ec;
  --ccide-parchment: #f7f8fa;
  --ccide-parchment-2: #fff;
  --ccide-accent: #2463eb;
}

.ccide *,
.ccide *::before,
.ccide *::after {
  box-sizing: border-box;
}

.ccide-lede {
  max-width: 78ch;
  margin: 0 auto 16px;
  font-size: 0.95rem;
  line-height: 1.6;
  /* The page's own tokens, so this copy stays readable on the page background
     whatever theme the editor is set to. */
  color: var(--color-text-muted, #5f6368);
}

.ccide-lede strong {
  color: var(--color-text, #202124);
}

.ccide-page {
  display: flex;
  flex-direction: column;
  height: min(72vh, 780px);
  min-height: 460px;
  margin: 0 auto;
  max-width: 1280px;
  border: 1px solid var(--ccide-rule);
  border-radius: 12px;
  overflow: hidden;
  background: var(--ccide-parchment-2);
  color: var(--ccide-ink);
  box-shadow: 0 10px 30px rgba(20, 36, 72, 0.08);
}

.ccide-topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 14px;
  border-bottom: 1px solid var(--ccide-rule);
  background: var(--ccide-parchment);
}

.ccide-heading {
  display: flex;
  align-items: baseline;
  gap: 10px;
  min-width: 0;
  flex: 1;
}

.ccide-picker {
  display: inline-flex;
}

.ccide-select {
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  min-height: 32px;
  padding: 4px 8px;
  color: var(--ccide-ink);
  background: var(--ccide-parchment-2);
  border: 1px solid var(--ccide-rule);
  border-radius: 8px;
  cursor: pointer;
}
.ccide-select:hover {
  border-color: var(--ccide-accent);
}

/* The download size is the thing a reader on a phone actually cares about,
   so it sits next to the language rather than in a tooltip. */
.ccide-weight {
  font-size: 0.75rem;
  color: var(--ccide-muted);
  white-space: nowrap;
}

.ccide-sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.ccide-subtitle {
  font-size: 0.8rem;
  color: var(--ccide-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ccide-actions {
  display: flex;
  gap: 6px;
}

.ccide-ghost,
.ccide-run,
.ccide-clear {
  min-height: 32px;
  padding: 4px 14px;
  font: inherit;
  font-size: 0.78rem;
  border-radius: 999px;
  border: 1px solid var(--ccide-rule);
  cursor: pointer;
}

.ccide-ghost,
.ccide-clear {
  color: var(--ccide-ink-soft);
  background: var(--ccide-parchment-2);
}
.ccide-ghost:hover,
.ccide-clear:hover {
  border-color: var(--ccide-accent);
  color: var(--ccide-accent);
}

.ccide-run {
  color: var(--ccide-parchment-2);
  background: var(--ccide-accent);
  border-color: var(--ccide-accent);
}
.ccide-run:disabled {
  opacity: 0.55;
  cursor: default;
}

.ccide [hidden] {
  display: none !important;
}

/* ------------------------------------------------------------------ files */

.ccide-filebar {
  flex: 0 0 auto;
  max-height: 168px;
  overflow-y: auto;
  padding: 8px 14px 10px;
  border-bottom: 1px solid var(--ccide-rule);
  background: var(--ccide-parchment);
}

.ccide-filebar-head {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.ccide-filebar-note {
  font-size: 0.75rem;
  color: var(--ccide-muted);
}

.ccide-filelist {
  list-style: none;
  margin: 8px 0 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.ccide-filelist:empty {
  margin: 0;
}

.ccide-filelist li {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 4px 6px 4px 10px;
  border: 1px solid var(--ccide-rule);
  border-radius: 999px;
  background: var(--ccide-parchment-2);
  font-size: 0.76rem;
  max-width: 100%;
}

/* A file the program wrote or changed, rather than one the reader attached. */
.ccide-filelist li.ccide-is-output {
  border-color: var(--ccide-accent);
}

.ccide-file-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ccide-file-size {
  color: var(--ccide-muted);
  white-space: nowrap;
}

.ccide-file-tag {
  color: var(--ccide-accent);
  font-size: 0.68rem;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  white-space: nowrap;
}

.ccide-file-act {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  padding: 0;
  font: inherit;
  font-size: 0.8rem;
  line-height: 1;
  color: var(--ccide-ink-soft);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 50%;
  cursor: pointer;
}
.ccide-file-act:hover {
  border-color: var(--ccide-accent);
  color: var(--ccide-accent);
}

/* Drag a file anywhere over the page and the whole editor says so. */
.ccide-page.ccide-is-dropping {
  outline: 2px dashed var(--ccide-accent);
  outline-offset: -4px;
}

/* ------------------------------------------------------------- the panes */

.ccide-split {
  display: grid;
  grid-template-columns: 1fr 1fr;
  flex: 1;
  min-height: 0;
}

.ccide-editor-pane {
  display: flex;
  min-height: 0;
  border-right: 1px solid var(--ccide-rule);
}

.ccide-gutter {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding: 12px 8px 12px 12px;
  text-align: right;
  user-select: none;
  color: var(--ccide-muted);
  background: var(--ccide-parchment);
  border-right: 1px solid var(--ccide-rule);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 15px;
  line-height: 1.55;
}

.ccide-editor,
.ccide-stdin {
  display: block;
  width: 100%;
  border: none;
  resize: none;
  padding: 12px 14px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  /* 16px keeps iOS Safari from zooming the viewport on focus; the gutter uses
     15px + the same line-height so the numbers still line up. */
  font-size: 16px;
  line-height: 1.55;
  color: var(--ccide-ink);
  background: var(--ccide-parchment-2);
  tab-size: 4;
  white-space: pre;
  overflow: auto;
}
.ccide-editor:focus,
.ccide-stdin:focus {
  outline: 2px solid var(--ccide-accent);
  outline-offset: -2px;
}

.ccide-output-pane {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.ccide-stdin {
  flex: 0 0 96px;
  border-bottom: 1px dashed var(--ccide-rule);
  background: var(--ccide-parchment);
  white-space: pre-wrap;
}

.ccide-output-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 8px 6px 14px;
  border-bottom: 1px solid var(--ccide-rule);
  background: var(--ccide-parchment);
  font-size: 0.72rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--ccide-muted);
}

.ccide-output {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px 14px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.85rem;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}

.ccide-hint,
.ccide-status {
  color: var(--ccide-muted);
  font-style: italic;
}

.ccide-stderr {
  color: #b42318;
}

@media (max-width: 820px) {
  .ccide-split {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr 1fr;
  }
  .ccide-editor-pane {
    border-right: none;
    border-bottom: 1px solid var(--ccide-rule);
  }
  .ccide-weight,
  .ccide-subtitle {
    display: none;
  }
}

/* =========================================================================
   Syntax colouring, themes, maximise, and the help panel.
   ========================================================================= */

/* ------------------------------------------------- the highlight overlay */

/*
 * A textarea cannot colour its own text, so the editor is two layers: a <pre>
 * showing coloured HTML, and the real textarea on top with transparent text
 * and a visible caret. They must share every metric that affects layout --
 * font, size, line-height, padding, tab-size, wrapping -- or the caret drifts
 * away from the glyphs beneath it.
 */
.ccide-code {
  position: relative;
  flex: 1;
  min-width: 0;
  min-height: 0;
}

.ccide-hl,
.ccide-code .ccide-editor {
  position: absolute;
  inset: 0;
  margin: 0;
  padding: 12px 14px;
  border: 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 16px;
  line-height: 1.55;
  tab-size: 4;
  white-space: pre;
  overflow: auto;
}

.ccide-hl {
  z-index: 0;
  pointer-events: none;
  color: var(--ccide-ink);
  background: var(--ccide-editor-bg, var(--ccide-parchment-2));
}

.ccide-hl code {
  font: inherit;
  white-space: inherit;
}

.ccide-code .ccide-editor {
  z-index: 1;
  width: 100%;
  height: 100%;
  resize: none;
  background: transparent;
  /* The glyphs come from the layer underneath; only the caret and the
     selection belong to the textarea. */
  color: transparent;
  caret-color: var(--ccide-ink);
}

.ccide-code .ccide-editor::selection {
  background: var(--ccide-selection, rgba(36, 99, 235, 0.24));
  color: transparent;
}

/* ------------------------------------------------------------ the themes */

/* Light is the default and lives on the editor surface itself, so a page that
   never sets a theme still gets a complete palette. */
.ccide-page {
  --ccide-editor-bg: #ffffff;
  --ccide-selection: rgba(36, 99, 235, 0.24);
  --ccide-t-com: #6a737d;
  --ccide-t-str: #0a7d33;
  --ccide-t-num: #b4530a;
  --ccide-t-kw: #a626a4;
  --ccide-t-fn: #2463eb;
  --ccide-t-op: #3a4452;
}

.ccide[data-ccide-theme="dark"] .ccide-page {
  --ccide-ink: #e6e9ef;
  --ccide-ink-soft: #c2c8d4;
  --ccide-muted: #8b93a4;
  --ccide-rule: #2b3242;
  --ccide-parchment: #161b26;
  --ccide-parchment-2: #1d2331;
  --ccide-accent: #6ea8fe;
  --ccide-editor-bg: #1d2331;
  --ccide-selection: rgba(110, 168, 254, 0.28);
  --ccide-t-com: #7d8799;
  --ccide-t-str: #7ed492;
  --ccide-t-num: #f0a868;
  --ccide-t-kw: #d79bf0;
  --ccide-t-fn: #6ea8fe;
  --ccide-t-op: #c2c8d4;
}

/* Deliberately not just "darker": maximum contrast against black, for anyone
   who finds the mid greys of the other two hard to read. */
.ccide[data-ccide-theme="contrast"] .ccide-page {
  --ccide-ink: #ffffff;
  --ccide-ink-soft: #e8e8e8;
  --ccide-muted: #b9b9b9;
  --ccide-rule: #4a4a4a;
  --ccide-parchment: #000000;
  --ccide-parchment-2: #0a0a0a;
  --ccide-accent: #ffd400;
  --ccide-editor-bg: #000000;
  --ccide-selection: rgba(255, 212, 0, 0.32);
  --ccide-t-com: #9fd1a0;
  --ccide-t-str: #7cf07c;
  --ccide-t-num: #ffb454;
  --ccide-t-kw: #ffd400;
  --ccide-t-fn: #61dafb;
  --ccide-t-op: #ffffff;
}

.ccide[data-ccide-theme="dark"] .ccide-stderr,
.ccide[data-ccide-theme="contrast"] .ccide-stderr {
  color: #ff8a80;
}

.ccide-t-com { color: var(--ccide-t-com); font-style: italic; }
.ccide-t-str { color: var(--ccide-t-str); }
.ccide-t-num { color: var(--ccide-t-num); }
.ccide-t-kw  { color: var(--ccide-t-kw); font-weight: 600; }
.ccide-t-fn  { color: var(--ccide-t-fn); }
.ccide-t-op  { color: var(--ccide-t-op); }

/* --------------------------------------------------------- maximise mode */

/*
 * Fills the viewport with just the editor. The nav is fixed at z-index 50, so
 * this has to sit above it to actually cover the screen.
 */
.ccide-page.ccide-max {
  position: fixed;
  inset: 0;
  z-index: 2000;
  max-width: none;
  height: 100vh;
  height: 100dvh;
  margin: 0;
  border: 0;
  border-radius: 0;
  box-shadow: none;
}

body.ccide-maxed {
  overflow: hidden;
}

/* ------------------------------------------------------------ help panel */

.ccide-help {
  flex: 0 0 auto;
  max-height: 240px;
  overflow-y: auto;
  padding: 12px 14px 14px;
  border-bottom: 1px solid var(--ccide-rule);
  background: var(--ccide-parchment);
  font-size: 0.82rem;
  line-height: 1.55;
  color: var(--ccide-ink-soft);
}

.ccide-help dl {
  margin: 0;
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 6px 14px;
  align-items: baseline;
}

.ccide-help dt {
  font-weight: 700;
  color: var(--ccide-ink);
  white-space: nowrap;
}

.ccide-help dd {
  margin: 0;
}

.ccide-help code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.92em;
  padding: 1px 5px;
  border-radius: 5px;
  background: var(--ccide-parchment-2);
  border: 1px solid var(--ccide-rule);
  color: var(--ccide-ink);
}

@media (max-width: 640px) {
  .ccide-help dl {
    grid-template-columns: 1fr;
    gap: 2px;
  }
  .ccide-help dd {
    margin: 0 0 8px;
  }
}

/*
 * The bar carries eight controls once theme, help and maximise are on it, and
 * on a phone that is wider than the screen. It does not scroll -- the shell
 * clips it -- so without this Run sits past the right edge and cannot be
 * pressed at all. Let it use two rows instead: the language and theme menus
 * on one, the buttons on the next.
 */
@media (max-width: 700px) {
  .ccide-topbar {
    flex-wrap: wrap;
    gap: 8px;
  }
  .ccide-heading {
    flex: 1 1 100%;
  }
  .ccide-actions {
    flex: 1 1 100%;
    flex-wrap: wrap;
    justify-content: flex-end;
  }
  .ccide-ghost,
  .ccide-run {
    padding: 4px 10px;
  }
}

/* ------------------------------------------------------- the split divider */

/*
 * The panes are sized from one custom property so the divider only ever has
 * to move that number; the grid does the rest, and the same rule works when
 * the layout flips to rows on a narrow screen.
 */
.ccide-split {
  grid-template-columns: var(--ccide-split, 50%) 8px minmax(0, 1fr);
}

.ccide-gripper {
  position: relative;
  cursor: col-resize;
  background: var(--ccide-parchment);
  border-left: 1px solid var(--ccide-rule);
  border-right: 1px solid var(--ccide-rule);
  touch-action: none;
}

/* A 8px target is fine for a mouse and cruel on a finger, so the grab area
   reaches past the visible bar without moving anything on screen. */
.ccide-gripper::before {
  content: "";
  position: absolute;
  inset: 0 -6px;
}

/* The two lines that say "this is a handle" rather than a border. */
.ccide-gripper::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 2px;
  height: 26px;
  transform: translate(-50%, -50%);
  border-left: 2px dotted var(--ccide-muted);
  opacity: 0.85;
}

.ccide-gripper:hover,
.ccide-gripper:focus-visible {
  background: var(--ccide-accent);
  outline: none;
}

.ccide-gripper:hover::after,
.ccide-gripper:focus-visible::after {
  border-color: var(--ccide-parchment-2);
}

/* While dragging, the pointer must not select text or flip cursors as it
   crosses the panes. */
body.ccide-dragging,
body.ccide-dragging * {
  cursor: col-resize !important;
  user-select: none !important;
}

body.ccide-dragging-rows,
body.ccide-dragging-rows * {
  cursor: row-resize !important;
  user-select: none !important;
}

@media (max-width: 820px) {
  .ccide-split {
    grid-template-columns: 1fr;
    grid-template-rows: var(--ccide-split, 50%) 8px minmax(0, 1fr);
  }
  .ccide-gripper {
    cursor: row-resize;
    border-left: 0;
    border-right: 0;
    border-top: 1px solid var(--ccide-rule);
    border-bottom: 1px solid var(--ccide-rule);
  }
  .ccide-gripper::before {
    inset: -6px 0;
  }
  .ccide-gripper::after {
    width: 26px;
    height: 2px;
    border-left: 0;
    border-top: 2px dotted var(--ccide-muted);
  }
  .ccide-gripper:hover::after,
  .ccide-gripper:focus-visible::after {
    border-top-color: var(--ccide-parchment-2);
  }
}

/* ------------------------------------------------------------ icon button */

/*
 * The maximise control is an icon rather than a word, and the icon is drawn
 * here as plain line geometry -- four corner brackets, out for maximise and in
 * for restore. Nothing is taken from an icon set, so no licence or attribution
 * travels with the page. Font Awesome is loaded for the shared nav, but its
 * free icons carry a CC BY requirement, which is not worth inheriting for a
 * shape this simple.
 *
 * The accessible name lives in a visually hidden span and in aria-label, so
 * the button still announces itself as "Maximise" or "Restore".
 */
.ccide-icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  padding: 4px;
}

.ccide-icon {
  width: 15px;
  height: 15px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.ccide-icon-contract {
  display: none;
}

#ide-max[aria-pressed="true"] .ccide-icon-expand {
  display: none;
}

#ide-max[aria-pressed="true"] .ccide-icon-contract {
  display: block;
}
