:root {
  --ink: #1c2128;
  --muted: #5d6673;
  --paper: #e6e8ed;
  --panel: #f7f8fa;
  --panel-2: #ffffff;
  --line: #d5d9e0;
  --rail: #1c2128;
  --rail-ink: #e8eaee;
  --mark: #c4372f;
  --accent: #1f4fd8;
  --ok: #0f6b45;
  --warn: #8a4b08;
  --warn-bg: #fff6e8;
  --shadow: 0 12px 40px rgba(22, 26, 33, 0.16);
  --header: 48px;
  color: var(--ink);
  font-family: "Liberation Sans", "DejaVu Sans", "Segoe UI", sans-serif;
  font-size: 13px;
}
html[data-theme="dark"] {
  --ink: #e7e9ee;
  --muted: #a3acb8;
  --paper: #12151a;
  --panel: #1a1e25;
  --panel-2: #222833;
  --line: #313845;
  --warn-bg: #3a2c18;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  display: grid;
  grid-template-rows: var(--header) minmax(0, 1fr) auto;
  background: var(--paper);
  color: var(--ink);
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
#file {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
header {
  display: flex; align-items: center; gap: 10px;
  padding: 0 12px; background: var(--panel-2); border-bottom: 1px solid var(--line);
}
.mark {
  width: 18px; height: 18px; border: 1.5px solid var(--mark); border-radius: 50%;
  position: relative; flex: none;
}
.mark::before, .mark::after { content: ""; position: absolute; background: var(--mark); }
.mark::before { left: 50%; top: -3px; bottom: -3px; width: 1.5px; transform: translateX(-50%); }
.mark::after { top: 50%; left: -3px; right: -3px; height: 1.5px; transform: translateY(-50%); }
.brand { display: flex; flex-direction: column; line-height: 1.05; }
.brand strong { letter-spacing: 0.14em; font-size: 13px; font-weight: 700; }
.brand span { color: var(--muted); font-size: 11px; }
.spacer, .grow { flex: 1; }
.hist, .unit { display: flex; gap: 4px; }
.ghost, .hist button, .unit button, .mini, .stagebar button, #keys button {
  background: transparent; border: 1px solid var(--line); border-radius: 6px;
  padding: 4px 8px; color: var(--ink);
}
.unit button.on, .ops button.on, .presets button.on, .cands button.on, #order button.on {
  background: var(--ink); color: var(--panel); border-color: var(--ink);
}
html[data-theme="dark"] .unit button.on,
html[data-theme="dark"] .presets button.on,
html[data-theme="dark"] .cands button.on,
html[data-theme="dark"] #order button.on {
  background: #e7e9ee; color: #1c2128; border-color: #e7e9ee;
}
.body { display: grid; grid-template-columns: 76px minmax(0, 1fr) 380px; min-height: 0; }
.ops {
  background: var(--rail); color: var(--rail-ink);
  display: flex; flex-direction: column; gap: 2px; padding: 6px; overflow: auto;
}
.ops button {
  background: transparent; color: inherit; border: 0; border-radius: 8px;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 7px 2px; font-size: 10px; letter-spacing: 0.01em;
}
.ops button svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.6; }
.ops button.on { background: #2c333d; color: white; box-shadow: inset 2px 0 0 var(--mark); }
.ops button:hover { background: #262c35; }
.stage { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.thumbs {
  display: flex; gap: 6px; overflow: auto; padding: 8px 10px 0; min-height: 0;
}
.thumbs:empty { display: none; }
.thumbs button {
  border: 1px solid var(--line); background: var(--panel-2); border-radius: 4px;
  padding: 3px; display: flex; flex-direction: column; align-items: center; gap: 2px;
}
.thumbs button.on { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.thumbs canvas { display: block; width: 46px; height: auto; background: white; }
.thumbs span { font-size: 10px; color: var(--muted); }
.stagebar {
  display: flex; align-items: center; gap: 6px; padding: 6px 10px; color: var(--muted); flex-wrap: wrap;
}
.chip { display: flex; align-items: center; gap: 4px; font-size: 12px; }
#zoomPct, #sheetLabel { font-variant-numeric: tabular-nums; min-width: 3.2em; }
#viewport {
  flex: 1; min-height: 0; position: relative; overflow: hidden;
  display: flex; align-items: center; justify-content: center;
  background:
    linear-gradient(45deg, rgba(0,0,0,0.03) 25%, transparent 25%),
    linear-gradient(-45deg, rgba(0,0,0,0.03) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, rgba(0,0,0,0.03) 75%),
    linear-gradient(-45deg, transparent 75%, rgba(0,0,0,0.03) 75%);
  background-size: 22px 22px;
  background-position: 0 0, 0 11px, 11px -11px, -11px 0;
  cursor: grab;
}
#viewport.panning { cursor: grabbing; }
#dropHint {
  position: absolute; inset: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 6px; text-align: center;
  pointer-events: none; z-index: 2;
}
#dropHint strong { font-size: 22px; font-weight: 600; }
#dropHint p { margin: 0; color: var(--muted); max-width: 360px; }
#dropHint button { pointer-events: auto; }
#dropHint.hidden { display: none; }
body:not([data-ready="1"]) .ops,
body:not([data-ready="1"]) aside,
body:not([data-ready="1"]) .stagebar,
body:not([data-ready="1"]) .thumbs,
body:not([data-ready="1"]) #order,
body:not([data-ready="1"]) #board,
body:not([data-ready="1"]) #viewport,
body:not([data-ready="1"]) #fitBanner,
body:not([data-ready="1"]) .hist { display: none; }
body:not([data-ready="1"]) .body { display: block; }
body:not([data-ready="1"]) .stage { height: 100%; }
#welcome { display: none; height: 100%; }
body:not([data-ready="1"]) #welcome {
  display: flex; align-items: center; justify-content: center;
  height: 100%; padding: 28px 18px;
}
.dropzone {
  width: min(720px, 100%);
  min-height: min(460px, calc(100vh - 180px));
  border: 1.5px dashed #9aa3b2;
  border-radius: 18px;
  background: var(--panel-2);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; gap: 10px; padding: 36px 28px;
}
.dropzone:focus { outline: 2px solid var(--accent); outline-offset: 4px; }
.dropzone.drag { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 8%, var(--panel-2)); }
.eyebrow { margin: 0; letter-spacing: 0.14em; text-transform: uppercase; font-size: 11px; color: var(--muted); }
#welcomeTitle { margin: 0; font-size: 34px; line-height: 1.15; font-weight: 650; letter-spacing: -0.02em; }
.lead { margin: 0; max-width: 460px; color: var(--muted); font-size: 15px; line-height: 1.45; }
.welcome-actions { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; margin-top: 8px; }
.primary, #fitApply {
  background: var(--ink); color: var(--panel); border: 0; border-radius: 8px;
  padding: 10px 16px; font-size: 14px;
}
.primary:focus, #fitApply:focus, .dropzone button:focus { outline: 2px solid var(--accent); outline-offset: 2px; }
html[data-theme="dark"] .primary, html[data-theme="dark"] #fitApply { background: #e7e9ee; color: #1c2128; }
.welcome-actions .ghost {
  background: transparent; border: 1px solid var(--line); border-radius: 8px; padding: 10px 16px;
}
.welcome-msg { min-height: 1.2em; color: var(--warn); margin: 4px 0 0; max-width: 460px; }
.banner {
  display: flex; gap: 12px; align-items: center; justify-content: space-between;
  margin: 8px 10px 0; padding: 10px 12px; border-radius: 8px;
  background: var(--warn-bg); color: var(--warn); border: 1px solid color-mix(in srgb, var(--warn) 35%, transparent);
}
.banner[hidden] { display: none; }
.banner p { margin: 0; line-height: 1.35; }
.busy { display: flex; align-items: center; gap: 8px; color: var(--muted); min-width: 180px; }
.busy[hidden] { display: none; }
.bar {
  width: 72px; height: 4px; border-radius: 99px; background: var(--line); position: relative; overflow: hidden;
}
.bar::after {
  content: ""; position: absolute; inset: 0 auto 0 0; width: 40%; background: var(--accent);
  animation: slide 0.9s ease-in-out infinite;
}
@keyframes slide { 0% { transform: translateX(-100%); } 100% { transform: translateX(280%); } }
#viewport.drag { outline: 2px dashed var(--accent); outline-offset: -8px; }
#board {
  display: grid;
  grid-template-columns: 22px auto;
  grid-template-rows: 18px auto;
  transform-origin: center center;
  filter: drop-shadow(var(--shadow));
}
.ruler { background: var(--panel); display: block; }
.ruler.x { grid-column: 2; grid-row: 1; }
.ruler.y { grid-column: 1; grid-row: 2; }
#sheetWrap { grid-column: 2; grid-row: 2; position: relative; background: white; }
#pages canvas { display: block; background: white; }
#overlay { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
#order { display: flex; gap: 6px; overflow: auto; padding: 6px 10px 8px; }
#order:empty { display: none; }
#order button {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px;
  padding: 4px 8px; text-align: left; white-space: nowrap; font-size: 11px;
}
aside {
  background: var(--panel); border-left: 1px solid var(--line);
  min-height: 0; display: flex; flex-direction: column;
}
.scroll { overflow: auto; padding: 8px 10px 18px; }
.kicker { margin: 0 0 8px; color: var(--muted); font-size: 11px; line-height: 1.35; }
details.group {
  border: 1px solid var(--line); border-radius: 8px; background: var(--panel-2);
  margin: 0 0 8px; padding: 0 10px 8px;
}
details.group[hidden] { display: none; }
details.group summary {
  cursor: pointer; padding: 8px 0 6px; font-size: 11px; letter-spacing: 0.08em;
  text-transform: uppercase; color: var(--muted);
}
label { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin: 4px 0; }
label.check { justify-content: space-between; }
input[type=number], input[type=text], select {
  border: 1px solid var(--line); background: var(--panel); border-radius: 5px;
  padding: 4px 6px; width: 118px;
}
input[type=checkbox] { width: 15px; height: 15px; accent-color: var(--accent); }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 0 8px; }
.help { color: var(--muted); font-size: 12px; line-height: 1.35; margin: 4px 0 0; }
.fileline { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.fileline input[type=file] { width: auto; max-width: 100%; }
.fileline input[type=text] { width: 120px; }
.presets { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.presets button {
  width: 64px; min-height: 54px; border: 1px solid var(--line); background: var(--panel);
  border-radius: 6px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  font-size: 10px; padding: 4px;
}
.presets i { display: block; border: 1.5px solid var(--ink); background: white; }
html[data-theme="dark"] .presets i { background: #d7dbe3; }
.cands { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 6px; }
.cands button {
  border: 1px solid var(--line); background: var(--panel); border-radius: 6px;
  padding: 6px 4px; text-align: left; font-size: 11px; line-height: 1.25;
}
.cands button b { display: block; font-size: 12px; }
.cands button.on { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.sub { margin-top: 8px; font-size: 11px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
#gangList { list-style: none; margin: 0; padding: 0; }
#gangList li { border-top: 1px solid var(--line); padding: 6px 0; }
#gangList .grow { display: flex; justify-content: space-between; gap: 8px; }
.status {
  display: flex; align-items: center; gap: 12px;
  padding: 6px 12px; border-top: 1px solid var(--line); background: var(--panel-2);
  max-height: 96px;
}
#info { flex: 1; min-width: 0; max-height: 80px; overflow: auto; line-height: 1.35; font-variant-numeric: tabular-nums; }
#info b { font-weight: 650; }
.warn { color: var(--warn); background: var(--warn-bg); border-radius: 4px; padding: 2px 6px; display: inline-block; margin: 3px 6px 0 0; }
.note { color: var(--muted); font-size: 12px; margin-top: 3px; }
#dl {
  background: var(--ink); color: var(--panel); border: 0; border-radius: 6px;
  padding: 8px 12px; white-space: nowrap;
}
html[data-theme="dark"] #dl { background: #e7e9ee; color: #1c2128; }
#dl:disabled { opacity: 0.45; cursor: not-allowed; }
#keys { border: 1px solid var(--line); border-radius: 10px; background: var(--panel-2); color: var(--ink); padding: 0; }
#keys form { padding: 14px 16px 16px; min-width: 280px; }
#keys header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
#keys ul { margin: 0; padding: 0; list-style: none; display: grid; gap: 6px; }
kbd { border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; padding: 0 4px; font-size: 11px; }
@media (max-width: 980px) {
  body { height: auto; min-height: 100%; display: block; }
  header, .status { position: sticky; z-index: 3; }
  header { top: 0; }
  .status { bottom: 0; }
  .body { display: flex; flex-direction: column; }
  .ops { flex-direction: row; overflow: auto; }
  .ops button { min-width: 64px; }
  .stage { height: 72vh; min-height: 460px; }
  aside { border-left: 0; border-top: 1px solid var(--line); }
  .status { flex-wrap: wrap; }
  #dl { width: 100%; }
}
