:root {
  --bg: #f2f4f7;
  --card: #ffffff;
  --line: #dfe3ea;
  --text: #1f2733;
  --muted: #7a8494;
  --primary: #1a67d2;
  --primary-dark: #14509f;
  --danger: #c0392b;
}
* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: "Sarabun", "Segoe UI", Tahoma, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}
header {
  display: flex; align-items: center; gap: 12px;
  background: #14273f;
  color: #fff; padding: 10px 20px;
}
header h1 { font-size: 18px; margin: 0; }
.spacer { flex: 1; }
main { max-width: 1400px; margin: 16px auto; padding: 0 16px; }

.card {
  background: var(--card); border: 1px solid var(--line);
  border-radius: 10px; padding: 16px; margin-bottom: 14px;
}
h2 { font-size: 16px; margin: 0 0 12px; }
h3 { font-size: 15px; margin: 12px 0 8px; }
.muted { color: var(--muted); font-size: 12.5px; }
.bold { font-weight: 700; }
.right { text-align: right; }

button {
  font: inherit; border: 1px solid var(--line); border-radius: 8px;
  padding: 7px 14px; cursor: pointer; background: #fff; color: var(--text);
}
button.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
button.primary:hover { background: var(--primary-dark); }
button.ghost { background: transparent; }
button.ghost:hover { background: #eef1f6; }
button.danger { color: var(--danger); border-color: var(--danger); }
button:disabled { opacity: .5; cursor: default; }

input, textarea, select {
  font: inherit; width: 100%; padding: 6px 9px;
  border: 1px solid var(--line); border-radius: 7px; background: #fff;
}
label { display: flex; flex-direction: column; gap: 3px; font-size: 12.5px; color: #46505e; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 12px; margin-bottom: 8px; }

/* upload */
#dropZone {
  border: 2px dashed var(--line); border-radius: 10px;
  padding: 28px; text-align: center; transition: .15s;
}
#dropZone.drag { border-color: var(--primary); background: #eef4fd; }

/* project list */
.proj-row {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 6px; border-bottom: 1px solid var(--line); cursor: pointer;
}
.proj-row:hover { background: #f6f8fb; }
.proj-row .status { font-size: 12px; padding: 2px 10px; border-radius: 20px; background: #eee; }
.status.done { background: #d9f2e0; color: #16794c; }
.status.analyzing, .status.splitting, .status.uploaded { background: #fdeec7; color: #8a6100; }
.status.error { background: #fadbd8; color: var(--danger); }
.status.cancelled { background: #e8e2f7; color: #5b3fa8; }
#progressWrap { display: flex; align-items: center; gap: 8px; }
#progressWrap[hidden] { display: none; }

/* project head */
.project-head { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.project-head .meta { display: flex; gap: 10px; flex-wrap: wrap; }
.project-head input { width: 170px; }
.project-head input.short { width: 120px; }
.progress { width: 180px; height: 8px; background: #e6e9ef; border-radius: 6px; overflow: hidden; }
#progressBar { height: 100%; width: 0; background: var(--primary); transition: width .4s; }

/* tabs */
.tab-strip {
  display: flex; gap: 6px; overflow-x: auto; padding: 4px 2px 10px; scrollbar-width: thin;
}
.tab {
  white-space: nowrap; padding: 6px 13px; border-radius: 20px;
  border: 1px solid var(--line); background: #fff; cursor: pointer; font-size: 13px;
}
.tab.active { background: var(--primary); border-color: var(--primary); color: #fff; }
.tab.skipped { opacity: .55; font-style: italic; }
.tab.error { border-color: var(--danger); color: var(--danger); }

/* split view */
.split { display: grid; grid-template-columns: minmax(420px, 46%) 1fr; gap: 14px; align-items: start; }
@media (max-width: 1100px) { .split { grid-template-columns: 1fr; } }
.pane-title { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.pane-title h3 { margin: 0; }

/* crop */
#imgWrap { position: relative; user-select: none; margin-top: 8px; border: 1px solid var(--line); }
#imgWrap img { display: block; width: 100%; }
#cropRect {
  position: absolute; border: 2px solid var(--primary);
  background: rgba(26,103,210,.12); cursor: move; display: none;
}
#cropRect .handle {
  position: absolute; width: 14px; height: 14px; background: var(--primary);
  border-radius: 50%; border: 2px solid #fff;
}
#cropRect .handle.br { right: -8px; bottom: -8px; cursor: nwse-resize; }
.crop-preview { margin-top: 10px; display: flex; align-items: center; gap: 10px; }
.crop-preview img { max-height: 110px; max-width: 200px; border: 1px solid var(--line); border-radius: 6px; }
.skip-img { width: 100%; border: 1px solid var(--line); margin-top: 8px; }

/* prices */
.price-row { display: grid; grid-template-columns: 1fr 1fr 1.2fr; gap: 12px; align-items: end; margin: 8px 0; }
.cost-info { font-size: 13px; }
.row-end { display: flex; justify-content: flex-end; gap: 8px; margin: 10px 0 4px; }
.row-end .danger { margin-right: auto; }

/* BOM table */
#bomTable { width: 100%; border-collapse: collapse; }
#bomTable th, #bomTable td { border: 1px solid var(--line); padding: 3px 5px; font-size: 13px; }
#bomTable th { background: #eef1f6; font-weight: 600; }
#bomTable input { border: none; padding: 4px 5px; border-radius: 0; background: transparent; }
#bomTable input:focus { outline: 2px solid var(--primary); }
#bomTable td.num input { text-align: right; }
#bomTable td.line-total { text-align: right; min-width: 70px; }
#bomTable .del { color: var(--danger); cursor: pointer; border: none; background: none; padding: 2px 6px; }
#bomTable .drag-col { width: 32px; }
#bomTable .drag-cell { width: 32px; text-align: center; }
#bomTable .drag-handle {
  width: 24px; height: 24px; padding: 0; border: none; border-radius: 5px;
  color: var(--muted); background: transparent; cursor: grab; line-height: 1;
}
#bomTable .drag-handle:hover, #bomTable .drag-handle:focus {
  color: var(--primary); background: #eef4fd; outline: none;
}
#bomTable tr.dragging { opacity: .45; }

/* pager */
.pager { display: flex; justify-content: center; align-items: center; gap: 16px; margin: 12px 0 30px; }

/* modal */
dialog {
  border: none; border-radius: 12px; padding: 20px; width: min(680px, 92vw);
  box-shadow: 0 12px 40px rgba(0,0,0,.25);
}
dialog::backdrop { background: rgba(10,20,35,.45); }

/* embed mode (ฝังใน Open WebUI) */
body.embed header { display: none; }
body.embed #btnBack { display: none; }
body.embed main { margin: 8px auto; max-width: none; }

/* toast */
#toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: #223047; color: #fff; padding: 10px 20px; border-radius: 8px;
  box-shadow: 0 6px 18px rgba(0,0,0,.3); z-index: 99;
}
#toast.err { background: var(--danger); }
