:root {
  --bg: #f5f7fb;
  --panel: #ffffff;
  --panel-2: #f8fafc;
  --ink: #101828;
  --muted: #667085;
  --muted-2: #98a2b3;
  --line: #e4e7ec;
  --brand: #175cd3;
  --brand-2: #1849a9;
  --brand-soft: #eff8ff;
  --danger: #b42318;
  --danger-soft: #fff1f3;
  --ok: #067647;
  --shadow: 0 20px 60px rgba(16, 24, 40, 0.12);
  --radius-xl: 28px;
  --radius-lg: 22px;
  --radius-md: 16px;
  --a4-ratio: 0.7070707;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html {
  background: var(--bg);
  color: var(--ink);
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100dvh;
  background:
    radial-gradient(circle at top left, rgba(23, 92, 211, 0.16), transparent 34rem),
    linear-gradient(180deg, #eef4ff 0%, var(--bg) 34rem);
}

button,
input {
  font: inherit;
}

button {
  -webkit-tap-highlight-color: transparent;
}

img,
video,
canvas {
  max-width: 100%;
}

#app-shell {
  width: min(1180px, 100%);
  margin: 0 auto;
  padding: max(18px, env(safe-area-inset-top)) 18px max(90px, env(safe-area-inset-bottom));
}

.hero {
  background: linear-gradient(135deg, rgba(16, 24, 40, 0.96), rgba(24, 73, 169, 0.94));
  color: #fff;
  border-radius: 34px;
  padding: 24px;
  box-shadow: var(--shadow);
  overflow: hidden;
  position: relative;
}

.hero::after {
  content: "";
  position: absolute;
  inset: auto -12% -45% auto;
  width: 320px;
  height: 320px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.12);
}

.hero-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
  position: relative;
  z-index: 1;
}

.eyebrow {
  margin: 0 0 7px;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-size: 0.72rem;
  font-weight: 800;
  color: inherit;
  opacity: 0.74;
}

.hero h1 {
  margin: 0;
  font-size: clamp(2rem, 9vw, 4.6rem);
  line-height: 0.95;
  letter-spacing: -0.075em;
}

.hero-copy {
  max-width: 650px;
  margin: 14px 0 0;
  color: rgba(255, 255, 255, 0.78);
  font-size: 1.02rem;
  line-height: 1.55;
}

.hero-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-top: 28px;
  position: relative;
  z-index: 1;
}

.hero-stats div {
  background: rgba(255, 255, 255, 0.12);
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 18px;
  padding: 14px;
  backdrop-filter: blur(12px);
}

.hero-stats strong {
  display: block;
  font-size: 1.55rem;
  line-height: 1;
}

.hero-stats span {
  display: block;
  margin-top: 5px;
  color: rgba(255, 255, 255, 0.72);
  font-size: 0.82rem;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 0.86fr) minmax(360px, 1.14fr);
  gap: 18px;
  margin-top: 18px;
  align-items: start;
}

.panel {
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid rgba(228, 231, 236, 0.86);
  border-radius: var(--radius-xl);
  box-shadow: 0 14px 40px rgba(16, 24, 40, 0.08);
  overflow: hidden;
  backdrop-filter: blur(16px);
}

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

.section-head h2,
.modal-head h2 {
  margin: 0;
  font-size: 1.2rem;
  letter-spacing: -0.03em;
}

.sticky-head {
  position: sticky;
  top: 0;
  z-index: 5;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(14px);
}

.primary-btn,
.secondary-btn,
.icon-btn,
.ghost-icon,
.danger-btn,
.tiny-btn {
  border: 0;
  cursor: pointer;
  min-height: 44px;
  border-radius: 999px;
  font-weight: 800;
  transition: transform 0.16s ease, box-shadow 0.16s ease, background 0.16s ease;
}

.primary-btn {
  background: var(--brand);
  color: #fff;
  padding: 0 18px;
  box-shadow: 0 10px 22px rgba(23, 92, 211, 0.26);
}

.primary-btn:hover { background: var(--brand-2); }
.primary-btn:active,
.secondary-btn:active,
.icon-btn:active,
.choice-card:active,
.tiny-btn:active { transform: scale(0.985); }

.primary-btn:disabled,
.secondary-btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  box-shadow: none;
}

.secondary-btn {
  background: var(--panel-2);
  color: var(--ink);
  border: 1px solid var(--line);
  padding: 0 16px;
}

.icon-btn {
  flex: 0 0 auto;
  width: 46px;
  height: 46px;
  background: rgba(255, 255, 255, 0.14);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.18);
}

.icon-btn span { font-size: 1.15rem; }

.ghost-icon {
  width: 44px;
  height: 44px;
  background: var(--panel-2);
  color: var(--ink);
  font-size: 1.45rem;
  line-height: 1;
}

.danger-btn {
  background: var(--danger-soft);
  color: var(--danger);
  padding: 0 14px;
}

.tiny-btn {
  min-height: 36px;
  padding: 0 12px;
  background: #fff;
  color: var(--ink);
  border: 1px solid var(--line);
  font-size: 0.84rem;
}

.wide { width: 100%; }

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

.empty-state {
  margin: 18px;
  padding: 28px 18px;
  text-align: center;
  border: 1px dashed #c7d7fe;
  border-radius: var(--radius-lg);
  background: linear-gradient(180deg, #f8fbff, #fff);
}

.empty-icon {
  width: 64px;
  height: 64px;
  border-radius: 22px;
  display: grid;
  place-items: center;
  margin: 0 auto 14px;
  background: var(--brand-soft);
  color: var(--brand);
  font-size: 2rem;
}

.empty-state h3 { margin: 0 0 8px; }
.empty-state p {
  margin: 0 auto 18px;
  max-width: 380px;
  color: var(--muted);
  line-height: 1.5;
}

.cards-list {
  display: grid;
  gap: 12px;
  padding: 18px;
}

.card-item {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 14px;
  box-shadow: 0 8px 22px rgba(16, 24, 40, 0.05);
}

.card-item.dragging {
  opacity: 0.6;
  transform: scale(0.985);
}

.card-main-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.card-title {
  display: flex;
  align-items: center;
  gap: 10px;
}

.drag-handle {
  width: 32px;
  height: 42px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  background: var(--panel-2);
  color: var(--muted);
  cursor: grab;
  touch-action: none;
}

.card-title h3 {
  margin: 0;
  font-size: 1rem;
  letter-spacing: -0.02em;
}

.card-title p {
  margin: 3px 0 0;
  color: var(--muted);
  font-size: 0.84rem;
}

.card-actions {
  display: flex;
  gap: 7px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.side-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-top: 14px;
}

.card-item.single .side-grid { grid-template-columns: 1fr; }

.side-box {
  border: 1px solid var(--line);
  border-radius: 18px;
  overflow: hidden;
  background: var(--panel-2);
}

.side-preview {
  position: relative;
  aspect-ratio: var(--side-ratio, 1.585185);
  background:
    linear-gradient(135deg, #eef4ff, #f9fafb);
  display: grid;
  place-items: center;
  overflow: hidden;
}

.side-preview.ration { aspect-ratio: var(--a4-ratio); }

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

.side-preview.empty::before {
  content: "+";
  font-size: 2rem;
  color: var(--brand);
  width: 52px;
  height: 52px;
  border-radius: 999px;
  display: grid;
  place-items: center;
  background: #fff;
  box-shadow: 0 8px 20px rgba(23, 92, 211, 0.12);
}

.side-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px;
}

.side-meta strong {
  font-size: 0.88rem;
}

.side-meta span {
  display: block;
  margin-top: 2px;
  font-size: 0.76rem;
  color: var(--muted);
}

.side-tools {
  display: flex;
  gap: 6px;
}

.preview-panel {
  min-height: 420px;
}

.notice-area {
  display: grid;
  gap: 8px;
  padding: 14px 18px 0;
}

.notice {
  border-radius: 16px;
  padding: 12px 14px;
  line-height: 1.42;
  font-size: 0.9rem;
}

.notice.error {
  background: var(--danger-soft);
  color: var(--danger);
}

.notice.info {
  background: var(--brand-soft);
  color: #1849a9;
}

.preview-pages {
  display: grid;
  gap: 18px;
  padding: 18px;
  justify-items: center;
}

.preview-page-wrap {
  width: min(100%, 420px);
}

.preview-page-label {
  margin: 0 0 8px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
  text-align: center;
}

.a4-preview {
  position: relative;
  width: 100%;
  aspect-ratio: var(--a4-ratio);
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 12px 38px rgba(16, 24, 40, 0.15);
  overflow: hidden;
}

.preview-img,
.print-img {
  position: absolute;
  object-fit: cover;
  overflow: hidden;
  background: #fff;
}

.preview-img.rounded,
.print-img.rounded {
  border-radius: var(--card-radius, 3.18mm);
}

.preview-placeholder {
  position: absolute;
  display: grid;
  place-items: center;
  text-align: center;
  padding: 8px;
  color: var(--muted);
  font-size: 0.68rem;
  background: repeating-linear-gradient(45deg, #f2f4f7, #f2f4f7 8px, #fff 8px, #fff 16px);
  border-radius: inherit;
}

.modal {
  width: min(100% - 24px, 560px);
  max-height: min(90dvh, 760px);
  padding: 0;
  border: 0;
  background: transparent;
}

.modal::backdrop {
  background: rgba(16, 24, 40, 0.56);
  backdrop-filter: blur(10px);
}

.modal-card {
  background: #fff;
  border-radius: 30px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

.modal-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 18px;
  border-bottom: 1px solid var(--line);
}

.type-grid {
  display: grid;
  gap: 10px;
  padding: 18px;
}

.type-btn,
.choice-card {
  width: 100%;
  min-height: 72px;
  padding: 16px;
  text-align: left;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: #fff;
  cursor: pointer;
}

.type-btn strong,
.choice-card strong {
  display: block;
  color: var(--ink);
  font-size: 1rem;
}

.type-btn span,
.choice-card span {
  display: block;
  margin-top: 5px;
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1.35;
}

.type-btn:hover,
.choice-card:hover {
  border-color: #84adff;
  background: #f8fbff;
}

.special-box {
  border-top: 1px solid var(--line);
  padding: 18px;
  display: grid;
  gap: 12px;
}

.special-box.hidden { display: none; }

.special-box label,
.setting-field {
  display: grid;
  gap: 7px;
  font-weight: 800;
  color: var(--ink);
  font-size: 0.88rem;
}

.special-box input,
.setting-field input {
  width: 100%;
  min-height: 52px;
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 0 14px;
  outline: none;
}

.special-box input:focus,
.setting-field input:focus {
  border-color: #84adff;
  box-shadow: 0 0 0 4px #eff8ff;
}

.side-actions {
  display: grid;
  gap: 10px;
  padding: 18px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}

.crop-modal {
  width: min(100% - 18px, 920px);
}

.full-modal-card {
  max-height: 92dvh;
  display: flex;
  flex-direction: column;
}

.crop-stage {
  margin: 14px;
  min-height: 280px;
  max-height: 62dvh;
  background: #101828;
  border-radius: 22px;
  position: relative;
  overflow: hidden;
  display: grid;
  place-items: center;
  touch-action: none;
}

.crop-stage img {
  max-width: 100%;
  max-height: 62dvh;
  display: block;
  user-select: none;
  pointer-events: none;
}

.crop-box {
  position: absolute;
  border: 2px solid #fff;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.52), 0 0 0 1px rgba(16, 24, 40, 0.25) inset;
  cursor: move;
  touch-action: none;
}

.crop-box::before,
.crop-box::after {
  content: "";
  position: absolute;
  inset: 33.333% 0 auto;
  height: 1px;
  background: rgba(255, 255, 255, 0.65);
}

.crop-box::after {
  inset: 66.666% 0 auto;
}

.crop-handle {
  position: absolute;
  right: -13px;
  bottom: -13px;
  width: 30px;
  height: 30px;
  border-radius: 999px;
  background: #fff;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.28);
  cursor: nwse-resize;
}

.crop-handle::before {
  content: "";
  position: absolute;
  inset: 9px;
  border-right: 3px solid var(--brand);
  border-bottom: 3px solid var(--brand);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 14px 18px 18px;
  border-top: 1px solid var(--line);
}

.camera-modal {
  width: 100%;
  height: 100dvh;
  max-height: none;
  max-width: none;
}

.camera-card {
  width: 100%;
  height: 100dvh;
  background: #05070d;
  color: #fff;
  display: flex;
  flex-direction: column;
}

.camera-topbar {
  height: max(58px, calc(50px + env(safe-area-inset-top)));
  padding: env(safe-area-inset-top) 14px 8px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
}

.camera-close {
  justify-self: start;
  border: 0;
  background: rgba(255, 255, 255, 0.14);
  color: #fff;
  border-radius: 999px;
  min-height: 40px;
  padding: 0 14px;
  font-weight: 800;
}

.camera-viewport {
  flex: 1;
  position: relative;
  overflow: hidden;
  display: grid;
  place-items: center;
}

.camera-viewport video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.camera-frame {
  position: absolute;
  width: min(84vw, calc(70vh * var(--capture-ratio, 1.585185)));
  aspect-ratio: var(--capture-ratio, 1.585185);
  border: 3px solid rgba(255, 255, 255, 0.96);
  border-radius: 18px;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.42), 0 0 0 1px rgba(0, 0, 0, 0.28) inset;
}

.camera-frame.ration {
  width: min(80vw, calc(58vh * var(--a4-ratio)));
  border-radius: 10px;
}

.camera-bottom {
  padding: 12px 18px max(22px, env(safe-area-inset-bottom));
  display: grid;
  gap: 14px;
  justify-items: center;
}

.camera-bottom p {
  margin: 0;
  color: rgba(255, 255, 255, 0.7);
  text-align: center;
  line-height: 1.45;
  font-size: 0.9rem;
}

.capture-btn {
  width: 76px;
  height: 76px;
  border-radius: 999px;
  border: 5px solid rgba(255, 255, 255, 0.72);
  background: #fff;
  box-shadow: 0 0 0 5px rgba(255, 255, 255, 0.16);
}

.settings-modal .modal-card { padding-bottom: 0; }
.setting-field { padding: 18px 18px 8px; }
.muted-note {
  margin: 0;
  color: var(--muted);
  line-height: 1.45;
  padding: 4px 18px 16px;
  font-size: 0.9rem;
}

.print-root { display: none; }

@media (max-width: 900px) {
  #app-shell { padding-inline: 12px; }
  .workspace { grid-template-columns: 1fr; }
  .builder-panel { order: 1; }
  .preview-panel { order: 2; }
}

@media (max-width: 640px) {
  .hero { border-radius: 26px; padding: 20px; }
  .hero-top { align-items: flex-start; }
  .hero-stats { grid-template-columns: 1fr 1fr 1fr; gap: 7px; }
  .hero-stats div { padding: 11px; border-radius: 15px; }
  .hero-stats strong { font-size: 1.22rem; }
  .hero-stats span { font-size: 0.72rem; }
  .section-head { align-items: stretch; flex-direction: column; }
  .section-head .primary-btn,
  .output-actions { width: 100%; }
  .output-actions button { flex: 1; }
  .side-grid { grid-template-columns: 1fr; }
  .card-main-row { flex-direction: column; }
  .card-actions { justify-content: flex-start; }
  .modal { width: calc(100% - 16px); }
  .modal-card { border-radius: 26px; }
  .modal-actions { flex-direction: column-reverse; }
  .modal-actions button { width: 100%; }
}

@page {
  size: A4 portrait;
  margin: 0;
}

@media print {
  html,
  body {
    background: #fff !important;
    width: 210mm;
    margin: 0 !important;
    padding: 0 !important;
  }

  #app-shell,
  dialog,
  .modal {
    display: none !important;
  }

  .print-root {
    display: block !important;
  }

  .print-page {
    position: relative;
    width: 210mm;
    height: 297mm;
    margin: 0;
    background: #fff;
    overflow: hidden;
    page-break-after: always;
    break-after: page;
  }

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

  .print-img {
    display: block;
  }
}
