:root {
  color-scheme: light dark;
  --bg: #f5f7fb;
  --surface: #ffffff;
  --surface-2: #eef2f7;
  --text: #18202b;
  --muted: #667085;
  --border: #d9e0ea;
  --accent: #0f766e;
  --accent-strong: #0b5f59;
  --danger: #b42318;
  --ok: #067647;
  --shadow: 0 18px 60px rgba(15, 23, 42, 0.08);
}

* {
  box-sizing: border-box;
}

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

.app-shell {
  display: grid;
  grid-template-columns: minmax(340px, 460px) minmax(0, 1fr);
  gap: 18px;
  width: min(1500px, calc(100vw - 32px));
  margin: 16px auto;
}

.control-pane,
.result-pane {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.control-pane {
  padding: 20px;
}

.result-pane {
  display: flex;
  flex-direction: column;
  min-height: calc(100vh - 32px);
  padding: 16px;
}

.title-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}

h1 {
  margin: 0 0 6px;
  font-size: 22px;
  line-height: 1.2;
}

p {
  margin: 0;
  color: var(--muted);
  line-height: 1.5;
}

.status-pill {
  flex: 0 0 auto;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 10px;
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 13px;
}

.field {
  display: grid;
  gap: 6px;
}

.wide {
  grid-column: 1 / -1;
}

.field span {
  color: var(--muted);
  font-size: 13px;
}

.mode-switch {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.mode-switch label {
  position: relative;
}

.mode-switch input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.mode-switch span {
  display: grid;
  min-height: 40px;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}

.mode-switch input:checked + span {
  border-color: var(--accent);
  background: rgba(15, 118, 110, 0.10);
  color: var(--accent);
}

.upload-preview {
  display: grid;
  gap: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  background: var(--surface-2);
}

.upload-preview-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(76px, 1fr));
  gap: 8px;
}

.upload-preview-item {
  display: grid;
  gap: 6px;
  min-width: 0;
}

.upload-preview-item img {
  width: 100%;
  aspect-ratio: 1;
  border-radius: 6px;
  object-fit: cover;
  background: var(--surface);
}

.upload-preview-item span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--muted);
  font-size: 12px;
}

.upload-preview-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.upload-preview strong {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.upload-preview button {
  background: transparent;
  border-color: var(--border);
  color: var(--text);
}

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

input,
select,
textarea {
  width: 100%;
  min-height: 40px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  padding: 9px 10px;
  outline: none;
}

textarea {
  resize: vertical;
  min-height: 132px;
  line-height: 1.5;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.14);
}

.toggles {
  display: grid;
  gap: 8px;
}

.toggle {
  display: flex;
  align-items: center;
  gap: 9px;
  color: var(--muted);
  font-size: 14px;
}

.toggle input {
  width: 18px;
  min-height: 18px;
}

.actions {
  display: flex;
  gap: 10px;
}

button,
.download-row a {
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 10px 14px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  text-decoration: none;
  text-align: center;
}

button:hover,
.download-row a:hover {
  background: var(--accent-strong);
}

button:disabled,
.download-row a[aria-disabled="true"] {
  cursor: not-allowed;
  opacity: 0.52;
}

#cancelButton {
  background: transparent;
  border-color: var(--border);
  color: var(--text);
}

#cancelButton:hover {
  background: var(--surface-2);
}

.metrics-grid {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 14px;
}

.metric {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  background: var(--surface-2);
}

.metric span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 6px;
}

.metric strong {
  font-size: 19px;
  line-height: 1.1;
}

.preview-wrap {
  position: relative;
  min-height: min(62vh, 720px);
  border: 1px solid var(--border);
  border-radius: 8px;
  background:
    linear-gradient(45deg, rgba(102, 112, 133, 0.10) 25%, transparent 25%),
    linear-gradient(-45deg, rgba(102, 112, 133, 0.10) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, rgba(102, 112, 133, 0.10) 75%),
    linear-gradient(-45deg, transparent 75%, rgba(102, 112, 133, 0.10) 75%);
  background-position: 0 0, 0 10px, 10px -10px, -10px 0;
  background-size: 20px 20px;
  overflow: hidden;
}

.preview-empty {
  display: grid;
  place-items: center;
  min-height: min(62vh, 720px);
  color: var(--muted);
}

.preview-grid {
  display: none;
  width: 100%;
  min-height: min(62vh, 720px);
  gap: 10px;
  padding: 10px;
}

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

.preview-grid.count-2,
.preview-grid.count-3 {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.preview-tile {
  position: relative;
  display: grid;
  place-items: center;
  min-height: 220px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.55);
  overflow: hidden;
}

.preview-tile img {
  width: 100%;
  height: 100%;
  max-height: min(62vh, 720px);
  object-fit: contain;
}


.preview-download {
  position: absolute;
  right: 10px;
  bottom: 10px;
  border-radius: 6px;
  padding: 7px 10px;
  background: rgba(15, 118, 110, 0.92);
  color: #fff;
  font-size: 12px;
  text-decoration: none;
}

.preview-download[aria-disabled="true"] {
  display: none;
}

.preview-tile-label {
  position: absolute;
  top: 8px;
  left: 8px;
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.72);
  color: #fff;
  padding: 4px 8px;
  font-size: 12px;
}

.download-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 12px 0;
}

.download-row strong {
  display: block;
}

.download-row span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin-top: 3px;
}

.diagnostics {
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-bottom: 12px;
  overflow: hidden;
}

.diagnostics-head,
.diagnostic-row {
  display: grid;
  grid-template-columns: 36px 60px 104px repeat(4, 78px);
  gap: 8px;
  align-items: center;
  padding: 8px 10px;
  font-size: 12px;
}

.diagnostics-head {
  background: var(--surface-2);
  color: var(--muted);
  font-weight: 600;
}

.diagnostic-row {
  border-top: 1px solid var(--border);
}

.diagnostic-row strong {
  font-size: 13px;
}

.diagnostic-row .muted {
  color: var(--muted);
}

.diagnostic-row .warn {
  color: #b54708;
}

.diagnostic-row .bad {
  color: var(--danger);
}

.diagnostic-row .ok {
  color: var(--ok);
}

.tabs {
  display: flex;
  gap: 8px;
  border-bottom: 1px solid var(--border);
}

.tab {
  border: 0;
  border-radius: 6px 6px 0 0;
  background: transparent;
  color: var(--muted);
  padding: 9px 12px;
}

.tab:hover,
.tab.active {
  background: var(--surface-2);
  color: var(--text);
}

.panel {
  display: none;
  flex: 1;
  min-height: 180px;
  max-height: 30vh;
  margin: 0;
  padding: 12px;
  overflow: auto;
  border: 1px solid var(--border);
  border-top: 0;
  border-radius: 0 0 8px 8px;
  background: #0f172a;
  color: #dbeafe;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  line-height: 1.55;
  white-space: pre-wrap;
}

.panel.active {
  display: block;
}

.history-panel {
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
}

.history-row,
.pool-row {
  display: grid;
  grid-template-columns: 72px repeat(4, minmax(82px, 1fr)) minmax(120px, 1.2fr);
  gap: 8px;
  align-items: center;
  padding: 9px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}

.history-row span {
  color: var(--muted);
}

.pool-panel {
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
}

.pool-row {
  grid-template-columns: 42px 58px minmax(120px, 1fr) repeat(4, minmax(76px, 1fr));
}

.pool-row span {
  color: var(--muted);
}

.ok {
  color: var(--ok);
}

.bad {
  color: var(--danger);
}

.hidden {
  display: none !important;
}

@media (max-width: 980px) {
  .app-shell {
    grid-template-columns: 1fr;
    width: min(100vw - 20px, 760px);
    margin: 10px auto;
  }

  .result-pane {
    min-height: auto;
  }

  .metrics-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .preview-grid.count-2,
  .preview-grid.count-3 {
    grid-template-columns: 1fr;
  }

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

  .mode-switch {
    grid-template-columns: 1fr;
  }

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

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

  .diagnostics-head,
  .diagnostic-row {
    grid-template-columns: 36px repeat(4, minmax(58px, 1fr));
  }

  .diagnostics-head span:nth-child(n + 6):not(:last-child),
  .diagnostic-row span:nth-child(n + 6):not(:last-child) {
    display: none;
  }
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f172a;
    --surface: #111827;
    --surface-2: #172033;
    --text: #eef2ff;
    --muted: #9aa4b2;
    --border: #27354a;
    --shadow: 0 18px 60px rgba(0, 0, 0, 0.22);
  }
}
