:root {
  color-scheme: light;
  --ink: #25201e;
  --muted: #746964;
  --paper: #fffaf7;
  --panel: #ffffff;
  --line: #eaded7;
  --accent: #f1641e;
  --accent-dark: #d45218;
  --accent-soft: #fff0e8;
  --blue: #f1641e;
  --green: #47705e;
  --danger: #bd3e2e;
  --shadow: 0 12px 32px rgba(72, 46, 33, .08);
  font-family: Inter, "Segoe UI", "Microsoft YaHei", system-ui, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  color: var(--ink);
  background:
    radial-gradient(circle at 8% 0%, rgba(241, 100, 30, .09), transparent 30rem),
    radial-gradient(circle at 92% 100%, rgba(255, 190, 148, .11), transparent 34rem),
    var(--paper);
}

button, input, select { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .45; }

.app-header {
  min-height: 76px;
  padding: 14px 22px;
  display: flex;
  align-items: center;
  gap: 12px;
  background: rgba(255, 255, 255, .96);
  color: var(--ink);
  border-bottom: 1px solid #eee3dc;
  box-shadow: 0 4px 18px rgba(63, 42, 31, .07);
}

.brand-mark {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  background: var(--accent);
  color: white;
  box-shadow: 0 6px 15px rgba(241, 100, 30, .24);
  font-size: 21px;
  font-weight: 800;
}

.app-header h1 { margin: 0; font-size: 1.25rem; line-height: 1.2; }
.app-header p { margin: 4px 0 0; color: var(--muted); font-size: .82rem; }
.header-meta { order: 3; margin-left: auto; display: flex; align-items: center; gap: 16px; }
.admin-link { padding: 6px 9px; color: var(--accent-dark); background: #fff4ee; border: 1px solid #f1c3ab; border-radius: 7px; font-size: .72rem; font-weight: 700; text-decoration: none; white-space: nowrap; }
.admin-link:hover { color: white; background: var(--accent); border-color: var(--accent); }
.mobile-admin-link { display: none; }
.privacy-note { color: var(--muted); font-size: .84rem; display: flex; align-items: center; gap: 8px; }
.privacy-note span { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 0 4px rgba(241,100,30,.12); }
.studio-signature { padding-left: 16px; border-left: 1px solid #eaded7; color: var(--accent-dark); font: 700 .72rem/1.2 "Segoe UI", sans-serif; letter-spacing: .15em; white-space: nowrap; }
.history-controls { order: 4; width: 198px; flex: 0 0 198px; margin-left: 4px; display: grid; grid-template-columns: 96px 96px; gap: 6px; align-items: center; }
.history-controls button { width: 96px; min-height: 34px; padding: 0 10px; color: var(--accent-dark); background: #fff8f4; border: 1px solid #f3c5ad; border-radius: 8px; font-size: .78rem; font-weight: 700; }
.history-controls button:not(:disabled):hover { color: white; background: var(--accent); border-color: var(--accent); }
.history-controls button:disabled { opacity: .36; }
.history-controls span { grid-column: 1 / -1; width: 198px; min-height: 1em; overflow: hidden; color: var(--muted); font-size: .66rem; line-height: 1; text-align: center; text-overflow: ellipsis; white-space: nowrap; }

.workspace {
  width: 100%;
  min-height: calc(100vh - 79px);
  display: grid;
  grid-template-columns: 300px minmax(430px, 1fr) 410px;
  gap: 14px;
  padding: 14px;
}

.panel, .stage-panel {
  background: var(--panel);
  border: 1px solid #eee3dd;
  border-radius: 14px;
  box-shadow: var(--shadow);
}

.controls-panel { padding: 0 18px; overflow-y: auto; }
.step-section { padding: 19px 0; border-bottom: 1px solid var(--line); }
.step-section:last-child { border-bottom: 0; }
.section-title { display: flex; align-items: center; gap: 9px; margin-bottom: 14px; }
.section-title > span { width: 24px; height: 24px; border-radius: 7px; display: grid; place-items: center; background: var(--ink); color: white; font-size: .78rem; font-weight: 700; }
.section-title h2 { margin: 0; font-size: .98rem; }

.upload-zone {
  min-height: 76px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  border: 1.5px dashed #e0b39c;
  border-radius: 10px;
  background: #fffdfb;
  transition: border .2s, background .2s;
  cursor: pointer;
}
.upload-zone:hover { border-color: var(--accent); background: var(--accent-soft); }
.upload-zone input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.upload-zone strong { font-size: .9rem; }
.upload-zone small { color: var(--muted); font-size: .74rem; }
.upload-zone:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(241,100,30,.13); outline: none; }
.upload-zone kbd { margin-left: 5px; padding: 2px 6px; color: var(--accent-dark); background: var(--accent-soft); border: 1px solid #f3c4ab; border-radius: 5px; font: 700 .7rem/1.3 inherit; }

.field-group { margin-top: 14px; }
.two-cols { display: grid; grid-template-columns: 1fr 104px; gap: 9px; }
label > span, .minimum-field > span { display: block; margin-bottom: 6px; color: var(--muted); font-size: .78rem; }
input, select {
  width: 100%;
  height: 40px;
  padding: 0 10px;
  color: var(--ink);
  background: white;
  border: 1px solid #dcd0ca;
  border-radius: 8px;
  outline: none;
}
input:focus, select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(241,100,30,.13); }
.scale-readout { margin: 10px 0; padding: 8px 10px; border-radius: 7px; color: #a74418; background: #fff3ec; border: 1px solid #ffd4be; font-size: .76rem; }
.unit-converter { margin-top: 12px; padding: 11px; border: 1px solid var(--line); border-radius: 9px; background: #fffdfb; }
.unit-converter > strong { display: block; margin-bottom: 8px; font-size: .8rem; }
.display-unit-field { display: grid; grid-template-columns: 1fr 118px; gap: 8px; align-items: center; }
.display-unit-field > span { margin: 0; color: var(--ink); font-size: .76rem; }
.display-unit-field select { height: 34px; }
.display-unit-help { display: block; margin: 7px 0 0; color: var(--muted); font-size: .68rem; line-height: 1.45; }
.converter-title { display: block; margin-bottom: 6px; color: var(--muted); font-size: .72rem; font-weight: 650; }
.converter-inputs { display: grid; grid-template-columns: 1fr 74px; gap: 7px; }
.converter-inputs input, .converter-inputs select { height: 34px; }
.unit-converter output { display: block; margin-top: 8px; color: var(--green); font-size: .74rem; line-height: 1.45; }

.primary-button, .secondary-button, .text-button {
  min-height: 38px;
  border-radius: 8px;
  font-weight: 650;
}
.primary-button { color: #fff; background: var(--accent); border: 1px solid var(--accent); box-shadow: 0 5px 13px rgba(241,100,30,.18); }
.primary-button:hover { background: #dd5819; border-color: #dd5819; }
.secondary-button { width: 100%; color: var(--ink); background: white; border: 1px solid #dcd0ca; }
.secondary-button:hover { border-color: #e39872; background: #fff8f4; }
.reference-actions { display: grid; grid-template-columns: 1fr; gap: 7px; }
.reference-actions.has-secondary { grid-template-columns: 1fr 106px; }
.cancel-button { min-height: 38px; color: var(--accent-dark); background: #fff2eb; border: 1px solid #f0b596; border-radius: 8px; font-weight: 650; }
.cancel-button:hover { color: #ad3f10; background: #ffe5d8; border-color: #e98c5e; }
.cancel-button[hidden] { display: none; }
.text-button { padding: 6px 0; color: var(--accent-dark); background: transparent; border: 0; font-size: .82rem; }
.text-button:hover { color: #ad3f10; }
.text-button.danger { color: var(--danger); }
.upload-zone + .text-button { width: 100%; margin-top: 5px; }
.button-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.button-row.compact { margin-top: 6px; }
.helper { margin: 10px 0 0; color: var(--muted); font-size: .76rem; line-height: 1.55; }
.helper.no-top { margin-top: -5px; margin-bottom: 10px; }

.material-list { display: flex; flex-direction: column; gap: 7px; }
.material-row {
  display: grid;
  grid-template-columns: 22px 1fr 78px 24px;
  gap: 6px;
  align-items: center;
  padding: 7px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: #fffdfb;
}
.material-row.active { border-color: var(--accent); background: var(--accent-soft); }
.material-row input[type="radio"] { width: 16px; height: 16px; accent-color: var(--accent-dark); }
.material-row input[type="text"], .material-row input[type="number"] { height: 32px; padding: 0 7px; font-size: .8rem; }
.material-row .remove-material { border: 0; background: none; color: #a58a7b; padding: 0; font-size: 1.05rem; }
.minimum-field { display: block; margin-top: 10px; }
.minimum-field > div { display: flex; align-items: center; }
.minimum-field input { border-radius: 8px 0 0 8px; }
.minimum-field em { height: 40px; padding: 0 10px; display: flex; align-items: center; color: var(--muted); background: #f7f3f1; border: 1px solid #dcd0ca; border-left: 0; border-radius: 0 8px 8px 0; font-size: .8rem; font-style: normal; }
.area-rule-card { margin-top: 12px; padding: 11px; border: 1px solid #f1c3ab; border-radius: 10px; background: linear-gradient(145deg, #fffdfb, #fff3ec); }
.rule-toggle { display: flex; align-items: flex-start; gap: 8px; cursor: pointer; }
.rule-toggle > input { width: 18px; height: 18px; margin-top: 2px; accent-color: var(--accent-dark); }
.rule-toggle > span { flex: 1; }
.rule-toggle strong { display: block; font-size: .82rem; }
.rule-toggle small { display: block; margin-top: 3px; color: var(--muted); font-size: .7rem; line-height: 1.4; }
.area-rule-fields { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 11px; opacity: .48; pointer-events: none; }
.area-rule-card.enabled .area-rule-fields { opacity: 1; pointer-events: auto; }
.area-rule-fields label > span { font-size: .7rem; }
.area-rule-fields label > div { display: flex; }
.area-rule-fields input { height: 35px; border-radius: 7px 0 0 7px; }
.area-rule-fields em { height: 35px; padding: 0 7px; display: flex; align-items: center; white-space: nowrap; color: var(--muted); background: #f7f3f1; border: 1px solid #dcd0ca; border-left: 0; border-radius: 0 7px 7px 0; font-size: .68rem; font-style: normal; }
.area-rule-card > p { margin: 9px 0 0; color: var(--muted); font-size: .68rem; line-height: 1.45; }

.stage-panel { min-width: 0; display: flex; flex-direction: column; overflow: hidden; }
#desktopStageAnchor, #mobileStageAnchor { display: none; }
.stage-toolbar { min-height: 58px; padding: 11px 16px; display: flex; align-items: center; justify-content: space-between; gap: 16px; border-bottom: 1px solid var(--line); }
.stage-toolbar strong { display: block; max-width: 300px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font-size: .9rem; }
.stage-toolbar > div > span { color: var(--muted); font-size: .74rem; }
.legend { display: flex; gap: 12px; white-space: nowrap; }
.legend span { display: flex; align-items: center; gap: 5px; color: var(--muted); font-size: .72rem; }
.legend i { width: 15px; height: 3px; display: inline-block; border-radius: 2px; }
.legend-reference { background: var(--blue); }
.legend-box { background: var(--accent); }

.canvas-shell {
  position: relative;
  flex: 1;
  min-height: 500px;
  display: grid;
  place-items: start center;
  overflow: auto;
  background-color: #f1ebe7;
  background-image: linear-gradient(45deg, rgba(255,255,255,.7) 25%, transparent 25%), linear-gradient(-45deg, rgba(255,255,255,.7) 25%, transparent 25%), linear-gradient(45deg, transparent 75%, rgba(255,255,255,.7) 75%), linear-gradient(-45deg, transparent 75%, rgba(255,255,255,.7) 75%);
  background-size: 18px 18px;
  background-position: 0 0, 0 9px, 9px -9px, -9px 0;
}
#editorCanvas { display: block; max-width: 100%; background: white; box-shadow: 0 5px 24px rgba(60,42,33,.14); touch-action: none; }
#editorCanvas[hidden] { display: none !important; }
.empty-state { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 7px; text-align: center; color: var(--muted); }
.empty-state.hidden { display: none; }
.empty-state strong { color: var(--ink); font-size: 1rem; }
.empty-state span { padding: 0 20px; font-size: .8rem; }
.empty-icon { width: 62px; height: 62px; margin-bottom: 4px; display: grid; place-items: center; border: 2px solid #c99068; border-radius: 16px; font-family: Georgia, serif; font-size: 1.35rem; }
.stage-status { min-height: 42px; padding: 12px 16px; color: var(--muted); background: #fffdfb; border-top: 1px solid var(--line); font-size: .76rem; }

.result-panel { min-width: 0; padding: 18px; overflow: hidden; display: flex; flex-direction: column; }
.result-heading { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }
.result-heading p { margin: 0 0 5px; color: var(--muted); font-size: .75rem; }
.result-heading h2 { margin: 0; font-size: 1.12rem; }
.result-heading > span { padding: 6px 9px; color: var(--accent-dark); background: var(--accent-soft); border-radius: 7px; font-size: .76rem; font-weight: 700; }
.summary-grid { display: grid; grid-template-columns: 1fr 1.35fr; gap: 8px; margin: 18px 0; }
.summary-grid > div { padding: 12px; border: 1px solid var(--line); border-radius: 10px; background: #fffaf7; }
.summary-grid span { display: block; margin-bottom: 5px; color: var(--muted); font-size: .72rem; }
.summary-grid strong { font-size: 1rem; }
.selection-toolbar { margin-bottom: 10px; padding: 9px; display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: center; border: 1px solid #efd6c9; border-radius: 9px; background: #fffaf7; }
.selection-toolbar > div { display: flex; align-items: center; gap: 7px; }
.selection-toolbar strong { font-size: .75rem; }
.clear-selection { padding: 0; color: var(--accent-dark); background: transparent; border: 0; font-size: .7rem; }
.selected-only-toggle { display: flex; align-items: center; gap: 5px; cursor: pointer; }
.selected-only-toggle input, .select-all-label input { width: 15px; height: 15px; accent-color: var(--accent-dark); }
.selected-only-toggle span { margin: 0; color: var(--ink); font-size: .72rem; }
.material-assignment { grid-column: 1 / -1; display: grid !important; grid-template-columns: 1fr 82px; gap: 6px !important; }
.material-assignment select { height: 32px; min-width: 0; padding: 0 7px; font-size: .72rem; }
.material-assignment button { min-height: 32px; color: white; background: var(--accent-dark); border: 1px solid var(--accent-dark); border-radius: 7px; font-size: .72rem; font-weight: 650; }
.material-assignment button:hover { background: #bd4714; }
.delete-selected { grid-column: 1 / -1; min-height: 32px; color: #ad421d; background: #fff3ed; border: 1px solid #efb596; border-radius: 7px; font-size: .74rem; font-weight: 650; }
.delete-selected:hover { background: #ffe7da; border-color: #e58959; }
.select-all-label { display: inline-flex; align-items: center; gap: 5px; color: var(--muted); cursor: pointer; }
.table-wrap { flex: 1; min-width: 0; min-height: 160px; max-height: calc(100vh - 390px); overflow-x: hidden; overflow-y: auto; scrollbar-gutter: stable; border: 1px solid var(--line); border-radius: 10px; }
table { width: 100%; min-width: 0; table-layout: fixed; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th:nth-child(1) { width: 34%; }
th:nth-child(2) { width: 22%; }
th:nth-child(3) { width: 28%; }
th:nth-child(4) { width: 16%; }
th { position: sticky; top: 0; z-index: 1; padding: 9px 4px; text-align: right; color: var(--muted); background: #f8f3f0; border-bottom: 1px solid var(--line); font-size: .7rem; font-weight: 650; }
th:first-child, td:first-child { text-align: left; }
td { padding: 9px 4px; text-align: right; border-bottom: 1px solid #f0e7e2; font-size: .72rem; }
td:nth-child(2), td:nth-child(3), td:nth-child(4) { white-space: nowrap; }
td .method-area { display: inline-block; padding: 2px 5px; color: #a74418; background: #fff0e8; border-radius: 4px; font-size: .68rem; white-space: nowrap; }
td .method-cm { color: var(--muted); font-size: .7rem; white-space: nowrap; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr[data-box-id] { cursor: pointer; }
tbody tr[data-box-id]:hover, tbody tr.selected { background: var(--accent-soft); }
tbody tr.multi-selected { box-shadow: inset 3px 0 0 var(--accent); background: #fff4ee; }
tbody tr.excluded td:not(:first-child) { opacity: .38; }
td .box-cell { display: flex; align-items: center; gap: 5px; }
td .box-cell input[type="checkbox"] { width: 15px; height: 15px; flex: 0 0 auto; accent-color: var(--accent-dark); }
td .box-info { min-width: 0; display: flex; flex-direction: column; align-items: flex-start; gap: 2px; }
td .box-material { max-width: 82px; overflow: hidden; color: #b54b1c; font-size: .62rem; white-space: nowrap; text-overflow: ellipsis; }
td input { width: 48px; height: 27px; padding: 0 5px; font-size: .74rem; }
.empty-row td { padding: 32px 8px; text-align: center !important; color: var(--muted); }
.total-card { margin-top: 14px; padding: 16px; display: flex; align-items: center; justify-content: space-between; gap: 12px; color: white; background: linear-gradient(125deg, #f1641e, #df5618); border: 1px solid #d95117; border-radius: 11px; box-shadow: 0 10px 24px rgba(241,100,30,.2); }
.total-card span { display: block; margin-bottom: 5px; font-size: .78rem; }
.total-card small { color: #ffe1d1; font-size: .7rem; }
.total-card strong { color: white; font-size: 1.55rem; font-variant-numeric: tabular-nums; }
.result-footnote { margin: 10px 2px 0; color: var(--muted); font-size: .72rem; line-height: 1.5; }

.toast { position: fixed; left: 50%; bottom: 24px; z-index: 20; padding: 10px 15px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none; color: white; background: rgba(42,35,32,.96); border: 1px solid rgba(241,100,30,.32); border-radius: 8px; box-shadow: var(--shadow); font-size: .82rem; transition: .2s ease; }
.toast.show { transform: translate(-50%, 0); opacity: 1; }

@media (max-width: 1240px) {
  .workspace { grid-template-columns: 292px minmax(400px, 1fr); }
  .result-panel { grid-column: 1 / -1; }
  .table-wrap { max-height: 340px; }
}

@media (max-width: 1020px) and (min-width: 761px) {
  .privacy-note { display: none; }
}

@media (max-width: 760px) {
  .app-header { padding: 12px 14px; }
  .app-header > div:nth-child(2) p { display: none; }
  .history-controls { order: 3; width: 174px; flex-basis: 174px; margin-left: auto; grid-template-columns: 84px 84px; }
  .history-controls span { display: none; }
  .history-controls button { width: 84px; min-height: 32px; padding: 0 8px; }
  .privacy-note { display: none; }
  .header-meta { display: none; }
  .mobile-admin-link { margin-left: auto; padding: 5px 8px; display: inline-flex; align-items: center; color: var(--accent-dark); background: #fff4ee; border: 1px solid #efbea5; border-radius: 7px; font-size: .7rem; font-weight: 700; text-decoration: none; white-space: nowrap; }
  .studio-signature { padding-left: 0; border-left: 0; font-size: .64rem; }
  .workspace { display: flex; flex-direction: column; padding: 9px; }
  .controls-panel { overflow: visible; }
  .controls-panel .stage-panel { width: 100%; min-height: 330px; margin: 10px 0 14px; border-radius: 10px; box-shadow: 0 8px 20px rgba(72,46,33,.08); }
  .controls-panel .canvas-shell { min-height: 250px; }
  .controls-panel .stage-toolbar { min-height: 48px; padding: 9px 11px; }
  .controls-panel .stage-status { min-height: 36px; padding: 9px 11px; }
  .stage-toolbar { align-items: flex-start; flex-direction: column; }
  .legend { align-self: stretch; }
  .result-panel { min-height: 520px; }
  .table-wrap { max-height: 320px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
}
