:root {
  --bg: #f6f4ef;
  --paper: #fffdf9;
  --ink: #1d1b18;
  --ink-2: #57514a;
  --muted: #8a8278;
  --line: #e2dcd2;
  --line-2: #cfc6b8;
  --accent: #b5532a;
  --accent-ink: #fff;
  --accent-soft: #f6e5dc;
  --ok: #2f6b4a;
  --ok-soft: #e3efe7;
  --warn: #9a5b00;
  --warn-soft: #fbefd9;
  --wood: #e7d3b3;
  --radius: 6px;
  --font: 'IBM Plex Sans Thai', system-ui, sans-serif;
  --mono: 'IBM Plex Mono', ui-monospace, monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font: 15px/1.55 var(--font); -webkit-font-smoothing: antialiased; }
button, input, select { font: inherit; color: inherit; }
.mono { font-family: var(--mono); }
.muted { color: var(--muted); }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* ---------- top bar */
.bar {
  display: flex; align-items: center; gap: 16px; padding: 10px 20px;
  border-bottom: 1px solid var(--line); background: var(--paper); position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 10px; color: var(--ink); text-decoration: none; font-weight: 600; font-size: 17px; }
.preset select { padding: 7px 10px; border: 1px solid var(--line-2); border-radius: var(--radius); background: var(--paper); max-width: 240px; }
.actions { margin-left: auto; display: flex; gap: 8px; align-items: center; }
.btn { padding: 7px 14px; border-radius: var(--radius); border: 1px solid var(--line-2); background: var(--paper); cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: var(--ink-2); }
.btn.primary { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.btn.primary:hover { background: #000; }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.accent { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

.menu { position: relative; }
.menu-list {
  position: absolute; right: 0; top: calc(100% + 6px); width: 300px; background: var(--paper);
  border: 1px solid var(--line-2); border-radius: var(--radius); padding: 6px; box-shadow: 0 8px 24px rgba(29, 27, 24, .12);
}
.menu-list button { display: block; width: 100%; text-align: left; padding: 8px 10px; border: 0; background: none; border-radius: 4px; cursor: pointer; }
.menu-list button:hover, .menu-list button:focus-visible { background: var(--bg); outline: none; }
.menu-list b { display: block; font-weight: 500; }
.menu-list small { color: var(--muted); font-size: 12.5px; }
.menu-list hr { border: 0; border-top: 1px solid var(--line); margin: 6px 0; }
.narrow-only { display: none; }

/* ---------- layout */
.layout { display: grid; grid-template-columns: 330px minmax(0, 1fr); min-height: calc(100vh - 57px); }
.side { border-right: 1px solid var(--line); background: var(--paper); padding: 18px 20px 40px; }
.stage { padding: 18px 22px 30px; min-width: 0; }

.side-head { margin-bottom: 10px; }
.title-input { width: 100%; border: 0; border-bottom: 1px solid transparent; background: none; font-size: 19px; font-weight: 600; padding: 2px 0; }
.title-input:hover, .title-input:focus { border-bottom-color: var(--line-2); outline: none; }
.side-head p { margin: 2px 0 0; font-size: 13px; }

.error { background: var(--warn-soft); color: var(--warn); border-radius: var(--radius); padding: 9px 12px; font-size: 14px; margin: 8px 0 12px; }

fieldset { border: 0; border-top: 1px solid var(--line); margin: 0; padding: 14px 0 6px; }
legend { font-size: 12.5px; font-weight: 600; color: var(--ink-2); padding: 0 8px 0 0; text-transform: none; }
.field { display: grid; grid-template-columns: 1fr 132px; align-items: center; gap: 4px 10px; margin: 8px 0; }
.field > label { font-size: 14px; color: var(--ink-2); }
.field .hint { grid-column: 1 / -1; font-size: 12px; color: var(--muted); margin-top: -2px; }
.inp { display: flex; align-items: center; border: 1px solid var(--line-2); border-radius: var(--radius); background: #fff; }
.inp:focus-within { border-color: var(--ink); }
.inp input, .inp select { border: 0; background: none; padding: 6px 8px; width: 100%; min-width: 0; outline: none; font-family: var(--mono); font-size: 14px; }
.inp select { font-family: var(--font); }
.inp .unit { padding-right: 8px; font-size: 12px; color: var(--muted); white-space: nowrap; }
.toggle { display: flex; align-items: center; gap: 8px; cursor: pointer; }
.toggle input { width: 18px; height: 18px; accent-color: var(--ink); }

/* ---------- viewer */
.viewer-wrap { position: relative; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; background: #f3f0ea; }
.viewer { height: min(56vh, 560px); min-height: 320px; }
.viewer canvas { display: block; width: 100% !important; height: 100% !important; cursor: grab; touch-action: none; }
.viewer-tools { position: absolute; left: 12px; right: 12px; bottom: 12px; display: flex; gap: 12px; align-items: center; flex-wrap: wrap; pointer-events: none; }
.viewer-tools > * { pointer-events: auto; }
.seg { display: inline-flex; background: var(--paper); border: 1px solid var(--line-2); border-radius: var(--radius); overflow: hidden; }
.seg button { border: 0; background: none; padding: 5px 11px; cursor: pointer; font-size: 13px; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button:hover { background: var(--bg); }
.explode { display: inline-flex; align-items: center; gap: 8px; background: var(--paper); border: 1px solid var(--line-2); border-radius: var(--radius); padding: 3px 10px; font-size: 13px; }
.explode input { width: 120px; accent-color: var(--ink); }
.dims { position: absolute; top: 10px; left: 12px; margin: 0; font-size: 13px; color: var(--ink-2); background: rgba(255, 253, 249, .85); padding: 3px 8px; border-radius: 4px; }
.sel-info { position: absolute; top: 10px; right: 12px; margin: 0; font-size: 13px; background: var(--paper); border: 1px solid var(--line-2); padding: 6px 10px; border-radius: var(--radius); max-width: 60%; }

/* ---------- summary */
.summary { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; margin: 14px 0; }
.stat { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 14px; }
.stat .k { font-size: 12.5px; color: var(--muted); }
.stat .v { font-family: var(--mono); font-size: 21px; font-weight: 500; }
.stat .v small { font-family: var(--font); font-size: 13px; color: var(--ink-2); font-weight: 400; }
.stat.warn { border-color: #e8c98f; background: var(--warn-soft); }
.stat.ok .v { color: var(--ok); }
.stat.warn .v { color: var(--warn); }

/* ---------- tabs */
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); overflow-x: auto; scrollbar-width: none; }
.tabs button { border: 0; background: none; padding: 9px 14px; cursor: pointer; color: var(--ink-2); border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap; }
.tabs button[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--accent); font-weight: 500; }
.panel { padding: 16px 0; }
.panel h3 { font-size: 15px; margin: 18px 0 8px; }
.panel h3:first-child { margin-top: 0; }
.panel p { margin: 6px 0; }

table { border-collapse: collapse; width: 100%; font-size: 14px; background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); }
.table-wrap { overflow-x: auto; }
th, td { text-align: left; padding: 7px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-weight: 500; color: var(--ink-2); font-size: 13px; background: #faf8f4; white-space: nowrap; }
td.n, th.n { text-align: right; font-family: var(--mono); white-space: nowrap; }
tbody tr { cursor: default; }
tbody tr.pick { cursor: pointer; }
tbody tr.pick:hover { background: var(--bg); }
tbody tr.on { background: var(--accent-soft); }
tfoot td { font-weight: 600; border-bottom: 0; }
.id { font-family: var(--mono); font-size: 12.5px; color: var(--ink-2); }
.band { display: inline-flex; gap: 3px; vertical-align: middle; }
.band i { width: 14px; height: 4px; background: var(--line); border-radius: 1px; }
.band i.on { background: var(--accent); }

.list { margin: 0; padding-left: 20px; }
.list li { margin: 5px 0; }
.list li.sub { list-style: none; margin-left: -4px; color: var(--ink-2); font-size: 14px; }
.check { display: flex; gap: 10px; align-items: flex-start; padding: 8px 12px; border-radius: var(--radius); margin: 6px 0; background: var(--ok-soft); }
.check.bad { background: var(--warn-soft); }
.check .tag { font-size: 12px; font-weight: 600; padding: 1px 7px; border-radius: 3px; background: var(--ok); color: #fff; white-space: nowrap; }
.check.bad .tag { background: var(--warn); }
.note { font-size: 13px; color: var(--muted); }
.callout { border-left: 3px solid var(--accent); padding: 8px 12px; background: var(--paper); margin: 10px 0; }

/* ---------- sheet layouts */
.sheets { display: grid; grid-template-columns: repeat(auto-fill, minmax(420px, 1fr)); gap: 14px; }
.sheet { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; }
.sheet header { display: flex; justify-content: space-between; font-size: 13px; color: var(--ink-2); margin-bottom: 6px; gap: 10px; }
.sheet svg { width: 100%; height: auto; display: block; }
.sheet .stock { fill: #efe9df; stroke: var(--line-2); }
.sheet .part { fill: var(--wood); stroke: #8a6c50; stroke-width: 2; cursor: pointer; }
.sheet .part.on { fill: var(--accent); }
.sheet text { font-family: var(--mono); fill: #3b3128; pointer-events: none; }
.sheet .part.on + text, .sheet .part.on + text + text { fill: #fff; }

/* ---------- settings */
.grid-2 { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 4px 24px; }
.grid-2 .field { grid-template-columns: 1fr 120px; }

.foot { padding: 14px 22px 26px; font-size: 12.5px; border-top: 1px solid var(--line); }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px); background: var(--ink); color: var(--paper); padding: 9px 16px; border-radius: var(--radius); opacity: 0; transition: .2s; pointer-events: none; font-size: 14px; }
.toast.show { opacity: 1; transform: translateX(-50%); }
.print-only { display: none; }

/* ---------- responsive */
@media (max-width: 1080px) {
  .summary { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .actions .ghost { display: none; }
  .narrow-only { display: block; }
}
@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; }
  .side { border-right: 0; border-bottom: 1px solid var(--line); order: 2; }
  .stage { order: 1; padding: 12px 16px 20px; }
  .side { padding: 16px; }
  .bar { padding: 8px 16px; gap: 10px; flex-wrap: wrap; }
  .preset { order: 3; flex: 1 1 100%; }
  .preset select { max-width: none; width: 100%; }
  .viewer { height: 46vh; }
  .sheets { grid-template-columns: 1fr; }
  .menu-list { width: min(300px, calc(100vw - 32px)); }
}

/* ---------- print (report / PDF) */
@media print {
  @page { size: A4; margin: 14mm; }
  body { background: #fff; font-size: 11pt; }
  .bar, .layout, .foot, .toast { display: none !important; }
  .print-only { display: block; }
  .print-only h1 { font-size: 18pt; margin: 0 0 4pt; }
  .print-only h2 { font-size: 13pt; margin: 14pt 0 6pt; break-after: avoid; }
  .print-only img.shot { width: 100%; max-height: 95mm; object-fit: contain; border: 1px solid #ddd; }
  .print-only table { font-size: 9.5pt; break-inside: auto; }
  .print-only tr { break-inside: avoid; }
  .print-only .sheet { break-inside: avoid; border: 1px solid #ccc; margin-bottom: 8pt; }
  .print-only .sheets { grid-template-columns: 1fr 1fr; }
}
