:root {
  --bg: #14161b;
  --panel: #1d2027;
  --panel-2: #262a33;
  --line: #333845;
  --text: #e8eaef;
  --muted: #9aa1b0;
  --accent: #ffb020;
  --accent-text: #1a1300;
  --ok: #3ddc84;
  --danger: #ff5a5a;
  color-scheme: dark;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

a { color: var(--accent); }

button, .file-button {
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.5rem 0.9rem;
  cursor: pointer;
}
button:hover, .file-button:hover { border-color: #4a5060; }
button:disabled { opacity: 0.5; cursor: default; }
button.primary, .file-button.primary {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
  font-weight: 600;
}
button.danger { color: var(--danger); }
.link-button { background: none; border: none; color: var(--accent); padding: 0.3rem 0; }

input[type=text], input:not([type]) {
  font: inherit;
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 10px;
  padding: 0.55rem 0.8rem;
}
input[type=range] { accent-color: var(--accent); vertical-align: middle; }
input[type=checkbox] { accent-color: var(--accent); }

.topbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.7rem 1rem;
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  background: rgba(20, 22, 27, 0.92);
  backdrop-filter: blur(6px);
  z-index: 10;
}
.brand { display: flex; align-items: center; gap: 0.5rem; color: var(--text); text-decoration: none; font-weight: 700; }
.puzzle-title { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

main { max-width: 1100px; margin: 0 auto; padding: 1rem; }

.hero { padding: 2.5rem 0 1.5rem; }
.hero h1 { font-size: clamp(2rem, 6vw, 3.2rem); line-height: 1.1; margin: 0 0 1rem; }
.lead { color: var(--muted); max-width: 46rem; font-size: 1.1rem; }
.start-form { display: flex; gap: 0.5rem; flex-wrap: wrap; margin: 1.5rem 0 0.5rem; }
.start-form input { flex: 1 1 14rem; max-width: 24rem; }

.recent ul { list-style: none; padding: 0; }
.recent li { padding: 0.3rem 0; }

.how h2, .recent h2 { margin-top: 2rem; }
.steps { padding-left: 1.2rem; max-width: 50rem; }
.steps li { margin-bottom: 0.7rem; }
.tips { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 0.5rem 1.2rem; max-width: 50rem; }
.tips li { margin-bottom: 0.4rem; color: var(--muted); }
.credits { color: var(--muted); font-size: 0.85rem; margin-top: 2rem; }

.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.hint { color: var(--muted); font-size: 0.92rem; }
.error { color: var(--danger); }
.warn { color: var(--accent); }

.tabs { display: flex; gap: 0.4rem; margin-bottom: 1rem; overflow-x: auto; }
.tabs button { border-radius: 999px; white-space: nowrap; }
.tabs button.active { background: var(--text); color: var(--bg); border-color: var(--text); }
.count { display: inline-block; min-width: 1.4em; padding: 0 0.35em; border-radius: 999px; background: var(--accent); color: var(--accent-text); font-size: 0.8em; font-weight: 700; }

.add-controls { display: flex; flex-wrap: wrap; align-items: center; gap: 1rem; }
.check { display: inline-flex; align-items: center; gap: 0.4rem; color: var(--muted); cursor: pointer; }

.photo-card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 0.8rem; margin: 1rem 0; }
.photo-head { display: flex; justify-content: space-between; gap: 1rem; margin-bottom: 0.5rem; }
.canvas-wrap { overflow: auto; border-radius: 8px; background: #000; }
.photo-canvas { display: block; width: 100%; height: auto; }
.photo-summary { margin: 0.6rem 0; }
.photo-controls { display: flex; flex-wrap: wrap; gap: 0.6rem; align-items: center; justify-content: flex-end; }
.sensitivity { margin-right: auto; color: var(--muted); }

.photo-thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 0.6rem; }
.photo-thumb { padding: 0.3rem; display: flex; flex-direction: column; gap: 0.3rem; font-size: 0.85rem; }
.photo-thumb img { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; border-radius: 6px; }

.filters { display: flex; gap: 0.4rem; flex-wrap: wrap; }
.filters button { padding: 0.3rem 0.8rem; border-radius: 999px; font-size: 0.9rem; }
.filters button.active { border-color: var(--accent); color: var(--accent); }

.piece-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 0.5rem; }
.piece-card { position: relative; display: flex; flex-direction: column; align-items: center; padding: 0.6rem 0.2rem 0.3rem; min-height: 100px; }
.piece-card.failed { border-color: #5a2a2a; }
.piece-number { font-weight: 700; font-size: 0.9rem; }
.badge { font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.badge.corner { color: var(--accent); }
.badge.edge { color: #6cb6ff; }
.badge.failed { color: var(--danger); }

.piece-box { position: relative; flex: none; }
.piece-box img, .board img.placed { position: absolute; pointer-events: none; user-select: none; }
.piece-card .piece-box { margin: 6px 0 10px; }

dialog {
  width: min(900px, 96vw);
  max-height: 92vh;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 0 1rem 1rem;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.6); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; position: sticky; top: 0; background: var(--panel); z-index: 2; padding-top: 0.6rem; }
.dialog-head h2 { font-size: 1.15rem; margin: 0.4rem 0; }
.close { font-size: 1.4rem; line-height: 1; padding: 0.2rem 0.6rem; }
.dialog-actions { display: flex; gap: 0.5rem; flex-wrap: wrap; margin: 0.5rem 0 1rem; }
.debug-image { max-width: 100%; background: #fff; border-radius: 8px; }

.match-row { display: flex; gap: 1rem; align-items: center; padding: 0.8rem 0; border-top: 1px solid var(--line); }
.match-side { display: flex; flex-direction: column; align-items: center; min-width: 90px; font-size: 0.85rem; }
.match-list { display: flex; gap: 0.5rem; overflow-x: auto; padding-bottom: 0.3rem; flex: 1; align-items: center; }
.match { display: flex; flex-direction: column; align-items: center; gap: 0.2rem; padding: 0.4rem; flex: none; }
.match-label { font-size: 0.85rem; }
.prob { color: hsl(calc(var(--p) * 120), 75%, 60%); font-weight: 600; }
.pair { display: flex; align-items: center; padding: 6px; }
.pair-b { margin-left: -1px; }
.highlight-right { margin: 8px; }
.highlight-right::after {
  content: "";
  position: absolute;
  right: -5px;
  top: 15%;
  bottom: 15%;
  width: 4px;
  border-radius: 2px;
  background: var(--accent);
}

.solve-bar { display: flex; gap: 1rem; align-items: center; flex-wrap: wrap; }
.solve-status { flex: 1; min-width: 14rem; }
.solve-options { display: flex; gap: 1rem; flex-wrap: wrap; align-items: center; margin: 1rem 0; color: var(--muted); }
.spinner { display: inline-block; width: 1em; height: 1em; border: 2px solid var(--muted); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.8s linear infinite; vertical-align: -0.15em; margin-right: 0.5em; }
@keyframes spin { to { transform: rotate(360deg); } }

.group { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 0.8rem; margin: 1rem 0; }
.group-head { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-bottom: 0.5rem; }
.group-head h3 { margin: 0; font-size: 1.05rem; }
.group-head button { padding: 0.25rem 0.6rem; font-size: 0.85rem; }
.spacer { flex: 1; }
.board-scroll { overflow: auto; max-height: 80vh; background: #0d0e12; border-radius: 10px; }
.board { position: relative; margin: 0 auto; }
.placed-label {
  position: absolute;
  transform: translate(-50%, -50%);
  background: none;
  border: none;
  padding: 0;
  min-width: 1.5em;
  min-height: 1.5em;
  font-weight: 700;
  color: #fff;
  text-shadow: 0 0 3px #000, 0 0 6px #000;
}
.link {
  position: absolute;
  transform: translate(-50%, -50%);
  font-size: 0.7rem;
  font-weight: 700;
  padding: 0 0.25em;
  border-radius: 4px;
  pointer-events: none;
  background: hsla(calc(var(--p) * 120), 80%, 35%, 0.9);
}
.layout-grid { border-collapse: collapse; font-variant-numeric: tabular-nums; }
.layout-grid td { border: 1px solid var(--line); padding: 0.25rem 0.5rem; text-align: center; }

@media (max-width: 600px) {
  main { padding: 0.6rem; }
  .match-row { flex-direction: column; align-items: stretch; }
  .match-side { flex-direction: row; gap: 0.6rem; }
}
