html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  touch-action: none;
  font-family: sans-serif;
  background: #888;
}

#app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#canvas-wrap {
  flex: 1 1 auto;
  position: relative;
  min-height: 0;
  overflow: hidden;
}

#view {
  position: absolute;
  inset: 0;
  display: block;
  touch-action: none;
}

#panel {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  background: #2b2b2b;
  border-top: 1px solid #000;
  overflow-x: auto;
  white-space: nowrap;
}

.panel-btn {
  flex: 0 0 auto;
  padding: 10px 16px;
  font-size: 14px;
  font-weight: bold;
  color: #eee;
  background: #444;
  border: 1px solid #222;
  border-radius: 4px;
  cursor: pointer;
}

.panel-btn:hover {
  background: #555;
}

.panel-btn:active {
  background: #333;
}

.menu-btn {
  background: #555;
  border-color: #111;
}

.component-btn.armed {
  background: #2f6f2f;
  border-color: #9fef9f;
  color: #fff;
}

.component-btn:disabled,
.component-btn.disabled {
  opacity: 0.35;
  cursor: not-allowed;
  pointer-events: none;
}

.component-list {
  display: flex;
  gap: 8px;
  overflow-x: auto;
}

/* ── Application menu and modal views ── */
.app-menu {
  position: fixed;
  z-index: 1200;
  min-width: 230px;
  padding: 5px;
  background: #303030;
  border: 1px solid #666;
  border-radius: 6px;
  box-shadow: 0 8px 28px #0009;
}

.app-menu[hidden],
.modal-backdrop[hidden] { display: none; }

.app-menu button {
  display: block;
  width: 100%;
  padding: 9px 12px;
  border: 0;
  border-radius: 4px;
  color: #eee;
  background: transparent;
  text-align: left;
  cursor: pointer;
}

.app-menu button:hover { background: #4b4b4b; }

.modal-backdrop {
  position: fixed;
  z-index: 1300;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: #0009;
}

.app-modal {
  display: flex;
  flex-direction: column;
  width: min(820px, calc(100vw - 32px));
  max-height: min(760px, calc(100vh - 32px));
  color: #eee;
  background: #292929;
  border: 1px solid #666;
  border-radius: 8px;
  box-shadow: 0 12px 40px #000b;
}

.import-conflict-dialog {
  width: min(520px, calc(100vw - 32px));
  padding: 0;
  color: #eee;
  background: #292929;
  border: 1px solid #666;
  border-radius: 8px;
  box-shadow: 0 12px 40px #000b;
}

.import-conflict-dialog::backdrop { background: #0009; }
.import-conflict-dialog form { display: flex; flex-direction: column; }
.import-conflict-dialog header,
.import-conflict-dialog footer {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 14px;
}
.import-conflict-dialog header { border-bottom: 1px solid #555; }
.import-conflict-dialog footer {
  justify-content: flex-end;
  border-top: 1px solid #555;
}
.import-conflict-dialog h2 { margin: 0; font-size: 18px; }
.import-conflict-dialog select { min-width: 220px; }

.app-modal > header,
.app-modal > footer {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 14px;
}

.app-modal > header { border-bottom: 1px solid #555; }
.app-modal > footer {
  justify-content: flex-end;
  border-top: 1px solid #555;
}

.app-modal h2 {
  flex: 1;
  margin: 0;
  font-size: 18px;
}

.icon-button {
  width: 30px;
  height: 30px;
  padding: 0;
  color: #ddd;
  background: #444;
  border: 1px solid #666;
  border-radius: 4px;
  font-size: 20px;
  cursor: pointer;
}

.app-modal-body {
  min-height: 90px;
  padding: 14px;
  overflow: auto;
}

.dialog-button,
.library-row button,
.find-result button {
  padding: 7px 11px;
  color: #eee;
  background: #444;
  border: 1px solid #666;
  border-radius: 4px;
  cursor: pointer;
}

.dialog-button.primary { background: #315d7a; border-color: #79b8df; }
.dialog-button.danger,
.library-row button.danger { background: #713737; border-color: #a85a5a; }
.dialog-button:disabled,
.library-row button:disabled,
.find-result button:disabled {
  opacity: 0.42;
  cursor: default;
}

.dialog-textarea {
  box-sizing: border-box;
  width: 100%;
  min-height: 310px;
  resize: vertical;
  padding: 10px;
  color: #eee;
  background: #1d1d1d;
  border: 1px solid #555;
  border-radius: 4px;
  font: 12px/1.4 ui-monospace, monospace;
}

.dialog-field {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
}

.dialog-field > label { min-width: 130px; color: #bbb; }
.dialog-field input,
.dialog-field select,
.library-row input,
.library-row select {
  padding: 7px 8px;
  color: #eee;
  background: #383838;
  border: 1px solid #666;
  border-radius: 4px;
}

.library-list,
.find-results {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.library-group {
  position: relative;
  overflow: hidden;
  background: #303030;
  border: 1px solid #525252;
  border-radius: 6px;
}

.library-group-header {
  display: grid;
  grid-template-columns: 28px 24px minmax(120px, 1fr) auto;
  align-items: center;
  gap: 5px;
  min-height: 38px;
  padding: 3px 8px;
  background: #3b3b3b;
}

.library-triangle {
  width: 26px;
  height: 28px;
  padding: 0;
  color: #ddd;
  background: transparent;
  border: 0;
  cursor: pointer;
}

.library-group-name { font-size: 14px; }
.library-group-count {
  min-width: 24px;
  padding: 2px 7px;
  color: #bbb;
  background: #292929;
  border-radius: 10px;
  text-align: center;
  font-size: 11px;
}

.library-group-body {
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 6px;
}

.library-group-body[hidden] { display: none; }

.library-drag-handle {
  color: #999;
  cursor: grab;
  user-select: none;
  text-align: center;
  touch-action: none;
}

.library-drag-handle:active { cursor: grabbing; }
.library-group-drag:not([draggable="true"]) { opacity: 0.25; cursor: default; }
.library-group.dragging,
.library-row.dragging { opacity: 0.42; }

.library-drop-before::before,
.library-drop-after::after,
.library-drop-at-end::after {
  position: absolute;
  z-index: 3;
  right: 5px;
  left: 5px;
  height: 3px;
  content: "";
  background: #79b8df;
  border-radius: 2px;
  box-shadow: 0 0 4px #79b8df;
  pointer-events: none;
}

.library-drop-before::before { top: 0; }
.library-drop-after::after,
.library-drop-at-end::after { bottom: 0; }

.library-row {
  position: relative;
  display: grid;
  grid-template-columns: 24px 28px minmax(150px, 1fr) auto auto;
  align-items: center;
  gap: 7px;
  padding: 7px;
  background: #343434;
  border: 1px solid #4c4c4c;
  border-radius: 5px;
}

.library-row:has(.library-group-entry) {
  grid-template-columns: 24px 28px minmax(130px, 1fr) minmax(120px, 180px) auto auto;
}

.library-row .library-name {
  overflow: hidden;
  text-overflow: ellipsis;
  font-weight: bold;
}

.library-row .library-origin { color: #999; font-size: 11px; }
.library-group-entry { min-width: 0; }

.find-result {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px;
  background: #343434;
  border-radius: 5px;
}

.find-result .find-name { flex: 1; font-weight: bold; }
.dialog-note { color: #aaa; font-size: 12px; }
.dialog-error { color: #ff8f8f; white-space: pre-wrap; }

/* ── ROM contents editor ─────────────────────────────────────── */

.app-modal:has(.rom-editor) {
  width: min(980px, calc(100vw - 32px));
}

.rom-editor {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.rom-mode-bar,
.rom-editor-options {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.rom-editor-options {
  justify-content: space-between;
  padding: 8px 10px;
  background: #343434;
  border-radius: 5px;
}

.rom-editor-options label {
  display: flex;
  align-items: center;
  gap: 6px;
}

.rom-editor-options select,
.rom-editor-options input[type="number"] {
  padding: 5px 7px;
  color: #eee;
  background: #383838;
  border: 1px solid #666;
  border-radius: 4px;
}

.rom-editor-options input[type="number"] {
  box-sizing: border-box;
  width: 62px;
}

.rom-editor-content {
  min-height: 330px;
}

.rom-memory-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 88px;
  gap: 10px;
  height: 370px;
}

.rom-values-region {
  min-width: 0;
  padding: 8px;
  overflow: auto;
  background: #202020;
  border: 1px solid #555;
  border-radius: 5px;
  outline: none;
}

.rom-values-region:focus {
  border-color: #79b8df;
}

.rom-bit-canvas-region {
  padding: 0;
  overflow: hidden;
}

.rom-bit-canvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: crosshair;
  touch-action: none;
}

.rom-minimap-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 0;
  padding: 5px;
  background: #202020;
  border: 1px solid #555;
  border-radius: 5px;
}

.rom-bit-minimap-panel,
.rom-cell-minimap-panel {
  align-self: start;
}

.rom-minimap {
  width: 76px;
  height: 330px;
  border: 1px solid #666;
  cursor: crosshair;
  touch-action: none;
}

.rom-minimap-limit,
.rom-minimap-range {
  color: #aaa;
  font: 10px/1.2 ui-monospace, monospace;
}

.rom-minimap-range {
  color: #9fd7ff;
}

.rom-bit-view {
  display: grid;
  gap: 4px;
  min-width: max-content;
}

.rom-entry-line {
  display: flex;
  align-items: center;
}

.rom-bit-entries,
.rom-table-entries {
  display: flex;
  align-items: center;
}

.rom-bit-entries {
  /* Half a 17px bit cell between adjacent words. */
  gap: 8.5px;
}

.rom-table-entries {
  gap: 5px;
}

.rom-bit-word {
  display: flex;
  gap: 3px;
}

.rom-address {
  display: inline-block;
  width: 64px;
  color: #aaa;
  font: 12px/1.4 ui-monospace, monospace;
  text-align: right;
  padding-right: 8px;
}

.rom-line-address {
  flex: 0 0 52px;
  width: 52px;
  padding-right: 6px;
}

.rom-bit {
  width: 17px;
  height: 17px;
  padding: 0;
  border: 1px solid #777;
  border-radius: 2px;
  background: #f4f4f4;
  cursor: pointer;
}

.rom-bit.on {
  background: #ff7777;
  border-color: #ffbbbb;
}

.rom-table {
  display: grid;
  gap: 5px;
  min-width: max-content;
}

.rom-table input {
  box-sizing: border-box;
  flex: 0 0 auto;
  padding: 6px 8px;
  color: #111;
  border: 1px solid #aaa;
  border-radius: 3px;
  font: 12px/1.3 ui-monospace, monospace;
}

.rom-json {
  min-height: 360px;
}

.rom-mapping-label {
  display: block;
  margin: 12px 0 5px;
  color: #bbb;
}

.rom-mapping {
  min-height: 90px;
}

@media (max-width: 720px) {
  .modal-backdrop { padding: 8px; }
  .app-modal { width: calc(100vw - 16px); max-height: calc(100vh - 16px); }
  .library-row {
    grid-template-columns: 20px 24px minmax(100px, 1fr) auto auto;
  }
  .library-row:has(.library-group-entry) {
    grid-template-columns: 20px 24px minmax(100px, 1fr) auto auto;
  }
  .library-row > .library-group-entry {
    grid-column: 3 / -1;
    width: auto;
  }
}

/* ── Tab bar ── */
#tab-bar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  background: #333;
  border-bottom: 1px solid #1a1a1a;
  overflow-x: auto;
  white-space: nowrap;
  min-height: 34px;
}

#tab-list {
  display: flex;
  gap: 2px;
  overflow-x: auto;
  flex: 1 1 auto;
}

#esc-btn {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 28px;
  padding: 0 9px;
  font-size: 12px;
  font-weight: bold;
  color: #ccc;
  background: #3a3a3a;
  border: 1px solid #2a2a2a;
  border-radius: 4px;
  cursor: pointer;
}

#esc-btn:hover {
  background: #4a4a4a;
  color: #fff;
}

#view-back-btn {
  display: none;
  flex: 0 0 auto;
  width: 28px;
  height: 28px;
  padding: 0;
  color: #fff;
  background: #315d7a;
  border: 1px solid #79b8df;
  border-radius: 4px;
  font-size: 19px;
  line-height: 1;
  cursor: pointer;
}

#app.view-mode #view-back-btn { display: inline-flex; align-items: center; justify-content: center; }
#app.view-mode #canvas-wrap { box-shadow: inset 0 0 0 2px #79b8df; }
#app.view-mode #panel { opacity: 0.42; }
#app.view-mode #panel button { pointer-events: none; }

.history-btn {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  padding: 0;
  color: #ddd;
  background: #3a3a3a;
  border: 1px solid #2a2a2a;
  border-radius: 4px;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
}

.history-btn:hover:not(:disabled) { background: #4a4a4a; color: #fff; }
.history-btn:disabled { opacity: 0.35; cursor: default; }

.tab-btn {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 6px 5px 10px;
  font-size: 13px;
  color: #aaa;
  background: #3a3a3a;
  border: 1px solid #2a2a2a;
  border-radius: 4px;
  cursor: pointer;
  user-select: none;
  max-width: 180px;
}

.tab-btn:hover {
  background: #4a4a4a;
  color: #ddd;
}

.tab-btn.active {
  background: #555;
  color: #fff;
  border-color: #666;
}

.tab-btn .tab-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tab-btn .tab-close {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  font-size: 16px;
  line-height: 1;
  color: #888;
  border-radius: 3px;
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 0;
}

.tab-btn .tab-close:hover {
  background: #c44;
  color: #fff;
}

#new-tab-btn {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  font-size: 18px;
  font-weight: bold;
  color: #aaa;
  background: #3a3a3a;
  border: 1px solid #2a2a2a;
  border-radius: 4px;
  cursor: pointer;
}

#new-tab-btn:hover {
  background: #4a4a4a;
  color: #fff;
}

/* ── Component popup menus ── */
.component-popup {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 4px;
  background: #3a3a3a;
  border: 1px solid #555;
  border-radius: 4px;
  z-index: 1000;
}

.component-popup .component-btn {
  display: block;
  width: 100%;
}

.category-btn {
  font-style: italic;
}

/* ── Component details editor ── */
#component-details {
  flex: 0 0 auto;
  display: flex;
  gap: 8px;
  padding: 8px;
  background: #2a2a2a;
  border-bottom: 1px solid #444;
  overflow-x: auto;
  white-space: nowrap;
  min-height: 40px;
  align-items: center;
}

#component-details-content {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.component-details-field {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.component-details-field label {
  font-size: 10px;
  color: #999;
}

.component-details-field input {
  width: 64px;
  font-size: 12px;
  padding: 2px 4px;
  border: 1px solid #555;
  border-radius: 3px;
  background: #3a3a3a;
  color: #fff;
  text-align: center;
}

.component-details-field input:focus {
  border-color: #6cf;
  outline: none;
}
