/* openjp2 playground — drop-decode, encode-anything, codestream explorer. */

:root {
  --bg: #16181d;
  --panel: #1f2229;
  --ink: #d7dae0;
  --muted: #8b93a1;
  --accent: #4da3ff;
  --marker: #2f6f4f;
  --marker-hot: #c9a227;
  --danger: #c95b5b;
  font-size: 15px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}

header { padding: 1.5rem 2rem 0.5rem; }
header h1 { margin: 0 0 0.25rem; }
.subtitle { color: var(--muted); max-width: 70ch; }

/* busy indicator (web-worker activity): fixed to the viewport so its
   appearance/disappearance never shifts the page content */
.busy {
  position: fixed;
  top: 1rem;
  right: 1.25rem;
  z-index: 20;
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.3rem 0.8rem;
  border-radius: 1rem;
  background: #2c313a;
  color: var(--ink);
  font-size: 0.85rem;
  box-shadow: 0 2px 8px rgb(0 0 0 / 40%);
}
.busy[hidden] { display: none; }
.busy::before {
  content: "";
  width: 0.8rem;
  height: 0.8rem;
  border: 2px solid #4a5261;
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: busy-spin 0.8s linear infinite;
}
@keyframes busy-spin {
  to { transform: rotate(360deg); }
}

.code, code { font-family: ui-monospace, "Cascadia Mono", Menlo, monospace; }

.columns {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  padding: 1rem 2rem 2rem;
  align-items: flex-start;
}

.panel {
  flex: 1 1 24rem;
  max-width: 40rem;
  background: var(--panel);
  border-radius: 0.5rem;
  padding: 1rem;
}

.panel h2 { margin: 0 0 0.5rem; }
.hint { color: var(--muted); font-size: 0.9rem; margin-top: 0; }

.dropzone {
  position: relative;
  min-height: 10rem;
  border: 2px dashed #3a3f4a;
  border-radius: 0.4rem;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background: #181b20;
}
.dropzone canvas {
  max-width: 100%;
  max-height: 24rem;
  height: auto; /* keep the aspect ratio when scaled down */
  image-rendering: pixelated;
  display: block;
}
.dropzone.dragover { border-color: var(--accent); background: #1b2430; }
.dropzone.has-image { border-style: solid; }
.dropzone-label { color: var(--muted); position: absolute; pointer-events: none; }
.has-image .dropzone-label { display: none; }

.info { display: grid; grid-template-columns: max-content 1fr; gap: 0.15rem 0.75rem; }
.info dt { color: var(--muted); }
.info dd { margin: 0; }

.controls { margin-top: 0.75rem; display: flex; flex-direction: column; gap: 0.5rem; }
.row { display: flex; gap: 0.5rem; align-items: center; }
button {
  background: var(--accent);
  color: #0c1118;
  border: 0;
  border-radius: 0.35rem;
  padding: 0.45rem 1rem;
  font-weight: 600;
  cursor: pointer;
}
button:disabled { background: #3a3f4a; color: var(--muted); cursor: default; }

/* explorer */
.explorer-preview {
  background: #181b20;
  border: 1px solid #2c313a;
  border-radius: 0.35rem;
  overflow: auto;
  max-height: 20rem;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  margin-top: 0.75rem;
}
/* zoomed: natural/magnified size in a scrollable box, top-left aligned.
   The max-width/max-height fit constraints must be lifted, otherwise
   every zoom level clamps back to the container size (and the two
   independent clamps distort the aspect ratio). */
.explorer-preview.zoomed { justify-content: flex-start; }
.explorer-preview.zoomed canvas {
  max-width: none;
  max-height: none;
}
.dropzone.zoomed {
  align-items: flex-start;
  justify-content: flex-start;
  overflow: auto;
  max-height: 30rem;
}
.dropzone.zoomed canvas {
  max-width: none;
  max-height: none;
}
.explorer-preview canvas {
  image-rendering: pixelated;
  max-width: 100%;
  height: auto; /* keep the aspect ratio when max-width constrains the box */
}

.hex {
  font-family: ui-monospace, "Cascadia Mono", Menlo, monospace;
  font-size: 0.8rem;
  line-height: 1.35;
  overflow: auto;
  max-height: 26rem;
  border: 1px solid #2c313a;
  border-radius: 0.35rem;
  background: #14161a;
  padding: 0.4rem 0.6rem;
  white-space: pre;
}
.hex-empty { color: var(--muted); }
.hex .addr { color: var(--muted); }
.hex .byte { color: var(--ink); }
.hex .cut { color: var(--danger); text-decoration: line-through; }

/* byte category colours */
.hex .box { background: #2a3140; }
.hex .box-body { background: #232937; }
.hex .marker { background: var(--marker); }
.hex .marker-body { background: #24383f; }
.hex .tile-part { background: #6b5a22; }
.hex .tile-data { background: #33291a; }
.hex .codestream { background: #241f33; }
.hex .corrupt { background: #4a2026; }
.hex .selected { outline: 2px solid var(--accent); outline-offset: -1px; }

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem 0.9rem;
  margin-top: 0.5rem;
  font-size: 0.78rem;
  color: var(--muted);
}
.legend-item { padding: 0.05rem 0.4rem; border-radius: 0.2rem; }

.tree {
  margin-top: 0.75rem;
  font-family: ui-monospace, "Cascadia Mono", Menlo, monospace;
  font-size: 0.82rem;
  max-height: 16rem;
  overflow: auto;
  border: 1px solid #2c313a;
  border-radius: 0.35rem;
  background: #14161a;
  padding: 0.4rem 0.6rem;
}
.tree ul { list-style: none; margin: 0; padding-left: 0.9rem; }
.tree li { min-width: 0; }
/* children are a sibling of the row; collapsed by default */
.tree .tree-children { display: none; }
.tree li.open > .tree-children { display: block; }
.tree-node { display: flex; gap: 0.35rem; align-items: baseline; cursor: pointer; }
.tree-node:hover { color: var(--accent); }
.tree-node.selected { color: var(--accent); font-weight: 700; }
.tree-toggle {
  flex: none;
  width: 0.85rem;
  color: var(--muted);
  cursor: pointer;
  transition: transform 0.12s ease;
}
.tree li.open > .tree-node > .tree-toggle { transform: rotate(90deg); }
.tree-toggle-empty { visibility: hidden; }
.tree-label {
  flex: 1 1 auto;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tree-range {
  flex: none;
  color: var(--muted);
  font-size: 0.75rem;
  margin-left: auto;
  font-variant-numeric: tabular-nums;
}
.tree-node.beyond-cut { color: #5a4a50; text-decoration: line-through; }

.presets { display: flex; flex-wrap: wrap; gap: 0.35rem; }
.presets button {
  background: #2c313a;
  color: var(--ink);
  padding: 0.25rem 0.6rem;
  font-size: 0.78rem;
  font-weight: 500;
}
.presets button.active {
  background: var(--accent);
  color: #0c1118;
  font-weight: 700;
}

.muted { color: var(--muted); font-size: 0.82rem; }

/* '?' help icons: a larger explanation popover on hover/focus */
.help {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
  width: 1.15rem;
  height: 1.15rem;
  margin-left: 0.4rem;
  border-radius: 50%;
  background: #2c313a;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 700;
  cursor: help;
  position: relative;
}
.help:hover,
.help:focus {
  background: var(--accent);
  color: #0c1118;
  outline: none;
}
.help:hover::after,
.help:focus::after {
  content: attr(data-help);
  position: absolute;
  z-index: 30;
  top: 1.6rem;
  left: 50%;
  transform: translateX(-50%);
  width: max-content;
  max-width: min(28rem, 85vw);
  padding: 0.6rem 0.8rem;
  background: #0d1014;
  color: var(--ink);
  border: 1px solid #3a3f4a;
  border-radius: 0.4rem;
  font-size: 0.82rem;
  font-weight: 400;
  line-height: 1.45;
  text-align: left;
  white-space: pre-line;
  box-shadow: 0 4px 16px rgb(0 0 0 / 50%);
  pointer-events: none;
}

/* collapsible advanced settings */
.advanced { margin-top: 0.4rem; }
.advanced > summary {
  cursor: pointer;
  color: var(--muted);
  font-size: 0.85rem;
  user-select: none;
}
.advanced > summary:hover { color: var(--accent); }
.advanced-body {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-top: 0.5rem;
  padding: 0.5rem 0.7rem;
  border-left: 2px solid #2c313a;
}

.controls input[type="number"] { width: 4.5rem; }
.controls input[type="text"] { width: 14rem; }
.controls select { background: #2c313a; color: var(--ink); border: 1px solid #3a3f4a; border-radius: 0.3rem; padding: 0.15rem 0.4rem; }
.controls input[type="number"], .controls input[type="text"] {
  background: #181b20;
  color: var(--ink);
  border: 1px solid #3a3f4a;
  border-radius: 0.3rem;
  padding: 0.15rem 0.4rem;
}
.area-inputs { display: inline-flex; gap: 0.3rem; }
.area-inputs input { width: 3.8rem; }
.resolution-row { flex-wrap: wrap; }
.progression-row { flex-wrap: wrap; }
.progression-row #progression-label { font-size: 0.85rem; }

/* truncation controls */
.trunc-bytes { flex-wrap: wrap; }
.trunc-bytes > label {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}
.trunc-bytes input[type="number"] { width: 6.5rem; }
#trunc-slider { flex: 1 1 12rem; min-width: 8rem; }
#trunc-label { min-width: 5.5rem; text-align: right; font-variant-numeric: tabular-nums; }

.marker-detail { margin-top: 0.5rem; min-height: 2.5rem; }
.marker-detail a { color: var(--accent); }
.marker-name { font-weight: 700; }
.marker-spec { color: var(--muted); }

#trunc-status { color: var(--muted); min-height: 1.2rem; }

footer { padding: 0 2rem 2rem; color: var(--muted); font-size: 0.85rem; }
footer a { color: var(--accent); }

/* effective encoder settings + conflict warnings */
.effective {
  margin-top: 0.6rem;
  font-size: 0.82rem;
  line-height: 1.5;
}
.effective b { color: var(--ink); }
.effective .file { color: #9fb7d0; }
.effective .warn { color: var(--marker-hot); }

/* per-layer byte thresholds */
.layer-sizes { margin-top: 0.4rem; font-size: 0.82rem; }
.layer-sizes .hint { color: var(--muted); margin-bottom: 0.25rem; }
.layer-sizes table { border-collapse: collapse; }
.layer-sizes th, .layer-sizes td {
  text-align: right;
  padding: 0.1rem 0.6rem 0.1rem 0;
  font-variant-numeric: tabular-nums;
}
.layer-sizes th { color: var(--muted); font-weight: 500; }
.layer-sizes tbody tr, .layer-sizes tr:not(:first-child) { cursor: pointer; }
.layer-sizes tr:hover td { color: var(--accent); }
