/* Tildagon studio — editor, programs drawer, and live badge emulator.
   Dark, dense, lime accent; tokens transcribed from the design handoff. */

/* IBM Plex, vendored by build.sh into dist/fonts/ (the site stays zero-CDN).
   Opening src/ directly just falls back to the system stacks below. */
@font-face {
  font-family: "IBM Plex Sans";
  font-style: normal;
  font-weight: 100 700;
  font-display: swap;
  src: url("fonts/plex-sans-var.woff2") format("woff2");
}
@font-face {
  font-family: "IBM Plex Mono";
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url("fonts/plex-mono-400.woff2") format("woff2");
}
@font-face {
  font-family: "IBM Plex Mono";
  font-style: normal;
  font-weight: 500;
  font-display: swap;
  src: url("fonts/plex-mono-500.woff2") format("woff2");
}
@font-face {
  font-family: "IBM Plex Mono";
  font-style: normal;
  font-weight: 600;
  font-display: swap;
  src: url("fonts/plex-mono-600.woff2") format("woff2");
}

:root {
  --bg: #141414;          /* app / dock */
  --page: #0f0f0f;
  --bar: #171717;         /* top bar */
  --flyout: #181818;
  --editor-bg: #161616;
  --card: #191919;        /* info modal */
  --console: #1b1b1b;
  --input: #1d1d1d;
  --btn: #232323;         /* secondary buttons */
  --btn-hover: #2e2e2e;

  --text: #f2f2ef;
  --text-2: #e8e8e5;
  --text-soft: #cfcfca;
  --muted: #9a9a95;
  --dim: #8b8b86;
  --faint: #7d7d78;
  --fainter: #6a6a66;
  --gutter: #4a4a46;

  --accent: #aecb3a;
  --accent-text: #c5df5e;
  --accent-10: rgba(174, 203, 58, 0.1);
  --accent-12: rgba(174, 203, 58, 0.12);
  --accent-14: rgba(174, 203, 58, 0.14);
  --accent-22: rgba(174, 203, 58, 0.22);
  --accent-border: rgba(174, 203, 58, 0.4);

  --flash: #f4c430;
  --flash-12: rgba(244, 196, 48, 0.12);
  --flash-16: rgba(244, 196, 48, 0.16);
  --flash-20: rgba(244, 196, 48, 0.2);
  --flash-26: rgba(244, 196, 48, 0.26);
  --flash-border: rgba(244, 196, 48, 0.5);

  --error: #e07a7a;
  --warn: #d9a441;

  --badge-blue: #2f6ff0;         /* the on-badge accent (USB / /apps) */
  --badge-blue-text: #7fd4ff;
  --badge-blue-head: #7fb0ff;

  --line-06: rgba(255, 255, 255, 0.06);
  --line-07: rgba(255, 255, 255, 0.07);
  --line-08: rgba(255, 255, 255, 0.08);
  --line-09: rgba(255, 255, 255, 0.09);
  --line-10: rgba(255, 255, 255, 0.1);
  --line-12: rgba(255, 255, 255, 0.12);
  --line-14: rgba(255, 255, 255, 0.14);

  --sans: "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text-2);
  font-family: var(--sans);
  font-size: 14px;
}

body { display: flex; flex-direction: column; }

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

button { font-family: inherit; }
.spacer { flex: 1; }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb {
  background: var(--line-12);
  border-radius: 6px;
  border: 2px solid transparent;
  background-clip: padding-box;
}
::-webkit-scrollbar-track { background: transparent; }

/* --- top bar ---------------------------------------------------------------- */
header {
  flex: none;
  display: flex;
  align-items: center;
  gap: 12px;
  height: 56px;
  padding: 0 18px;
  background: var(--bar);
  border-bottom: 1px solid var(--line-07);
}

.brand {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 15px;
  color: #e9e9e6;
  white-space: nowrap;
}
.brand strong { font-weight: 600; }
.brand-hex { color: var(--accent); font-size: 17px; }
.brand-sub { color: var(--dim); }

/* Programs button — opens the drawer; lime "active" tint while it's open. */
#btn-programs {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 34px;
  padding: 0 13px;
  margin-left: 6px;
  border-radius: 8px;
  border: 1px solid var(--line-14);
  background: var(--btn);
  color: var(--text-soft);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
}
#btn-programs:hover { background: #2a2a2a; }
body.drawer-open #btn-programs {
  background: var(--accent-14);
  border-color: var(--accent-border);
}
.count-badge {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--faint);
}

/* Device-status pill — persistent connection indicator. */
#device-pill {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 32px;
  padding: 0 12px;
  border-radius: 8px;
  background: #1a1a1a;
  border: 1px solid var(--line-09);
  font-family: var(--mono);
  font-size: 12px;
  color: #c9c9c4;
  cursor: default;
  white-space: nowrap;
}
#device-pill.can-connect { cursor: pointer; }
#device-pill.can-connect:hover { background: #202020; }
#device-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #5a5a55;
  flex: none;
}
#device-pill.connected #device-dot {
  background: var(--accent);
  box-shadow: 0 0 7px rgba(174, 203, 58, 0.7);
}

/* Flash to badge — the primary action, amber. */
#btn-flash {
  display: flex;
  align-items: center;
  gap: 7px;
  height: 34px;
  padding: 0 15px;
  border-radius: 8px;
  border: 1px solid var(--flash-border);
  background: var(--flash-16);
  color: var(--flash);
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
#btn-flash:hover:not(:disabled) { background: var(--flash-26); }
#btn-flash:disabled { opacity: 0.45; cursor: default; }

#btn-new-top {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 34px;
  padding: 0 13px;
  border-radius: 8px;
  border: 1px solid var(--line-14);
  background: var(--btn);
  color: #e6e6e3;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
}
#btn-new-top:hover { background: var(--btn-hover); }

.info-btn {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  border: 1px solid var(--line-12);
  background: var(--btn);
  color: var(--text-soft);
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
}
.info-btn:hover { background: var(--btn-hover); }

#link-repo {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

/* --- main layout -------------------------------------------------------------- */
main {
  flex: 1;
  display: flex;
  min-height: 0;
}

#stage {
  flex: 1;
  min-width: 0;
  display: flex;
  position: relative;
}

/* --- programs drawer -------------------------------------------------------- */
/* Slides in from the left over the body; a scrim dims the editor + emulator.
   Clicking the scrim (or ✕, or Esc) closes it. */
#drawer[hidden] { display: none; }
#drawer { position: absolute; inset: 0; z-index: 70; }

#drawer-scrim {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
}

.drawer-panel {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 326px;
  max-width: 85vw;
  z-index: 20;
  background: var(--flyout);
  border-right: 1px solid var(--line-10);
  box-shadow: 14px 0 40px rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
}

.drawer-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 15px 16px 11px;
}
.drawer-title {
  flex: 1;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.08em;
  color: var(--dim);
  text-transform: uppercase;
}
#btn-drawer-close {
  width: 26px;
  height: 26px;
  border-radius: 7px;
  border: 1px solid var(--line-12);
  background: var(--btn);
  color: var(--text-soft);
  cursor: pointer;
  font-size: 12px;
}
#btn-drawer-close:hover { background: var(--btn-hover); }

.drawer-search {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 32px;
  margin: 0 16px 10px;
  padding: 0 10px;
  border-radius: 8px;
  background: var(--input);
  border: 1px solid var(--line-08);
  flex: none;
}
.search-glyph { font-size: 12px; }
#gallery-search {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-size: 13px;
  color: var(--text-2);
  background: transparent;
  border: none;
  outline: none;
  padding: 0;
}
#gallery-search::placeholder { color: #6f6f6a; }
#gallery-search::-webkit-search-cancel-button { display: none; }

#gallery-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 0 8px 16px;
}

/* display:flex would defeat the hidden attribute (search filter, empty
   sections) — same trap as #drawer[hidden]/#screen-boot[hidden] below. */
.card[hidden], .drawer-section[hidden] { display: none; }

.drawer-section {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.08em;
  color: var(--dim);
  text-transform: uppercase;
}
.drawer-section .count { color: var(--gutter); }
.drawer-section .rule { flex: 1; height: 1px; background: var(--line-06); }

#mine-empty {
  margin: 2px 8px 4px;
  font-size: 12px;
  line-height: 1.5;
  color: var(--faint);
}
#mine-empty b { color: var(--text-soft); font-weight: 600; }

.drawer-divider {
  height: 1px;
  background: var(--line-06);
  margin: 16px 8px 4px;
}

/* Samples disclosure — the bundled demos, demoted to a quiet collapsed
   section at the bottom of your programs. */
#btn-samples {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  border: none;
  background: none;
  cursor: pointer;
  font-family: var(--sans);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--fainter);
}
#btn-samples[hidden] { display: none; }
#btn-samples:hover { color: var(--muted); }
#btn-samples .count { color: var(--gutter); }
#btn-samples .spacer { flex: 1; }
#btn-samples .chevron { font-size: 9px; width: 10px; display: inline-block; }
.samples-caption {
  font-size: 10px;
  font-weight: 400;
  letter-spacing: 0;
  text-transform: none;
  color: #565650;
}

#samples-list[hidden] { display: none; }
#samples-list { padding-top: 2px; opacity: 0.85; }

/* On-the-badge disclosure — same shape as Samples, but the blue USB accent so
   it reads as "the physical device", distinct from the neutral sections. */
#btn-badge-sec {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  border: none;
  background: none;
  cursor: pointer;
  font-family: var(--sans);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--badge-blue-head);
}
#btn-badge-sec[hidden] { display: none; }
#btn-badge-sec:hover { color: #a9ccff; }
#btn-badge-sec .count { color: #4a5a72; }
#btn-badge-sec .spacer { flex: 1; }
#btn-badge-sec .chevron { font-size: 9px; width: 10px; display: inline-block; }
.badge-caption {
  font-family: var(--mono);
  font-size: 10px;
  font-weight: 400;
  letter-spacing: 0;
  text-transform: none;
  color: #4a5a72;
}
#badge-list[hidden] { display: none; }
#badge-list { padding: 2px 0 0; }

/* "Read apps from badge" call-to-action (before the first read this session). */
.badge-cta {
  width: calc(100% - 12px);
  margin: 2px 6px 4px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  align-items: flex-start;
  padding: 10px 12px;
  border: 1px dashed rgba(47, 111, 240, 0.4);
  background: rgba(47, 111, 240, 0.06);
  border-radius: 9px;
  color: #93bdf2;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  text-align: left;
}
.badge-cta:hover { background: rgba(47, 111, 240, 0.12); }
.badge-cta .badge-cta-sub {
  font-family: var(--mono);
  font-size: 10px;
  color: #6f7f95;
}
.badge-note {
  margin: 4px 8px 8px;
  font-size: 12px;
  color: var(--dim);
  line-height: 1.4;
}
.badge-reread {
  display: block;
  margin: 4px 8px 6px;
  border: none;
  background: none;
  padding: 2px;
  font-family: var(--mono);
  font-size: 10.5px;
  color: #6f7f95;
  cursor: pointer;
}
.badge-reread:hover { color: #93bdf2; }

/* Compiled-format tag on a badge app's card. */
.card-format {
  color: var(--dim);
  font: 9.5px var(--mono);
  margin-left: 6px;
}

/* --- demo card (row layout) --------------------------------------------------------- */
.card {
  position: relative;
  display: flex;
  gap: 11px;
  align-items: center;
  padding: 8px;
  border-radius: 9px;
}
.card:hover { background: #1e1e1e; }
.card.active::before {
  content: "";
  position: absolute;
  left: 1px;
  top: 9px;
  bottom: 9px;
  width: 3px;
  border-radius: 3px;
  background: var(--accent);
}
.card-open {
  flex: 1;
  min-width: 0;
  align-self: stretch;
  display: flex;
  gap: 11px;
  align-items: center;
  padding: 0;
  border: none;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.card-open:focus-visible {
  outline: 2px solid var(--accent-border);
  outline-offset: 2px;
  border-radius: 5px;
}

.card .thumb {
  width: 66px;
  height: 42px;
  border-radius: 6px;
  overflow: hidden;
  flex: none;
  border: 1px solid var(--line-08);
}

.card-text { display: block; flex: 1; min-width: 0; }
.card-name {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.card-meta {
  display: block;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--faint);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card-actions { display: flex; gap: 5px; flex: none; }
.card-actions button {
  width: 22px;
  height: 22px;
  border-radius: 6px;
  background: rgba(30, 30, 30, 0.9);
  border: 1px solid var(--line-10);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  padding: 0;
  cursor: pointer;
  opacity: 0.7;
  transition: opacity 0.12s;
}
.card-actions button:hover { opacity: 1; }
.card-flash { color: var(--flash); }
.card-dup { color: var(--text-soft); }
.card-del { color: var(--error); }

/* preview thumbnails: captured badge frames on a skeleton placeholder */
.thumb {
  background: #0a0a0a;
  position: relative;
}
/* Drawn as the round badge screen: a circle as tall as the tile, centred.
   The screen disc is the inscribed circle of the square framebuffer, so a
   full-bleed square image shown round IS exactly the screen. */
.thumb.has-preview::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  height: 100%;
  aspect-ratio: 1;
  border-radius: 50%;
  background-image: var(--preview);
  background-size: 100% 100%;
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.07);
}
.thumb:not(.has-preview)::after {
  content: "";
  position: absolute;
  inset: 0;
  background:
    radial-gradient(circle at 50% 50%, rgba(255, 255, 255, 0.05) 0 38%, transparent 40%),
    linear-gradient(100deg, transparent 30%, rgba(255, 255, 255, 0.035) 50%, transparent 70%);
  background-size: 100% 100%, 220% 100%;
  animation: thumb-sheen 2.2s linear infinite;
}
@keyframes thumb-sheen {
  from { background-position: 0 0, -120% 0; }
  to { background-position: 0 0, 120% 0; }
}

/* --- editor pane -------------------------------------------------------------------- */
#editor-pane {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--editor-bg);
}

#toolbar {
  flex: none;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line-07);
  overflow-x: auto;
  scrollbar-width: thin;
}

#run-dot {
  flex: none;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 8px rgba(174, 203, 58, 0.6);
  cursor: help;
}
#run-dot.stale {
  background: var(--warn);
  box-shadow: 0 0 8px rgba(216, 178, 74, 0.65);
}
#run-dot.pending {
  background: var(--warn);
  box-shadow: 0 0 8px rgba(216, 178, 74, 0.55);
}

#run-status {
  flex: none;
  display: flex;
  align-items: center;
  min-height: 32px;
  padding: 6px 18px;
  border-top: 1px solid rgba(216, 178, 74, 0.22);
  color: #d7c183;
  background: rgba(216, 178, 74, 0.08);
  font-size: 12.5px;
  line-height: 1.35;
}
#run-status[hidden] { display: none; }

/* --- project file tabs ----------------------------------------------------- */
#file-tabs {
  flex: none;
  display: flex;
  align-items: stretch;
  gap: 2px;
  padding: 6px 12px 0;
  border-bottom: 1px solid var(--line-07);
  overflow-x: auto;
  scrollbar-width: thin;
  background: rgba(255, 255, 255, 0.015);
}
#file-tabs[hidden] { display: none; }
.file-tab {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 5px 12px;
  border: 1px solid transparent;
  border-bottom: none;
  border-radius: 7px 7px 0 0;
  background: none;
  color: #9a9a94;
  font: 500 12.5px/1.2 var(--mono, ui-monospace, monospace);
  cursor: pointer;
  white-space: nowrap;
}
.file-tab:hover { color: #d5d5cf; background: rgba(255, 255, 255, 0.04); }
.file-tab.active {
  color: #f0f0ec;
  background: var(--editor-bg);
  border-color: var(--line-07);
  position: relative;
}
.file-tab.active::after {
  /* cover the tab bar's bottom border so the active tab joins the editor */
  content: "";
  position: absolute;
  left: 0; right: 0; bottom: -1px;
  height: 1px;
  background: var(--editor-bg);
}
.file-tab .file-tab-c { color: #82aaff; }
.file-tab.active .file-tab-c { color: #a9c5ff; }
.file-tab.has-error { color: #e07a7a; }
.file-tab.has-error::before {
  content: "●";
  font-size: 8px;
  color: #e07a7a;
}
.file-tab-close {
  font-size: 10px;
  color: #6f6f69;
  padding: 2px 3px;
  border-radius: 4px;
}
.file-tab-close:hover { color: #e07a7a; background: rgba(224, 122, 122, 0.12); }
.file-add { position: relative; display: inline-flex; }
.file-add-btn { font-size: 14px; padding: 4px 10px; color: #7d7d78; }
.file-add-menu {
  position: absolute;
  top: 100%;
  left: 0;
  z-index: 30;
  min-width: 240px;
  background: #1d1d1d;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 8px;
  padding: 4px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.5);
}
.file-add-menu[hidden] { display: none; }
.file-add-menu button {
  display: block;
  width: 100%;
  text-align: left;
  padding: 8px 10px;
  background: none;
  border: none;
  border-radius: 6px;
  color: #e8e8e3;
  font-size: 13px;
  cursor: pointer;
}
.file-add-menu button:hover { background: rgba(174, 203, 58, 0.12); }
.file-add-hint {
  display: block;
  margin-top: 2px;
  font-size: 11px;
  color: #8b8b85;
}
#file-hint {
  flex: none;
  padding: 5px 18px;
  font-size: 12px;
  line-height: 1.4;
  color: #9fb3d9;
  background: rgba(130, 170, 255, 0.07);
  border-bottom: 1px solid rgba(130, 170, 255, 0.14);
}
#file-hint[hidden] { display: none; }
#file-hint b { color: #c9d9f7; }
#file-hint code {
  font-family: var(--mono, ui-monospace, monospace);
  color: #c9d9f7;
}
#file-hint .build-ok { color: #9fd97a; }
#file-hint .build-err { color: #e07a7a; }
#file-hint .build-todo { color: #d7c183; }
#file-hint .build-busy { color: #9fb3d9; }
#file-hint .build-btn {
  margin-left: 2px;
  padding: 2px 10px;
  font-size: 11.5px;
}
#file-hint .hint-sep { color: #5c6b86; margin: 0 8px; }

/* --- sensor row: tilt pad + device-motion toggle ---------------------------- */
#sensor-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 14px 0;
  font-size: 11.5px;
  color: #8b8b85;
}
#tilt-pad {
  position: relative;
  flex: none;
  width: 46px;
  height: 46px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: radial-gradient(circle at 50% 40%, rgba(255, 255, 255, 0.05), rgba(0, 0, 0, 0.25));
  cursor: grab;
  touch-action: none;
}
#tilt-pad:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
#tilt-pad.active { border-color: rgba(174, 203, 58, 0.55); }
#tilt-cross::before,
#tilt-cross::after {
  content: "";
  position: absolute;
  background: rgba(255, 255, 255, 0.08);
}
#tilt-cross::before { left: 8%; right: 8%; top: 50%; height: 1px; }
#tilt-cross::after { top: 8%; bottom: 8%; left: 50%; width: 1px; }
#tilt-dot {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  background: var(--accent);
  box-shadow: 0 0 8px rgba(174, 203, 58, 0.6);
  pointer-events: none;
}
#btn-devmotion {
  padding: 4px 10px;
  border-radius: 7px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: none;
  color: #b9b9b3;
  font-size: 11.5px;
  cursor: pointer;
}
#btn-devmotion.on {
  border-color: rgba(174, 203, 58, 0.55);
  color: #cde08a;
  background: rgba(174, 203, 58, 0.1);
}

/* --- PWA update bar + offline controls -------------------------------------- */
#update-bar {
  position: fixed;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  z-index: 60;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border-radius: 10px;
  background: #1d241d;
  border: 1px solid rgba(174, 203, 58, 0.45);
  color: #e8f0d8;
  font-size: 13px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.55);
}
#update-bar[hidden] { display: none; }
.info-inline-btn {
  margin-top: 6px;
  margin-right: 8px;
  padding: 3px 10px;
  font-size: 12px;
}

/* A stale/unavailable preview tints the whole editor pane so it's visible in
   peripheral vision, not just in the status bar the eye may not be on. */
#editor-pane.stale {
  background: #241a1a;
  transition: background-color 150ms ease;
}
#editor-pane.stale #editor { background: #241a1a; }
#editor-pane.stale .cm-editor,
#editor-pane.stale .cm-gutters {
  background: #241a1a !important;
}

#snip-name {
  font: inherit;
  font-size: 15px;
  font-weight: 600;
  color: #f0f0ee;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 3px 8px;
  /* the one shrinkable toolbar item: give it a floor so a narrow toolbar
     scrolls instead of squeezing the name to nothing */
  min-width: 6em;
  width: 14em;
  text-overflow: ellipsis;
}
#snip-name:not(.as-label):hover,
#snip-name:not(.as-label):focus {
  border-color: var(--line-12);
  background: var(--input);
  outline: none;
}
#snip-name.as-label { pointer-events: none; }

#dirty-dot { color: var(--warn); flex: none; font-size: 12px; }

/* Provenance chip — where the current buffer came from. */
#prov-chip {
  flex: none;
  display: flex;
  align-items: center;
  gap: 6px;
  font-family: var(--mono);
  font-size: 11.5px;
  color: var(--faint);
  cursor: help;
  white-space: nowrap;
}
#prov-chip[hidden] { display: none; }
#prov-chip .prov-dot {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--accent);
  flex: none;
}
#prov-chip.badge .prov-dot {
  background: var(--badge-blue);
  box-shadow: 0 0 6px rgba(47, 111, 240, 0.7);
}
#prov-chip.badge-ro, #prov-chip.badge {
  font-size: 11px;
  border-radius: 5px;
  padding: 2px 8px;
}
#prov-chip.badge-ro {
  color: var(--muted);
  background: var(--input);
  border: 1px solid var(--line-10);
}
#prov-chip.badge {
  color: var(--badge-blue-text);
  background: rgba(47, 111, 240, 0.12);
  border: 1px solid rgba(47, 111, 240, 0.4);
}
#prov-chip .prov-origin {
  font-size: 10.5px;
  color: var(--fainter);
  background: var(--input);
  border: 1px solid var(--line-08);
  border-radius: 5px;
  padding: 2px 7px;
}

/* Amber toolbar action variant for writing source back to a badge. */
.tbtn-amber {
  color: var(--flash);
  border-color: var(--flash-border);
  background: var(--flash-16);
}
.tbtn-amber:hover:not(:disabled) { background: var(--flash-26); }

.tbtn {
  flex: none;
  font: inherit;
  font-size: 13px;
  color: var(--text-soft);
  background: var(--btn);
  border: 1px solid var(--line-12);
  border-radius: 7px;
  padding: 5px 11px;
  cursor: pointer;
  white-space: nowrap;
}
.tbtn:hover:not(:disabled) { background: var(--btn-hover); }
.tbtn:disabled { opacity: 0.4; cursor: default; }

.tbtn-text {
  background: transparent;
  border-color: transparent;
  color: var(--muted);
  padding: 5px 6px;
}
.tbtn-text:hover:not(:disabled) { background: transparent; color: #e8e8e6; }

#toolbar-msg {
  flex: 1;
  min-width: 2rem;
  text-align: right;
  font-size: 12px;
  color: var(--dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  opacity: 0;
  transition: opacity 0.3s;
}
#toolbar-msg.show { opacity: 1; }

#size-badge {
  flex: none;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--faint);
  cursor: help;
}
#size-badge.warn { color: var(--warn); }
#size-badge.over { color: var(--error); font-weight: 700; }

#lib-menu { position: relative; flex: none; }
#lib-menu summary {
  list-style: none;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  cursor: pointer;
  color: var(--text-soft);
  border: 1px solid var(--line-12);
  background: var(--btn);
  border-radius: 7px;
  font-size: 15px;
  user-select: none;
}
#lib-menu summary::-webkit-details-marker { display: none; }
#lib-menu summary:hover { background: var(--btn-hover); }
#lib-menu .menu-items {
  /* fixed + JS-anchored on open: absolute would be clipped into #toolbar's
     overflow-x:auto scroll container instead of overlaying the editor */
  position: fixed;
  z-index: 60;
  display: flex;
  flex-direction: column;
  min-width: 13.5em;
  background: var(--input);
  border: 1px solid var(--line-12);
  border-radius: 8px;
  padding: 4px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
}
#lib-menu .menu-items button {
  font: inherit;
  font-size: 13px;
  text-align: left;
  color: var(--text-2);
  background: transparent;
  border: none;
  border-radius: 5px;
  padding: 6px 9px;
  cursor: pointer;
  white-space: nowrap;
}
#lib-menu .menu-items button:hover { background: var(--btn); }
#lib-menu .menu-items hr {
  border: none;
  border-top: 1px solid var(--line-08);
  margin: 4px 3px;
}

#editor { flex: 1; min-height: 0; }
#editor .cm-editor { height: 100%; }
#editor .cm-scroller { overflow: auto; }

kbd {
  font: 12px var(--mono);
  color: #dcdcd6;
  background: #0e0e0e;
  border: 1px solid var(--line-12);
  border-radius: 4px;
  padding: 1px 6px;
}
code {
  font: 12px var(--mono);
  color: #c3e88d;
  background: #0e0e0e;
  border-radius: 4px;
  padding: 1px 6px;
}

.swatch-hint {
  display: inline-block;
  width: 0.8em;
  height: 0.8em;
  border-radius: 3px;
  border: 1px solid rgba(255, 255, 255, 0.4);
  background: linear-gradient(135deg, #f55089 0 50%, #2eadd9 50% 100%);
  vertical-align: -0.05em;
}

/* --- emulator pane ---------------------------------------------------------------- */
#badge-pane {
  flex: none;
  width: 452px;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  border-left: 1px solid var(--line-07);
  overflow-y: auto;
}

#badge-stage {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
  min-height: 0;
}

#badge {
  position: relative;
  width: min(100%, 372px);
  aspect-ratio: 1;
  background: url("badge.png") center / contain no-repeat;
  user-select: none;
  touch-action: none;
}

#screen {
  position: absolute;
  left: 34.11%;
  top: 33.56%;
  width: 32.74%;
  height: 32.74%;
  border-radius: 50%;
  background: #000;
}

#screen-boot {
  position: absolute;
  left: 34.11%;
  top: 33.56%;
  width: 32.74%;
  height: 32.74%;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 12%;
  font-size: 0.72rem;
  color: var(--dim);
  background: rgba(0, 0, 0, 0.75);
}
#screen-boot.error { color: var(--error); }
/* the explicit display:flex above would defeat the hidden attribute */
#screen-boot[hidden] { display: none; }

.led {
  position: absolute;
  width: 3.2%;
  height: 3.2%;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  background: rgba(255, 255, 255, 0.06);
  pointer-events: none;
  transition: background 60ms linear, box-shadow 60ms linear;
}

.btn-hotspot {
  position: absolute;
  width: 8.5%;
  height: 8.5%;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  align-items: center;
  justify-content: center;
  color: transparent;
  border: 1px solid transparent;
  cursor: pointer;
  font: 11px var(--mono);
}
#badge:hover .btn-hotspot {
  color: rgba(255, 255, 255, 0.75);
  border-color: rgba(255, 255, 255, 0.28);
  background: rgba(0, 0, 0, 0.25);
}
.btn-hotspot.held {
  color: #10130a !important;
  background: var(--accent) !important;
  border-color: var(--accent) !important;
}

#badge-hint {
  flex: none;
  padding: 0 20px 6px;
  font-size: 12px;
  color: var(--faint);
  text-align: center;
}

/* --- transport ----------------------------------------------------------------- */
#transport {
  flex: none;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
}

#transport > button {
  flex: none;
  width: 34px;
  height: 34px;
  border-radius: 8px;
  border: 1px solid var(--line-12);
  background: var(--btn);
  color: #e8e8e6;
  font-size: 13px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}
#transport > button:hover:not(:disabled) { background: var(--btn-hover); }
#transport > button:disabled { opacity: 0.35; cursor: default; }
#time-controls {
  flex: 1;
  min-width: 120px;
  display: grid;
  gap: 6px;
}
#pace-wrap {
  display: flex;
  align-items: center;
  gap: 6px;
  width: max-content;
  color: #c9c9c4;
  font-size: 11px;
  line-height: 1;
  white-space: nowrap;
  cursor: pointer;
}
#pace {
  width: 14px;
  height: 14px;
  margin: 0;
  accent-color: var(--accent);
  cursor: pointer;
}
#speed-row { display: flex; align-items: center; gap: 8px; }
#speed-wrap { flex: 1; display: flex; align-items: center; min-width: 40px; }
#speed {
  flex: 1;
  width: 100%;
  height: 6px;
  border-radius: 3px;
  background: #2a2a2a;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
  cursor: pointer;
}
#speed::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  border: none;
  box-shadow: 0 0 0 3px var(--accent-22);
}
#speed::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  border: none;
  box-shadow: 0 0 0 3px var(--accent-22);
}
#speed::-moz-range-track { height: 6px; border-radius: 3px; background: #2a2a2a; }
/* Firefox paints the track pseudo over the element background, hiding the JS
   gradient fill — use its native progress pseudo for the lime fill instead. */
#speed::-moz-range-progress { height: 6px; border-radius: 3px; background: var(--accent); }

#speed-label {
  flex: none;
  font-family: var(--mono);
  font-size: 12px;
  color: #c9c9c4;
  min-width: 3.2em;
  text-align: right;
}
.stat {
  flex: none;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--dim);
  min-width: 4em;
  text-align: right;
}

/* --- console --------------------------------------------------------------------- */
#console {
  flex: none;
  margin: 0 16px 16px;
  border-radius: 8px;
  background: var(--console);
  border: 1px solid var(--line-07);
}
#console summary {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 11px 14px;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
  list-style: none;
}
#console summary::-webkit-details-marker { display: none; }
.console-arrow { font-size: 9px; transition: transform 0.12s; }
#console[open] .console-arrow { transform: rotate(90deg); }
#console-log {
  margin: 0;
  padding: 6px 14px 10px;
  max-height: 180px;
  overflow-y: auto;
  font: 11.5px/1.45 var(--mono);
  white-space: pre-wrap;
  border-top: 1px solid var(--line-07);
}
.c-out { color: var(--muted); }
.c-err { color: var(--error); }

/* --- info window --------------------------------------------------------------------- */
#info-dialog {
  width: 540px;
  max-width: 88vw;
  max-height: 82vh;
  background: var(--card);
  color: var(--text-2);
  border: 1px solid var(--line-10);
  border-radius: 14px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.6);
  padding: 0;
}
#info-dialog::backdrop {
  background: rgba(0, 0, 0, 0.62);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}

.info-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 20px 22px 16px;
  border-bottom: 1px solid var(--line-07);
}
.info-head .brand-hex { font-size: 18px; }
#info-title { flex: 1; font-size: 17px; font-weight: 600; color: var(--text); }
#btn-info-close {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  border: 1px solid var(--line-12);
  background: #242424;
  color: var(--text-soft);
  cursor: pointer;
  font-size: 14px;
}
#btn-info-close:hover { background: #2f2f2f; }

.info-body {
  padding: 18px 22px;
  display: flex;
  flex-direction: column;
  gap: 15px;
}
.info-row { display: flex; gap: 12px; }
.info-row > div { font-size: 13.5px; line-height: 1.5; color: #c6c6c1; }
.info-row b { color: #eaeae7; font-weight: 600; }
.info-glyph { flex: none; font-size: 16px; line-height: 1.4; }
.info-hash { font-family: var(--mono); font-size: 15px; color: var(--dim); }
.info-dot { color: var(--warn); }

.info-foot {
  padding: 15px 22px 20px;
  border-top: 1px solid var(--line-07);
  font-size: 12px;
  line-height: 1.55;
  color: var(--dim);
}

/* --- flash dialog ----------------------------------------------------------- */
#flash-dialog, #badge-dialog {
  background: var(--card);
  color: var(--text-2);
  border: 1px solid var(--line-10);
  border-radius: 12px;
  padding: 0;
  width: min(94vw, 34rem);
  font-size: 0.9rem;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.6);
}
#flash-dialog::backdrop, #badge-dialog::backdrop {
  background: rgba(0, 0, 0, 0.62);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}
#flash-dialog header, #badge-dialog header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.6rem 0.9rem;
  border-bottom: 1px solid var(--line-08);
  background: transparent;
  height: auto;
}
.flash-body { padding: 0.75rem 0.9rem 0.9rem; }
.flash-body p, .flash-body ol { margin: 0.4rem 0; line-height: 1.5; }
.flash-body ol { padding-left: 1.3rem; }
.flash-body button {
  font: inherit;
  font-size: 13px;
  color: var(--text-soft);
  background: var(--btn);
  border: 1px solid var(--line-12);
  border-radius: 7px;
  padding: 5px 11px;
  cursor: pointer;
}
.flash-body button:hover:not(:disabled) { background: var(--btn-hover); }
.flash-primary { color: var(--accent-text) !important; border-color: var(--accent-border) !important; background: var(--accent-12) !important; }
.flash-primary:hover:not(:disabled) { background: var(--accent-22) !important; }
.flash-err, .err { color: var(--error); }
.flash-ok { color: var(--accent); }
.dim { color: var(--dim); }
.flash-busy { color: var(--dim); }
.flash-anydev {
  display: block;
  margin-top: 0.6rem;
  font-size: 0.78rem;
  color: var(--dim);
}
.flash-log {
  margin: 0.5rem 0;
  padding: 0.45rem 0.6rem;
  max-height: 11rem;
  overflow-y: auto;
  background: var(--page);
  border: 1px solid var(--line-08);
  border-radius: 6px;
  font: 11.5px/1.5 var(--mono);
  white-space: pre-wrap;
}
.flash-ports { display: flex; flex-direction: column; gap: 0.3rem; margin: 0.5rem 0; }
.flash-port {
  display: flex;
  gap: 0.5rem;
  align-items: baseline;
  border: 1px solid var(--line-08);
  border-radius: 6px;
  padding: 0.4rem 0.6rem;
  cursor: pointer;
}
.flash-port:has(input:checked) { border-color: var(--accent); }
.flash-port.off { opacity: 0.55; cursor: default; }
.flash-port em { display: block; font-style: normal; font-size: 0.78rem; }
.flash-opts { margin: 0.6rem 0; display: flex; flex-direction: column; gap: 0.5rem; }
.flash-opts input {
  font: inherit;
  color: var(--text-2);
  background: var(--input);
  border: 1px solid var(--line-12);
  border-radius: 5px;
  padding: 0.15rem 0.4rem;
}
.flash-opts fieldset {
  border: 1px solid var(--line-08);
  border-radius: 6px;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}
.flash-opts legend { color: var(--dim); font-size: 0.78rem; padding: 0 0.3rem; }
.flash-note { font-size: 0.76rem; margin: 0.1rem 0 0; }
.flash-adv summary { color: var(--dim); font-size: 0.78rem; cursor: pointer; }
.flash-adv label { margin-right: 0.8rem; }
.flash-bar {
  display: inline-block;
  width: 5.5rem;
  height: 0.45rem;
  border: 1px solid var(--line-12);
  border-radius: 4px;
  overflow: hidden;
  vertical-align: middle;
  margin-left: 0.3rem;
}
.flash-bar div { height: 100%; background: var(--accent); }
.flash-bar.warn div { background: var(--warn); }
.flash-bar.over div { background: var(--error); }

/* --- middling widths: shrink the emulator before the editor starves -------------- */
@media (max-width: 1250px) and (min-width: 901px) {
  #badge-pane { width: 380px; }
  #transport { gap: 8px; padding-inline: 12px; }
  #stat-t { display: none; }
}

/* --- narrow screens --------------------------------------------------------- */
@media (max-width: 900px) {
  /* The top bar tightens; the drawer covers most of the width on demand;
     float.js turns the badge pane into the draggable PiP disc. */
  header { gap: 8px; padding: 0 12px; }
  .brand-sub { display: none; }
  /* the device pill is chrome, not a control — reclaim its width on phones */
  #device-pill { display: none; }
  #btn-programs { margin-left: 0; }

  #stage { flex-direction: column; overflow: hidden; }

  /* Fallback layout if float.js hasn't run yet: badge stacked above editor. */
  #editor-pane { min-height: 0; height: 100%; }
  #badge-pane {
    width: auto;
    border-left: none;
    border-bottom: 1px solid var(--line-07);
  }
  #badge { width: min(100%, 420px); margin: 0 auto; }

  /* ===== floating "picture-in-picture" badge (body.float-active) ===========
     JS (float.js) adds .float-active on narrow screens and .badge-expanded
     when the disc is opened up. Everything here is scoped to those classes so
     the pre-JS fallback above and the desktop layout stay untouched. */
  :root { --mini-size: clamp(150px, 43vw, 220px); }

  /* Editor owns the whole viewport; the badge pane leaves the flow. */
  body.float-active #editor-pane {
    border-right: none;
    min-height: 0;
    height: 100%;
  }

  /* ---- the floating badge (mini) ---- */
  body.float-active #badge-pane {
    position: fixed;
    z-index: 40;
    left: var(--fx, auto);
    top: var(--fy, auto);
    width: var(--mini-size);
    height: var(--mini-size);
    min-width: 0;
    max-width: none;
    padding: 0;
    margin: 0;
    gap: 0;
    background: transparent;
    /* the hexagon clip lives on #badge-stage; a drop-shadow filter (unlike
       box-shadow) hugs that clipped silhouette */
    filter: drop-shadow(0 10px 28px rgba(0, 0, 0, 0.55));
    touch-action: none;
    transition: filter 120ms ease;
  }
  body.float-active.badge-dragging #badge-pane {
    transition: none;
    filter: drop-shadow(0 16px 40px rgba(0, 0, 0, 0.6))
      drop-shadow(0 0 6px rgba(174, 203, 58, 0.45));
    cursor: grabbing;
  }

  /* In mini, show the screen + LED ring: scale the badge up so the ring
     fills the tile and clip the tile to the badge's own hexagon. The badge
     itself is inert (drag the tile; no accidental tilt or button presses). */
  body.float-active:not(.badge-expanded) #badge-pane { cursor: grab; }
  body.float-active:not(.badge-expanded) #badge-stage {
    position: absolute;
    inset: 0;
    padding: 0;
    overflow: hidden;
    clip-path: polygon(25% 0, 75% 0, 100% 50%, 75% 100%, 25% 100%, 0 50%);
    background: var(--console);
  }
  body.float-active:not(.badge-expanded) #badge {
    width: 100%;
    height: 100%;
    margin: 0;
    transform: scale(1.28);
    transform-origin: center;
    pointer-events: none;
  }
  body.float-active:not(.badge-expanded) #transport,
  body.float-active:not(.badge-expanded) #badge-hint,
  body.float-active:not(.badge-expanded) #console {
    display: none;
  }

  /* ---- the expand / collapse handle ---- */
  .badge-fab {
    position: absolute;
    z-index: 2;
    display: flex;
    align-items: center;
    justify-content: center;
    font: inherit;
    line-height: 1;
    color: var(--text-2);
    background: rgba(20, 20, 20, 0.72);
    border: 1px solid rgba(255, 255, 255, 0.22);
    -webkit-backdrop-filter: blur(2px);
    backdrop-filter: blur(2px);
    cursor: pointer;
    padding: 0;
  }
  body.float-active:not(.badge-expanded) .badge-fab {
    bottom: 8px;
    left: 50%;
    transform: translateX(-50%);
    width: 30px;
    height: 22px;
    border-radius: 999px;
    font-size: 0.85rem;
  }

  /* ---- expanded overlay ---- */
  body.float-active.badge-expanded #badge-pane {
    inset: 0;
    z-index: 50;
    width: auto;
    height: auto;
    border: none;
    border-radius: 0;
    box-shadow: none;
    filter: none;
    background: var(--bg);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    padding: 3rem 0 1.4rem;
    overflow-y: auto;
    touch-action: auto;
  }
  body.float-active.badge-expanded #badge-stage {
    flex: none;
    width: 100%;
  }
  body.float-active.badge-expanded #badge {
    width: min(78vw, 340px);
    transform: none;
    pointer-events: auto;
  }
  body.float-active.badge-expanded #transport {
    width: 100%;
    gap: 8px;
    padding-inline: 12px;
  }
  body.float-active.badge-expanded #stat-t { display: none; }
  body.float-active.badge-expanded #console { width: calc(100% - 32px); }
  /* Touch devices have no hover: surface the button hotspots so they're usable. */
  body.float-active.badge-expanded .btn-hotspot {
    color: rgba(255, 255, 255, 0.75);
    border-color: rgba(255, 255, 255, 0.28);
    background: rgba(0, 0, 0, 0.25);
  }
  body.float-active.badge-expanded .badge-fab {
    top: 10px;
    right: 12px;
    width: 34px;
    height: 34px;
    border-radius: 8px;
    font-size: 1.1rem;
  }

  /* In mini the button hotspots are far too small to hit — turn them off so a
     stray tap can't fire a badge button (dragging is handled on the disc). */
  body.float-active:not(.badge-expanded) .btn-hotspot { pointer-events: none; }
}
