:root {
  color-scheme: light;
  --bg: #f4f1eb;
  --surface: rgba(255, 255, 255, 0.92);
  --surface-solid: #ffffff;
  --ink: #1e2328;
  --muted: #687078;
  --line: #d8d4cc;
  --accent: #6d235a;
  --accent-dark: #4f1741;
  --accent-soft: #f2e4ee;
  --danger: #a53232;
  --shadow: 0 12px 34px rgba(39, 31, 25, 0.13);
  --radius: 14px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--ink);
  background: var(--bg);
}

body {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

button, input, select { font: inherit; }
button { touch-action: manipulation; }

.app-header {
  min-height: 78px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 14px 20px;
  background: var(--surface-solid);
  border-bottom: 1px solid var(--line);
  z-index: 20;
}

.app-header h1 {
  margin: 0;
  font-size: clamp(1.2rem, 2vw, 1.65rem);
  letter-spacing: -0.02em;
}

.app-header p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.header-actions, .button-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.button {
  appearance: none;
  border: 1px solid #bdb7ad;
  border-radius: 9px;
  padding: 8px 12px;
  background: rgba(255, 255, 255, 0.86);
  color: var(--ink);
  cursor: pointer;
  font-weight: 650;
  line-height: 1.1;
  box-shadow: 0 2px 7px rgba(31, 25, 20, 0.06);
  transition: transform 120ms ease, box-shadow 120ms ease, border-color 120ms ease, background 120ms ease;
}

.button:hover:not(:disabled) {
  transform: translateY(-1px);
  border-color: var(--accent);
  box-shadow: 0 5px 15px rgba(109, 35, 90, 0.13);
}

.button:active:not(:disabled) { transform: translateY(0); }
.button:disabled { opacity: 0.45; cursor: not-allowed; }
.button.primary { background: var(--accent); border-color: var(--accent); color: white; }
.button.primary:hover:not(:disabled) { background: var(--accent-dark); }
.button.small { flex: 1 1 auto; padding: 7px 9px; font-size: 0.82rem; }
.button.wide { width: 100%; margin-top: 8px; }

.app-shell {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 286px minmax(0, 1fr);
}

.control-panel {
  min-height: 0;
  overflow-y: auto;
  padding: 14px;
  border-right: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.78);
  backdrop-filter: blur(12px);
}

.panel-section {
  padding: 13px;
  margin-bottom: 11px;
  background: var(--surface-solid);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 5px 16px rgba(39, 31, 25, 0.05);
}

.panel-section h2 {
  font-size: 0.92rem;
  margin: 0 0 11px;
}

.field-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin: 9px 0 5px;
  color: #383e44;
  font-size: 0.8rem;
  font-weight: 700;
}

.field-label output { color: var(--accent); }

input[type="range"] { width: 100%; accent-color: var(--accent); }
input[type="checkbox"] { width: 17px; height: 17px; accent-color: var(--accent); }
input[type="text"], input[type="number"], select {
  width: 100%;
  border: 1px solid #c8c2b9;
  border-radius: 8px;
  padding: 8px 9px;
  color: var(--ink);
  background: white;
  outline: none;
}

input[type="text"]:focus, input[type="number"]:focus, select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.number-with-unit {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  border: 1px solid #c8c2b9;
  border-radius: 8px;
  overflow: hidden;
  background: white;
}
.number-with-unit input { border: 0; border-radius: 0; min-width: 0; }
.number-with-unit span { padding: 0 10px; color: var(--muted); font-size: 0.8rem; }

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

.template-size-grid > label,
.overlap-grid > label {
  display: grid;
  gap: 5px;
  color: #383e44;
  font-size: 0.76rem;
  font-weight: 700;
}

.export-section #filenameInput {
  font-weight: 700;
  border-color: #aa7f9e;
  background: #fffafd;
}

.check-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 11px;
  font-size: 0.83rem;
  cursor: pointer;
}

.field-help {
  margin: 7px 0 0;
  color: var(--muted);
  font-size: 0.74rem;
  line-height: 1.4;
}

.page-list {
  display: grid;
  gap: 6px;
  max-height: 230px;
  overflow-y: auto;
  padding-right: 2px;
}

.page-list-item {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: 8px;
  padding: 7px;
  border: 1px solid #ddd8d0;
  border-radius: 8px;
  background: #fbfaf8;
  cursor: pointer;
}

.page-list-item:hover { border-color: #ad839f; }
.page-list-item.is-excluded { opacity: 0.55; }
.page-list-item strong { display: block; font-size: 0.76rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.page-list-item span { color: var(--muted); font-size: 0.68rem; }
.empty-list { color: var(--muted); font-size: 0.78rem; margin: 0; }

.workspace-panel {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.workspace-toolbar {
  min-height: 44px;
  padding: 8px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.88);
  font-size: 0.8rem;
}

.status-text { color: var(--muted); }
.status-text.warning { color: var(--danger); font-weight: 700; }
.group-size { font-weight: 700; white-space: nowrap; }

.workspace-viewport {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: auto;
  background-color: #ddd9d2;
  background-image:
    linear-gradient(rgba(255,255,255,.22) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,.22) 1px, transparent 1px);
  background-size: 20px 20px;
}

.workspace-viewport.drag-over::after {
  content: "Drop PDF files to add them";
  position: fixed;
  inset: 95px 22px 22px 308px;
  display: grid;
  place-items: center;
  border: 3px dashed var(--accent);
  border-radius: 18px;
  background: rgba(255,255,255,.88);
  color: var(--accent);
  font-size: 1.35rem;
  font-weight: 800;
  z-index: 2000;
  pointer-events: none;
}

.empty-state {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  justify-items: center;
  text-align: center;
  padding: 28px;
  color: var(--muted);
  z-index: 3;
  pointer-events: none;
}

.empty-state[hidden] { display: none; }
.empty-state h2 { margin: 12px 0 4px; color: #42484d; }
.empty-state p { margin: 0; }
.drop-icon {
  display: grid;
  place-items: center;
  width: 74px;
  height: 88px;
  border: 2px solid var(--accent);
  border-radius: 10px;
  background: white;
  color: var(--accent);
  font-weight: 900;
  box-shadow: var(--shadow);
}

.world-scaler {
  position: relative;
  transform-origin: top left;
  margin: 28px;
}

.world {
  position: relative;
  width: 901px;
  height: 1249px;
  min-width: 901px;
  min-height: 1249px;
  transform-origin: top left;
}

.template-layer, .page-layer { position: absolute; inset: 0; }
.template-layer { pointer-events: none; z-index: 0; }
.page-layer { z-index: 5; }

.sheet-template {
  position: absolute;
  width: 841px;
  height: 1189px;
  border: 2px dashed rgba(109, 35, 90, 0.62);
  background: rgba(255, 255, 255, 0.2);
}

.sheet-template::before {
  content: attr(data-label);
  position: absolute;
  top: 6px;
  left: 8px;
  padding: 3px 6px;
  border-radius: 5px;
  background: rgba(109, 35, 90, 0.85);
  color: white;
  font-size: 12px;
  font-weight: 800;
}

.arranger-page {
  position: absolute;
  background: transparent;
  border: 1px solid #5f6266;
  box-shadow: 0 7px 18px rgba(25, 25, 25, 0.22);
  cursor: grab;
  user-select: none;
  touch-action: none;
  overflow: hidden;
}

.arranger-page:hover { outline: 2px solid rgba(109, 35, 90, 0.45); }
.arranger-page.is-dragging { cursor: grabbing; outline: 3px solid var(--accent); box-shadow: 0 14px 32px rgba(25,25,25,.3); }
.arranger-page.is-excluded { opacity: 0.34; filter: grayscale(0.85); border-style: dashed; }
.arranger-page canvas {
  width: 100%;
  height: 100%;
  display: block;
  pointer-events: none;
  mix-blend-mode: multiply;
}

.page-badge {
  position: absolute;
  top: 5px;
  left: 5px;
  z-index: 3;
  display: flex;
  align-items: center;
  gap: 5px;
  max-width: calc(100% - 10px);
  padding: 4px 6px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid rgba(0,0,0,.18);
  box-shadow: 0 2px 7px rgba(0,0,0,.13);
  font-size: 11px;
  font-weight: 800;
  cursor: default;
}

.page-badge span { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.page-badge input { flex: 0 0 auto; }

.page-number-label {
  position: absolute;
  top: 5px;
  right: 5px;
  z-index: 4;
  max-width: calc(100% - 12px);
  padding: 5px 8px;
  border-radius: 6px;
  background: rgba(31, 35, 40, 0.88);
  color: #fff;
  box-shadow: 0 2px 7px rgba(0,0,0,.2);
  font-size: 12px;
  font-weight: 900;
  line-height: 1.1;
  letter-spacing: .01em;
  pointer-events: none;
  white-space: nowrap;
}

.page-size-label {
  position: absolute;
  right: 5px;
  bottom: 5px;
  z-index: 2;
  padding: 3px 5px;
  border-radius: 4px;
  background: rgba(31, 35, 40, 0.8);
  color: white;
  font-size: 10px;
  pointer-events: none;
}

.loading-overlay {
  position: fixed;
  inset: 0;
  z-index: 5000;
  display: grid;
  place-items: center;
  background: rgba(31, 29, 27, 0.38);
  backdrop-filter: blur(3px);
}
.loading-overlay[hidden] { display: none; }
.loading-card {
  min-width: 270px;
  display: grid;
  justify-items: center;
  gap: 8px;
  padding: 25px;
  border-radius: 15px;
  background: white;
  box-shadow: var(--shadow);
}
.loading-card span { color: var(--muted); font-size: 0.82rem; }
.spinner {
  width: 34px;
  height: 34px;
  border: 4px solid #e4dbe1;
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 850px) {
  body { overflow: auto; }
  .app-header { align-items: flex-start; flex-direction: column; }
  .app-shell { grid-template-columns: 1fr; }
  .control-panel { overflow: visible; border-right: 0; border-bottom: 1px solid var(--line); }
  .workspace-panel { min-height: 70vh; }
  .workspace-viewport.drag-over::after { inset: 20px; }
}

.world.snap-grid-visible {
  background-image:
    linear-gradient(to right, rgba(91, 65, 83, 0.08) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(91, 65, 83, 0.08) 1px, transparent 1px);
  background-size: var(--snap-grid-size, 10px) var(--snap-grid-size, 10px);
}


.page-source-item {
  grid-template-columns: auto minmax(0, 1fr) 58px;
  cursor: pointer;
}

.page-source-text {
  min-width: 0;
}

.page-source-text .copies-summary {
  display: block;
  margin-top: 2px;
  color: var(--accent);
  font-weight: 700;
}

.copy-control {
  display: grid;
  gap: 3px;
  color: var(--muted);
  font-size: 0.65rem;
  font-weight: 800;
  text-align: center;
  cursor: default;
}

.copy-control input {
  width: 58px;
  padding: 5px 4px;
  text-align: center;
}

.arranger-page.list-highlight {
  outline: 3px solid rgba(109, 35, 90, 0.62);
}

select {
  cursor: pointer;
}

.source-copies-list {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(118px, 1fr));
  gap: 4px;
  padding-top: 6px;
  border-top: 1px solid #e6e1da;
}

.copy-list-item {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 3px 5px;
  border: 1px solid #e2ddd6;
  border-radius: 6px;
  background: #fff;
}

.copy-list-item.is-excluded {
  opacity: .48;
}

.copy-list-item input {
  flex: 0 0 auto;
  margin: 0;
}

.copy-list-label {
  min-width: 0;
  padding: 0;
  border: 0;
  background: transparent;
  color: #3f3940;
  font: inherit;
  font-size: .68rem;
  font-weight: 800;
  text-align: left;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}

.copy-list-label:hover {
  color: var(--accent);
  text-decoration: underline;
}

.arranger-page.copy-focus {
  outline: 4px solid rgba(109, 35, 90, .85);
  box-shadow: 0 0 0 7px rgba(109, 35, 90, .18), 0 14px 32px rgba(25,25,25,.3);
}
