:root {
  --paper: #f4efe6;
  --paper-deep: #e9dfd0;
  --panel: #fbf8f2;
  --ink: #211d19;
  --muted: #7d7468;
  --muted-deep: #5d554b;
  --line: #d9cdbd;
  --line-dark: #c8b8a5;
  --clay: #c86d4a;
  --clay-deep: #a65338;
  --clay-soft: rgba(200, 109, 74, .11);
  --cyan: #19b9ca;
  --night: #17191c;
  --ease: cubic-bezier(.32, .72, 0, 1);
  --radius: 8px;
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  color: var(--ink);
  background:
    radial-gradient(circle at 83% 2%, rgba(25, 185, 202, .07), transparent 26rem),
    radial-gradient(circle at 13% 0%, rgba(200, 109, 74, .11), transparent 25rem),
    linear-gradient(180deg, var(--paper), #f0e8dc 75%, #eee5d8);
  font-family: Inter, "Noto Sans SC", ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  line-height: 1.55;
  letter-spacing: 0;
}

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .48; }

.app-shell { width: min(1240px, calc(100vw - 40px)); margin: 0 auto; padding: 22px 0 32px; }

.topbar { display: flex; align-items: center; justify-content: space-between; gap: 20px; padding: 0 0 20px; border-bottom: 1px solid var(--line); }
.brand { display: inline-flex; align-items: center; gap: 12px; color: inherit; text-decoration: none; }
.brand-mark { display: grid; grid-template-columns: repeat(4, 8px); gap: 2px; width: 38px; padding: 4px; border: 1px solid var(--line-dark); background: var(--panel); }
.brand-mark span { width: 7px; height: 7px; background: var(--ink); }
.brand-mark span:nth-child(2), .brand-mark span:nth-child(7), .brand-mark span:nth-child(10), .brand-mark span:nth-child(13) { background: var(--clay); }
.brand-mark span:nth-child(5), .brand-mark span:nth-child(12) { background: var(--cyan); }
.brand-kicker, .eyebrow, .rail-title, .section-kicker, .topbar-code, .rail-footer { display: block; color: var(--muted); font-size: 10px; letter-spacing: .14em; text-transform: uppercase; }
.brand strong { display: block; font: 600 24px/1.05 Georgia, "Times New Roman", serif; }
.topbar-meta { display: flex; align-items: center; gap: 14px; color: var(--muted); font-size: 12px; }
.status-chip { display: inline-flex; align-items: center; gap: 7px; padding: 6px 9px; border: 1px solid var(--line); background: rgba(251, 248, 242, .65); border-radius: 999px; }
.status-chip i { width: 7px; height: 7px; border-radius: 50%; background: #6e8b5e; box-shadow: 0 0 0 3px rgba(110, 139, 94, .13); }

.layout { display: grid; grid-template-columns: 220px minmax(0, 1fr); gap: clamp(28px, 5vw, 68px); padding-top: 30px; }
.rail { display: flex; min-height: 580px; flex-direction: column; border-right: 1px solid var(--line); padding-right: 24px; }
.rail-title { padding-bottom: 13px; border-bottom: 1px solid var(--line); }
.mode-nav { display: grid; gap: 4px; margin-top: 14px; }
.mode-btn { display: grid; grid-template-columns: 25px minmax(0, 1fr) auto; gap: 8px; align-items: center; width: 100%; padding: 12px 8px; border: 1px solid transparent; border-radius: var(--radius); background: transparent; text-align: left; transition: background .2s var(--ease), border-color .2s var(--ease), transform .2s var(--ease); }
.mode-btn:hover { transform: translateX(2px); border-color: var(--line); background: rgba(251, 248, 242, .54); }
.mode-btn.is-active { border-color: var(--line-dark); background: var(--panel); box-shadow: 0 11px 28px rgba(62, 50, 37, .06); }
.mode-index { color: var(--clay-deep); font-size: 10px; font-weight: 700; letter-spacing: .06em; }
.mode-copy b, .mode-copy small { display: block; }
.mode-copy b { font-size: 14px; font-weight: 650; }
.mode-copy small { margin-top: 2px; color: var(--muted); font-size: 11px; }
.mode-arrow { color: var(--muted); font-size: 16px; }
.rail-note { display: grid; grid-template-columns: 22px 1fr; gap: 9px; align-items: start; margin-top: auto; padding: 14px 0; border-top: 1px solid var(--line); color: var(--muted-deep); font-size: 12px; }
.rail-note p { margin: 0; }
.rail-note-mark { display: grid; place-items: center; width: 19px; height: 19px; color: var(--clay-deep); border: 1px solid var(--clay); border-radius: 50%; font-size: 14px; line-height: 1; }
.rail-footer { padding-top: 14px; }

.workspace { min-width: 0; }
.workspace-heading { display: flex; justify-content: space-between; gap: 24px; align-items: end; padding-bottom: 22px; border-bottom: 1px solid var(--line); }
.section-kicker { margin: 0 0 8px; color: var(--clay-deep); }
.workspace-heading h1 { margin: 0; font: 600 clamp(30px, 4vw, 48px)/1.05 Georgia, "Times New Roman", serif; }
.section-description { max-width: 560px; margin: 11px 0 0; color: var(--muted-deep); font-size: 14px; }
.workspace-counter { display: flex; flex-direction: column; align-items: flex-end; color: var(--muted); font-size: 10px; letter-spacing: .12em; text-transform: uppercase; }
.workspace-counter strong { color: var(--ink); font: 600 28px/1 Georgia, serif; }

.tool-panel { display: none; grid-template-columns: minmax(220px, .76fr) minmax(330px, 1.24fr); gap: clamp(22px, 4vw, 44px); padding: 26px 0 30px; }
.tool-panel.is-visible { display: grid; }
.tool-controls { display: flex; flex-direction: column; gap: 24px; min-width: 0; }
.control-group { padding-bottom: 20px; border-bottom: 1px solid var(--line); }
.group-heading { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; margin-bottom: 12px; font-size: 14px; font-weight: 650; }
.group-heading small { color: var(--muted); font-size: 9px; font-weight: 500; letter-spacing: .11em; }
.preset-row, .segmented { display: grid; grid-template-columns: repeat(4, 1fr); gap: 5px; }
.segmented { grid-template-columns: repeat(3, 1fr); }
.preset-btn, .convert-preset-btn, .segmented button { min-height: 35px; border: 1px solid var(--line); border-radius: 5px; background: rgba(251, 248, 242, .58); font-size: 12px; transition: background .2s, border-color .2s, color .2s; }
.preset-btn:hover, .convert-preset-btn:hover, .segmented button:hover { border-color: var(--clay); background: var(--clay-soft); }
.preset-btn.is-selected, .convert-preset-btn.is-selected, .segmented button.is-selected { border-color: var(--clay); color: var(--clay-deep); background: var(--clay-soft); font-weight: 700; }
.field-label { display: flex; justify-content: space-between; margin: 16px 0 6px; color: var(--muted-deep); font-size: 12px; }
.field-label span { color: var(--muted); font-size: 10px; letter-spacing: .1em; }
.input-with-unit { display: flex; align-items: center; border-bottom: 1px solid var(--line-dark); }
.input-with-unit input { width: 100%; min-width: 0; padding: 5px 0 7px; border: 0; outline: 0; background: transparent; font: 600 23px/1 Georgia, serif; }
.input-with-unit span { color: var(--muted); font-size: 11px; white-space: nowrap; }
.fixed-output { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 14px; padding: 10px 0 2px; border-top: 1px solid var(--line); color: var(--muted); font-size: 11px; }
.fixed-output strong { color: var(--ink); font: 600 14px ui-monospace, SFMono-Regular, Consolas, monospace; }
.custom-size-row { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; gap: 10px; align-items: center; margin-top: 10px; color: var(--muted-deep); font-size: 11px; }
.custom-size-row input { min-width: 0; height: 35px; padding: 6px 9px; border: 1px solid var(--line); border-radius: 5px; outline: 0; background: rgba(251, 248, 242, .58); }
.custom-size-row input:focus { border-color: var(--clay); box-shadow: 0 0 0 2px var(--clay-soft); }
.custom-size-row b { min-width: 20px; display: inline-block; color: var(--ink); font-weight: 650; }
.final-size-select { position: relative; display: block; }
.final-size-select select { width: 100%; min-height: 40px; appearance: none; padding: 8px 34px 8px 11px; border: 1px solid var(--line); border-radius: 5px; outline: 0; background: rgba(251, 248, 242, .58); font-size: 12px; cursor: pointer; }
.final-size-select select:focus { border-color: var(--clay); box-shadow: 0 0 0 2px var(--clay-soft); }
.final-size-select > span { position: absolute; top: 8px; right: 12px; color: var(--muted); pointer-events: none; }
.crop-summary { display: flex; justify-content: space-between; gap: 12px; margin-top: 10px; color: var(--muted); font-size: 10px; }
.crop-summary strong { color: var(--ink); font-weight: 600; }
.edit-tools { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.edit-tools button { min-height: 36px; border: 1px solid var(--line); border-radius: 5px; background: rgba(251, 248, 242, .58); font-size: 11px; }
.edit-tools button:hover { border-color: var(--clay); background: var(--clay-soft); }
.edit-tools button.is-selected { border-color: var(--clay); background: var(--clay); color: #fffaf3; }
.color-group { display: grid; gap: 0; }
.color-row { display: grid; grid-template-columns: 18px minmax(0, 1fr) auto auto; gap: 9px; align-items: center; padding: 9px 0; border-top: 1px solid rgba(217, 205, 189, .68); color: var(--muted-deep); font-size: 12px; }
.color-row:first-of-type { border-top: 0; }
.color-preview { width: 16px; height: 16px; border: 1px solid rgba(33, 29, 25, .22); background: #2a2420; }
.color-row input[type=color] { width: 24px; height: 24px; padding: 0; border: 0; background: transparent; cursor: pointer; }
.color-row code { color: var(--muted); font-size: 10px; font-family: ui-monospace, SFMono-Regular, Consolas, monospace; }
.control-actions { display: grid; gap: 8px; margin-top: auto; }
.primary-btn, .secondary-btn, .icon-btn { border-radius: var(--radius); transition: transform .2s var(--ease), background .2s, border-color .2s; }
.primary-btn { min-height: 42px; border: 1px solid var(--clay-deep); background: var(--clay); color: #fffaf3; font-weight: 650; box-shadow: 0 9px 18px rgba(166, 83, 56, .14); }
.primary-btn:hover { transform: translateY(-1px); background: var(--clay-deep); }
.primary-btn span, .secondary-btn span { margin-right: 6px; font-size: 17px; vertical-align: -1px; }
.secondary-btn { min-height: 38px; border: 1px solid var(--line-dark); background: transparent; color: var(--muted-deep); font-size: 12px; }
.secondary-btn:hover { border-color: var(--clay); background: var(--clay-soft); }

.preview-column { min-width: 0; }
.preview-head, .preview-foot { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.preview-head { min-height: 44px; padding: 0 0 10px; border-bottom: 1px solid var(--line); }
.preview-head strong { display: block; margin-top: 3px; font-size: 13px; font-weight: 650; }
.icon-btn { display: grid; place-items: center; width: 34px; height: 34px; border: 1px solid var(--line-dark); background: var(--panel); font-size: 18px; }
.icon-btn:hover { border-color: var(--clay); background: var(--clay-soft); transform: translateY(-1px); }
.canvas-frame { position: relative; display: grid; place-items: center; min-height: min(56vw, 500px); margin-top: 15px; overflow: hidden; border: 1px solid var(--line-dark); background-color: var(--night); }
.canvas-frame canvas { width: min(100%, 512px); height: min(100%, 512px); aspect-ratio: 1; image-rendering: pixelated; image-rendering: crisp-edges; }
.grid-preview-frame { width: 100%; min-height: 0; aspect-ratio: 1; background: #f8f7f3; }
.grid-preview-frame .grid-preview-canvas { width: 100%; height: 100%; max-width: none; max-height: none; }
.canvas-frame .grid-preview-canvas {
  background-color: var(--grid-field-color);
  background-image:
    linear-gradient(to right, var(--grid-line-color) 0 var(--grid-line-width), transparent var(--grid-line-width)),
    linear-gradient(to bottom, var(--grid-line-color) 0 var(--grid-line-width), transparent var(--grid-line-width));
  background-size:
    calc(100% / var(--grid-divisions)) 100%,
    100% calc(100% / var(--grid-divisions));
  box-shadow:
    inset calc(-1 * var(--grid-line-width)) 0 var(--grid-line-color),
    inset 0 calc(-1 * var(--grid-line-width)) var(--grid-line-color);
}
.preview-foot { padding-top: 10px; color: var(--muted); font-size: 9px; letter-spacing: .1em; }
.preview-foot span:first-child { color: var(--clay-deep); }

.dropzone { display: flex; align-items: center; gap: 12px; min-height: 86px; padding: 15px; border: 1px dashed var(--line-dark); border-radius: var(--radius); background: rgba(251, 248, 242, .44); cursor: pointer; transition: border-color .2s, background .2s; }
.dropzone:hover, .dropzone.is-dragging { border-color: var(--clay); background: var(--clay-soft); }
.dropzone input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.drop-icon { display: grid; place-items: center; width: 29px; height: 29px; border: 1px solid var(--clay); border-radius: 50%; color: var(--clay-deep); font-size: 20px; line-height: 1; }
.dropzone b, .dropzone small { display: block; }
.dropzone b { font-size: 13px; }
.dropzone small { margin-top: 3px; color: var(--muted); font-size: 11px; }
.source-meta { margin-top: 8px; color: var(--muted); font: 10px ui-monospace, SFMono-Regular, Consolas, monospace; }
.image-adjustments { display: grid; gap: 10px; margin-top: 13px; padding-top: 13px; border-top: 1px solid var(--line); }
.background-toggle { display: flex; align-items: center; gap: 9px; cursor: pointer; }
.background-toggle input { width: 16px; height: 16px; margin: 0; accent-color: var(--clay); }
.background-toggle b, .background-toggle small { display: block; }
.background-toggle b { font-size: 12px; }
.background-toggle small { margin-top: 1px; color: var(--muted); font-size: 10px; }
.tolerance-row { display: grid; grid-template-columns: auto minmax(70px, 1fr) 24px; gap: 9px; align-items: center; color: var(--muted-deep); font-size: 10px; }
.tolerance-row input { width: 100%; accent-color: var(--clay); }
.tolerance-row input:disabled { opacity: .35; }
.tolerance-row output { color: var(--clay-deep); font: 10px ui-monospace, SFMono-Regular, Consolas, monospace; text-align: right; }
.hint-line { margin-top: 8px; color: var(--muted); font-size: 11px; }
.select-wrap { position: relative; display: block; margin-top: 7px; }
.select-wrap select { width: 100%; min-height: 35px; appearance: none; padding: 7px 30px 7px 10px; border: 1px solid var(--line); border-radius: 5px; outline: 0; background: rgba(251, 248, 242, .58); font-size: 12px; }
.select-wrap span { position: absolute; top: 7px; right: 11px; color: var(--muted); pointer-events: none; }
.pixel-preview-frame { width: 100%; min-height: 0; aspect-ratio: 1; background: #f8f7f3; background-image: none; transition: border-color .2s, box-shadow .2s; }
.pixel-preview-frame.is-dragging { border-color: var(--clay); box-shadow: inset 0 0 0 3px var(--clay-soft); }
.preview-stage { position: relative; display: none; overflow: hidden; aspect-ratio: var(--preview-aspect, 1); cursor: grab; touch-action: none; outline: 0; }
.preview-stage.is-visible { display: block; }
.preview-stage:focus-visible { box-shadow: inset 0 0 0 2px var(--clay); }
.preview-stage.is-moving { cursor: grabbing; }
.preview-stage.is-editing { cursor: crosshair; }
.preview-stage.is-wide { width: 100%; height: auto; }
.preview-stage.is-tall { width: auto; height: 100%; }
.pixel-preview-frame canvas { display: block; width: 100%; height: 100%; max-width: none; max-height: none; object-fit: fill; }
.pixel-preview-frame.is-pixel canvas { image-rendering: pixelated; image-rendering: crisp-edges; }
.reference-grid { --reference-divisions: 64; position: absolute; inset: 0; pointer-events: none; background-image: linear-gradient(to right, rgba(20, 18, 16, .28) 1px, transparent 1px), linear-gradient(to bottom, rgba(20, 18, 16, .28) 1px, transparent 1px); background-size: calc(100% / var(--reference-divisions)) 100%, 100% calc(100% / var(--reference-divisions)); box-shadow: inset -1px -1px rgba(20, 18, 16, .28); }
.reference-grid.is-hidden { display: none; }
.crop-frame { position: absolute; left: calc(var(--crop-x, 0) * 100% / var(--reference-divisions)); top: calc(var(--crop-y, 0) * 100% / var(--reference-divisions)); width: calc(var(--crop-size, 16) * 100% / var(--reference-divisions)); height: calc(var(--crop-size, 16) * 100% / var(--reference-divisions)); border: 2px solid #19b9ca; box-shadow: 0 0 0 999px rgba(23, 25, 28, .18), inset 0 0 0 1px rgba(255, 255, 255, .72); pointer-events: none; }
.crop-frame span { position: absolute; left: -2px; bottom: 100%; padding: 3px 6px; background: #19b9ca; color: #102226; font-size: 9px; font-weight: 700; white-space: nowrap; }
.crop-frame.is-hidden { display: none; }
.empty-state { position: absolute; display: grid; gap: 4px; place-items: center; color: var(--muted); text-align: center; pointer-events: none; }
.empty-state span { color: var(--clay); font-size: 34px; }
.empty-state b { color: var(--muted-deep); font-size: 13px; }
.empty-state small { font-size: 11px; }
.empty-state.is-hidden { display: none; }
.preview-switch { display: grid; grid-template-columns: 1fr 1fr; gap: 3px; padding: 3px; border: 1px solid var(--line); border-radius: 6px; background: rgba(251, 248, 242, .58); }
.preview-switch button { min-width: 64px; min-height: 28px; padding: 4px 8px; border: 0; border-radius: 4px; background: transparent; color: var(--muted); font-size: 11px; }
.preview-switch button.is-selected { background: var(--ink); color: var(--panel); }
.preview-toolbar { display: flex; align-items: center; gap: 7px; }
.grid-toggle { display: flex; align-items: center; gap: 6px; min-height: 36px; padding: 4px 9px; border: 1px solid var(--line); border-radius: 6px; color: var(--muted-deep); background: rgba(251, 248, 242, .58); font-size: 11px; cursor: pointer; }
.grid-toggle input { width: 14px; height: 14px; margin: 0; accent-color: var(--clay); }
.preview-color { display: flex; align-items: center; gap: 6px; min-height: 36px; padding: 4px 8px; border: 1px solid var(--line); border-radius: 6px; color: var(--muted-deep); background: rgba(251, 248, 242, .58); font-size: 11px; cursor: pointer; }
.preview-color input { width: 18px; height: 18px; padding: 0; border: 0; background: transparent; cursor: pointer; }
.position-tools { display: inline-flex; align-items: center; gap: 8px; }
.position-tools button { min-height: 25px; padding: 3px 8px; border: 1px solid var(--line-dark); border-radius: 4px; background: transparent; color: var(--muted-deep); font-size: 10px; }
.position-tools button:not(:disabled):hover { border-color: var(--clay); background: var(--clay-soft); }

.workspace-footer { display: flex; justify-content: space-between; gap: 18px; padding-top: 13px; border-top: 1px solid var(--line); color: var(--muted); font-size: 9px; letter-spacing: .12em; }

@media (max-width: 840px) {
  .app-shell { width: min(100% - 28px, 680px); padding-top: 15px; }
  .layout { display: block; padding-top: 21px; }
  .rail { min-height: auto; padding: 0 0 18px; border: 0; }
  .rail-title, .rail-note, .rail-footer { display: none; }
  .mode-nav { grid-template-columns: 1fr 1fr; margin-top: 0; }
  .mode-btn { border-color: var(--line); background: rgba(251, 248, 242, .46); }
  .mode-btn.is-active { background: var(--panel); }
  .tool-panel.is-visible { grid-template-columns: 1fr; }
  .tool-controls { order: 1; }
  .preview-column { order: 0; }
  .canvas-frame { min-height: min(82vw, 500px); }
  .grid-preview-frame, .pixel-preview-frame { min-height: 0; }
  .workspace-heading h1 { font-size: 38px; }
}

@media (max-width: 500px) {
  .app-shell { width: calc(100% - 22px); }
  .topbar { align-items: flex-start; }
  .topbar-meta { display: grid; justify-items: end; gap: 5px; }
  .brand strong { font-size: 21px; }
  .workspace-heading { align-items: start; }
  .workspace-counter { padding-top: 4px; }
  .workspace-counter strong { font-size: 22px; }
  .workspace-heading h1 { font-size: 32px; }
  .section-description { font-size: 13px; }
  .preview-foot { display: grid; gap: 3px; }
  .preview-head { align-items: flex-start; }
  .preview-toolbar { align-items: flex-end; flex-direction: column; }
  .workspace-footer { display: grid; gap: 4px; }
}
