:root {
  color-scheme: light;
  --bg: #eef3f8;
  --surface: #ffffff;
  --surface-alt: #f8fafc;
  --line: #d6dee8;
  --text: #122033;
  --muted: #5b6677;
  --accent: #2457d6;
  --accent-hover: #1d46ad;
  --success: #0f766e;
  --danger: #b42318;
  --shadow: 0 18px 48px rgba(16, 24, 40, 0.08);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  min-height: 100%;
}

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

button,
input,
textarea,
select {
  font: inherit;
}

input[type="text"],
input[type="number"],
select {
  width: 100%;
  min-height: 2.7rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  color: var(--text);
  padding: 0.62rem 0.78rem;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

select {
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 18px) 50%, calc(100% - 13px) 50%;
  background-repeat: no-repeat;
  background-size: 5px 5px, 5px 5px;
  padding-right: 2.4rem;
}

input[type="text"]:focus,
input[type="number"]:focus,
select:focus {
  border-color: rgba(36, 87, 214, 0.6);
  box-shadow: 0 0 0 3px rgba(36, 87, 214, 0.12);
}

input[type="text"].is-invalid {
  border-color: var(--danger);
}

input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

button {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  padding: 0.78rem 1rem;
  border-radius: 8px;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease, transform 0.08s ease, box-shadow 0.15s ease;
}

button:hover:not(:disabled) {
  border-color: rgba(36, 87, 214, 0.35);
  background: #f6f8fc;
}

button:active:not(:disabled) {
  transform: translateY(1px);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.page {
  width: min(1240px, calc(100vw - 32px));
  margin: 0 auto;
  padding: 20px 0 28px;
}

.masthead {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 16px;
  padding-bottom: 14px;
  margin-bottom: 16px;
  border-bottom: 1px solid var(--line);
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--accent);
  font-size: 0.76rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0;
}

.masthead h1,
.queue-panel h2,
.pages-panel h2,
.control-panel h2 {
  margin: 0;
  line-height: 1.1;
}

.masthead h1 {
  font-size: 1.9rem;
}

.masthead-meta {
  font-size: 0.95rem;
  color: var(--muted);
  white-space: nowrap;
}

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1.7fr) minmax(300px, 0.9fr);
  gap: 16px;
  align-items: start;
}

.queue-panel,
.preview-panel,
.pages-panel,
.image-panel,
.control-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.queue-panel,
.preview-panel,
.pages-panel,
.image-panel {
  padding: 16px;
  grid-column: 1;
}

.preview-panel {
  grid-row: 1;
}

.queue-panel {
  grid-row: 3;
}

.pages-panel {
  grid-row: 2;
}

.image-panel {
  grid-row: 4;
}

.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  margin-bottom: 12px;
}

.panel-head h2,
.control-panel h2 {
  font-size: 1.15rem;
}

.panel-head p,
#page-selection-note,
#queue-note {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 0.92rem;
}

.empty-state {
  padding: 18px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  background: var(--surface-alt);
  color: var(--muted);
}

.file-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 10px;
}

.file-item {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-alt);
  padding: 12px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  align-items: start;
}

.file-item.is-active {
  border-color: rgba(36, 87, 214, 0.48);
  background: #ffffff;
  box-shadow: inset 0 0 0 1px rgba(36, 87, 214, 0.18);
}

.file-main {
  min-width: 0;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 12px;
  align-items: start;
}

.file-index {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 2rem;
  height: 2rem;
  border-radius: 999px;
  background: #e7eefc;
  color: var(--accent);
  font-size: 0.88rem;
  font-weight: 700;
}

.file-title {
  margin: 0;
  font-weight: 600;
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.file-meta {
  margin-top: 4px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  color: var(--muted);
  font-size: 0.88rem;
}

.file-options {
  margin-top: 12px;
  display: grid;
  grid-template-columns: minmax(180px, 1fr) auto;
  gap: 8px 12px;
  align-items: end;
}

.section-name-field {
  grid-column: 1 / -1;
}

.section-name-field > span {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0;
}

.range-field,
.rotate-group,
.field-group {
  display: grid;
  gap: 6px;
}

.slider-group {
  display: grid;
  gap: 8px;
}

.option-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-alt);
}

.option-grid .field-group:last-child {
  grid-column: 1 / -1;
}

.option-grid.is-disabled {
  opacity: 0.62;
}

.tool-section {
  display: grid;
  gap: 10px;
  padding: 12px 0;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}

.slider-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.range-field span,
.rotate-group > span,
.field-group label,
.slider-head label {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0;
}

.slider-head span {
  color: var(--text);
  font-size: 0.84rem;
  font-weight: 600;
}

.file-options .field-help {
  grid-column: 1 / -1;
  color: var(--muted);
  font-size: 0.82rem;
}

.image-picker {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 10px;
  align-items: center;
}

.image-picker button {
  min-height: 2.45rem;
  padding: 0 0.85rem;
}

.image-picker span {
  min-width: 0;
  color: var(--muted);
  font-size: 0.86rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.field-error {
  color: var(--danger) !important;
  font-weight: 600;
}

.mini-check {
  grid-column: 1 / -1;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 2rem;
  color: var(--text);
  font-size: 0.9rem;
}

.mini-check input {
  width: 1rem;
  height: 1rem;
  accent-color: var(--accent);
}

.segmented {
  display: inline-grid;
  grid-template-columns: repeat(4, 2.8rem);
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: #ffffff;
}

.segmented button {
  min-height: 2.7rem;
  border: 0;
  border-radius: 0;
  padding: 0;
  background: transparent;
  font-size: 0.9rem;
}

.segmented button + button {
  border-left: 1px solid var(--line);
}

.segmented button.is-selected {
  background: #e7eefc;
  color: var(--accent);
  font-weight: 700;
}

.file-error {
  color: var(--danger);
  font-weight: 600;
}

.file-actions {
  display: flex;
  gap: 6px;
}

.small-btn {
  min-height: 2.45rem;
  padding: 0 0.72rem;
  font-size: 0.86rem;
  font-weight: 600;
}

.icon-btn {
  width: 2.45rem;
  height: 2.45rem;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.95rem;
  line-height: 1;
}

.control-panel {
  padding: 16px;
  display: grid;
  gap: 12px;
}

.dropzone {
  width: 100%;
  display: grid;
  gap: 8px;
  padding: 18px;
  min-height: 168px;
  text-align: center;
  place-items: center;
  border-style: dashed;
  background: #f8fbff;
}

.dropzone:hover:not(:disabled) {
  background: #f3f7ff;
}

.dropzone.is-dragging {
  border-color: var(--accent);
  background: #edf4ff;
}

.image-dropzone {
  width: 100%;
  display: grid;
  gap: 8px;
  padding: 18px;
  min-height: 132px;
  text-align: center;
  place-items: center;
  border-style: dashed;
  background: #f8fbff;
}

.image-dropzone:hover:not(:disabled) {
  background: #f3f7ff;
}

.image-dropzone.is-dragging {
  border-color: var(--success);
  background: #ecfdf8;
}

.dropzone-kicker {
  color: var(--accent);
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0;
}

.dropzone-title {
  font-size: 1rem;
  font-weight: 600;
  line-height: 1.35;
}

.dropzone-hint {
  color: var(--muted);
  font-size: 0.9rem;
}

.button-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.check-row {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 2.7rem;
  color: var(--text);
}

.check-row input {
  width: 1rem;
  height: 1rem;
  accent-color: var(--accent);
}

.primary {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

.primary:hover:not(:disabled) {
  border-color: var(--accent-hover);
  background: var(--accent-hover);
}

.status-block,
.output-block {
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

.status-label {
  margin-bottom: 4px;
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0;
}

#status,
#output {
  color: var(--text);
  line-height: 1.45;
  word-break: break-word;
}

.download-link {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
  color: var(--accent);
  font-weight: 600;
  text-decoration: none;
}

.download-link:hover {
  text-decoration: underline;
}

.page-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
}

.page-toolbar button {
  min-height: 2.35rem;
  padding: 0 0.82rem;
  font-size: 0.86rem;
  font-weight: 600;
}

.page-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(92px, 1fr));
  gap: 10px;
}

.page-grid-empty {
  grid-column: 1 / -1;
  padding: 18px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  background: var(--surface-alt);
  color: var(--muted);
  text-align: center;
}

.page-tile {
  display: grid;
  gap: 8px;
  min-height: 132px;
  padding: 8px;
  align-content: start;
  border-color: var(--line);
  background: var(--surface-alt);
}

.page-tile.is-selected {
  border-color: rgba(36, 87, 214, 0.7);
  background: #eef4ff;
  box-shadow: inset 0 0 0 1px rgba(36, 87, 214, 0.22);
}

.page-tile.has-blank-after {
  border-bottom-width: 3px;
  border-bottom-color: var(--success);
}

.page-thumbnail {
  display: grid;
  place-items: center;
  min-height: 86px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #ffffff;
  color: var(--muted);
  font-weight: 700;
}

.page-tile-label {
  min-height: 1.2rem;
  color: var(--text);
  font-size: 0.78rem;
  font-weight: 700;
  line-height: 1.2;
  text-align: center;
}

.image-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 12px 0;
}

.image-actions button {
  min-height: 2.45rem;
  padding: 0 0.85rem;
  font-weight: 600;
}

.image-status {
  min-height: 1.45rem;
  margin-bottom: 12px;
  color: var(--muted);
  line-height: 1.45;
}

.image-list {
  display: grid;
  gap: 8px;
  margin-bottom: 12px;
}

.image-list:empty {
  display: none;
}

.image-list-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
  padding: 9px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-alt);
}

.image-list-item.is-active {
  border-color: rgba(36, 87, 214, 0.48);
  background: #ffffff;
  box-shadow: inset 0 0 0 1px rgba(36, 87, 214, 0.18);
}

.image-list-main {
  min-width: 0;
  border: 0;
  padding: 0;
  background: transparent;
  text-align: left;
}

.image-list-main:hover:not(:disabled) {
  background: transparent;
}

.image-list-name,
.image-list-meta {
  display: block;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.image-list-name {
  font-weight: 700;
}

.image-list-meta {
  margin-top: 3px;
  color: var(--muted);
  font-size: 0.82rem;
}

.image-preview-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.image-preview {
  min-width: 0;
  margin: 0;
}

.image-preview figcaption {
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0;
}

.image-canvas-shell {
  min-height: 280px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background:
    linear-gradient(45deg, #f3f6fa 25%, transparent 25%),
    linear-gradient(-45deg, #f3f6fa 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #f3f6fa 75%),
    linear-gradient(-45deg, transparent 75%, #f3f6fa 75%);
  background-color: #ffffff;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0;
  background-size: 20px 20px;
  overflow: hidden;
}

.image-canvas-shell canvas {
  display: block;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 520px;
  touch-action: none;
}

.image-placeholder {
  padding: 20px;
  color: var(--muted);
  text-align: center;
}

.preview-shell {
  min-height: 620px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-alt);
  overflow: hidden;
}

.preview-placeholder {
  min-height: 620px;
  display: grid;
  place-items: center;
  padding: 20px;
  color: var(--muted);
  text-align: center;
}

#preview-frame {
  width: 100%;
  height: 620px;
  border: 0;
  display: block;
  background: #ffffff;
}

.control-panel {
  grid-column: 2;
  grid-row: 1 / span 4;
  position: sticky;
  top: 16px;
}

@media (max-width: 960px) {
  .page {
    width: min(100vw - 24px, 1240px);
    padding-top: 14px;
  }

  .layout {
    grid-template-columns: 1fr;
  }

  .queue-panel,
  .preview-panel,
  .pages-panel,
  .image-panel,
  .control-panel {
    grid-column: auto;
    grid-row: auto;
  }

  .preview-shell,
  .preview-placeholder,
  #preview-frame {
    min-height: 520px;
    height: 520px;
  }

  .control-panel {
    position: static;
  }

  .preview-panel {
    order: 1;
  }

  .pages-panel {
    order: 2;
  }

  .queue-panel {
    order: 3;
  }

  .image-panel {
    order: 4;
  }

  .control-panel {
    order: 5;
  }
}

@media (max-width: 700px) {
  .masthead {
    align-items: start;
    flex-direction: column;
  }

  .masthead-meta {
    white-space: normal;
  }

  .panel-head {
    flex-direction: column;
    align-items: start;
  }

  .file-item {
    grid-template-columns: 1fr;
  }

  .file-main {
    grid-template-columns: 1fr;
  }

  .file-options {
    grid-template-columns: 1fr;
  }

  .segmented {
    width: 100%;
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .file-actions {
    justify-content: flex-end;
  }

  .button-row {
    grid-template-columns: 1fr;
  }

  .option-grid {
    grid-template-columns: 1fr;
  }

  .image-picker {
    grid-template-columns: 1fr;
  }

  .image-preview-grid {
    grid-template-columns: 1fr;
  }

  .image-actions {
    display: grid;
    grid-template-columns: 1fr;
  }
}
