:root {
  color-scheme: dark;
  --bg: #101214;
  --panel: #20242a;
  --panel-2: #2b3037;
  --line: #454c56;
  --text: #f3f0da;
  --muted: #aab0a6;
  --accent: #00d2a0;
  --amber: #ffd24d;
  --danger: #ff6d6d;
  --screen-bg: #071129;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(circle at 25% 0%, rgba(0, 210, 160, 0.12), transparent 28rem),
    linear-gradient(135deg, #111317 0%, #181b20 52%, #0d1518 100%);
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input,
select,
textarea {
  font: inherit;
}

button,
select,
input,
textarea,
.nav-button {
  border: 1px solid var(--line);
  background: #15191e;
  color: var(--text);
}

button,
.nav-button {
  min-height: 2.25rem;
  padding: 0.45rem 0.7rem;
  border-radius: 6px;
  cursor: pointer;
}

button:hover,
button:focus-visible,
.nav-button:hover,
.nav-button:focus-visible {
  border-color: var(--accent);
  outline: none;
}

.nav-button {
  display: inline-grid;
  place-items: center;
  text-decoration: none;
}

.hidden-file {
  display: none;
}

input,
select {
  min-height: 2.25rem;
  border-radius: 6px;
  padding: 0.35rem 0.5rem;
}

label {
  display: block;
  margin-bottom: 0.35rem;
  color: var(--muted);
  font-size: 0.78rem;
  text-transform: uppercase;
}

.app-shell {
  min-height: 100vh;
}

.viewer-shell {
  min-height: 100vh;
  background: var(--page-outer-bg, transparent);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem clamp(1rem, 4vw, 2rem);
  border-bottom: 1px solid var(--line);
  background: rgba(16, 18, 20, 0.88);
  position: sticky;
  top: 0;
  z-index: 3;
  backdrop-filter: blur(14px);
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  min-width: 0;
}

.brand-mark {
  display: grid;
  width: 3rem;
  height: 3rem;
  place-items: center;
  border: 2px solid var(--amber);
  color: var(--amber);
  font-family: "Courier New", monospace;
  font-weight: 900;
  letter-spacing: 0;
}

h1 {
  margin: 0;
  font-size: clamp(1.1rem, 3vw, 1.45rem);
}

p {
  margin: 0;
}

#pageSummary {
  margin-top: 0.15rem;
  color: var(--muted);
  font-size: 0.9rem;
}

.page-jump {
  display: flex;
  align-items: end;
  gap: 0.55rem;
  flex-wrap: wrap;
}

.persistence-status {
  max-width: 17rem;
  color: var(--muted);
  font-size: 0.75rem;
  line-height: 1.2;
}

.persistence-status.is-error {
  color: var(--danger);
}

.workbench {
  display: grid;
  grid-template-columns: minmax(18rem, 23rem) minmax(0, 1fr);
  min-height: calc(100vh - 5.1rem);
  align-items: start;
}

.mode-page .workbench,
.mode-glyphs .workbench {
  height: calc(100vh - 5.1rem);
  min-height: 0;
  overflow: hidden;
}

.mode-settings .workbench,
.mode-actions .workbench {
  display: block;
}

.tools {
  border-right: 1px solid var(--line);
  background: rgba(32, 36, 42, 0.95);
  padding: 1rem;
}

.mode-page .tools,
.mode-glyphs .tools {
  height: calc(100vh - 5.1rem);
  overflow-y: auto;
}

.mode-settings .tools,
.mode-actions .tools {
  border-right: 0;
  border-bottom: 1px solid var(--line);
  padding-bottom: 0.7rem;
}

.mode-settings .tabs,
.mode-actions .tabs {
  max-width: 34rem;
  margin-bottom: 0;
}

.tabs {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 0.4rem;
  margin-bottom: 1rem;
}

.tab-button {
  background: #15191e;
  color: var(--muted);
}

.tab-button.is-active {
  background: var(--accent);
  color: #061411;
  border-color: var(--accent);
  font-weight: 800;
}

.mode-panel {
  display: none;
}

.mode-panel.is-active {
  display: block;
}

.tool-group {
  margin-bottom: 1rem;
}

.tool-row {
  display: flex;
  align-items: end;
  gap: 0.65rem;
  margin-bottom: 1rem;
}

.tool-row > * {
  flex: 1;
  min-width: 0;
}

#charInput {
  width: 100%;
  height: 3rem;
  font-family: "Courier New", monospace;
  font-size: 1.6rem;
  text-align: center;
}

input[type="color"] {
  width: 100%;
  padding: 0.15rem;
}

.swatch-row {
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  gap: 0.3rem;
}

.selection-status {
  margin: 0 0 0.45rem;
  color: var(--muted);
  font-size: 0.78rem;
}

.cell.is-range-selected {
  outline: 1px solid rgba(0, 210, 160, 0.8);
  outline-offset: -1px;
  background: rgba(0, 210, 160, 0.12);
  z-index: 1;
}

.cell.is-range-selected.is-selected {
  outline: 2px solid var(--amber);
  background: rgba(255, 210, 77, 0.2);
  z-index: 2;
}

.swatch-button {
  min-height: 1.9rem;
  padding: 0;
  border-radius: 4px;
  background: var(--swatch);
}

.swatch-button.is-active {
  border-color: var(--amber);
  box-shadow: 0 0 0 2px rgba(255, 210, 77, 0.24);
}

.charset-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(2rem, 1fr));
  gap: 0.25rem;
  padding-right: 0.2rem;
}

.glyph-button {
  display: grid;
  place-items: center;
  grid-template-rows: 1.45rem auto;
  min-height: 3.25rem;
  padding: 0;
  border-radius: 4px;
  overflow: hidden;
}

.glyph-button.is-active {
  border-color: var(--amber);
  color: var(--amber);
}

.glyph-canvas {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  pointer-events: none;
}

.glyph-button .glyph-canvas {
  width: 1.45rem;
  height: 1.45rem;
}

.glyph-label {
  width: 100%;
  min-width: 0;
  padding: 0 0.12rem 0.18rem;
  color: var(--muted);
  font-size: 0.56rem;
  line-height: 1;
  overflow: hidden;
  text-align: center;
  text-overflow: ellipsis;
  white-space: nowrap;
  pointer-events: none;
}

.glyph-button.is-active .glyph-label {
  color: var(--amber);
}

.bitmap-editor {
  display: grid;
  grid-template-columns: repeat(16, minmax(0, 1fr));
  width: min(100%, 18rem);
  border: 1px solid var(--line);
  gap: 1px;
  background: #0b0f14;
  contain: content;
  touch-action: none;
}

.bitmap-editor-large {
  width: min(100%, 42rem);
  background: #303842;
}

.bitmap-pixel {
  aspect-ratio: 1 / 1;
  min-height: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: #15191e;
}

.bitmap-pixel.is-on {
  background: var(--amber);
}

.glyph-animation-tools {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  justify-content: flex-end;
}

.glyph-animation-tools button {
  min-height: 2rem;
  padding: 0.3rem 0.5rem;
  font-size: 0.78rem;
}

.glyph-animation-preview {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-top: 0.7rem;
  color: var(--muted);
  font-size: 0.78rem;
  text-transform: uppercase;
}

.glyph-animation-preview canvas {
  width: 4rem;
  height: 4rem;
  border: 1px solid var(--line);
  background: var(--screen-bg);
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.stage {
  min-width: 0;
  padding: clamp(0.8rem, 3vw, 1.5rem);
}

.stage.is-page-mode,
.mode-glyphs .stage {
  height: calc(100vh - 5.1rem);
  overflow: auto;
}

.stage-panel {
  display: none;
}

#pageStage {
  background: var(--page-outer-bg, transparent);
}

.stage-panel.is-active {
  display: block;
}

.screen-shell {
  width: min(100%, 66rem);
  margin: 0 auto;
  background: #05070a;
  border: 1px solid #3f4d5b;
  box-shadow: 0 1rem 2.5rem rgba(0, 0, 0, 0.42), inset 0 0 0 0.45rem #15191e;
  padding: clamp(0.55rem, 1.4vw, 1rem);
}

.teletext-screen {
  --cols: 40;
  --rows: 24;
  position: relative;
  display: block;
  width: 100%;
  background: var(--screen-bg);
  border: 1px solid #243853;
  overflow: hidden;
  line-height: 1;
}

.teletext-canvas {
  display: block;
  width: 100%;
  height: auto;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  pointer-events: none;
}

.cell-grid {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
  grid-template-rows: repeat(var(--rows), minmax(0, 1fr));
}

.cell-grid.action-grid {
  touch-action: manipulation;
  z-index: 2;
  pointer-events: auto;
}

.action-area {
  display: block;
  min-width: 0;
  min-height: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  cursor: pointer;
  touch-action: manipulation;
  text-decoration: none;
}

.action-area:hover,
.action-area:focus-visible {
  outline: 2px solid var(--amber);
  outline-offset: -2px;
}

.cell {
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  user-select: none;
  cursor: crosshair;
  contain: strict;
}

.cell:hover,
.cell.is-selected {
  outline: 2px solid var(--amber);
  outline-offset: -2px;
  z-index: 1;
}

.viewer-links {
  display: flex;
  flex-wrap: wrap;
  gap: 0.55rem;
  width: min(100%, 66rem);
  margin: 1rem auto 0;
}

.viewer-links button {
  border-color: #56746d;
}

.viewer-stage {
  min-height: 100vh;
  display: block;
  padding: 0;
}

.viewer-shell .teletext-screen {
  border: 0;
  width: 100vw;
  min-height: 0;
}

.glyph-workspace.is-active {
  display: grid;
  grid-template-columns: minmax(22rem, 1fr) minmax(16rem, 24rem);
  gap: 1rem;
  align-items: start;
  min-height: 100%;
}

.glyph-canvas-panel {
  position: sticky;
  top: 0;
}

.glyph-browser-panel {
  max-height: calc(100vh - 7.1rem);
  overflow-y: auto;
  padding-right: 0.25rem;
}

.glyph-canvas-panel,
.glyph-browser-panel,
.data-workspace,
.settings-workspace,
.actions-workspace {
  min-width: 0;
}

.glyph-workspace-header {
  margin-bottom: 0.8rem;
}

.glyph-workspace-header h2 {
  margin: 0;
  font-family: "Courier New", monospace;
  font-size: clamp(1.7rem, 4vw, 3rem);
}

.glyph-workspace-header p {
  color: var(--muted);
}

.glyph-browser {
  grid-template-columns: repeat(auto-fill, minmax(2.6rem, 1fr));
}

.palette-editor {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(6.5rem, 1fr));
  gap: 0.4rem;
}

.palette-item {
  display: grid;
  grid-template-columns: 3rem minmax(0, auto);
  gap: 0.35rem;
  align-items: center;
}

.palette-item input[type="color"] {
  width: 3rem;
}

.palette-item button {
  min-width: 0;
  padding-inline: 0.45rem;
}

.settings-workspace.is-active {
  display: grid;
  grid-template-columns: repeat(2, minmax(16rem, 1fr));
  gap: 0.65rem;
  align-items: start;
  max-width: 82rem;
  margin: 0 auto;
}

.actions-workspace.is-active {
  display: grid;
  grid-template-columns: minmax(24rem, 1.2fr) minmax(18rem, 0.8fr);
  gap: 0.75rem;
  align-items: start;
  max-width: 82rem;
  margin: 0 auto;
}

.action-preview-panel,
.action-controls-panel {
  min-width: 0;
}

.action-preview-panel h2 {
  margin: 0 0 0.65rem;
  font-size: 1rem;
}

.action-preview-shell {
  border: 1px solid var(--line);
  background: #05070a;
  padding: 0.45rem;
}

.action-preview {
  border: 0;
}

.action-area-editor {
  display: grid;
  place-items: center;
  color: #061411;
  background: rgba(0, 210, 160, 0.38);
  border: 2px solid var(--accent);
  font-weight: 800;
}

.action-area-editor:hover,
.action-area-editor:focus-visible {
  background: rgba(255, 210, 77, 0.42);
  border-color: var(--amber);
}

.action-area-editor.is-selected {
  color: #071129;
  background: rgba(255, 210, 77, 0.72);
  border: 3px solid var(--amber);
  box-shadow: 0 0 0 2px rgba(255, 210, 77, 0.22);
  z-index: 2;
}

.action-area-ghost {
  min-width: 0;
  min-height: 0;
  border: 2px dashed var(--amber);
  background: rgba(255, 210, 77, 0.14);
  pointer-events: none;
  z-index: 3;
}

.settings-section {
  border: 1px solid var(--line);
  background: rgba(32, 36, 42, 0.72);
  padding: 0.65rem;
}

.settings-section h2 {
  margin: 0 0 0.65rem;
  font-size: 1rem;
}

.settings-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(9rem, 1fr));
  gap: 0.55rem;
}

.action-controls-grid {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.action-controls-grid .action-target-field {
  grid-column: span 2;
}

.settings-actions {
  display: flex;
  align-items: end;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-top: 0.55rem;
}

.settings-actions > * {
  min-width: 8rem;
}

.settings-actions input[type="color"] {
  width: 4rem;
  min-width: 4rem;
}

.data-workspace.is-active {
  display: block;
}

#jsonArea {
  width: 100%;
  min-height: 28rem;
  resize: vertical;
  padding: 0.75rem;
  border-radius: 6px;
  font-family: "Courier New", monospace;
  font-size: 0.82rem;
  line-height: 1.35;
}

.status-line {
  color: var(--muted);
  font-size: 0.88rem;
}

.status-line.is-error {
  color: var(--danger);
}

.directory-shell {
  width: min(70rem, calc(100% - 2rem));
  margin: 0 auto;
  padding: 2rem 0 4rem;
}

.directory-header {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 1rem;
  padding-bottom: 1.5rem;
  border-bottom: 1px solid var(--line);
}

.directory-kicker {
  color: var(--amber);
  font: 0.8rem "Courier New", monospace;
  text-transform: uppercase;
}

.directory-header h1 {
  margin-top: 0.3rem;
}

.directory-nav {
  display: flex;
  gap: 0.5rem;
}

.directory-list {
  display: grid;
  gap: 0.5rem;
  margin-top: 1rem;
}

.directory-search-label {
  margin-top: 1.25rem;
}

.directory-search {
  width: min(28rem, 100%);
}

.directory-item {
  display: grid;
  grid-template-columns: 5rem minmax(0, 1fr) auto;
  align-items: center;
  gap: 1rem;
  padding: 0.8rem 1rem;
  border: 1px solid var(--line);
  background: rgba(32, 36, 42, 0.8);
  border-radius: 6px;
}

.directory-number {
  color: var(--amber);
  font: 1.3rem "Courier New", monospace;
  font-weight: 700;
}

.directory-item h2,
.directory-item p {
  margin: 0;
}

.directory-item-links {
  display: flex;
  gap: 0.4rem;
}

.directory-item h2 {
  font-size: 1rem;
}

.directory-item p {
  margin-top: 0.25rem;
  color: var(--muted);
  font-size: 0.82rem;
}

.link-list {
  display: grid;
  gap: 0.45rem;
}

.link-item {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 0.5rem;
  padding: 0.45rem 0.5rem;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
}

.link-item span {
  min-width: 0;
  overflow-wrap: anywhere;
}

.link-item.is-selected {
  border-color: var(--amber);
  background: rgba(255, 210, 77, 0.12);
}

.link-item button {
  min-height: 1.8rem;
  padding: 0.2rem 0.45rem;
}

@media (max-width: 820px) {
  .topbar {
    position: static;
    align-items: flex-start;
    flex-direction: column;
  }

  .workbench {
    grid-template-columns: 1fr;
  }

  .tools {
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .glyph-workspace.is-active {
    grid-template-columns: 1fr;
  }

  .glyph-canvas-panel {
    position: static;
  }

  .glyph-browser-panel {
    max-height: none;
    overflow-y: visible;
  }

  .settings-workspace.is-active {
    grid-template-columns: 1fr;
  }

  .actions-workspace.is-active {
    grid-template-columns: 1fr;
  }

  .action-controls-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .action-controls-grid .action-target-field {
    grid-column: span 2;
  }

}
