:root {
  color-scheme: light;
  --page: #f3f5f6;
  --surface: #ffffff;
  --surface-soft: #f8f9fa;
  --ink: #1e292c;
  --muted: #687579;
  --line: #dce2e3;
  --line-strong: #c5cecf;
  --primary: #087f73;
  --primary-dark: #05665e;
  --primary-soft: #dff3ef;
  --coral: #e35e4f;
  --focus: #0d8f82;
  --shadow: 0 12px 32px rgba(31, 50, 53, 0.08);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { min-width: 320px; background: var(--page); }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background: var(--page);
  font-family: Inter, "Segoe UI", "Microsoft YaHei", Arial, sans-serif;
  font-size: 14px;
  letter-spacing: 0;
}

button, input, select { font: inherit; }
button, label, select, input[type="range"] { -webkit-tap-highlight-color: transparent; }
button { color: inherit; }
button:not(:disabled), select, input, label[for] { cursor: pointer; }
button:focus-visible, input:focus-visible, select:focus-visible, label:has(input:focus-visible) {
  outline: 3px solid rgba(13, 143, 130, 0.24);
  outline-offset: 2px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.topbar {
  height: 72px;
  padding: 0 28px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  color: var(--ink);
  text-decoration: none;
}

.empty-grid {
  display: grid;
  grid-template-columns: repeat(3, 8px);
  grid-template-rows: repeat(3, 8px);
  gap: 2px;
}

.empty-grid i { display: block; background: var(--primary); }
.empty-grid i:nth-child(2), .empty-grid i:nth-child(6) { background: var(--coral); }
.empty-grid i:nth-child(4), .empty-grid i:nth-child(8) { background: #f0b83f; }

.brand-logo { width: 32px; height: 32px; display: block; object-fit: contain; image-rendering: pixelated; }
.brand h1 { margin: 0; font-size: 17px; font-weight: 700; line-height: 1.15; }
.brand small { display: block; margin-top: 2px; color: var(--muted); font-size: 9px; font-weight: 700; letter-spacing: 1.3px; }

.privacy-note { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; }
.status-dot { width: 7px; height: 7px; border-radius: 50%; background: #36a56d; box-shadow: 0 0 0 4px rgba(54, 165, 109, 0.12); }

.app-shell {
  width: 100%;
  display: grid;
  grid-template-columns: 310px minmax(0, 1fr);
  min-height: calc(100vh - 72px);
}
body.palette-panel-open .app-shell { width: calc(100% - 376px); }

.control-panel {
  background: var(--surface);
  border-right: 1px solid var(--line);
}

.control-section { padding: 22px 24px; border-bottom: 1px solid var(--line); }
.control-section h2, .materials-section h2 { margin: 0; font-size: 14px; font-weight: 700; }
.section-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.section-heading output { color: var(--muted); font-variant-numeric: tabular-nums; }

.icon-button {
  width: 28px;
  height: 28px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--muted);
  font-size: 22px;
  line-height: 1;
}
.icon-button:hover { background: var(--surface-soft); color: var(--ink); }

.drop-zone {
  min-height: 126px;
  padding: 18px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 7px;
  text-align: center;
  background: var(--surface-soft);
  border: 1px dashed var(--line-strong);
  border-radius: 6px;
  transition: border-color 160ms ease, background 160ms ease;
}
.drop-zone:hover, .drop-zone.is-dragging { border-color: var(--primary); background: var(--primary-soft); }
.drop-zone.has-image { border-style: solid; }
.upload-symbol {
  width: 30px;
  height: 30px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  color: var(--surface);
  background: var(--primary);
  font-size: 22px;
  line-height: 1;
}
.drop-zone strong { max-width: 230px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.drop-zone small { color: var(--muted); }
.text-button { display: block; margin: 11px auto 0; padding: 3px; border: 0; background: transparent; color: var(--primary); font-weight: 600; }
.starter-actions { display: flex; justify-content: center; gap: 20px; }
.autosave-bar { min-height: 24px; margin-top: 10px; display: flex; align-items: center; justify-content: center; gap: 12px; color: var(--muted); font-size: 11px; }
.autosave-bar .text-button { margin: 0; font-size: 11px; }
.autosave-bar.is-saved > span::before { content: ""; width: 6px; height: 6px; margin-right: 6px; display: inline-block; border-radius: 50%; background: #36a56d; vertical-align: 1px; }
.autosave-bar.is-error > span { color: #8f2e27; }

.field-label { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 13px; }
.field-label strong { font-size: 13px; font-weight: 700; font-variant-numeric: tabular-nums; }
.range { width: 100%; height: 4px; margin: 15px 0 4px; accent-color: var(--primary); }
.range-ticks { display: flex; justify-content: space-between; color: var(--muted); font-size: 11px; }
.select-label { margin-top: 18px; }
select {
  min-width: 112px;
  height: 36px;
  padding: 0 30px 0 10px;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: 5px;
}

.segmented-control { display: grid; grid-template-columns: 1fr 1fr; gap: 0; margin: 14px 0 12px; padding: 0; border: 1px solid var(--line-strong); border-radius: 5px; overflow: hidden; }
.segmented-control label { position: relative; }
.segmented-control label + label { border-left: 1px solid var(--line-strong); }
.segmented-control input { position: absolute; opacity: 0; pointer-events: none; }
.segmented-control span { min-height: 36px; display: grid; place-items: center; color: var(--muted); background: var(--surface); }
.segmented-control input:checked + span { color: var(--surface); background: var(--primary); font-weight: 700; }

.switch-row { min-height: 42px; display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.switch { position: relative; width: 38px; height: 22px; }
.switch input { position: absolute; opacity: 0; }
.switch i { position: absolute; inset: 0; border-radius: 12px; background: #c8d0d1; transition: background 160ms ease; }
.switch i::after { content: ""; position: absolute; width: 16px; height: 16px; top: 3px; left: 3px; border-radius: 50%; background: var(--surface); box-shadow: 0 1px 3px rgba(30, 41, 44, 0.22); transition: transform 160ms ease; }
.switch input:checked + i { background: var(--primary); }
.switch input:checked + i::after { transform: translateX(16px); }

.summary-section { border-bottom: 0; }
.stats-row { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; margin-top: 14px; }
.stats-row div { min-width: 0; }
.stats-row strong { display: block; font-size: 24px; line-height: 1.1; font-variant-numeric: tabular-nums; }
.stats-row span { display: block; margin-top: 5px; color: var(--muted); font-size: 12px; }

.workspace { min-width: 0; background: var(--page); }
.workspace-toolbar { min-height: 64px; padding: 12px 24px; display: flex; align-items: center; justify-content: space-between; gap: 18px; background: var(--surface); border-bottom: 1px solid var(--line); }
.view-tabs { display: flex; align-self: stretch; }
.view-tabs button { position: relative; padding: 0 14px; border: 0; background: transparent; color: var(--muted); font-weight: 600; }
.view-tabs button[aria-selected="true"] { color: var(--ink); }
.view-tabs button[aria-selected="true"]::after { content: ""; position: absolute; left: 14px; right: 14px; bottom: -13px; height: 3px; background: var(--primary); }
.view-tabs button:disabled { cursor: not-allowed; opacity: 0.42; }

.toolbar-actions { display: flex; align-items: center; justify-content: flex-end; gap: 10px; }
.toolbar-icon-button { flex: 0 0 38px; width: 38px; height: 38px; border: 1px solid var(--line-strong); font-size: 24px; }
.toolbar-icon-button:hover:not(:disabled) { border-color: var(--primary); color: var(--primary); }
.zoom-control { display: flex; align-items: center; gap: 8px; margin-right: 4px; color: var(--muted); }
.zoom-control input { width: 92px; accent-color: var(--primary); }
.zoom-control output { width: 40px; font-variant-numeric: tabular-nums; }
.primary-button, .secondary-button {
  min-height: 38px;
  padding: 0 15px;
  border-radius: 5px;
  font-weight: 700;
  transition: background 150ms ease, border-color 150ms ease;
}
.primary-button { color: var(--surface); background: var(--primary); border: 1px solid var(--primary); }
.primary-button:hover:not(:disabled) { background: var(--primary-dark); border-color: var(--primary-dark); }
.secondary-button { color: var(--ink); background: var(--surface); border: 1px solid var(--line-strong); }
.secondary-button:hover:not(:disabled) { border-color: var(--primary); color: var(--primary); }
button:disabled { cursor: not-allowed; opacity: 0.45; }

.error-banner { margin: 16px 24px 0; padding: 11px 14px; color: #8f2e27; background: #fde9e7; border: 1px solid #f0b8b3; border-radius: 5px; }

.preview-stage {
  height: max(520px, calc(100vh - 184px));
  min-height: 520px;
  margin: 24px;
  display: grid;
  place-items: center;
  overflow: auto;
  background-color: #e9edef;
  background-image: linear-gradient(45deg, rgba(118, 134, 139, 0.08) 25%, transparent 25%), linear-gradient(-45deg, rgba(118, 134, 139, 0.08) 25%, transparent 25%), linear-gradient(45deg, transparent 75%, rgba(118, 134, 139, 0.08) 75%), linear-gradient(-45deg, transparent 75%, rgba(118, 134, 139, 0.08) 75%);
  background-size: 24px 24px;
  background-position: 0 0, 0 12px, 12px -12px, -12px 0;
  border: 1px solid var(--line);
  border-radius: 6px;
}
.preview-stage canvas { display: block; margin: auto; max-width: none; background: var(--surface); box-shadow: var(--shadow); image-rendering: pixelated; }
.preview-stage canvas.is-editable { cursor: crosshair; touch-action: none; user-select: none; }
.preview-stage canvas.is-panning { cursor: grabbing; }
.empty-state { display: flex; flex-direction: column; align-items: center; gap: 14px; color: var(--muted); }
.empty-grid { grid-template-columns: repeat(3, 20px); grid-template-rows: repeat(3, 20px); gap: 4px; opacity: 0.5; }
.empty-state strong { font-size: 15px; }

.materials-section { margin: 0 24px 32px; padding-top: 4px; }
.materials-heading { min-height: 54px; display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; border-bottom: 1px solid var(--line-strong); }
.materials-heading p { margin: 5px 0 0; color: var(--muted); font-size: 12px; }
.materials-heading > span { color: var(--muted); font-variant-numeric: tabular-nums; }
.materials-empty { padding: 30px 0; color: var(--muted); text-align: center; border-bottom: 1px solid var(--line); }
.materials-list { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); column-gap: 28px; }
.material-row { min-height: 54px; display: grid; grid-template-columns: 28px minmax(0, 1fr) auto; align-items: center; gap: 11px; border-bottom: 1px solid var(--line); }
.swatch { width: 24px; height: 24px; border: 1px solid rgba(31, 41, 44, 0.16); border-radius: 3px; }
.material-name { min-width: 0; }
.material-name strong { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.material-name small { display: block; margin-top: 2px; color: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.material-count { font-weight: 700; font-variant-numeric: tabular-nums; }
.material-count small { margin-left: 3px; color: var(--muted); font-weight: 400; }

.app-dialog {
  width: min(720px, calc(100% - 32px));
  max-height: calc(100vh - 40px);
  padding: 0;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  box-shadow: 0 24px 64px rgba(20, 35, 38, 0.22);
}
.app-dialog::backdrop { background: rgba(22, 32, 34, 0.46); }
.dialog-heading { min-height: 70px; padding: 18px 20px; display: flex; align-items: flex-start; justify-content: space-between; gap: 18px; border-bottom: 1px solid var(--line); }
.dialog-heading h2 { margin: 0; font-size: 16px; }
.dialog-heading p { margin: 5px 0 0; color: var(--muted); font-size: 12px; }
.current-cell-color { min-height: 43px; padding: 10px 20px 0; display: flex; align-items: center; gap: 9px; color: var(--muted); }
.current-cell-color .swatch { flex: 0 0 auto; }
.current-cell-color strong { color: var(--ink); }
.paint-tool-status { margin: 4px 16px 0; padding: 9px 10px; display: flex; align-items: center; gap: 8px; color: var(--muted); background: var(--surface-soft); border: 1px solid var(--line); border-radius: 5px; }
.paint-tool-status strong { color: var(--ink); }
.paint-tool-status .swatch { flex: 0 0 auto; width: 20px; height: 20px; }
.palette-controls { padding: 12px 20px; display: grid; grid-template-columns: minmax(0, 1fr) 148px auto; align-items: end; gap: 10px; border-bottom: 1px solid var(--line); }
.palette-controls label, .blank-size-fields label { display: grid; gap: 6px; color: var(--muted); font-size: 12px; }
.palette-controls input, .palette-controls select, .blank-size-fields input {
  width: 100%;
  height: 38px;
  padding: 0 10px;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: 5px;
}
.palette-grid { max-height: 52vh; padding: 14px 20px 20px; display: grid; grid-template-columns: repeat(auto-fill, minmax(62px, 1fr)); gap: 8px; overflow-y: auto; }
.palette-option { min-width: 0; min-height: 78px; padding: 6px 4px; display: grid; justify-items: center; align-content: center; gap: 3px; background: var(--surface); border: 1px solid var(--line); border-radius: 5px; }
.palette-option:hover { border-color: var(--primary); background: var(--primary-soft); }
.palette-option[aria-selected="true"] { border-color: var(--primary); box-shadow: inset 0 0 0 1px var(--primary); }
.palette-option-swatch { width: 30px; height: 30px; border: 1px solid rgba(31, 41, 44, 0.18); border-radius: 3px; }
.palette-option strong { font-size: 11px; }
.palette-option small { max-width: 100%; overflow: hidden; color: var(--muted); font-size: 10px; text-overflow: ellipsis; white-space: nowrap; }
.palette-no-results { margin: 0; padding: 30px 20px; color: var(--muted); text-align: center; }
.color-dialog {
  position: fixed;
  z-index: 20;
  inset: 84px 16px 16px auto;
  width: 344px;
  height: calc(100vh - 100px);
  max-height: none;
  margin: 0;
  overflow: hidden;
  box-shadow: 0 16px 48px rgba(20, 35, 38, 0.2);
}
.color-dialog[open] { display: flex; flex-direction: column; }
.color-dialog .dialog-heading { min-height: 62px; padding: 14px 16px; }
.color-dialog .current-cell-color { min-height: 40px; padding: 9px 16px 0; }
.color-dialog .palette-controls { padding: 10px 16px; grid-template-columns: minmax(0, 1fr) 104px; }
.color-dialog .palette-controls label:first-child { grid-column: 1 / -1; }
.color-dialog .palette-controls .secondary-button { width: 100%; }
.color-dialog .palette-controls .secondary-button[aria-pressed="true"] { color: var(--primary-dark); background: var(--primary-soft); border-color: var(--primary); }
.color-dialog .palette-grid { flex: 1; min-height: 0; max-height: none; padding: 12px 16px 16px; grid-template-columns: repeat(3, minmax(0, 1fr)); grid-auto-rows: 78px; align-content: start; }
.color-dialog::backdrop { display: none; }
.blank-dialog { width: min(460px, calc(100% - 32px)); }
.blank-size-fields { padding: 22px 20px; display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.dialog-actions { padding: 14px 20px 20px; display: flex; justify-content: flex-end; gap: 10px; }

@media (max-width: 1040px) {
  body.palette-panel-open .app-shell { width: calc(100% - 336px); }
  .color-dialog { width: 304px; }
  .app-shell { grid-template-columns: 280px minmax(0, 1fr); }
  .control-section { padding-left: 20px; padding-right: 20px; }
  .workspace-toolbar { align-items: stretch; flex-direction: column; gap: 8px; }
  .view-tabs { min-height: 38px; }
  .view-tabs button[aria-selected="true"]::after { bottom: -9px; }
  .toolbar-actions { justify-content: space-between; flex-wrap: wrap; }
  .zoom-control { margin-right: auto; }
}

@media (max-width: 760px) {
  body.palette-panel-open { padding-bottom: 48vh; }
  body.palette-panel-open .app-shell { width: 100%; }
  .topbar { height: 64px; padding: 0 16px; }
  .privacy-note { font-size: 0; }
  .app-shell { display: block; }
  .control-panel { border-right: 0; border-bottom: 1px solid var(--line); }
  .control-section { padding: 18px 16px; }
  .upload-section { display: grid; grid-template-columns: 1fr; }
  .drop-zone { min-height: 112px; }
  .workspace-toolbar { padding: 10px 16px 12px; }
  .toolbar-actions { display: grid; grid-template-columns: 1fr 1fr; }
  .zoom-control { grid-column: 1 / -1; width: 100%; }
  .zoom-control input { flex: 1; }
  .primary-button, .secondary-button { width: 100%; padding: 0 10px; }
  .toolbar-icon-button { width: 38px; grid-row: 2; justify-self: start; }
  .toolbar-actions .secondary-button { grid-column: 1; }
  .toolbar-actions .primary-button { grid-column: 2; }
  .preview-stage { height: min(620px, 68vh); min-height: 420px; margin: 16px; place-items: center; }
  .materials-section { margin: 0 16px 24px; }
  .materials-list { grid-template-columns: 1fr; }
  .palette-controls { grid-template-columns: 1fr 1fr; }
  .palette-controls label:first-child { grid-column: 1 / -1; }
  .palette-controls .secondary-button { width: 100%; }
  .palette-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .color-dialog {
    inset: auto 0 0;
    width: 100%;
    height: 48vh;
    max-height: 48vh;
    border-width: 1px 0 0;
    border-radius: 6px 6px 0 0;
  }
  .color-dialog .dialog-heading { min-height: 54px; padding: 10px 16px; }
  .color-dialog .current-cell-color { display: none; }
  .color-dialog .paint-tool-status { margin-top: 8px; }
  .color-dialog .palette-controls { grid-template-columns: minmax(0, 1fr) 100px; }
  .color-dialog .palette-controls label:first-child { grid-column: auto; }
  .color-dialog .palette-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: 0.01ms !important; }
}
