:root {
  --paper: #f8f6f3;
  --paper-deep: #f1ece5;
  --ink: #3c332e;
  --ink-soft: rgba(60, 51, 46, 0.74);
  --coral: #ff9169;
  --coral-deep: #f67d54;
  --coral-dark: #dc6843;
  --white: #fffdf9;
  --line: rgba(255, 145, 105, 0.32);
  --line-strong: rgba(220, 104, 67, 0.52);
  --shadow-soft: rgba(255, 145, 105, 0.12);
}

*, *::before, *::after { box-sizing: border-box; }
* { margin: 0; padding: 0; }
html { background: #fff; }
body {
  min-width: 320px;
  min-height: 100vh;
  overflow-x: hidden;
  background: #fff;
  color: var(--ink);
  font-family: "Times New Roman", Georgia, serif;
}
button, input { font: inherit; }
button { cursor: pointer; }
a { color: inherit; text-decoration: none; }

.side-rail {
  position: fixed;
  z-index: 10;
  inset: 0 0 0 auto;
  width: 4.25rem;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--coral);
  color: var(--white);
  box-shadow: -12px 0 28px var(--shadow-soft);
}
.side-rail span {
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  font-size: 0.72rem;
  letter-spacing: 0.26em;
  text-transform: uppercase;
}

.page { min-height: 100vh; padding-right: 4.25rem; }
.tool-header {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: clamp(1rem, 3vw, 2.8rem);
  align-items: center;
  width: min(100%, 96rem);
  margin: 0 auto;
  padding: clamp(1.4rem, 4vw, 3.2rem) clamp(1rem, 4vw, 3.6rem) clamp(1.2rem, 3vw, 2.4rem);
  border-bottom: 1px solid var(--line);
}
.title-block { min-width: 0; grid-column: 1; grid-row: 1; }
.title-block h1 {
  color: var(--coral-dark);
  font-family: "JingHuaSong", "STSong", "SimSun", serif;
  font-size: clamp(2.5rem, 5vw, 4.8rem);
  font-weight: 400;
  line-height: 0.96;
  text-wrap: balance;
}
.back-link {
  grid-column: 2;
  grid-row: 1;
  justify-self: end;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 2.75rem;
  padding: 0.7rem 0.9rem;
  border: 1px solid var(--line-strong);
  background: rgba(255, 253, 249, 0.74);
  color: var(--coral-dark);
  font-size: 0.88rem;
  transition: background 0.2s ease, color 0.2s ease, transform 0.2s ease;
}
.back-link:hover, .back-link:focus-visible {
  background: var(--coral);
  color: var(--white);
  outline: none;
  transform: translateY(-2px);
}

.workspace {
  display: grid;
  grid-template-columns: minmax(19rem, 23rem) minmax(0, 1fr);
  width: min(100%, 96rem);
  min-height: calc(100vh - 12rem);
  margin: 0 auto;
  background: rgba(255, 253, 249, 0.7);
}
.controls {
  min-width: 0;
  border-right: 1px solid var(--line);
  background: rgba(248, 246, 243, 0.86);
}
.control-section {
  padding: 1.25rem clamp(1rem, 2vw, 1.4rem) 1.4rem;
  border-bottom: 1px solid var(--line);
}
.section-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
}
.section-heading h2 {
  color: var(--coral-dark);
  font-family: "JingHuaSong", "STSong", "SimSun", serif;
  font-size: 1.32rem;
  font-weight: 400;
}
.section-heading span,
.field-label span,
.range-label span {
  color: rgba(220, 104, 67, 0.78);
  font-size: 0.68rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}
.section-heading span { letter-spacing: 0.16em; }
.section-note {
  max-width: 68ch;
  margin: -0.35rem 0 1rem;
  color: var(--ink-soft);
  font-size: 0.86rem;
  line-height: 1.6;
  text-wrap: pretty;
}

.drop-zone {
  display: grid;
  place-items: center;
  min-height: 8rem;
  padding: 1rem;
  border: 1px dashed var(--line-strong);
  background: rgba(255, 145, 105, 0.06);
  color: var(--coral-dark);
  text-align: center;
  transition: background 0.2s ease, color 0.2s ease, border-color 0.2s ease;
}
.drop-zone.is-dragging {
  border-color: var(--coral-dark);
  background: var(--coral);
  color: var(--white);
}
.drop-zone.has-image { border-style: solid; }
.drop-zone strong { display: block; font-size: 1rem; font-weight: 400; }
.drop-zone small {
  display: block;
  margin-top: 0.42rem;
  color: var(--ink-soft);
  font-size: 0.76rem;
  line-height: 1.5;
}
.drop-zone.is-dragging small { color: var(--white); }
.file-input { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }

.range-row { margin-bottom: 1rem; }
.range-row:last-child { margin-bottom: 0; }
.range-label,
.field-label {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.58rem;
  color: var(--ink-soft);
  font-size: 0.84rem;
}
.range-label output { color: var(--coral-dark); font-variant-numeric: tabular-nums; }
input[type="range"] {
  width: 100%;
  height: 1.15rem;
  appearance: none;
  background: transparent;
  cursor: ew-resize;
  touch-action: pan-y;
  user-select: none;
  -webkit-user-select: none;
}
input[type="range"]::-webkit-slider-runnable-track { height: 2px; background: rgba(220, 104, 67, 0.42); }
input[type="range"]::-moz-range-track { height: 2px; background: rgba(220, 104, 67, 0.42); }
input[type="range"]::-webkit-slider-thumb {
  width: 0.9rem;
  height: 0.9rem;
  margin-top: -0.4rem;
  appearance: none;
  border: 1px solid var(--coral-dark);
  border-radius: 0;
  background: var(--white);
}
input[type="range"]::-moz-range-thumb {
  width: 0.9rem;
  height: 0.9rem;
  border: 1px solid var(--coral-dark);
  border-radius: 0;
  background: var(--white);
}
input[type="range"]:disabled { cursor: not-allowed; opacity: 0.36; }

.field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.7rem; }
.color-field { min-width: 0; }
.color-control {
  display: grid;
  grid-template-columns: 2.65rem minmax(0, 1fr);
  align-items: center;
  min-height: 2.8rem;
  border: 1px solid var(--line);
  background: rgba(255, 253, 249, 0.74);
}
.color-control input[type="color"] {
  width: 2.65rem;
  height: 2.65rem;
  padding: 0.25rem;
  border: 0;
  background: transparent;
}
.color-control output {
  min-width: 0;
  padding: 0 0.65rem;
  overflow: hidden;
  color: var(--ink-soft);
  font-size: 0.74rem;
  text-overflow: ellipsis;
  text-transform: uppercase;
}
.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  min-height: 2.8rem;
  margin-top: 0.7rem;
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--line);
  background: rgba(255, 253, 249, 0.74);
  color: var(--ink-soft);
  font-size: 0.84rem;
}
.toggle-row input {
  width: 1rem;
  height: 1rem;
  accent-color: var(--coral-dark);
}

.segmented { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.5rem; }
.composition-modes { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.segment {
  min-height: 2.75rem;
  padding: 0.55rem;
  border: 1px solid var(--line);
  background: rgba(255, 253, 249, 0.74);
  color: var(--ink);
  transition: background 0.18s ease, color 0.18s ease, border-color 0.18s ease;
}
.segment:hover, .segment:focus-visible { border-color: var(--coral-dark); outline: none; }
.segment[aria-pressed="true"] { border-color: var(--coral-dark); background: var(--coral); color: var(--white); }
.brush-modes { margin-bottom: 1rem; }

.direction-compass {
  position: relative;
  width: min(100%, 13rem);
  aspect-ratio: 1;
  margin: 0 auto 0.75rem;
  overflow: hidden;
  border: 1px solid var(--line);
  background: rgba(255, 253, 249, 0.76);
}
.direction-compass::before,
.direction-compass::after {
  position: absolute;
  top: 50%;
  left: 50%;
  background: rgba(220, 104, 67, 0.16);
  content: "";
  pointer-events: none;
}
.direction-compass::before { width: 100%; height: 1px; transform: translate(-50%, -50%); }
.direction-compass::after { width: 1px; height: 100%; transform: translate(-50%, -50%); }
.direction-origin {
  position: absolute;
  z-index: 2;
  top: 50%;
  left: 50%;
  width: 0.55rem;
  height: 0.55rem;
  border: 1px solid var(--coral-dark);
  background: var(--white);
  transform: translate(-50%, -50%);
}
.direction-ray {
  position: absolute;
  z-index: 3;
  top: 50%;
  left: 50%;
  width: var(--ray-length);
  height: 1.5rem;
  border: 0;
  background: transparent;
  transform: translateY(-50%) rotate(var(--ray-angle));
  transform-origin: left center;
}
.direction-ray::before {
  position: absolute;
  top: 50%;
  right: 0;
  left: 0;
  height: 1px;
  background: var(--line-strong);
  content: "";
}
.direction-ray::after {
  position: absolute;
  top: 50%;
  right: 0;
  width: 0.6rem;
  height: 0.6rem;
  border: 1px solid var(--coral-dark);
  background: var(--white);
  content: "";
  transform: translate(50%, -50%);
}
.direction-ray[aria-pressed="true"]::before { height: 2px; background: var(--coral-dark); }
.direction-ray[aria-pressed="true"]::after { background: var(--coral); }
.direction-list { display: flex; flex-wrap: wrap; gap: 0.4rem; margin-bottom: 1rem; }
.direction-chip {
  min-height: 2.25rem;
  padding: 0.42rem 0.58rem;
  border: 1px solid var(--line);
  background: rgba(255, 253, 249, 0.76);
  color: var(--ink-soft);
  font-size: 0.76rem;
  font-variant-numeric: tabular-nums;
}
.direction-chip[aria-selected="true"] { border-color: var(--coral-dark); background: var(--coral); color: var(--white); }
.direction-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 0.5rem; margin: 0.2rem 0 1rem; }
.compact-action {
  min-height: 2.65rem;
  padding: 0.55rem 0.65rem;
  border: 1px solid var(--coral-dark);
  background: var(--coral);
  color: var(--white);
  transition: background 0.18s ease, color 0.18s ease;
}
.compact-action:hover, .compact-action:focus-visible { background: var(--coral-dark); outline: none; }
.compact-action.secondary { background: transparent; color: var(--coral-dark); }
.compact-action.secondary:hover, .compact-action.secondary:focus-visible { background: rgba(255, 145, 105, 0.12); }
.compact-action:disabled { cursor: not-allowed; opacity: 0.45; }

.actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.55rem;
  padding: 1.25rem clamp(1rem, 2vw, 1.4rem) 1.5rem;
}
.actions.three { grid-template-columns: 1fr 1fr; }
.actions.three .action:first-child { grid-column: 1 / -1; }
.action {
  min-height: 3rem;
  padding: 0.7rem;
  border: 1px solid var(--coral-dark);
  background: var(--coral);
  color: var(--white);
  transition: background 0.18s ease, color 0.18s ease, transform 0.18s ease;
}
.action:hover, .action:focus-visible { background: var(--coral-dark); outline: none; transform: translateY(-2px); }
.action.secondary { background: transparent; color: var(--coral-dark); }
.action.secondary:hover, .action.secondary:focus-visible { background: rgba(255, 145, 105, 0.12); color: var(--coral-dark); }
.action:disabled { cursor: wait; opacity: 0.55; transform: none; }

.preview { display: grid; grid-template-rows: auto minmax(0, 1fr) auto; min-width: 0; }
.preview-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  min-height: 3.6rem;
  padding: 0.8rem 1.2rem;
  border-bottom: 1px solid var(--line);
  color: var(--coral-dark);
}
.preview-bar h2 {
  font-family: "JingHuaSong", "STSong", "SimSun", serif;
  font-size: 1.18rem;
  font-weight: 400;
}
.preview-tools {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.6rem;
  min-width: 0;
}
.file-meta {
  overflow: hidden;
  max-width: 55%;
  color: var(--ink-soft);
  font-size: 0.74rem;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.preview-tools .file-meta { max-width: min(24rem, 38vw); }
.compare-button {
  flex: 0 0 auto;
  min-height: 2.25rem;
  padding: 0.45rem 0.65rem;
  border: 1px solid var(--line-strong);
  background: rgba(255, 253, 249, 0.74);
  color: var(--coral-dark);
  font-family: inherit;
  font-size: 0.72rem;
  white-space: nowrap;
  transition: background 0.18s ease, color 0.18s ease;
}
.compare-button:hover,
.compare-button:focus-visible,
.compare-button.is-active {
  background: var(--coral);
  color: var(--white);
  outline: none;
}
.canvas-stage {
  position: relative;
  display: grid;
  place-items: center;
  min-height: 36rem;
  padding: clamp(1.25rem, 4vw, 3rem);
  overflow: hidden;
  background: rgba(255, 145, 105, 0.08);
}
.canvas-stage::before {
  position: absolute;
  inset: 1rem;
  border: 1px solid rgba(255, 145, 105, 0.14);
  content: "";
  pointer-events: none;
}
.canvas-frame {
  position: relative;
  z-index: 1;
  max-width: 100%;
  background: #fff;
  box-shadow: 1rem 1rem 0 rgba(220, 104, 67, 0.16);
}
.canvas-frame.transparent-preview {
  background-color: #fff;
  background-image: conic-gradient(rgba(220, 104, 67, 0.1) 25%, transparent 0 50%, rgba(220, 104, 67, 0.1) 0 75%, transparent 0);
  background-size: 1.2rem 1.2rem;
}
.canvas-frame.is-comparing { background: #fff; background-image: none; }
.canvas-frame::before, .canvas-frame::after {
  position: absolute;
  z-index: 2;
  width: 1rem;
  height: 1rem;
  border-color: var(--coral-dark);
  content: "";
  pointer-events: none;
}
.canvas-frame::before { top: -0.55rem; left: -0.55rem; border-top: 1px solid; border-left: 1px solid; }
.canvas-frame::after { right: -0.55rem; bottom: -0.55rem; border-right: 1px solid; border-bottom: 1px solid; }
.output-canvas {
  display: block;
  width: auto;
  height: auto;
  max-width: min(100%, 68rem);
  max-height: calc(100vh - 13rem);
}
.output-canvas[hidden] { display: none; }
.preview-footer { display: grid; grid-template-columns: repeat(3, 1fr); border-top: 1px solid var(--line); }
.readout {
  min-width: 0;
  padding: 0.78rem 1.2rem;
  border-right: 1px solid var(--line);
  color: var(--ink-soft);
  font-size: 0.7rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.readout:last-child { border-right: 0; }
.readout b {
  display: block;
  margin-top: 0.28rem;
  overflow: hidden;
  color: var(--coral-dark);
  font-size: 0.82rem;
  font-weight: 400;
  letter-spacing: 0;
  white-space: nowrap;
  text-overflow: ellipsis;
  text-transform: none;
}

.drop-zone:focus-visible,
input[type="range"]:focus-visible,
input[type="color"]:focus-visible,
input[type="checkbox"]:focus-visible,
.segment:focus-visible,
.direction-ray:focus-visible,
.direction-chip:focus-visible,
.compact-action:focus-visible,
.action:focus-visible,
.back-link:focus-visible {
  outline: 3px solid rgba(220, 104, 67, 0.24);
  outline-offset: 3px;
}

@media (min-width: 901px) {
  .preview {
    position: sticky;
    top: 0;
    align-self: start;
    height: 100vh;
    max-height: 100vh;
    overflow: hidden;
  }

  .preview .canvas-stage { min-height: 0; }
}

@media (max-width: 900px) {
  .side-rail { position: relative; width: 100%; height: 2.5rem; }
  .side-rail span { writing-mode: horizontal-tb; transform: none; }
  .page { padding-right: 0; }
  .tool-header { grid-template-columns: minmax(0, 1fr) auto; align-items: center; }
  .back-link { width: max-content; }
  .workspace { grid-template-columns: 1fr; }
  .preview { position: static; height: auto; max-height: none; overflow: visible; }
  .controls { border-right: 0; border-bottom: 1px solid var(--line); }
  .canvas-stage { min-height: 28rem; }
  .output-canvas { max-height: 70vh; }
}

@media (max-width: 560px) {
  .tool-header { padding: 1rem; }
  .title-block h1 { font-size: 2.05rem; line-height: 1.04; }
  .back-link { min-height: 2.45rem; padding: 0.62rem 0.72rem; font-size: 0.78rem; }
  .control-section { padding-inline: 1rem; }
  .field-grid { grid-template-columns: 1fr; }
  .canvas-stage { min-height: 22rem; padding: 1.6rem 1rem 2rem; }
  .preview-bar { padding-inline: 1rem; }
  .file-meta { max-width: 48%; }
  .preview-tools .file-meta { max-width: 42vw; }
  .compare-button { padding-inline: 0.5rem; font-size: 0.66rem; }
  .preview-footer { grid-template-columns: 1fr 1fr; }
  .readout { padding-inline: 1rem; }
  .readout:last-child { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: 0.01ms !important; }
}
