:root {
  color-scheme: light;
  --ink: #17201a;
  --muted: #647067;
  --paper: #fbfaf6;
  --surface: #ffffff;
  --line: #d9ded7;
  --accent: #1b8a5a;
  --accent-strong: #0f6d44;
  --warn: #b7372f;
  --shadow: 0 18px 50px rgba(24, 33, 27, 0.1);
}

* {
  box-sizing: border-box;
}

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

button,
input,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

#app {
  width: min(1180px, 100%);
  margin: 0 auto;
  padding: 18px;
}

.app-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 0 14px;
}

.app-header h1 {
  margin: 0;
  font-size: clamp(1.8rem, 5vw, 3.4rem);
  letter-spacing: 0;
}

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

.count-pill {
  flex: 0 0 auto;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 8px 14px;
  background: var(--surface);
  color: var(--muted);
}

.tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #eef2ed;
  position: sticky;
  top: 0;
  z-index: 4;
}

.tab-button,
.primary-button,
.ghost-button,
.subtle-button,
.danger-button {
  min-height: 42px;
  border-radius: 7px;
  border: 1px solid transparent;
  padding: 9px 13px;
}

.tab-button {
  background: transparent;
  color: var(--muted);
}

.tab-button.active {
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 1px 8px rgba(20, 26, 22, 0.08);
}

.view {
  display: none;
  padding-top: 18px;
}

.view.active {
  display: block;
}

.toolbar,
.section-heading,
.panel-card-footer,
.toggle-row,
.button-stack {
  display: flex;
  align-items: center;
  gap: 10px;
}

.toolbar,
.section-heading {
  justify-content: space-between;
  margin-bottom: 14px;
}

.primary-file {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 10px 14px;
  border-radius: 7px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
}

.primary-file input {
  display: none;
}

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

.primary-button:hover,
.primary-file:hover {
  background: var(--accent-strong);
}

.primary-button:disabled {
  background: #b8c4bc;
  border-color: #b8c4bc;
  cursor: not-allowed;
}

.ghost-button {
  background: var(--surface);
  border-color: var(--line);
  color: var(--ink);
}

.subtle-button {
  min-height: 34px;
  background: transparent;
  border-color: transparent;
  color: var(--muted);
  padding: 5px 8px;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.subtle-button:hover {
  color: var(--ink);
}

.subtle-button:disabled {
  color: #a8b0aa;
  cursor: not-allowed;
  text-decoration: none;
}

.danger-button {
  min-height: 34px;
  padding: 6px 10px;
  background: #fff5f3;
  border-color: #efc6c0;
  color: var(--warn);
}

.capture-workbench,
.generate-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  gap: 18px;
  align-items: start;
}

.preview-pane,
.strip-shell {
  position: relative;
  min-height: 360px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.preview-viewport {
  position: relative;
  min-height: 360px;
  overflow: hidden;
  touch-action: none;
}

.preview-pane > .empty-message {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  z-index: 1;
}

.preview-stage {
  position: absolute;
  left: 0;
  top: 0;
  transform-origin: 0 0;
}

.source-canvas {
  display: none;
  width: auto;
  max-width: none;
  height: auto;
}

.source-canvas.has-image {
  display: block;
}

.grid-overlay {
  position: absolute;
  left: 0;
  top: 0;
  pointer-events: none;
  display: none;
  background-image:
    linear-gradient(to right, rgba(27, 138, 90, 0.8) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(27, 138, 90, 0.8) 1px, transparent 1px);
}

.grid-overlay.visible {
  display: block;
}

.grid-overlay.detected {
  background-image: none;
}

.grid-overlay.manual {
  background-image: none;
  pointer-events: auto;
  touch-action: none;
}

.pin-layer {
  position: absolute;
  left: 0;
  top: 0;
  pointer-events: none;
}

.pin-polygon {
  fill: rgba(27, 138, 90, 0.08);
  stroke: var(--accent);
  stroke-width: 2;
  vector-effect: non-scaling-stroke;
}

.pin-label {
  position: absolute;
  transform: translate(-50%, -50%);
  border-radius: 999px;
  padding: 2px 7px;
  background: var(--accent);
  color: #fff;
  font-size: 0.78rem;
  pointer-events: none;
}

.corner-handle {
  position: absolute;
  width: 22px;
  height: 22px;
  transform: translate(-50%, -50%);
  border: 3px solid #fff;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.24);
  cursor: grab;
  padding: 0;
  touch-action: none;
}

.corner-handle:active {
  cursor: grabbing;
  background: var(--accent-strong);
}

.detected-box {
  position: absolute;
  border: 2px solid var(--accent);
  background: rgba(27, 138, 90, 0.1);
}

.detected-box::after {
  content: attr(data-index);
  position: absolute;
  left: 4px;
  top: 4px;
  min-width: 24px;
  border-radius: 999px;
  padding: 2px 6px;
  background: var(--accent);
  color: #fff;
  font-size: 0.78rem;
}

.controls-panel,
.generate-controls,
.tag-form,
.tag-item {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  padding: 14px;
}

.controls-panel {
  display: grid;
  gap: 14px;
}

.detect-status {
  margin: -4px 0 0;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.5;
}

.control-row {
  display: grid;
  gap: 7px;
}

.number-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

input,
select {
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  padding: 8px 10px;
  color: var(--ink);
}

input[type="range"] {
  padding: 0;
}

input[type="color"] {
  width: 48px;
  padding: 3px;
}

.empty-message {
  color: var(--muted);
  text-align: center;
  padding: 44px 20px;
  margin: 0;
}

.section-heading {
  margin-top: 22px;
}

.section-heading h2,
.slot-requirements h2 {
  margin: 0;
  font-size: 1rem;
}

.panel-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 12px;
}

.panel-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  overflow: hidden;
}

.panel-card img {
  display: block;
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: contain;
  background: #f2f4f1;
  border-bottom: 1px solid var(--line);
}

.panel-card-footer {
  justify-content: space-between;
  padding: 9px 10px;
}

.tag-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 0 10px 10px;
}

.chip {
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 5px 8px;
  background: #fff;
  color: var(--muted);
  font-size: 0.82rem;
}

.chip.active {
  color: #fff;
  border-color: transparent;
}

.strip {
  display: grid;
  gap: 10px;
  width: min(100%, 520px);
  margin: 0 auto;
  padding: 18px;
}

.strip-panel {
  background: #fff;
  margin: 0;
}

.strip-panel img {
  display: block;
  width: 100%;
  max-height: 260px;
  object-fit: contain;
}

.fullscreen-strip {
  position: fixed !important;
  inset: 0 !important;
  z-index: 2147483647 !important;
  display: grid;
  place-items: center;
  background: #fff;
  padding: calc(20px + env(safe-area-inset-top)) 12px calc(88px + env(safe-area-inset-bottom));
  box-sizing: border-box;
}

.fullscreen-strip[hidden] {
  display: none;
}

.fullscreen-strip-content {
  width: min(calc(100vw - 24px), 40dvh);
  height: 68dvh;
  max-height: calc(100svh - 132px);
  display: grid;
  grid-template-rows: repeat(4, minmax(0, 1fr));
  gap: 3px;
  align-content: center;
}

.fullscreen-strip-panel {
  min-height: 0;
  margin: 0;
  display: grid;
  place-items: center;
  background: #fff;
}

.fullscreen-strip-panel img {
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

body.fullscreen-active {
  background: #fff;
  overflow: hidden;
}

body.fullscreen-active #app {
  visibility: hidden;
}

.generate-controls {
  display: grid;
  gap: 16px;
}

.seed-box,
.slot-control {
  display: grid;
  gap: 7px;
}

.button-stack {
  flex-direction: column;
}

.button-stack button {
  width: 100%;
}

.slot-requirements {
  display: grid;
  gap: 10px;
}

#slotRequirementControls {
  display: grid;
  gap: 9px;
}

.tag-form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 10px;
  margin-bottom: 14px;
}

.tag-list {
  display: grid;
  gap: 12px;
}

.tag-item {
  display: grid;
  gap: 12px;
}

.tag-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.slot-checks {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.tag-settings {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

@media (max-width: 820px) {
  #app {
    padding: 12px;
  }

  .app-header {
    align-items: flex-start;
    flex-direction: column;
  }

  .capture-workbench,
  .generate-layout {
    grid-template-columns: 1fr;
  }

  .toolbar,
  .section-heading {
    align-items: stretch;
    flex-direction: column;
  }

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