:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #eff6ff;
  --success: #059669;
  --success-soft: #ecfdf5;
  --warn: #b45309;
  --warn-soft: #fffbeb;
  --radius: 12px;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.05);
  --shadow-paper: 0 16px 40px -12px rgba(15, 23, 42, 0.22), 0 2px 8px rgba(15, 23, 42, 0.08);
}

* {
  box-sizing: border-box;
}

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

.hidden {
  display: none !important;
}

/* Top bar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(255, 255, 255, 0.94);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  padding: 12px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: var(--primary);
  color: #fff;
  display: grid;
  place-items: center;
  flex-shrink: 0;
}

.brand h1 {
  margin: 0;
  font-size: 17px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.subtitle {
  margin: 1px 0 0;
  font-size: 12.5px;
  color: var(--muted);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.privacy-pill {
  font-size: 12px;
  color: var(--success);
  background: var(--success-soft);
  padding: 6px 10px;
  border-radius: 999px;
  font-weight: 500;
  margin-right: 4px;
}

/* Buttons */
.btn {
  border: 1px solid transparent;
  border-radius: 9px;
  padding: 8px 14px;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

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

.btn-primary:not(:disabled):hover {
  background: var(--primary-hover);
}

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border-color: var(--border-strong);
}

.btn-secondary:not(:disabled):hover {
  background: #f8fafc;
}

.link-btn {
  background: none;
  border: none;
  padding: 2px 6px;
  color: var(--primary);
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  border-radius: 6px;
}

.link-btn:hover {
  background: var(--primary-soft);
}

/* Main Workspace */
.workspace {
  display: grid;
  grid-template-columns: 390px 1fr;
  gap: 18px;
  max-width: 1460px;
  margin: 0 auto;
  padding: 18px 20px 28px;
  align-items: start;
}

@media (max-width: 980px) {
  .workspace {
    grid-template-columns: 1fr;
  }
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow-sm);
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.panel-header h2 {
  margin: 0;
  font-size: 14.5px;
  font-weight: 700;
}

.panel-header-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

/* Upload Slots */
.upload-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.upload-slot {
  border: 1.5px dashed var(--border-strong);
  border-radius: 10px;
  background: #f8fafc;
  min-height: 124px;
  overflow: hidden;
  transition: border-color 0.15s, background 0.15s;
}

.upload-slot.dragover {
  border-color: var(--primary);
  background: var(--primary-soft);
}

.upload-slot.has-file {
  border-style: solid;
  border-color: var(--border);
  background: #fff;
}

.slot-empty {
  height: 124px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 10px;
  text-align: center;
  cursor: pointer;
}

.slot-empty:hover {
  background: #f1f5f9;
}

.slot-plus {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--primary-soft);
  color: var(--primary);
  font-size: 20px;
  font-weight: 600;
  line-height: 30px;
  margin-bottom: 6px;
}

.slot-title {
  font-size: 13px;
  font-weight: 600;
}

.slot-hint {
  font-size: 11px;
  color: var(--muted);
  margin-top: 2px;
}

.slot-filled {
  display: flex;
  flex-direction: column;
}

.slot-thumb-wrap {
  position: relative;
  background: #f1f5f9;
  aspect-ratio: 1.5858;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 6px;
}

.slot-thumb-wrap img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  border-radius: 4px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.15);
}

.detect-badge {
  position: absolute;
  top: 6px;
  left: 6px;
  font-size: 10px;
  font-weight: 600;
  padding: 2px 6px;
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.72);
  color: #fff;
}

.detect-badge.warn {
  background: rgba(180, 83, 9, 0.88);
}

.slot-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 8px;
  border-top: 1px solid var(--border);
  gap: 4px;
}

.slot-label {
  font-size: 11.5px;
  font-weight: 600;
  color: var(--muted);
}

.slot-btns {
  display: flex;
  gap: 3px;
}

.mini-btn {
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  font-size: 11px;
  font-weight: 600;
  padding: 3px 6px;
  border-radius: 6px;
  cursor: pointer;
}

.mini-btn:hover {
  background: var(--primary-soft);
  border-color: #bfdbfe;
  color: var(--primary);
}

/* Segmented Controls */
.segmented {
  display: flex;
  background: #f1f5f9;
  padding: 3px;
  border-radius: 9px;
  gap: 3px;
}

.seg-btn {
  flex: 1;
  border: none;
  background: transparent;
  padding: 6px 8px;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  border-radius: 7px;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}

.seg-btn.active {
  background: #fff;
  color: var(--text);
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.08);
}

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

.field-label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 6px;
}

.mt-12 {
  margin-top: 12px;
}

/* Sliders */
.sliders-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 12px;
}

.slider-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  color: var(--muted);
}

.slider-field.full {
  grid-column: 1 / -1;
}

.slider-field span {
  display: flex;
  justify-content: space-between;
}

.slider-field strong {
  color: var(--text);
}

.slider-field input[type="range"] {
  width: 100%;
  accent-color: var(--primary);
  margin: 0;
}

/* Layout Presets */
.layout-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.layout-card {
  display: flex;
  align-items: center;
  gap: 9px;
  text-align: left;
  padding: 8px 9px;
  border: 1.5px solid var(--border);
  border-radius: 9px;
  background: #fff;
  cursor: pointer;
  transition: all 0.15s;
}

.layout-card:hover {
  border-color: var(--border-strong);
}

.layout-card.active {
  border-color: var(--primary);
  background: var(--primary-soft);
}

.layout-icon {
  width: 28px;
  height: 36px;
  border: 1.5px solid var(--border-strong);
  border-radius: 4px;
  background: #fff;
  padding: 3px;
  display: flex;
  flex-shrink: 0;
  gap: 2px;
}

.layout-card.active .layout-icon {
  border-color: var(--primary);
}

.layout-icon span {
  display: block;
  background: #94a3b8;
  border-radius: 1.5px;
}

.layout-card.active .layout-icon span {
  background: var(--primary);
}

.icon-vcenter {
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.icon-vcenter span {
  width: 16px;
  height: 9px;
}

.icon-vtop {
  flex-direction: column;
  justify-content: flex-start;
  align-items: center;
}
.icon-vtop span {
  width: 15px;
  height: 7px;
}

.icon-side {
  flex-direction: row;
  justify-content: center;
  align-items: center;
}
.icon-side span {
  width: 9px;
  height: 6px;
}

.icon-twosets {
  display: grid;
  grid-template-columns: 1fr 1fr;
  align-content: space-around;
  justify-items: center;
}
.icon-twosets span {
  width: 8px;
  height: 5px;
}

.layout-name {
  font-size: 12px;
  font-weight: 700;
  color: var(--text);
  line-height: 1.2;
}

.layout-desc {
  font-size: 10.5px;
  color: var(--muted);
  margin-top: 2px;
}

/* Toggles */
.toggles {
  display: flex;
  flex-direction: column;
  gap: 7px;
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12.5px;
  color: var(--text);
  cursor: pointer;
}

.checkbox-row input {
  accent-color: var(--primary);
  width: 15px;
  height: 15px;
  margin: 0;
}

/* Preview Stage */
.preview-stage {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: calc(100vh - 106px);
  background: #e2e8f0;
  background-image: radial-gradient(#cbd5e1 1px, transparent 1px);
  background-size: 18px 18px;
  border-radius: 14px;
  border: 1px solid var(--border-strong);
  padding: 14px 20px 26px;
}

.preview-meta {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  background: rgba(255, 255, 255, 0.92);
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text);
  box-shadow: var(--shadow-sm);
  margin-bottom: 16px;
}

.preview-meta .dot {
  color: var(--muted);
}

.warn-tag {
  background: var(--warn-soft);
  color: var(--warn);
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11.5px;
}

.paper-viewport {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

.paper-sheet {
  background: #fff;
  box-shadow: var(--shadow-paper);
  border-radius: 2px;
  overflow: hidden;
  max-width: 100%;
}

.paper-sheet canvas {
  display: block;
  width: 100%;
  height: auto;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 18px;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.65);
  backdrop-filter: blur(3px);
}

.modal-dialog {
  position: relative;
  background: #fff;
  border-radius: 14px;
  width: min(860px, 96vw);
  max-height: 92vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.35);
}

.modal-header {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-header h3 {
  margin: 0;
  font-size: 16px;
}

.modal-header p {
  margin: 2px 0 0;
  font-size: 12.5px;
  color: var(--muted);
}

.icon-close {
  border: none;
  background: #f1f5f9;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  font-size: 15px;
  cursor: pointer;
}

.modal-body {
  padding: 18px;
  background: #0f172a;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: auto;
  flex: 1;
}

.corner-stage {
  position: relative;
  display: inline-block;
  user-select: none;
  touch-action: none;
}

.corner-stage canvas {
  display: block;
  max-width: 100%;
  max-height: 62vh;
  border-radius: 4px;
}

.corner-svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.corner-svg polygon {
  fill: rgba(37, 99, 235, 0.16);
  stroke: #38bdf8;
  stroke-width: 0.6;
  vector-effect: non-scaling-stroke;
}

.corner-handle {
  position: absolute;
  width: 28px;
  height: 28px;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: #2563eb;
  border: 2.5px solid #ffffff;
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  display: grid;
  place-items: center;
  cursor: grab;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.45);
}

.corner-handle:active {
  cursor: grabbing;
  transform: translate(-50%, -50%) scale(1.15);
  background: #0284c7;
}

.modal-footer {
  padding: 12px 18px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}

.modal-footer-left,
.modal-footer-right {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

/* Print CSS */
.print-only {
  display: none;
}

@media print {
  .no-print {
    display: none !important;
  }
  body {
    background: #fff;
    margin: 0;
  }
  .print-only {
    display: block !important;
  }
  #printImage {
    display: block;
    width: 100vw;
    height: auto;
  }
}
