/* วัดด้วยกล้อง — ปุ่มใหญ่กดด้วยนิ้วโป้งได้, ตัวหนังสือบนภาพกล้องมีขอบดำ อ่านออกกลางแดด */
:root {
  --yellow: #ffc20e;
  --yellow-d: #e0a800;
  --orange: #ff7a00;
  --green: #2ee86a;
  --blue: #36b3ff;
  --ink: #111;
  --paper: #f6f4ee;
  --card: #fff;
  --muted: #5b5b5b;
  --radius: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  font-family: system-ui, "Noto Sans Thai", "Sarabun", "Leelawadee UI", sans-serif;
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--paper); }
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
.small { font-size: 14px; color: var(--muted); }

/* ---------- ปุ่ม ---------- */
.btn {
  font: inherit; font-size: 19px; font-weight: 700;
  min-height: 60px; padding: 10px 18px;
  border-radius: 14px; border: 3px solid transparent;
  cursor: pointer; touch-action: manipulation; user-select: none;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  line-height: 1.15; text-align: center;
}
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.4; }
.btn-primary { background: var(--yellow); color: #000; border-color: #000; }
.btn-dark { background: rgba(0, 0, 0, 0.72); color: #fff; border-color: rgba(255, 255, 255, 0.85); }
.btn-ghost { background: transparent; color: var(--ink); border-color: var(--ink); }
.btn-danger { background: #c62a12; color: #fff; border-color: #000; }
.btn-sm { min-height: 44px; font-size: 16px; padding: 6px 14px; }
.btn-xl { width: 100%; min-height: 84px; font-size: 28px; }
.btn-toggle[aria-pressed="true"] { background: var(--blue); color: #000; border-color: #000; }
.btn.armed { background: #c62a12; border-color: #fff; }

/* ---------- หน้าแรก / หน้าผล ---------- */
.screen { max-width: 640px; margin: 0 auto; padding: calc(16px + var(--safe-t)) 16px calc(28px + var(--safe-b)); }
.hero { display: flex; gap: 14px; align-items: center; margin: 8px 0 18px; }
.hero img { border-radius: 16px; box-shadow: 0 2px 0 #000; }
.hero h1 { font-size: 30px; }
.hero p { margin: 2px 0 0; color: var(--muted); }
.card { background: var(--card); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; border: 2px solid #e4e1d6; font-size: 17px; }
.card h2 { font-size: 20px; }
.card.warn { background: #fff3d6; border-color: var(--orange); }
.card.warn h2 { font-size: 19px; margin-bottom: 8px; }
.card.warn p { margin: 6px 0; }
.tips { margin: 14px 0 0; padding-left: 22px; line-height: 1.6; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
.history { list-style: none; margin: 0; padding: 0; }
.history li { border-top: 1px solid #eee; }
.history li.empty { color: var(--muted); padding: 12px 0; border: 0; }
.history button.item {
  width: 100%; background: none; border: 0; font: inherit; text-align: left;
  padding: 12px 4px; min-height: 64px; cursor: pointer; display: block; color: inherit;
}
.history .name { font-weight: 700; font-size: 18px; }
.history .meta { color: var(--muted); font-size: 14px; }
.disclaimer {
  text-align: center; font-weight: 700; color: #8a3b00;
  background: #fff3d6; border: 2px dashed var(--orange); border-radius: 12px; padding: 10px; margin-top: 16px;
}

.field { display: block; margin: 6px 0; }
.field span { display: block; font-size: 15px; color: var(--muted); margin-bottom: 4px; }
.field input { width: 100%; font: inherit; font-size: 20px; font-weight: 700; padding: 12px; border-radius: 12px; border: 2px solid #000; background: #fff; }
.plan { background: #fff; border: 2px solid #000; border-radius: var(--radius); overflow: hidden; margin: 10px 0 14px; }
.plan svg { display: block; width: 100%; height: auto; }
.summary dl { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 0; }
.summary dt { color: var(--muted); }
.summary dd { margin: 0; font-weight: 700; font-variant-numeric: tabular-nums; }
.summary .big { font-size: 22px; }
.sides-table { width: 100%; border-collapse: collapse; font-size: 16px; }
.sides-table th, .sides-table td { text-align: left; padding: 6px 4px; border-top: 1px solid #eee; font-variant-numeric: tabular-nums; }
.sides-table th { color: var(--muted); font-weight: 600; }
.sides-table .long { color: #c25400; font-weight: 700; }
.res-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.res-actions .btn-primary { grid-column: 1 / -1; }
.saved-tag { background: #d8f5dc; color: #0b5d1e; font-weight: 700; padding: 4px 12px; border-radius: 999px; }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + var(--safe-b)); transform: translateX(-50%);
  background: #000; color: #fff; padding: 12px 18px; border-radius: 12px; font-weight: 700; z-index: 50;
  max-width: 90vw; text-align: center;
}

/* ---------- หน้าวัด AR ---------- */
/* ข้อความบนภาพกล้อง: ตัวขาว ขอบดำหนา อ่านได้กลางแดด */
.outline {
  color: #fff; font-weight: 800;
  text-shadow:
    -2px -2px 0 #000, 2px -2px 0 #000, -2px 2px 0 #000, 2px 2px 0 #000,
    0 -2px 0 #000, 0 2px 0 #000, -2px 0 0 #000, 2px 0 0 #000, 0 0 6px #000;
}
#ar-overlay {
  display: none; position: fixed; inset: 0; background: transparent;
  font-family: inherit; overflow: hidden; touch-action: none; user-select: none;
}
#ar-overlay.active { display: block; }
body.in-ar { background: transparent; }
body.in-ar > .screen { display: none; }

.ar-top {
  position: absolute; top: calc(10px + var(--safe-t)); left: 10px; right: 10px;
  display: flex; align-items: flex-start; justify-content: space-between; gap: 8px;
}
.ar-top .btn { min-height: 52px; font-size: 17px; padding: 8px 14px; }
.ar-status { flex: 1; text-align: center; font-size: 20px; padding-top: 12px; }
.ar-badges { position: absolute; top: calc(72px + var(--safe-t)); left: 10px; right: 10px; display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.badge-pill { background: rgba(0, 0, 0, 0.75); color: #fff; border: 2px solid #fff; border-radius: 999px; padding: 4px 12px; font-weight: 700; font-size: 16px; }
.badge-pill.chain { border-color: var(--blue); color: #bfe8ff; }
.ar-warn {
  position: absolute; top: calc(112px + var(--safe-t)); left: 50%; transform: translateX(-50%);
  background: var(--orange); color: #000; border: 3px solid #000; border-radius: 12px;
  padding: 8px 14px; font-weight: 800; font-size: 18px; white-space: nowrap;
}

.crosshair {
  position: absolute; left: 50%; top: 50%; width: 56px; height: 56px; margin: -28px 0 0 -28px; pointer-events: none;
}
.crosshair::before, .crosshair::after {
  content: ""; position: absolute; background: #fff; box-shadow: 0 0 0 2px #000;
}
.crosshair::before { left: 26px; top: 0; width: 4px; height: 56px; }
.crosshair::after { top: 26px; left: 0; height: 4px; width: 56px; }
.crosshair.nohit { opacity: 0.45; }
.live-label {
  position: absolute; left: 50%; top: calc(50% + 40px); transform: translateX(-50%);
  font-size: 34px; white-space: nowrap; pointer-events: none; text-align: center; line-height: 1.15;
}
.live-label small { display: block; font-size: 17px; }
.live-label.long { color: var(--orange); }
.live-label.good { color: var(--green); }

#ar-labels { position: absolute; inset: 0; pointer-events: none; }
.seg-label {
  position: absolute; left: 0; top: 0; font-size: 22px; white-space: nowrap; will-change: transform;
}
.seg-label.long { color: var(--orange); }
.seg-label.calib { color: var(--blue); }

.ar-toast {
  position: absolute; left: 50%; top: 34%; transform: translateX(-50%);
  font-size: 22px; text-align: center; max-width: 92vw; background: rgba(0, 0, 0, 0.55);
  padding: 10px 16px; border-radius: 14px; pointer-events: none;
}

.ar-bottom {
  position: absolute; left: 8px; right: 8px; bottom: calc(10px + var(--safe-b));
  display: flex; flex-direction: column; gap: 8px;
}
.ar-stats {
  display: flex; flex-wrap: wrap; gap: 4px 16px; justify-content: center; align-items: baseline;
  background: rgba(0, 0, 0, 0.5); border-radius: 14px; padding: 8px 10px; font-size: 18px;
}
.ar-stats .k { font-weight: 600; opacity: 0.9; }
.ar-stats b { font-size: 22px; }
.ar-stats .area { flex-basis: 100%; text-align: center; }
.ar-stats small { display: block; font-size: 17px; }
.ar-row { display: flex; gap: 8px; }
.ar-row .btn { flex: 1; min-height: 64px; font-size: 18px; padding: 6px 8px; }
.ar-row .btn-mark { flex: 1.6; min-height: 84px; font-size: 24px; }

.calib-panel {
  position: absolute; left: 10px; right: 10px; top: calc(70px + var(--safe-t));
  background: rgba(0, 0, 0, 0.9); color: #fff; border: 3px solid var(--blue); border-radius: 18px;
  padding: 14px; font-size: 17px;
}
.calib-panel h3 { font-size: 20px; margin-bottom: 6px; color: #bfe8ff; }
.calib-step { margin: 6px 0; }
.calib-measured { font-size: 22px; margin: 4px 0; }
.calib-label { display: block; margin: 6px 0 4px; }
.stepper { display: flex; gap: 6px; }
.stepper .btn { min-height: 52px; padding: 4px; flex: 1; font-size: 16px; }
.stepper input {
  width: 96px; flex: 1.4; font: inherit; font-size: 24px; font-weight: 800; text-align: center;
  border-radius: 12px; border: 3px solid #fff; background: #fff; color: #000;
}
.presets { display: flex; gap: 6px; margin-top: 8px; }
.presets .btn { flex: 1; color: #fff; border-color: #fff; }
.calib-result { font-size: 20px; font-weight: 700; margin: 10px 0 4px; }
.calib-advice { background: var(--orange); color: #000; font-weight: 800; padding: 8px 10px; border-radius: 10px; margin: 6px 0; }
.calib-actions { display: flex; gap: 8px; margin-top: 10px; }
.calib-actions .btn { flex: 1; }
