:root {
  --bg: #f7f4ee;
  --surface: #ffffff;
  --surface-low: #f0ece3;
  --border: #ddd6c8;
  --ink: #2b2722;
  --ghost: #8a8477;
  --muted: #6d685c;
  --accent: #d4621a;
  --accent-hover: #b85315;
  --accent-ink: #ffffff;
  --checker: #e4ded2;
  color-scheme: light;
}

html[data-theme="dark"] {
  --bg: #171614;
  --surface: #1f1e1b;
  --surface-low: #272521;
  --border: #3a3730;
  --ink: #ece7dc;
  --ghost: #8b8577;
  --muted: #a39d8e;
  --accent: #e07a33;
  --accent-hover: #ee8a45;
  --checker: #2e2c26;
  color-scheme: dark;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: ui-sans-serif, system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--ink);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

button {
  font: inherit;
  cursor: pointer;
}

/* ---------- header ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-wrap: wrap;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.topbar h1 {
  font-size: 17px;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.badge {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-ink);
  white-space: nowrap;
}

.theme-btn {
  background: var(--surface-low);
  border: 1px solid var(--border);
  color: var(--ink);
  padding: 6px 14px;
  border-radius: 8px;
  font-size: 13px;
}

.theme-btn:hover {
  border-color: var(--accent);
}

/* ---------- layout ---------- */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  gap: 18px;
  padding: 18px 20px;
  max-width: 1280px;
  width: 100%;
  margin: 0 auto;
  align-items: start;
  flex: 1;
}

.stage {
  min-width: 0;
}

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

@media (max-width: 860px) {
  .layout {
    grid-template-columns: 1fr;
  }
}

/* ---------- cards ---------- */
.card {
  border: 1px solid var(--border);
  background: var(--surface-low);
  border-radius: 10px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.card-title {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.seg {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.seg button {
  flex: 1 0 calc(50% - 4px);
  padding: 6px 4px;
  font-size: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink);
  border-radius: 6px;
}

.seg button.active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}

.check {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  cursor: pointer;
  user-select: none;
}

.check input {
  accent-color: var(--accent);
}

.stats {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.stat {
  display: flex;
  justify-content: space-between;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
}

.stat span {
  color: var(--ghost);
}

/* ---------- drop zone ---------- */
.drop-zone {
  border: 2px dashed var(--border);
  border-radius: 12px;
  background: var(--surface);
  min-height: 320px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  text-align: center;
  padding: 24px;
  transition: border-color 0.15s;
}

.drop-zone.drag {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 6%, var(--surface));
}

.drop-main {
  font-size: 17px;
  font-weight: 600;
}

.drop-hint {
  font-size: 12px;
  color: var(--ghost);
}

/* ---------- crop ---------- */
.crop-stage {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.crop-wrap {
  position: relative;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  background: var(--checker);
  touch-action: none;
  user-select: none;
}

#crop-canvas {
  display: block;
  width: 100%;
  height: auto;
}

.crop-box {
  position: absolute;
  border: 2px solid var(--accent);
  cursor: move;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.35);
  touch-action: none;
}

.crop-handle {
  position: absolute;
  right: -9px;
  bottom: -9px;
  width: 18px;
  height: 18px;
  border-radius: 4px;
  background: var(--accent);
  cursor: nwse-resize;
}

.crop-tip {
  font-size: 12px;
  color: var(--ghost);
  text-align: center;
}

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

/* ---------- buttons ---------- */
.btn {
  padding: 8px 16px;
  font-size: 13px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink);
  border-radius: 8px;
}

.btn:hover {
  border-color: var(--accent);
}

.btn-accent {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}

.btn-accent:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.btn:disabled {
  opacity: 0.5;
  cursor: default;
}

/* ---------- grid ---------- */
.grid {
  display: grid;
  grid-template-columns: repeat(24, 1fr);
  aspect-ratio: 1;
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
  border: 1px solid var(--border);
  background: var(--checker);
  user-select: none;
}

.grid.lines .cell {
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
}

.cell {
  aspect-ratio: 1;
  cursor: pointer;
  background: transparent;
  position: relative;
}

.cell:hover {
  outline: 1px solid var(--ink);
  outline-offset: -1px;
  z-index: 1;
}

.cell.sel {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
  z-index: 2;
}

.cell.dim {
  opacity: 0.12;
}

.cell .num {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 9px;
  font-family: ui-monospace, monospace;
  color: #fff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.85);
  pointer-events: none;
}

.detail-bar {
  margin: 10px auto 0;
  max-width: 560px;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  padding: 8px 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.detail-bar .swatch {
  width: 16px;
  height: 16px;
  border-radius: 4px;
  border: 1px solid var(--border);
}

/* ---------- legend ---------- */
.legend {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.legend .empty {
  font-size: 12px;
  color: var(--ghost);
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  font-family: ui-monospace, monospace;
  padding: 3px 6px;
  border-radius: 6px;
  cursor: pointer;
}

.legend-item:hover {
  background: var(--surface);
}

.legend-item.active {
  outline: 1px solid var(--accent);
}

.legend-item .sw {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  border: 1px solid var(--border);
  flex: none;
}

.legend-item .cnt {
  margin-left: auto;
  color: var(--ghost);
}

/* ---------- footer ---------- */
.foot {
  text-align: center;
  font-size: 12px;
  color: var(--ghost);
  padding: 16px 20px 24px;
}

/* ---------- probe ---------- */
#probe-out {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: 200px;
  overflow: auto;
  background: #fff;
  color: #111;
  border-top: 2px solid #f00;
  padding: 8px;
  font-size: 11px;
  z-index: 9999;
  white-space: pre-wrap;
}
