:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-soft: #f8fafc;
  --ink: #18202a;
  --muted: #667085;
  --line: #d9e0e7;
  --line-strong: #b8c2cc;
  --teal: #007c78;
  --coral: #df513c;
  --amber: #c98715;
  --green: #26734d;
  --blue: #2f68b7;
  --shadow: 0 18px 42px rgba(24, 32, 42, 0.08);
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font-family: Inter, "Avenir Next", "Segoe UI", system-ui, sans-serif;
  line-height: 1.5;
}

button,
input {
  font: inherit;
}

button {
  color: inherit;
}

canvas {
  display: block;
}

code,
pre {
  font-family: "SF Mono", "JetBrains Mono", Consolas, monospace;
}

.app-shell {
  width: min(1500px, calc(100vw - 28px));
  margin: 0 auto;
  padding: 18px 0 42px;
}

.app-header {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(360px, 0.72fr);
  gap: 18px;
  align-items: stretch;
  margin-bottom: 18px;
}

.app-header > div:first-child,
.status-strip,
.surface {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.app-header > div:first-child {
  padding: 24px;
}

.eyebrow,
.section-kicker {
  margin: 0 0 8px;
  color: var(--teal);
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

h1,
h2,
h3,
p,
dl {
  margin-top: 0;
}

h1 {
  margin-bottom: 10px;
  font-size: 2.4rem;
  line-height: 1.08;
  letter-spacing: 0;
}

h2 {
  margin-bottom: 8px;
  font-size: 1.32rem;
  line-height: 1.2;
  letter-spacing: 0;
}

h3 {
  margin-bottom: 8px;
  font-size: 0.95rem;
  letter-spacing: 0;
}

.lede,
.step-copy p,
.fine-print {
  color: var(--muted);
}

.lede {
  max-width: 72ch;
  margin-bottom: 0;
  font-size: 1.02rem;
}

.status-strip {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1px;
  overflow: hidden;
  background: var(--line);
}

.status-strip div {
  min-width: 0;
  padding: 18px;
  background: var(--surface);
}

.status-strip dt {
  color: var(--muted);
  font-size: 0.8rem;
}

.status-strip dd {
  margin: 4px 0 0;
  font-size: 1.1rem;
  font-weight: 800;
}

.workbench {
  display: grid;
  grid-template-columns: minmax(320px, 430px) minmax(0, 1fr);
  gap: 18px;
  align-items: start;
}

.surface {
  min-width: 0;
}

.image-surface {
  position: sticky;
  top: 14px;
  padding: 18px;
}

.pipeline-surface {
  padding: 0;
  overflow: clip;
}

.surface-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: start;
  margin-bottom: 14px;
}

.surface-head h2 {
  margin-bottom: 0;
}

.small-button,
.step-nav button,
.move-grid button {
  border: 1px solid var(--line-strong);
  background: var(--surface);
  border-radius: 8px;
  cursor: pointer;
  transition: border-color 0.18s ease, background 0.18s ease, transform 0.18s ease;
}

.small-button:hover,
.step-nav button:hover,
.move-grid button:hover {
  border-color: var(--teal);
  transform: translateY(-1px);
}

.small-button {
  padding: 9px 12px;
  white-space: nowrap;
  font-weight: 700;
}

.file-drop,
.control-block {
  display: grid;
  gap: 8px;
  margin-bottom: 14px;
  padding: 12px;
  background: var(--surface-soft);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.file-drop span,
.control-block label {
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 700;
}

.file-drop input {
  width: 100%;
}

.preset-panel {
  display: grid;
  gap: 10px;
  margin-bottom: 14px;
}

.preset-head {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: center;
}

.preset-head span {
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 800;
}

.preset-head small {
  color: var(--teal);
  font-size: 0.76rem;
  font-weight: 800;
  text-align: right;
}

.preset-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.preset-card {
  display: grid;
  grid-template-columns: 46px minmax(0, 1fr);
  gap: 8px;
  align-items: center;
  min-width: 0;
  padding: 7px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
  cursor: pointer;
  text-align: left;
}

.preset-card:hover {
  border-color: var(--teal);
}

.preset-card.is-active {
  border-color: var(--teal);
  background: #edf8f7;
}

.preset-thumb {
  width: 46px;
  aspect-ratio: 1 / 1;
  border-radius: 6px;
  background: linear-gradient(135deg, #e4ecf2, #fff);
  object-fit: cover;
}

.synthetic-thumb {
  display: block;
  background:
    linear-gradient(135deg, rgba(223, 81, 60, 0.78), transparent 48%),
    linear-gradient(45deg, rgba(0, 124, 120, 0.74), transparent 54%),
    repeating-linear-gradient(90deg, #ffffff 0 4px, #cdd7df 4px 8px);
}

.preset-card strong,
.preset-card span {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.preset-card strong {
  font-size: 0.78rem;
}

.preset-card span {
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 700;
}

.range-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 42px;
  gap: 10px;
  align-items: center;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--coral);
}

.move-grid {
  display: grid;
  grid-template-columns: repeat(3, 44px);
  grid-template-rows: repeat(2, 40px);
  gap: 8px;
  justify-content: center;
  margin: 6px 0 14px;
}

.move-grid button {
  font-size: 1.1rem;
  font-weight: 800;
}

#moveUpBtn {
  grid-column: 2;
}

#moveLeftBtn {
  grid-column: 1;
  grid-row: 2;
}

#moveRightBtn {
  grid-column: 3;
  grid-row: 2;
}

#moveDownBtn {
  grid-column: 2;
  grid-row: 2;
}

.image-frame {
  padding: 8px;
  background: #111820;
  border-radius: 8px;
}

#imageCanvas {
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: 6px;
  cursor: crosshair;
}

.mini-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-top: 12px;
}

.mini-grid article {
  min-width: 0;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
}

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

.mini-grid canvas {
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: 6px;
  background: #fff;
}

.recon-image-card {
  display: grid;
  gap: 10px;
  margin-top: 12px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
}

.recon-image-card h3 {
  margin-bottom: 2px;
  color: var(--ink);
}

.recon-image-card p {
  margin-bottom: 0;
  color: var(--muted);
  font-size: 0.84rem;
}

.recon-image-card canvas {
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: 6px;
  background: #fff;
}

.recon-stats {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.recon-stats span {
  min-width: 0;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 800;
  text-align: center;
}

.fine-print {
  margin: 12px 0 0;
  font-size: 0.86rem;
}

.step-nav {
  position: sticky;
  top: 0;
  z-index: 5;
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 8px;
  padding: 12px;
  background: rgba(255, 255, 255, 0.94);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(12px);
}

.step-nav button {
  min-height: 42px;
  padding: 8px 10px;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 800;
  text-align: center;
}

.step-nav button.is-active {
  background: #edf8f7;
  border-color: var(--teal);
  color: var(--teal);
}

.step-section {
  padding: 24px;
  border-bottom: 1px solid var(--line);
  scroll-margin-top: 76px;
}

.step-section:last-child {
  border-bottom: 0;
}

.step-copy p {
  margin-bottom: 12px;
}

.step-copy {
  margin-bottom: 18px;
}

.formula {
  overflow-x: auto;
  padding: 12px;
  background: #fbfcfd;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.matrix-pair,
.zigzag-layout {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
  align-items: start;
}

.matrix-title {
  margin-bottom: 7px;
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 800;
}

.matrix {
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  gap: 2px;
  min-width: 0;
}

.matrix-cell {
  position: relative;
  display: grid;
  place-items: center;
  aspect-ratio: 1 / 1;
  min-width: 0;
  padding: 2px;
  border: 1px solid rgba(24, 32, 42, 0.08);
  border-radius: 5px;
  color: #111820;
  font-size: 0.72rem;
  font-variant-numeric: tabular-nums;
  font-weight: 750;
  overflow: hidden;
}

.wide-number-matrix .matrix-cell {
  font-size: 0.64rem;
}

#dctMatrix {
  width: min(50%, 520px);
  max-width: 100%;
  margin-inline: auto;
}

.cell-index {
  position: absolute;
  top: 2px;
  left: 3px;
  color: rgba(24, 32, 42, 0.58);
  font-size: 0.52rem;
  font-weight: 800;
}

.cell-zero {
  color: #7a8794;
}

.order-matrix .matrix-cell {
  background: #f4f8fb;
  color: var(--blue);
  font-size: 1.12rem;
}

.order-matrix .cell-value {
  position: relative;
  z-index: 3;
  min-width: 1.62em;
  padding: 2px 4px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.92);
  color: #1d5594;
}

.zigzag-map {
  position: relative;
}

.zigzag-map .matrix-cell {
  z-index: 1;
}

.zigzag-path {
  position: absolute;
  inset: 0;
  z-index: 2;
  overflow: visible;
  pointer-events: none;
}

.zigzag-path polyline {
  fill: none;
  stroke: var(--coral);
  opacity: 0.68;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 1.35;
  vector-effect: non-scaling-stroke;
}

.zigzag-path circle {
  fill: #fff;
  stroke: var(--coral);
  opacity: 0.78;
  stroke-width: 1.3;
  vector-effect: non-scaling-stroke;
}

.zigzag-path .zigzag-start {
  fill: var(--green);
  stroke: #fff;
}

.zigzag-path .zigzag-end {
  fill: var(--coral);
  stroke: #fff;
}

.metric-line {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-top: 12px;
}

.metric-line span {
  padding: 10px 12px;
  background: var(--surface-soft);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--muted);
  font-weight: 700;
}

.zigzag-sequence {
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  gap: 5px;
}

.zigzag-item {
  min-width: 0;
  padding: 6px 4px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  text-align: center;
  font-variant-numeric: tabular-nums;
  overflow: hidden;
}

.zigzag-item small {
  display: block;
  color: var(--muted);
  font-size: 0.58rem;
  font-weight: 800;
}

.zigzag-item strong {
  display: block;
  color: var(--ink);
  font-size: 0.72rem;
}

.zigzag-item span {
  display: block;
  color: var(--muted);
  font-size: 0.56rem;
  font-weight: 700;
}

.zigzag-item.is-zero {
  background: #f3f5f7;
  color: #8a96a3;
}

.table-wrap {
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 620px;
  background: var(--surface);
  font-size: 0.88rem;
}

th,
td {
  padding: 9px 10px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}

th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: #f7fafc;
  color: var(--muted);
  font-size: 0.76rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

tbody tr:last-child td {
  border-bottom: 0;
}

.mono {
  font-family: "SF Mono", "JetBrains Mono", Consolas, monospace;
  font-size: 0.82rem;
}

.bitstream-panel {
  margin-top: 12px;
  padding: 12px;
  background: #111820;
  border-radius: 8px;
  color: #f2f7fb;
}

.bitstream-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  margin-bottom: 8px;
  color: #c8d3df;
  font-size: 0.86rem;
}

#bitstream {
  display: block;
  max-height: 210px;
  overflow: auto;
  color: #f6d365;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  line-height: 1.7;
}

@media (max-width: 1180px) {
  .app-header,
  .workbench {
    grid-template-columns: 1fr;
  }

  .image-surface {
    position: static;
  }
}

@media (max-width: 760px) {
  .app-shell {
    width: min(100vw - 18px, 1500px);
    padding-top: 9px;
  }

  .app-header > div:first-child,
  .image-surface,
  .step-section {
    padding: 16px;
  }

  h1 {
    font-size: 1.85rem;
  }

  .status-strip,
  .mini-grid,
  .recon-stats,
  .preset-grid,
  .matrix-pair,
  .zigzag-layout,
  .metric-line {
    grid-template-columns: 1fr;
  }

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

  .step-section {
    scroll-margin-top: 160px;
  }

  .matrix-cell {
    font-size: 0.62rem;
  }

  .wide-number-matrix .matrix-cell {
    font-size: 0.54rem;
  }

  #dctMatrix {
    width: 100%;
  }
}
