:root {
  --green: #1f7a3d;
  --green-dark: #155c2d;
  --green-soft: #e8f4ec;
  --ink: #1d2a22;
  --muted: #66756b;
  --line: #dfe6e0;
  --bg: #f5f7f3;
  --card: #ffffff;
  --warn: #b7791f;
  --warn-soft: #fdf4e3;
  --bad: #c0392b;
  --bad-soft: #fdecea;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(20, 40, 25, .06), 0 4px 16px rgba(20, 40, 25, .05);
  font-family: "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); font-size: 15px; line-height: 1.5; }
img { max-width: 100%; display: block; }
h2, h3 { margin: 0; line-height: 1.25; }
h3 { font-size: 16px; margin-bottom: 12px; }
[hidden] { display: none !important; }
.muted { color: var(--muted); font-size: 14px; }

/* ------------------------------------------------ top bar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; flex-wrap: wrap; gap: 12px; align-items: center; justify-content: space-between;
  padding: 10px 24px; background: var(--card); border-bottom: 1px solid var(--line);
}
.brand { display: flex; gap: 10px; align-items: center; }
.brand-mark {
  width: 38px; height: 38px; border-radius: 10px; display: grid; place-items: center;
  background: var(--green); color: #fff; font-weight: 800; font-size: 20px;
}
.brand-text { display: flex; flex-direction: column; }
.brand-text span { font-size: 12px; color: var(--muted); }
.topbar-tools { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.badge {
  padding: 4px 10px; border-radius: 999px; font-size: 12px; font-weight: 600;
  background: var(--warn-soft); color: var(--warn);
}
.open-project { display: flex; gap: 6px; }
.open-project input { width: 190px; font-family: Consolas, monospace; }

/* ------------------------------------------------ layout */
.page { max-width: 1180px; margin: 0 auto; padding: 24px 20px 80px; display: grid; gap: 22px; }
.step { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 22px; box-shadow: var(--shadow); }
.step-head { display: flex; gap: 14px; align-items: flex-start; margin-bottom: 18px; }
.step-head p { margin: 4px 0 0; color: var(--muted); }
.step-num {
  flex: none; width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center;
  background: var(--green); color: #fff; font-weight: 700;
}
.grid.two { display: grid; grid-template-columns: 1.15fr 1fr; gap: 18px; }
.grid.two.single { grid-template-columns: 1fr; }
.card { border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; background: #fcfdfb; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

/* ------------------------------------------------ tabs (2D input mode: text vs drawing) */
.tabs { display: flex; gap: 6px; margin-bottom: 14px; border-bottom: 1px solid var(--line); }
.tab-btn {
  font: inherit; font-weight: 600; font-size: 14px; color: var(--muted); background: none; border: none;
  padding: 8px 4px 10px; margin-bottom: -1px; border-bottom: 2px solid transparent; cursor: pointer;
}
.tab-btn:hover { color: var(--ink); }
.tab-btn[aria-selected="true"] { color: var(--green-dark); border-bottom-color: var(--green); }
.icon-btn { padding: 8px 10px 10px; display: inline-flex; align-items: center; justify-content: center; }
.icon-btn svg { display: block; }

/* ------------------------------------------------ floor-count summary + add/remove (Tab "Từ bản vẽ tay") */
.floor-panel { margin-top: 12px; }
.floor-panel .floor-actions { display: flex; gap: 8px; margin-top: 6px; }

/* ------------------------------------------------ extra per-floor drawing slots (same dropzone + verdict card as the main image) */
.extra-slot { border-top: 1px dashed var(--line); padding-top: 12px; margin-top: 12px; }
.extra-slot .field { margin-bottom: 8px; }
.extra-slot .field .dropzone { margin-bottom: 8px; }
.extra-slot .extra-status { margin-top: 10px; }
.extra-slot .extra-clear { margin-top: 8px; }

#btn-draw-plot { margin-top: 10px; }

/* ------------------------------------------------ "Vẽ tay" draw tool */
.modal { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); display: grid; place-items: center; z-index: 50; padding: 16px; }
/* max-height + overflow stay as a fallback for very short viewports only — #draw-canvas's own height cap below is
   what actually keeps the whole modal small enough to fit without scrolling on ordinary screens. */
.modal-box { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; max-width: 640px; width: 100%; max-height: 90vh; overflow-y: auto; }
.modal-head p { margin: 4px 0 0; }
.modal-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-top: 12px; position: sticky; bottom: -18px; background: #fff; padding: 10px 0 4px; }
.modal-actions .spacer { flex: 1; }
/* At 390px "Hoàn tác", "Xóa hết" and "Dùng ảnh này" each broke over 2 lines (51px tall) next to "Hủy" at 31.5px,
   and at 320px "Dùng ảnh này" took 3 lines. A button label is never worth breaking: flex-wrap above lets the row
   take a second line instead when a few pixels are missing. */
.modal-actions .btn { white-space: nowrap; }
/* The canvas MUST stay in its own 700x480 proportion: pointer coordinates are converted per axis
   (canvas.width / rect.width and canvas.height / rect.height, see drawToolRawPoint in app.js), so a box whose
   proportion differs from 700/480 makes one metre across mean something else than one metre down — the wrong
   metres are then painted onto the PNG the AI reads. `width: auto; max-width: 100%; height: min(48vh,420px);
   aspect-ratio: 700/480` looked right but was not: with the height already resolved, max-width wins over
   aspect-ratio and squashes the box (307x405 at 390px wide = 2.28 across against 1.18 down). Only the
   width is set now; the canvas element's own width/height attributes supply the ratio for `height: auto`.
   .modal-actions above is sticky, so a taller canvas never pushes the buttons out of reach. */
#draw-canvas { display: block; width: 100%; height: auto; border: 1px solid var(--line); border-radius: 8px; cursor: crosshair; touch-action: none; margin-top: 10px; }
/* The metre scale of the sketch sits at the end of the tool row: it belongs to the drawing, not to one tool. */
.draw-scale { margin-left: auto; display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); border-bottom: none; }
.draw-scale select { padding: 4px 6px; font-size: 13px; }

@media (max-width: 860px) {
  .grid.two, .row { grid-template-columns: 1fr; }
  .topbar { padding: 10px 16px; }
  .open-project input { width: 150px; }
}

/* ------------------------------------------------ form */
.field { display: flex; flex-direction: column; gap: 5px; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 12px; }
input, select, textarea {
  font: inherit; color: var(--ink); padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: #fff; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(31, 122, 61, .25); border-color: var(--green); }
textarea { resize: vertical; }
.check { display: flex; gap: 8px; align-items: center; font-size: 14px; margin: 4px 0 12px; }
.check input { width: auto; }
.more summary { cursor: pointer; color: var(--green-dark); font-weight: 600; font-size: 14px; margin-bottom: 10px; }

/* ------------------------------------------------ buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 600; padding: 10px 18px; border-radius: 9px; border: 1px solid transparent;
  cursor: pointer; text-decoration: none; transition: background .15s, border-color .15s;
}
.btn.primary { background: var(--green); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--green-dark); }
.btn.ghost { background: #fff; color: var(--green-dark); border-color: var(--line); }
.btn.ghost:hover:not(:disabled) { border-color: var(--green); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.loading::before, .spinner {
  width: 14px; height: 14px; border-radius: 50%;
  border: 2px solid currentColor; border-right-color: transparent; animation: spin .8s linear infinite;
}
.btn.loading::before { content: ""; }
.actions { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-top: 18px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ------------------------------------------------ plot upload + verification */
.dropzone {
  position: relative; display: grid; place-items: center; text-align: center; min-height: 180px;
  border: 2px dashed var(--line); border-radius: var(--radius); background: #fff; cursor: pointer; overflow: hidden; color: var(--muted);
}
.dropzone.dragging { border-color: var(--green); background: var(--green-soft); }
.dropzone.locked { cursor: progress; opacity: .7; }
.dropzone.locked input { cursor: progress; }
.dropzone input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.dropzone img { max-height: 260px; object-fit: contain; }
.verify { margin-top: 12px; }
.verdict { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; padding: 10px 12px; border-radius: 9px; font-size: 14px; }
.verdict strong { font-size: 15px; }
.verdict span { color: var(--muted); }
.verdict.ok { background: var(--green-soft); color: var(--green-dark); }
.verdict.warn { background: var(--warn-soft); color: var(--warn); }
.verdict.bad { background: var(--bad-soft); color: var(--bad); }
.verdict.pending { background: #eef1ee; color: var(--muted); }
.verify .summary { margin: 10px 0 6px; }
.facts { display: grid; grid-template-columns: auto 1fr; gap: 2px 12px; margin: 8px 0; font-size: 14px; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; font-weight: 600; }
.issues { margin: 8px 0; padding-left: 18px; font-size: 14px; }
.issues li.error { color: var(--bad); }
.issues li.warning { color: var(--warn); }
/* Gợi ý mô tả dưới ô "Mô tả mong muốn": chỉ gợi ý, không chặn. */
.advice { margin-top: 8px; padding: 10px 12px; border-radius: var(--radius); background: var(--warn-soft); color: var(--warn); font-size: 13px; line-height: 1.5; }
.advice p { margin: 0; }
.advice p + p { margin-top: 6px; }
.error-text { color: var(--bad); font-size: 14px; }

/* ------------------------------------------------ progress + spec summary card */
.progress { margin-top: 16px; }
.bar { height: 8px; border-radius: 999px; background: #e7ece8; overflow: hidden; }
.bar span { display: block; height: 100%; width: 0; background: var(--green); transition: width .4s; }
.progress-text { margin: 8px 0 4px; font-size: 14px; }
.progress-wait { margin: 4px 0 0; }
.wait-note { margin: 10px 0 0; }
.wait-line { display: block; }
.wait-line.note { font-size: 13px; }
.verdict.pending .wait-line { width: 100%; }
.spec-summary { margin-top: 16px; padding: 14px 16px; border-radius: var(--radius); background: var(--green-soft); font-size: 14px; }
.spec-summary ul { margin: 6px 0 0; padding-left: 18px; }
.spec-summary .notes { color: var(--warn); }
.spec-summary .conversion { margin: 8px 0 0; }
.notes-box {
  margin-top: 16px; padding: 12px 14px; border-radius: var(--radius);
  background: var(--bad-soft); border: 1px solid var(--line); color: var(--bad); font-size: 14px;
}
.notes-box ul.notes { margin: 6px 0 0; padding-left: 18px; }
/* Set by renderNotesAndFailures when nothing failed: the box then carries plain run notes, not missing drawings,
   and the red "something went wrong" colours would read as an error the customer does not have. */
.notes-box.info { background: var(--warn-soft); color: var(--ink); }
.notes-box ul.notes + strong { display: block; margin-top: 10px; }

/* ------------------------------------------------ galleries */
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 14px; margin-top: 16px; }
.gallery.wide { grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); margin-top: 0; }
.tile { border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; background: #fff; margin: 0; display: flex; flex-direction: column; }
.tile .media { background: #f0f2ef; aspect-ratio: 3 / 4; display: grid; place-items: center; }
.gallery.wide .tile .media { aspect-ratio: 16 / 9; }
.tile .media img { width: 100%; height: 100%; object-fit: contain; }
.tile .placeholder { color: var(--muted); font-size: 13px; padding: 12px; text-align: center; }
.tile figcaption { display: flex; gap: 8px; align-items: center; justify-content: space-between; padding: 8px 10px; font-size: 13px; }
.tile.loading .media { position: relative; }
.tile.loading .media::after {
  content: "Đang xử lý…"; position: absolute; inset: 0; display: grid; place-items: center;
  background: rgba(255, 255, 255, .75); color: var(--green-dark); font-weight: 600;
}
.tile.failed { border-color: var(--bad); }
.tile.failed .media { background: var(--bad-soft); }
.tile.failed .placeholder { color: var(--bad); font-weight: 600; }
.render-group { margin-top: 18px; }
.render-group h3 { font-size: 15px; margin-bottom: 10px; color: var(--green-dark); }

/* ------------------------------------------------ estimate: 3 tabs + total */
.estimate { margin-top: 16px; }
.tabs { display: flex; flex-wrap: wrap; gap: 6px; border-bottom: 1px solid var(--line); }
.tab {
  font: inherit; font-weight: 600; padding: 9px 16px; border: 1px solid transparent; border-bottom: none;
  border-radius: 9px 9px 0 0; background: none; color: var(--muted); cursor: pointer; margin-bottom: -1px;
}
.tab:hover { color: var(--green-dark); }
.tab.active { background: #fff; color: var(--green-dark); border-color: var(--line); border-bottom: 1px solid #fff; }
.tab-panel { padding-top: 12px; }
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
/* Phones: keep the 5 columns readable and scroll horizontally inside the frame instead of squeezing the amounts. */
.table-wrap table { min-width: 560px; }
td.num:last-child { font-weight: 600; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { border-bottom: 1px solid var(--line); padding: 6px 8px; text-align: left; vertical-align: top; }
th { background: #f3f6f2; color: var(--muted); font-weight: 600; }
td.num, th.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
tr.group td { background: var(--green-soft); font-weight: 700; color: var(--green-dark); }
.part-total, .grand-total {
  display: flex; flex-wrap: wrap; gap: 12px; justify-content: space-between; align-items: center;
  margin-top: 12px; padding: 12px 14px; border-radius: var(--radius);
}
.part-total { background: #f3f6f2; font-weight: 600; }
.grand-total { background: var(--green); color: #fff; font-size: 17px; }
.grand-total strong { font-size: 20px; }

/* ------------------------------------------------ toast */
/* left/right, NOT left:50% + translateX: a shrink-to-fit box positioned at left:50% has only half the screen left
   as its containing block (195px of a 390px phone), so max-width never gets a turn and the error sentence broke
   over 5 lines at 390px and 6 at 320px. Same fix, same numbers as web/step1/style.css. */
.toast {
  position: fixed; left: 16px; right: 16px; bottom: 24px; z-index: 50;
  max-width: 640px; margin-inline: auto;
  padding: 12px 18px; border-radius: 10px; background: var(--ink); color: #fff; box-shadow: var(--shadow); font-size: 14px;
}
.toast.error { background: var(--bad); }
.toast.success { background: var(--green); }

/* ------------------------------------------------ điện thoại */
/* iOS zooms the whole page in when focus lands on a field smaller than 16px, and not one field on this screen
   reached it: every form control inherited .field's 13px, and #project-id in the top bar inherited body's 15px.
   .field keeps 13px for the LABEL — only the control it wraps is raised. Same rule, same 16px as
   web/step1/style.css. */
@media (max-width: 600px) {
  input, select, textarea,
  .field input, .field select, .field textarea,
  .open-project input { font-size: 16px; }
}
