:root {
  --bg: #f7f7f5;
  --surface: #ffffff;
  --surface-2: #f1f1ee;
  --ink: #171717;
  --muted: #6d6d66;
  --line: #d8d8d2;
  --line-strong: #b6b6ae;
  --accent: #171717;
  --danger: #a52626;
  --radius: 16px;
  --shadow: 0 12px 35px rgba(0,0,0,.06);
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }
body {
  margin: 0;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--ink);
  background: var(--bg);
}
button, input, select { font: inherit; }
button { color: inherit; }

.app-shell { max-width: 1700px; margin: 0 auto; padding: 28px; }
.topbar { display: flex; align-items: flex-end; justify-content: space-between; gap: 20px; margin-bottom: 22px; }
.eyebrow { font-size: 12px; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); font-weight: 700; }
h1 { font-size: clamp(28px, 4vw, 48px); line-height: 1; margin: 6px 0 0; letter-spacing: -.04em; }
h2 { font-size: 18px; margin: 0; }
h3 { margin: 0 0 8px; font-size: 18px; }
p { margin: 0; }
.muted { color: var(--muted); font-size: 13px; }
.privacy-badge { background: var(--surface); border: 1px solid var(--line); border-radius: 999px; padding: 9px 12px; font-size: 13px; box-shadow: var(--shadow); }

.tabs { display: grid; grid-template-columns: 1fr 1fr; background: #e9e9e5; padding: 4px; border-radius: 14px; margin-bottom: 18px; }
.tab { border: 0; background: transparent; padding: 13px 18px; border-radius: 11px; cursor: pointer; font-weight: 700; }
.tab.is-active { background: var(--surface); box-shadow: 0 2px 12px rgba(0,0,0,.07); }
.tab-panel { display: none; }
.tab-panel.is-active { display: block; }

.layout { display: grid; gap: 18px; }
.layout-merge { grid-template-columns: minmax(300px, 370px) 1fr; align-items: start; }
.sidebar { display: flex; flex-direction: column; gap: 14px; position: sticky; top: 16px; max-height: calc(100vh - 32px); overflow: auto; padding-right: 2px; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.panel-title-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.panel > h2 { margin-bottom: 14px; }

.file-pair { display: grid; gap: 10px; }
.upload-box { border: 1px dashed var(--line-strong); border-radius: 13px; padding: 14px; min-height: 92px; display: flex; flex-direction: column; justify-content: center; gap: 4px; cursor: pointer; transition: .15s ease; background: var(--surface); }
.upload-box:hover, .upload-box.is-dragover { border-color: var(--ink); background: var(--surface-2); }
.upload-box input, .file-button input { display: none; }
.upload-kicker { font-size: 11px; font-weight: 800; letter-spacing: .08em; }
.upload-box strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.field-row { display: grid; grid-template-columns: 90px 1fr; gap: 12px; align-items: center; }
.field-row.compact { margin-bottom: 5px; }
.field-row span { color: var(--muted); font-size: 13px; }
select, input[type="number"], input[type="text"] { width: 100%; border: 1px solid var(--line); background: #fff; border-radius: 9px; padding: 9px 10px; outline: none; }
select:focus, input[type="number"]:focus, input[type="text"]:focus { border-color: var(--ink); box-shadow: 0 0 0 2px rgba(0,0,0,.06); }

.size-controls { display: grid; gap: 12px; }
.size-preset-row { display: grid; gap: 6px; }
.size-preset-row > span, .size-dimensions label span { color: var(--muted); font-size: 12px; font-weight: 700; }
.size-dimensions { display: grid; grid-template-columns: minmax(0, 1fr) 40px minmax(0, 1fr); gap: 8px; align-items: end; }
.size-dimensions label { display: grid; gap: 6px; min-width: 0; }
.size-swap { height: 39px; padding: 0; font-size: 17px; }

.segmented { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; background: var(--surface-2); border-radius: 11px; padding: 4px; }
.segmented button { border: 0; background: transparent; border-radius: 8px; padding: 9px 8px; cursor: pointer; font-weight: 700; }
.segmented button.is-active { background: var(--surface); box-shadow: 0 2px 8px rgba(0,0,0,.08); }
.segmented.mini { width: 108px; }
.segmented.mini button { padding: 6px 8px; }

.control-stack { display: grid; gap: 13px; }
.dual-control-head { display: flex; justify-content: space-between; gap: 10px; margin-bottom: 6px; font-size: 13px; font-weight: 700; }
.dual-control-head output { color: var(--muted); font-variant-numeric: tabular-nums; }
.dual-control-grid { display: grid; grid-template-columns: 1fr 90px; gap: 9px; align-items: center; }
input[type="range"] { width: 100%; accent-color: var(--accent); }
.hint { margin-top: 12px; background: var(--surface-2); color: var(--muted); border-radius: 10px; padding: 10px 11px; font-size: 12px; line-height: 1.45; }
.compact-hint { margin-top: 0; }

.btn { border: 1px solid var(--line); background: var(--surface); border-radius: 10px; padding: 10px 13px; cursor: pointer; font-weight: 750; transition: transform .08s ease, background .15s ease; }
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: white; border-color: var(--accent); }
.btn-primary:hover:not(:disabled) { background: #303030; }
.btn-secondary { background: var(--surface-2); }
.btn-ghost { background: transparent; }
.btn-small { padding: 6px 9px; font-size: 12px; }
.btn-large { padding: 14px 16px; }
.btn-full { width: 100%; }
.button-row { display: flex; gap: 8px; margin-top: 12px; }
.button-row > * { flex: 1; }
.button-row.end { justify-content: flex-end; }
.button-row.end > * { flex: 0 0 auto; }

.workspace-panel { min-width: 0; background: #252525; border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); border: 1px solid #1d1d1d; }
.workspace-toolbar { min-height: 60px; display: flex; justify-content: space-between; align-items: center; gap: 18px; color: #fff; padding: 12px 16px; border-bottom: 1px solid rgba(255,255,255,.1); }
.workspace-toolbar .muted { margin-left: 10px; color: #b6b6b6; }
.legend { display: flex; gap: 14px; font-size: 12px; color: #c9c9c9; }
.legend span { display: flex; align-items: center; gap: 6px; }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.dot-a { background: #fff; }
.dot-b { background: #8a8a8a; }
.merge-viewport { height: calc(100vh - 174px); min-height: 650px; overflow: auto; display: block; position: relative; padding: 70px; outline: none; background-color: #2c2c2c; background-image: linear-gradient(rgba(255,255,255,.035) 1px, transparent 1px), linear-gradient(90deg, rgba(255,255,255,.035) 1px, transparent 1px); background-size: 24px 24px; }
#mergeCanvas { background: #fff; box-shadow: 0 24px 70px rgba(0,0,0,.35); display: none; touch-action: none; margin: 0 auto; }
#mergeCanvas.is-ready { display: block; }
.empty-state, .split-empty { text-align: center; color: var(--muted); }
.merge-viewport .empty-state { color: #c6c6c6; position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 24px; }
.empty-icon { width: 72px; height: 72px; border: 1px solid currentColor; border-radius: 20px; display: grid; place-items: center; margin: 0 auto 16px; font-weight: 900; letter-spacing: -.03em; }

.split-toolbar { display: flex; align-items: center; gap: 12px; margin-bottom: 18px; }
.split-toolbar > div:first-child { flex: 1; }
.split-toolbar h2 { margin-bottom: 4px; }
.file-button { display: inline-flex; align-items: center; }
.split-empty { padding: 80px 24px; }
.split-content.is-hidden { display: none; }
.split-content {
  --split-left: 38%;
  display: grid;
  grid-template-columns: minmax(360px, var(--split-left)) 10px minmax(0, 1fr);
  gap: 0;
  height: calc(100vh - 255px);
  min-height: 520px;
  overflow: hidden;
  align-items: stretch;
}
.ranges-panel {
  position: static;
  z-index: auto;
  min-width: 0;
  height: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  border-radius: var(--radius) 0 0 var(--radius);
  box-shadow: none;
}
.ranges-title { position: sticky; top: -18px; z-index: 3; background: var(--surface); padding-top: 18px; padding-bottom: 10px; margin-bottom: 4px; }
.range-list { display: grid; gap: 9px; }
.range-row { display: grid; grid-template-columns: 62px 62px minmax(0, 1fr) 34px; gap: 8px; align-items: center; padding: 10px; border: 1px solid var(--line); border-radius: 12px; }
.range-row label { display: grid; gap: 4px; min-width: 0; }
.range-row label span { font-size: 11px; color: var(--muted); font-weight: 700; }
.range-row .btn { align-self: end; white-space: nowrap; }
.range-row [data-action="export"] { grid-column: 1 / -1; width: 100%; }
.range-row [data-action="remove"] { grid-column: 4; grid-row: 1; width: 34px; padding-left: 0; padding-right: 0; }
.range-add-button { margin-top: 12px; }
.range-warnings { margin-top: 10px; font-size: 12px; color: var(--danger); line-height: 1.5; }
.ranges-footer { position: sticky; bottom: -18px; z-index: 3; background: linear-gradient(to bottom, rgba(255,255,255,0), var(--surface) 14px); padding-top: 22px; padding-bottom: 18px; margin-bottom: -18px; }
.split-divider { position: relative; height: 100%; cursor: col-resize; touch-action: none; background: transparent; outline: none; }
.split-divider::before { content: ''; position: absolute; top: 0; bottom: 0; left: 4px; width: 2px; background: var(--line); transition: background .15s ease, width .15s ease, left .15s ease; }
.split-divider span { position: absolute; top: 50%; left: 50%; width: 6px; height: 58px; transform: translate(-50%, -50%); border-radius: 999px; background: var(--line-strong); }
.split-divider:hover::before, .split-divider.is-dragging::before, .split-divider:focus-visible::before { left: 3px; width: 4px; background: var(--ink); }
.pages-section { min-width: 0; height: 100%; overflow: auto; padding: 4px 4px 24px 18px; }
.pages-heading { position: sticky; top: -4px; z-index: 4; display: flex; align-items: end; justify-content: space-between; gap: 16px; margin: 0 0 12px; padding: 8px 0 12px; background: var(--bg); }
.pages-heading h2 { margin-bottom: 4px; }
.pages-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 390px), 1fr)); gap: 16px; align-items: start; }
.page-card { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; overflow: hidden; box-shadow: var(--shadow); }
.page-card.is-dragging { opacity: .45; }
.page-card.is-drop-target { outline: 2px solid var(--ink); outline-offset: 2px; }
.page-card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 12px; border-bottom: 1px solid var(--line); cursor: grab; }
.page-card-head:active { cursor: grabbing; }
.page-index { font-weight: 850; }
.page-source { min-width: 0; flex: 1; color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.page-actions { display: flex; gap: 5px; }
.icon-btn { border: 1px solid var(--line); background: #fff; border-radius: 8px; min-width: 32px; height: 32px; padding: 0 8px; cursor: pointer; font-weight: 800; }
.icon-btn:hover { background: var(--surface-2); }
.page-canvas-wrap { min-height: 420px; background: #dededa; display: grid; place-items: center; padding: 16px; }
.page-canvas-wrap canvas { max-width: 100%; height: auto; box-shadow: 0 8px 28px rgba(0,0,0,.18); background: #fff; }
.render-placeholder { color: var(--muted); font-size: 13px; }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 16px); opacity: 0; pointer-events: none; background: #171717; color: #fff; padding: 11px 14px; border-radius: 10px; box-shadow: 0 12px 30px rgba(0,0,0,.24); font-size: 13px; transition: .2s ease; z-index: 999; }
.toast.is-visible { opacity: 1; transform: translate(-50%, 0); }
.toast.is-error { background: #7f1d1d; }

@media (max-width: 1050px) {
  .layout-merge { grid-template-columns: 1fr; }
  .sidebar { position: static; max-height: none; overflow: visible; }
  .merge-viewport { height: 72vh; min-height: 520px; }
}

@media (max-width: 700px) {
  .app-shell { padding: 14px; }
  .topbar { align-items: flex-start; flex-direction: column; }
  .privacy-badge { display: none; }
  .tabs { grid-template-columns: 1fr; }
  .split-toolbar { align-items: stretch; flex-direction: column; }
  .split-content { display: block; height: auto; min-height: 0; overflow: visible; }
  .ranges-panel { height: auto; max-height: 52vh; border-radius: var(--radius); margin-bottom: 16px; }
  .split-divider { display: none; }
  .pages-section { height: auto; overflow: visible; padding: 0; }
  .pages-heading { position: static; }
  .range-row { grid-template-columns: 1fr 1fr; }
  .range-row label:nth-child(3) { grid-column: 1 / -1; }
  .range-row .btn { width: 100%; }
  .pages-heading { align-items: flex-start; flex-direction: column; }
  .merge-viewport { padding: 28px; }
}
.page-canvas-wrap img { max-width: 100%; width: auto; height: auto; display: block; box-shadow: 0 8px 28px rgba(0,0,0,.18); background: #fff; }

