:root {
  --bg-app: #f3f4f6;
  --bg-panel: #ffffff;
  --bg-stage: #e5e7eb;
  --text-main: #111827;
  --text-muted: #6b7280;
  --border-color: #e5e7eb;
  --accent: #1d4ed8;
  --accent-hover: #1e40af;
  --accent-soft: #eff6ff;
  --danger: #dc2626;
  --radius-sm: 6px;
  --radius-md: 10px;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --shadow-card: 0 12px 32px rgba(15, 23, 42, 0.14), 0 2px 6px rgba(15, 23, 42, 0.08);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  color: var(--text-main);
  background: var(--bg-app);
  -webkit-font-smoothing: antialiased;
}

.hidden {
  display: none !important;
}

/* App Layout */
.app-shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 24px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

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

.brand-icon {
  width: 38px;
  height: 38px;
  border-radius: 9px;
  background: linear-gradient(135deg, #1d4ed8, #2563eb);
  color: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  box-shadow: 0 2px 6px rgba(29, 78, 216, 0.28);
}

.brand h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
  line-height: 1.25;
}

.brand-sub {
  margin: 2px 0 0;
  font-size: 12px;
  color: var(--text-muted);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

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

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

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

.btn-secondary {
  background: #ffffff;
  color: var(--text-main);
  border-color: #d1d5db;
}

.btn-secondary:hover {
  background: #f9fafb;
  border-color: #9ca3af;
}

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
}

.btn-ghost:hover {
  background: #f3f4f6;
  color: var(--text-main);
}

.btn-outline-sm,
.btn-secondary-sm {
  padding: 6px 10px;
  font-size: 12px;
  border-radius: var(--radius-sm);
}

.btn-outline-sm {
  background: #ffffff;
  color: var(--accent);
  border-color: #bfdbfe;
  flex: 1;
}

.btn-outline-sm:hover {
  background: var(--accent-soft);
}

.btn-secondary-sm {
  background: #111827;
  color: #ffffff;
}

.btn-secondary-sm:disabled {
  background: #d1d5db;
  color: #9ca3af;
  cursor: not-allowed;
}

.text-btn {
  background: none;
  border: none;
  padding: 2px 4px;
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
  cursor: pointer;
}

.text-btn:hover {
  text-decoration: underline;
}

.text-btn.danger {
  color: var(--danger);
}

/* Workspace Grid */
.workspace {
  display: grid;
  grid-template-columns: 380px 1fr;
  flex: 1;
  min-height: 0;
}

/* Control Sidebar */
.control-panel {
  background: var(--bg-panel);
  border-right: 1px solid var(--border-color);
  overflow-y: auto;
  padding: 16px 20px 32px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.panel-section {
  border-bottom: 1px solid #f1f5f9;
  padding-bottom: 16px;
}

.panel-section:last-child {
  border-bottom: none;
  padding-bottom: 0;
}

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

.section-header h2 {
  margin: 0;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.02em;
  color: #1e293b;
}

.size-badge {
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
}

/* Segmented Orientation Switch */
.segmented-control {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  background: #f1f5f9;
  padding: 4px;
  border-radius: 8px;
  margin-bottom: 12px;
}

.seg-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 8px 10px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: #475569;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
}

.seg-btn.active {
  background: #ffffff;
  color: var(--accent);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.seg-icon {
  display: inline-block;
  border: 1.5px solid currentColor;
  border-radius: 2px;
}

.seg-icon-land {
  width: 18px;
  height: 10px;
}

.seg-icon-port {
  width: 10px;
  height: 18px;
}

/* Submode box */
.submode-box {
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  padding: 10px;
  margin-bottom: 12px;
}

.hint-text {
  margin: 6px 0 0;
  font-size: 11.5px;
  line-height: 1.45;
  color: var(--text-muted);
}

/* Form Elements */
.field-block {
  margin-bottom: 12px;
}

.field-block:last-child {
  margin-bottom: 0;
}

.field-label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: #374151;
  margin-bottom: 6px;
}

.field-label-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
}

.field-label-row .field-label {
  margin-bottom: 0;
}

.airmail-control-box {
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  padding: 10px;
}

.size-preset-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.size-preset-btn {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  padding: 8px 10px;
  background: #ffffff;
  border: 1.5px solid #e2e8f0;
  border-radius: 8px;
  cursor: pointer;
  text-align: left;
  transition: all 0.15s ease;
}

.size-preset-btn:hover {
  border-color: #93c5fd;
}

.size-preset-btn.active {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.preset-title {
  font-size: 13px;
  font-weight: 700;
  color: #1e293b;
  font-variant-numeric: tabular-nums;
}

.size-preset-btn.active .preset-title {
  color: var(--accent);
}

.preset-sub {
  font-size: 11px;
  color: var(--text-muted);
}

.preset-manager-box {
  margin-top: 10px;
  padding: 10px;
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.preset-manager-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.preset-manager-title {
  font-size: 11.5px;
  font-weight: 700;
  color: #475569;
}

.preset-status-text {
  font-size: 11px;
  font-weight: 600;
  color: #059669;
}

.preset-name-row {
  display: flex;
  gap: 6px;
}

.preset-name-row input[type="text"] {
  flex: 1;
  min-width: 0;
  padding: 5px 8px;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  font-size: 12.5px;
  color: #1e293b;
  background: #ffffff;
}

.preset-name-row input[type="text"]:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(29, 78, 216, 0.12);
}

.preset-btn-row {
  display: flex;
  gap: 6px;
}

.preset-io-row {
  display: flex;
  gap: 6px;
  padding-top: 6px;
  border-top: 1px dashed #cbd5e1;
}

.preset-io-row .btn-mini {
  flex: 1;
  text-align: center;
}

.btn-mini {
  padding: 5px 8px;
  font-size: 11.5px;
  font-weight: 600;
  border-radius: 6px;
  border: 1px solid #cbd5e1;
  background: #ffffff;
  color: #334155;
  cursor: pointer;
}

.btn-mini:hover:not(:disabled) {
  background: #f1f5f9;
  border-color: #94a3b8;
}

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

.btn-mini-primary {
  background: var(--accent);
  color: #ffffff;
  border-color: var(--accent);
  flex: 1;
}

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

.btn-mini-danger {
  color: var(--danger);
  border-color: #fecaca;
}

.btn-mini-danger:hover:not(:disabled) {
  background: #fef2f2;
  border-color: #f87171;
}

.margin-panel {
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  padding: 10px;
  margin-bottom: 12px;
}

.inline-check {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11.5px;
  font-weight: 600;
  color: #475569;
  cursor: pointer;
}

.inline-check input {
  accent-color: var(--accent);
  cursor: pointer;
}

.margin-grid-4 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 10px;
}

.margin-cell {
  background: #ffffff;
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  padding: 6px 8px;
}

.margin-cell-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 3px;
}

.margin-cell-head label {
  font-size: 11.5px;
  font-weight: 700;
  color: #334155;
}

.margin-cell-head input[type="number"] {
  width: 54px;
  padding: 2px 4px;
  border: 1px solid #cbd5e1;
  border-radius: 4px;
  font-size: 11.5px;
  font-weight: 600;
  text-align: right;
  color: #1e293b;
}

.margin-cell input[type="range"] {
  margin: 0;
}

.custom-dim-panel {
  margin-top: 10px;
  padding: 10px;
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.custom-dim-panel.is-custom-active {
  border-color: var(--accent);
  background: #eff6ff;
}

.custom-dim-hint {
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 8px;
  line-height: 1.4;
}

.dim-drag-row {
  margin-bottom: 8px;
}

.dim-drag-row:last-child {
  margin-bottom: 0;
}

.dim-drag-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 3px;
}

.dim-drag-head label {
  font-size: 12px;
  font-weight: 600;
  color: #374151;
}

.dim-drag-head input[type="number"] {
  width: 68px;
  padding: 3px 6px;
  border: 1px solid #cbd5e1;
  border-radius: 5px;
  font-size: 12px;
  font-weight: 600;
  text-align: right;
  background: #ffffff;
  color: #1e293b;
}

.select-input,
textarea {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid #d1d5db;
  border-radius: var(--radius-sm);
  font-size: 13px;
  color: var(--text-main);
  background: #ffffff;
  font-family: inherit;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.dim-drag-head input[type="number"]:focus,
.select-input:focus,
textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(29, 78, 216, 0.12);
}

textarea {
  resize: vertical;
  line-height: 1.45;
}

.radio-pills {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.pill-option {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.pill-option input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.pill-option span {
  width: 100%;
  text-align: center;
  padding: 6px 8px;
  font-size: 12px;
  font-weight: 500;
  border: 1px solid #d1d5db;
  border-radius: var(--radius-sm);
  background: #ffffff;
  color: #374151;
  transition: all 0.15s ease;
}

.pill-option input:checked + span {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}

/* Sliders */
.slider-row {
  margin-bottom: 10px;
}

.slider-label-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  margin-bottom: 4px;
}

.slider-label-row label {
  font-weight: 600;
  color: #374151;
}

.slider-val {
  font-variant-numeric: tabular-nums;
  font-size: 12px;
  color: var(--accent);
  font-weight: 600;
}

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

/* Toggles */
.toggle-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 12px;
}

.toggle-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12.5px;
  color: #374151;
  cursor: pointer;
}

.toggle-item input[type="checkbox"] {
  width: 15px;
  height: 15px;
  accent-color: var(--accent);
  cursor: pointer;
}

/* Copies & Batch Queue */
.copies-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.stepper {
  display: inline-flex;
  align-items: center;
  border: 1px solid #d1d5db;
  border-radius: var(--radius-sm);
  overflow: hidden;
}

.stepper-btn {
  width: 28px;
  height: 28px;
  border: none;
  background: #f9fafb;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  color: #374151;
}

.stepper-btn:hover {
  background: #e5e7eb;
}

.stepper input {
  width: 44px;
  height: 28px;
  border: none;
  border-left: 1px solid #d1d5db;
  border-right: 1px solid #d1d5db;
  text-align: center;
  font-size: 13px;
  font-weight: 600;
}

.batch-actions {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
}

.saved-box {
  margin-top: 10px;
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  padding: 8px 10px;
}

.saved-box-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 11.5px;
  font-weight: 700;
  color: #475569;
  margin-bottom: 6px;
}

.saved-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 150px;
  overflow-y: auto;
}

.saved-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 8px;
  background: #ffffff;
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  font-size: 12px;
}

.saved-item-info {
  min-width: 0;
  flex: 1;
  cursor: pointer;
}

.saved-item-title {
  font-weight: 600;
  color: #1e293b;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.saved-item-sub {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.saved-item-del {
  background: none;
  border: none;
  color: #9ca3af;
  cursor: pointer;
  font-size: 14px;
  padding: 2px 4px;
}

.saved-item-del:hover {
  color: var(--danger);
}

.saved-empty {
  font-size: 11.5px;
  color: var(--text-muted);
  text-align: center;
  padding: 6px 0;
}

/* Preview Stage */
.preview-stage {
  display: flex;
  flex-direction: column;
  background: var(--bg-stage);
  background-image: radial-gradient(#cbd5e1 1px, transparent 1px);
  background-size: 18px 18px;
  min-width: 0;
  min-height: 0;
}

.preview-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px 12px;
  padding: 10px 20px;
  background: rgba(255, 255, 255, 0.88);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid #d1d5db;
  flex-shrink: 0;
}

.toolbar-right {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}

.ppi-calibrator {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  padding: 3px 8px;
  font-size: 11.5px;
  color: #475569;
}

.ppi-calibrator label {
  font-weight: 600;
  white-space: nowrap;
}

.ppi-calibrator select {
  border: 1px solid #cbd5e1;
  border-radius: 4px;
  background: #ffffff;
  font-size: 11.5px;
  padding: 2px 4px;
  color: #1e293b;
}

.ppi-calibrator input[type="number"] {
  width: 48px;
  border: 1px solid #cbd5e1;
  border-radius: 4px;
  background: #ffffff;
  font-size: 11.5px;
  font-weight: 600;
  padding: 2px 4px;
  text-align: center;
  color: #1e293b;
}

.ppi-unit {
  font-size: 11px;
  color: #64748b;
  font-weight: 600;
}

.preview-info {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12.5px;
  font-weight: 600;
  color: #334155;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #10b981;
}

.zoom-controls {
  display: inline-flex;
  background: #f1f5f9;
  padding: 3px;
  border-radius: 6px;
  gap: 2px;
}

.zoom-btn {
  border: none;
  background: transparent;
  padding: 4px 9px;
  font-size: 12px;
  font-weight: 600;
  color: #475569;
  border-radius: 4px;
  cursor: pointer;
}

.zoom-btn.active {
  background: #ffffff;
  color: var(--accent);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
}

.stage-canvas-wrap {
  flex: 1;
  overflow: auto;
  display: flex;
  padding: 28px;
  min-height: 0;
}

.ruler-sizer {
  margin: auto;
  position: relative;
  flex-shrink: 0;
}

.ruler-stage {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
  display: grid;
  grid-template-columns: 24px auto;
  grid-template-rows: 24px auto;
}

.ruler-corner {
  grid-column: 1;
  grid-row: 1;
  background: #f8fafc;
  border: 1px solid #cbd5e1;
  font-size: 9px;
  font-weight: 700;
  color: #64748b;
  display: flex;
  align-items: center;
  justify-content: center;
}

.ruler-top {
  grid-column: 2;
  grid-row: 1;
  height: 24px;
  background: #f8fafc;
  border-top: 1px solid #cbd5e1;
  border-right: 1px solid #cbd5e1;
  border-bottom: 1px solid #cbd5e1;
  position: relative;
  overflow: hidden;
}

.ruler-left {
  grid-column: 1;
  grid-row: 2;
  width: 24px;
  background: #f8fafc;
  border-left: 1px solid #cbd5e1;
  border-right: 1px solid #cbd5e1;
  border-bottom: 1px solid #cbd5e1;
  position: relative;
  overflow: hidden;
}

.ruler-tick-x {
  position: absolute;
  bottom: 0;
  width: 1px;
  background: #64748b;
}

.ruler-tick-x.major {
  height: 11px;
  background: #334155;
}

.ruler-tick-x.half {
  height: 7px;
}

.ruler-tick-x.minor {
  height: 4px;
  background: #94a3b8;
}

.ruler-label-x {
  position: absolute;
  top: 2px;
  transform: translateX(2px);
  font-size: 9px;
  font-weight: 600;
  color: #475569;
  line-height: 1;
}

.ruler-tick-y {
  position: absolute;
  right: 0;
  height: 1px;
  background: #64748b;
}

.ruler-tick-y.major {
  width: 11px;
  background: #334155;
}

.ruler-tick-y.half {
  width: 7px;
}

.ruler-tick-y.minor {
  width: 4px;
  background: #94a3b8;
}

.ruler-label-y {
  position: absolute;
  left: 2px;
  transform: translateY(2px);
  font-size: 9px;
  font-weight: 600;
  color: #475569;
  line-height: 1;
}

/* Word-style shaded margin zones & draggable markers on rulers */
.ruler-margin-shade {
  position: absolute;
  background: rgba(148, 163, 184, 0.28);
  pointer-events: none;
}

.ruler-margin-shade-x {
  top: 0;
  bottom: 0;
}

.ruler-margin-shade-y {
  left: 0;
  right: 0;
}

.ruler-margin-marker {
  position: absolute;
  z-index: 12;
  touch-action: none;
  user-select: none;
}

.ruler-margin-marker-x {
  top: 0;
  bottom: 0;
  width: 10px;
  transform: translateX(-50%);
  cursor: ew-resize;
}

.ruler-margin-marker-x::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-bottom: 7px solid #0284c7;
}

.ruler-margin-marker-y {
  left: 0;
  right: 0;
  height: 10px;
  transform: translateY(-50%);
  cursor: ns-resize;
}

.ruler-margin-marker-y::after {
  content: "";
  position: absolute;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 0;
  height: 0;
  border-top: 5px solid transparent;
  border-bottom: 5px solid transparent;
  border-right: 7px solid #0284c7;
}

.card-stage-box {
  grid-column: 2;
  grid-row: 2;
  position: relative;
  display: inline-block;
}

.card-mount {
  box-shadow: var(--shadow-card);
  background: #ffffff;
}

/* Word-style draggable margin guide lines over postcard */
.margin-guide-line {
  position: absolute;
  z-index: 8;
  touch-action: none;
  user-select: none;
}

.margin-guide-line.is-hidden-guide {
  display: none;
}

.margin-line-top,
.margin-line-bottom {
  left: 0;
  right: 0;
  height: 9px;
  transform: translateY(-50%);
  cursor: ns-resize;
}

.margin-line-top::after,
.margin-line-bottom::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: 50%;
  border-top: 1px dashed rgba(14, 165, 233, 0.55);
  transition: border-color 0.12s ease;
}

.margin-line-left,
.margin-line-right {
  top: 0;
  bottom: 0;
  width: 9px;
  transform: translateX(-50%);
  cursor: ew-resize;
}

.margin-line-left::after,
.margin-line-right::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  border-left: 1px dashed rgba(14, 165, 233, 0.55);
  transition: border-color 0.12s ease;
}

.margin-guide-line:hover::after,
.margin-guide-line.is-dragging::after {
  border-color: #0284c7;
  border-width: 1.5px;
}

/* On-canvas interactive drag-to-resize handles */
.resize-handle {
  position: absolute;
  z-index: 15;
  touch-action: none;
  user-select: none;
}

.resize-handle-e {
  top: 6px;
  right: -5px;
  bottom: 10px;
  width: 10px;
  cursor: ew-resize;
}

.resize-handle-e::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 4px;
  height: 28px;
  border-radius: 999px;
  background: rgba(37, 99, 235, 0.38);
  transition: background 0.15s ease, transform 0.15s ease;
}

.resize-handle-s {
  left: 6px;
  right: 10px;
  bottom: -5px;
  height: 10px;
  cursor: ns-resize;
}

.resize-handle-s::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 28px;
  height: 4px;
  border-radius: 999px;
  background: rgba(37, 99, 235, 0.38);
  transition: background 0.15s ease, transform 0.15s ease;
}

.resize-handle-se {
  right: -6px;
  bottom: -6px;
  width: 14px;
  height: 14px;
  cursor: nwse-resize;
  border-radius: 50%;
  background: #2563eb;
  border: 2px solid #ffffff;
  box-shadow: 0 1px 4px rgba(15, 23, 42, 0.3);
  transition: transform 0.15s ease;
}

.resize-handle-e:hover::after,
.resize-handle-s:hover::after {
  background: #2563eb;
}

.resize-handle-se:hover {
  transform: scale(1.2);
}

.drag-size-toast {
  position: absolute;
  right: 10px;
  bottom: 10px;
  z-index: 20;
  padding: 4px 8px;
  border-radius: 6px;
  background: rgba(15, 23, 42, 0.86);
  color: #ffffff;
  font-size: 11px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
  white-space: nowrap;
}

.print-tips-bar {
  padding: 10px 20px;
  background: #fffbeb;
  border-top: 1px solid #fde68a;
  font-size: 12px;
  color: #78350f;
  line-height: 1.45;
  flex-shrink: 0;
}

/* =========================================================
   ADDRESS CARD PHYSICAL LAYOUT
   Used identically in Preview and Print!
========================================================= */
.address-card {
  position: relative;
  background: #ffffff;
  color: #111827;
  overflow: hidden;
  -webkit-print-color-adjust: exact;
  print-color-adjust: exact;
}

.card-inner {
  width: 100%;
  height: 100%;
  position: relative;
}

/* Rotated Portrait Mode: physical page is horizontal,
   content box is vertical rotated -90deg inside it */
.address-card.is-rotated .card-inner {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) rotate(-90deg);
  transform-origin: center center;
}

.address-card.is-rotated-cw .card-inner {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) rotate(90deg);
  transform-origin: center center;
}

.address-card.is-rotated-180 .card-inner {
  width: 100%;
  height: 100%;
  transform: rotate(180deg);
  transform-origin: center center;
}

.card-surface {
  width: 100%;
  height: 100%;
  border: none;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  position: relative;
}

/* Regions inside card-surface */
.card-header-area {
  display: flex;
  flex-direction: column;
  position: relative;
  min-height: 0;
  flex-shrink: 0;
}

.card-header-row {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 2mm;
}

.card-header-row.right-only {
  justify-content: flex-end;
}

.card-top-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 1.5mm;
  margin-left: auto;
  flex-shrink: 0;
}

.return-top-wrap {
  flex: 1 1 auto;
  min-width: 0;
}

.stamp-guide {
  width: 50mm;
  height: 30mm;
  border: 0.25mm dashed #cbd5e1;
  border-radius: 0.6mm;
  color: #94a3b8;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", sans-serif;
  font-size: 7pt;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  line-height: 1.15;
  flex-shrink: 0;
  user-select: none;
}

.card-bottom-area {
  margin-top: auto;
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 3mm;
  min-height: 0;
  padding-top: 1.5mm;
}

.bottom-left-slot {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1.5mm;
  flex-shrink: 0;
  max-width: 42%;
}

.recipient-corner-wrap {
  margin-left: auto;
  max-width: 66%;
  min-width: 0;
  align-self: flex-end;
}

.orient-portrait .card-bottom-area {
  flex-direction: column;
  align-items: stretch;
  gap: 3.5mm;
}

.orient-portrait .recipient-corner-wrap {
  order: 1;
  max-width: 100%;
  margin-left: auto;
  align-self: flex-end;
}

.orient-portrait .bottom-left-slot {
  order: 2;
  max-width: 100%;
  align-self: flex-start;
}

.airmail-corner-mark {
  display: inline-block;
  border: 0.45mm solid #0f172a;
  padding: 1.4mm 2.8mm;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", sans-serif;
  font-size: 8.5pt;
  font-weight: 700;
  line-height: 1.25;
  letter-spacing: 0.05em;
  color: #0f172a;
  text-align: center;
  white-space: pre-wrap;
  word-break: break-word;
}

.airmail-corner-mark.is-unframed {
  border: none;
  padding: 0;
  text-align: left;
}

.to-tag {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 7.5pt;
  font-weight: 700;
  letter-spacing: 0.12em;
  color: #64748b;
  text-transform: uppercase;
  margin-bottom: 1mm;
}

.recipient-block {
  word-break: break-word;
  overflow-wrap: anywhere;
  color: #0f172a;
}

.recipient-line {
  display: block;
}

.recipient-line.is-first-bold {
  font-weight: 700;
}

.return-block {
  word-break: break-word;
  overflow-wrap: anywhere;
  color: #334155;
  line-height: 1.28;
}

.card-divider {
  width: 100%;
  height: 0;
  border: none;
  border-top: 0.3mm solid #cbd5e1;
  margin: 1.2mm 0;
}

.return-pos-bottom .card-header-area.no-top-items {
  display: none;
}

/* Print-only Container */
#print-area {
  display: none;
}

/* Responsive */
@media (max-width: 960px) {
  .app-shell {
    height: auto;
    overflow: visible;
  }

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

  .control-panel {
    border-right: none;
    border-bottom: 1px solid var(--border-color);
  }

  .stage-canvas-wrap {
    min-height: 420px;
  }
}

/* =========================================================
   PRINT MEDIA RULES
========================================================= */
@media print {
  html,
  body {
    margin: 0 !important;
    padding: 0 !important;
    background: #ffffff !important;
    width: auto !important;
    height: auto !important;
    overflow: visible !important;
  }

  .no-print,
  .stamp-guide,
  .app-shell {
    display: none !important;
  }

  #print-area {
    display: block !important;
    margin: 0 !important;
    padding: 0 !important;
  }

  .print-page {
    margin: 0 !important;
    padding: 0 !important;
    page-break-after: always;
    break-after: page;
    page-break-inside: avoid;
    break-inside: avoid;
  }

  .print-page:last-child {
    page-break-after: auto;
    break-after: auto;
  }
}
