/* Plotline — โทนสีเทาเข้ม (ink) + เขียวมะนาว (lime)
   ปุ่มใหญ่กดด้วยนิ้วโป้งได้, ตัวหนังสือบนภาพกล้องมีขอบดำ อ่านออกกลางแดด */
:root {
  --ink: #0f172a;        /* slate-900 */
  --ink-2: #1e293b;      /* slate-800 */
  --ink-3: #334155;
  --muted: #64748b;
  --line: #e2e8f0;
  --bg: #f3f5f9;
  --surface: #ffffff;
  --lime: #c6f35e;       /* สีหลัก */
  --lime-d: #a3d82f;
  --lime-tint: #f2fbd9;
  --sky: #38bdf8;        /* เทียบค่า / วัดต่อเป็นช่วง */
  --sky-d: #0369a1;
  --amber: #ff9f1c;      /* เส้นยาว / เตือน */
  --amber-d: #b45309;
  --amber-tint: #fff6e8;
  --green: #22c55e;
  --green-d: #15803d;
  --red: #ef4444;
  --red-d: #b91c1c;
  --radius: 20px;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.06), 0 8px 24px rgba(15, 23, 42, 0.06);
  --glass: rgba(15, 23, 42, 0.62);
  --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(--bg); }
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; line-height: 1.25; letter-spacing: -0.01em; }
.small { font-size: 14px; color: var(--muted); }

/* ---------- ปุ่ม ---------- */
.btn {
  font: inherit; font-size: 19px; font-weight: 700;
  min-height: 60px; padding: 10px 18px;
  border-radius: 16px; border: 1.5px 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;
  transition: transform 0.08s ease, background-color 0.15s ease, box-shadow 0.15s ease;
}
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.38; }
.btn-primary { background: var(--lime); color: var(--ink); box-shadow: 0 6px 18px rgba(163, 216, 47, 0.35); }
.btn-primary:active { background: var(--lime-d); }
.btn-dark {
  background: var(--glass); color: #fff; border-color: rgba(255, 255, 255, 0.22);
  -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px);
}
.screen .btn-dark { background: var(--ink); border-color: var(--ink); }
.btn-ghost { background: var(--surface); color: var(--ink); border-color: #cbd5e1; }
.btn-danger { background: var(--surface); color: var(--red-d); border-color: #fecaca; }
.btn-sm { min-height: 44px; font-size: 16px; padding: 6px 14px; border-radius: 12px; }
.btn-xl { width: 100%; min-height: 84px; font-size: 28px; border-radius: 22px; }
.btn-toggle[aria-pressed="true"] { background: var(--sky); color: var(--ink); border-color: var(--sky); }
.btn.armed { background: var(--red); border-color: var(--red); 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: 4px 0 16px; padding: 20px;
  border-radius: 24px; color: #fff;
  background:
    radial-gradient(120% 140% at 100% 0%, rgba(198, 243, 94, 0.28) 0%, rgba(198, 243, 94, 0) 55%),
    linear-gradient(160deg, var(--ink-2), var(--ink));
  box-shadow: 0 10px 30px rgba(15, 23, 42, 0.25);
}
.hero img { border-radius: 16px; box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.12); }
.hero h1 { font-size: 30px; }
.hero h1.brand { font-size: 36px; font-weight: 800; letter-spacing: -0.035em; line-height: 1.05; font-family: ui-sans-serif, system-ui, "Segoe UI", Roboto, sans-serif; }
.hero h1.brand span { color: var(--lime); }
.hero p { margin: 4px 0 0; color: #cbd5e1; }
.card {
  background: var(--surface); border-radius: var(--radius); padding: 18px; margin-bottom: 14px;
  border: 1px solid var(--line); box-shadow: var(--shadow); font-size: 17px;
}
.card h2 { font-size: 20px; }
.card.warn { background: var(--amber-tint); border-color: #fde3b8; box-shadow: none; }
.card.warn h2 { font-size: 19px; margin-bottom: 8px; color: #7c2d12; }
.card.warn p { margin: 6px 0; }
.card.recover { border-left: 6px solid var(--lime-d); }
.card.recover p { margin: 4px 0 0; }
.tips { margin: 16px 0 0; padding-left: 0; list-style: none; line-height: 1.55; }
.tips li { position: relative; padding: 6px 0 6px 28px; color: var(--ink-3); }
.tips li::before {
  content: ""; position: absolute; left: 4px; top: 13px; width: 10px; height: 10px; border-radius: 50%;
  background: var(--lime); box-shadow: 0 0 0 3px var(--lime-tint);
}
.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 var(--line); }
.history li:first-child { border-top: 0; }
.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 6px; min-height: 64px; cursor: pointer; display: block; color: inherit; border-radius: 12px;
}
.history button.item:active { background: var(--bg); }
.history .name { font-weight: 700; font-size: 18px; }
.history .meta { color: var(--muted); font-size: 14px; }
.disclaimer {
  text-align: center; font-weight: 600; font-size: 15px; color: var(--amber-d);
  background: transparent; border: 1px dashed #f3c98b; border-radius: 14px; padding: 10px; margin-top: 16px;
}

.field { display: block; margin: 6px 0; }
.field span { display: block; font-size: 14px; color: var(--muted); margin-bottom: 6px; font-weight: 600; }
.field input {
  width: 100%; font: inherit; font-size: 20px; font-weight: 700; padding: 14px 16px; border-radius: 14px;
  border: 1.5px solid #cbd5e1; background: var(--surface); color: var(--ink); outline: none;
}
.field input:focus { border-color: var(--ink); box-shadow: 0 0 0 4px rgba(198, 243, 94, 0.55); }
.plan { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; margin: 12px 0 14px; box-shadow: var(--shadow); }
.plan svg { display: block; width: 100%; height: auto; }
.summary dl { display: grid; grid-template-columns: auto 1fr; gap: 8px 16px; 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: 8px 4px; border-top: 1px solid var(--line); font-variant-numeric: tabular-nums; }
.sides-table th { color: var(--muted); font-weight: 600; font-size: 14px; border-top: 0; }
.sides-table .long { color: var(--amber-d); 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: #dcfce7; color: var(--green-d); font-weight: 700; font-size: 14px; padding: 6px 12px; border-radius: 999px; }
.note-warn { color: var(--amber-d); font-weight: 700; }
.note-bad { color: var(--red-d); font-weight: 700; }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + var(--safe-b)); transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 12px 18px; border-radius: 14px; font-weight: 700; z-index: 50;
  max-width: 90vw; text-align: center; box-shadow: 0 10px 30px rgba(15, 23, 42, 0.3);
}

/* วัดซ้ำ / เฉลี่ย / ความคลาดหมุดแรก */
.res-actions .btn-wide { grid-column: 1 / -1; min-height: 64px; }
.round-tabs { display: flex; gap: 4px; margin: 12px 0 0; padding: 4px; background: #e2e8f0; border-radius: 16px; }
.round-tab {
  flex: 1; font: inherit; font-size: 16px; font-weight: 700; min-height: 44px; color: var(--muted);
  border-radius: 12px; border: 0; background: transparent; cursor: pointer;
}
.round-tab[aria-pressed="true"] { background: var(--surface); color: var(--ink); box-shadow: 0 1px 3px rgba(15, 23, 42, 0.15); }
.card.average { border-left: 6px solid var(--line); }
.card.average.ok { border-left-color: var(--green); }
.card.average.warn { border-left-color: var(--amber); background: var(--amber-tint); }
.card.average h2 { margin-bottom: 8px; }
.card.average tr.current td { font-weight: 800; }
.card.average .sides-table { font-size: 15px; }
.card.average td { white-space: nowrap; }
.avg-area { font-size: 22px; margin: 14px 0 4px; line-height: 1.35; }
.avg-verdict { font-weight: 800; }
.card.average.ok .avg-verdict { color: var(--green-d); }
.card.average.warn .avg-verdict { color: var(--amber-d); }
.q-good { color: var(--green-d); } .q-fair { color: var(--amber-d); } .q-bad { color: var(--red-d); }
.row-wrap { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }

/* ---------- หน้าวัด 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; border-radius: 999px; }
.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: var(--glass); color: #fff; border: 1.5px solid rgba(255, 255, 255, 0.3); border-radius: 999px;
  padding: 5px 14px; font-weight: 700; font-size: 16px;
  -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px);
}
.badge-pill.chain { border-color: var(--sky); color: #bae6fd; }
.badge-pill.round { border-color: var(--lime); color: var(--lime); }
.ar-warn {
  /* อยู่ในแถวป้ายเดียวกัน ขึ้นบรรทัดใหม่ใต้ป้ายเสมอ จึงไม่ทับกัน */
  flex-basis: 100%; max-width: max-content; margin: 2px auto 0;
  background: var(--amber); color: var(--ink); border: 0; border-radius: 999px;
  padding: 9px 18px; font-weight: 800; font-size: 18px; white-space: nowrap;
  box-shadow: 0 0 0 3px rgba(0, 0, 0, 0.55);
}

.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; border-radius: 2px;
}
.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(--amber); }
.live-label.good { color: var(--lime); }

#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(--amber); }
.seg-label.calib { color: var(--sky); }

.ar-toast {
  position: absolute; left: 50%; top: 34%; transform: translateX(-50%);
  font-size: 22px; text-align: center; max-width: 92vw; background: rgba(15, 23, 42, 0.55);
  padding: 12px 18px; border-radius: 18px; pointer-events: none; white-space: pre-line;
}

.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: var(--glass); border: 1px solid rgba(255, 255, 255, 0.14); border-radius: 20px; padding: 10px 12px; font-size: 18px;
  -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px);
}
.ar-stats .k { font-weight: 600; opacity: 0.85; }
.ar-stats b { font-size: 22px; }
.ar-stats .area { flex-basis: 100%; text-align: center; }
.ar-stats .area b { color: var(--lime); }
.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; border-radius: 20px; }
.ar-row .btn-mark { flex: 1.6; min-height: 84px; font-size: 24px; border-radius: 24px; box-shadow: 0 0 0 3px rgba(0, 0, 0, 0.45), 0 8px 24px rgba(0, 0, 0, 0.35); }

.calib-panel {
  position: absolute; left: 10px; right: 10px; top: calc(70px + var(--safe-t));
  background: rgba(15, 23, 42, 0.9); color: #fff; border: 1.5px solid var(--sky); border-radius: 24px;
  padding: 16px; font-size: 17px;
  -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px);
}
.calib-panel h3 { font-size: 20px; margin-bottom: 6px; color: #bae6fd; }
.calib-step { margin: 6px 0; }
.calib-measured { font-size: 22px; margin: 4px 0; }
.calib-label { display: block; margin: 6px 0 4px; color: #cbd5e1; }
.stepper { display: flex; gap: 6px; }
.stepper .btn { min-height: 52px; padding: 4px; flex: 1; font-size: 16px; border-radius: 14px; }
.stepper input {
  width: 96px; flex: 1.4; font: inherit; font-size: 24px; font-weight: 800; text-align: center;
  border-radius: 14px; border: 0; background: #fff; color: var(--ink);
}
.presets { display: flex; gap: 6px; margin-top: 8px; }
.presets .btn { flex: 1; color: #fff; background: transparent; border-color: rgba(255, 255, 255, 0.35); }
.calib-result { font-size: 20px; font-weight: 700; margin: 10px 0 4px; }
.calib-advice { background: var(--amber); color: var(--ink); font-weight: 800; padding: 10px 12px; border-radius: 14px; margin: 6px 0; }
.calib-actions { display: flex; gap: 8px; margin-top: 10px; }
.calib-actions .btn { flex: 1; }
