:root {
  --page-bg: #d8dbdc;
  --text-main: #505355;
  --ink: #222;
  --line: #c9ced0;
  --surface: #f7f7f7;
  --card: #ffffff;
  --accent: #0d6a70;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Avenir Next", "Segoe UI", sans-serif;
  color: var(--ink);
  background: #eef1f2;
}

.app-shell {
  display: grid;
  grid-template-columns: 340px 1fr;
  min-height: 100vh;
}

.panel {
  border-right: 1px solid var(--line);
  padding: 20px;
  background: var(--surface);
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}

.panel h1 {
  margin: 0;
  font-size: 1.25rem;
}

.subtext {
  margin: 8px 0 16px;
  color: #5e6668;
  font-size: 0.9rem;
}

.label {
  display: block;
  margin: 12px 0 6px;
  font-weight: 600;
  font-size: 0.9rem;
}

input[type="text"],
select,
input[type="file"],
button {
  width: 100%;
  font: inherit;
}

input[type="text"],
select,
input[type="file"] {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
}

.button-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 12px;
}

button {
  border: none;
  border-radius: 8px;
  padding: 10px 12px;
  font-weight: 700;
  cursor: pointer;
  background: #1d7578;
  color: #fff;
}

button:hover {
  filter: brightness(0.96);
}

button.ghost {
  background: #d7dcde;
  color: #223;
}

button.download {
  background: var(--accent);
}

.status {
  font-size: 0.87rem;
  color: #566065;
}

.slot-list-wrap {
  margin-top: 14px;
}

.slot-list-wrap h2 {
  margin: 0 0 8px;
  font-size: 1rem;
}

.slot-list {
  display: grid;
  gap: 8px;
}

.slot-item {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px;
  display: grid;
  grid-template-columns: 52px 1fr auto;
  gap: 8px;
  align-items: center;
}

.slot-thumb {
  width: 52px;
  height: 52px;
  background: #eceff0;
  border-radius: 6px;
  border: 1px solid #d2d7d8;
  object-fit: cover;
}

.slot-item p {
  margin: 0;
  font-size: 0.78rem;
  line-height: 1.25;
}

.slot-item button {
  width: auto;
  font-size: 0.75rem;
  padding: 6px 8px;
  background: #58777a;
}

.canvas-wrap {
  padding: 20px;
  overflow: auto;
}

.pages {
  display: grid;
  gap: 28px;
}

.template-section {
  display: grid;
  gap: 12px;
}

.template-section h2 {
  margin: 0;
  font-size: 1rem;
  color: #404a4d;
}

.page {
  position: relative;
  width: min(100%, 1188px);
  aspect-ratio: 1188 / 917;
  background: var(--page-bg);
  border: 1px solid #bfc6c8;
  margin-inline: auto;
  container-type: inline-size;
}

.page::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 24px;
  background: #bab7b5;
}

.page h3 {
  margin: 0;
  position: absolute;
  top: 7%;
  left: 0;
  width: 100%;
  text-align: center;
  font-size: clamp(14px, 3.5cqi, 44px);
  color: var(--text-main);
  font-weight: 400;
}

.slot-box {
  position: absolute;
  background-color: #f4f5f5;
  background-image:
    linear-gradient(45deg, #dde0e1 25%, transparent 25%),
    linear-gradient(-45deg, #dde0e1 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #dde0e1 75%),
    linear-gradient(-45deg, transparent 75%, #dde0e1 75%);
  background-size: 16px 16px;
  background-position: 0 0, 0 8px, 8px -8px, -8px 0px;
  border: 1px dashed #aab2b4;
  overflow: hidden;
  cursor: pointer;
}

.slot-box.has-image {
  border: none;
}

.slot-box img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.slot-box span {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: #778083;
  font-size: clamp(9px, 1.5cqi, 18px);
  font-weight: 700;
}

.slot-label {
  position: absolute;
  text-align: center;
  color: var(--text-main);
  font-size: clamp(7px, 1.6cqi, 20px);
  line-height: 1.2;
  font-weight: 500;
  text-transform: uppercase;
  pointer-events: none;
  white-space: pre-line;
  letter-spacing: 0.03em;
}

.slot-label.compact {
  font-size: clamp(6px, 1.3cqi, 16px);
}

/* ── Crop icon overlay on filled slots ──────────────────────────────────────── */
.crop-icon {
  position: absolute;
  bottom: 6px;
  right: 6px;
  width: 28px;
  height: 28px;
  background: rgba(0, 0, 0, 0.45);
  border-radius: 5px;
  display: grid;
  place-items: center;
  color: #fff;
  opacity: 0;
  transition: opacity 0.15s;
  pointer-events: none;
  z-index: 2;
}

.slot-box:hover .crop-icon {
  opacity: 1 !important;
}

.crop-icon svg {
  width: 15px;
  height: 15px;
}

/* ── Crop modal ──────────────────────────────────────────────────────────────── */
#cropOverlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  z-index: 1000;
  align-items: center;
  justify-content: center;
}

.crop-modal {
  background: var(--surface);
  border-radius: 12px;
  padding: 20px;
  width: min(520px, 95vw);
  display: grid;
  gap: 12px;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.35);
}

.crop-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.crop-title {
  font-weight: 700;
  font-size: 1rem;
}

.crop-actions {
  display: flex;
  gap: 8px;
}

.crop-actions button {
  width: auto;
  padding: 7px 14px;
  font-size: 0.85rem;
}

.crop-hint {
  margin: 0;
  font-size: 0.78rem;
  color: #778083;
}

.crop-viewport {
  width: 100%;
  aspect-ratio: 4 / 3;
  background: #f4f5f5;
  background-image:
    linear-gradient(45deg, #dde0e1 25%, transparent 25%),
    linear-gradient(-45deg, #dde0e1 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #dde0e1 75%),
    linear-gradient(-45deg, transparent 75%, #dde0e1 75%);
  background-size: 16px 16px;
  background-position: 0 0, 0 8px, 8px -8px, -8px 0px;
  border-radius: 8px;
  overflow: hidden;
  display: grid;
  place-items: center;
  cursor: grab;
  position: relative;
}

.crop-viewport:active {
  cursor: grabbing;
}

#cropImg {
  max-width: none;
  max-height: none;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transform-origin: center;
  user-select: none;
  pointer-events: none;
}

.crop-zoom-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.crop-zoom-btn {
  width: 34px !important;
  height: 34px;
  padding: 0 !important;
  font-size: 1.2rem !important;
  font-weight: 400 !important;
  flex-shrink: 0;
}

#cropZoomRange {
  flex: 1;
  border: none;
  padding: 0;
  background: transparent;
  cursor: pointer;
}

#cropResetBtn {
  width: auto !important;
  padding: 7px 12px !important;
  font-size: 0.82rem !important;
  flex-shrink: 0;
}

/* ── Match preview modal ─────────────────────────────────────────────────────── */
#matchOverlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  z-index: 1000;
  align-items: center;
  justify-content: center;
}

.match-modal {
  background: var(--surface);
  border-radius: 12px;
  padding: 20px;
  width: min(680px, 96vw);
  max-height: 90vh;
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: 12px;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.35);
}

.match-header {
  display: grid;
  gap: 4px;
}

.match-title {
  font-weight: 700;
  font-size: 1rem;
}

.match-hint {
  margin: 0;
  font-size: 0.78rem;
  color: #778083;
}

.match-table-wrap {
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.match-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
}

.match-table thead th {
  background: #eef1f2;
  padding: 8px 10px;
  text-align: left;
  font-weight: 600;
  position: sticky;
  top: 0;
}

.match-table tbody tr {
  border-top: 1px solid var(--line);
}

.match-table tbody tr.unmatched {
  color: #a0a8ab;
}

.match-table td {
  padding: 7px 10px;
  vertical-align: middle;
}

.match-slot {
  font-weight: 500;
  max-width: 220px;
}

.match-file {
  font-family: monospace;
  font-size: 0.78rem;
  word-break: break-all;
}

.match-how {
  font-size: 0.74rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  white-space: nowrap;
}

.match-how--keyword { color: #0d6a70; }
.match-how--order   { color: #7a6a1e; }
.match-how--no-match { color: #b0b8bb; }

.match-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.match-actions button {
  width: auto;
  padding: 9px 20px;
}

@media (max-width: 1120px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .panel {
    position: static;
    height: auto;
    border-right: none;
    border-bottom: 1px solid var(--line);
  }
}
