:root {
  --bg: #11151f;
  --panel: #1b2130;
  --text: #e8ecf4;
  --muted: #9aa4b8;
  --accent: #ffb020;
  --accent-text: #1b1300;
  --line: #2c3447;
  --radius: 12px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

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

header {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
header h1 { font-size: 1.15rem; margin: 0; }

#menu {
  position: absolute; right: 12px; top: 100%;
  display: flex; flex-direction: column;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden; box-shadow: 0 8px 24px #0008;
}
#menu button {
  background: none; border: 0; color: var(--text); text-align: left;
  padding: 14px 18px; font-size: 1rem;
}
#menu button + button { border-top: 1px solid var(--line); }

main { max-width: 900px; margin: 0 auto; padding: 16px; padding-bottom: calc(env(safe-area-inset-bottom) + 32px); }
h2 { font-size: 1.2rem; margin: 4px 0 8px; }
.hint { color: var(--muted); font-size: .92rem; }

.actions { display: flex; gap: 10px; flex-wrap: wrap; margin: 12px 0; }
.actions > * { flex: 1 1 140px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 48px; padding: 10px 16px;
  border-radius: var(--radius); border: 1px solid var(--line);
  background: var(--panel); color: var(--text);
  font: inherit; font-weight: 600; cursor: pointer; user-select: none;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.big { min-height: 60px; font-size: 1.05rem; }
.btn:active { transform: scale(.98); }
.icon { background: none; border: 0; color: var(--text); font-size: 1.6rem; padding: 0 8px; cursor: pointer; }

.canvas-wrap { position: relative; margin: 12px 0; }
canvas { display: block; max-width: 100%; border-radius: 8px; }
#crop-canvas { touch-action: none; }
#loupe {
  position: absolute; top: 8px; width: 120px; height: 120px;
  border-radius: 50%; border: 3px solid var(--accent);
  pointer-events: none; box-shadow: 0 4px 16px #000a;
}

.form-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.form-grid label { display: flex; flex-direction: column; gap: 4px; font-size: .85rem; color: var(--muted); }
input[type=number] {
  min-height: 44px; padding: 8px 10px; font-size: 1rem;
  border-radius: 8px; border: 1px solid var(--line); background: var(--bg); color: var(--text);
}

.toggles { display: flex; flex-wrap: wrap; gap: 8px 20px; }
.toggle { display: flex; align-items: center; gap: 8px; color: var(--muted); }
.toggle input { width: 20px; height: 20px; }

.summary { display: flex; gap: 14px; align-items: center; background: var(--panel); border-radius: var(--radius); padding: 12px; }
.piece-box { flex: 0 0 96px; height: 96px; display: grid; place-items: center; }
.piece-box img { max-width: 96px; max-height: 96px; transition: transform .3s; }
.where { font-size: 1.25rem; font-weight: 700; margin: 0 0 4px; }
#instruction { margin: 0; color: var(--muted); }

#candidates { list-style: none; padding: 0; margin: 12px 0; display: grid; gap: 10px; }
#candidates li {
  display: grid; grid-template-columns: 110px 1fr; gap: 12px; align-items: center;
  background: var(--panel); border: 2px solid transparent; border-radius: var(--radius); padding: 8px; cursor: pointer;
}
#candidates li.selected { border-color: var(--accent); }
#candidates canvas { width: 110px; height: 110px; }
#candidates .title { font-weight: 700; }
#candidates .meta { color: var(--muted); font-size: .9rem; }
.bar { height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; margin-top: 6px; }
.bar > span { display: block; height: 100%; background: var(--accent); }

#status {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 20px); transform: translateX(-50%);
  max-width: calc(100% - 32px); padding: 12px 18px; border-radius: var(--radius);
  background: #2a3246; box-shadow: 0 8px 24px #000a; z-index: 20;
}
#status.error { background: #7a2430; }
#status.busy::before {
  content: ""; display: inline-block; width: 14px; height: 14px; margin-right: 10px; vertical-align: -2px;
  border: 2px solid var(--text); border-right-color: transparent; border-radius: 50%; animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
