/* ============================================================
   AI Safety — "Industrial Trust" theme
   Mobile-first · responsive · sidebar (desktop) + bottom nav (mobile)
   ============================================================ */

:root {
  /* Brand — Emerald Green */
  --brand-primary: #046A38;   /* emerald green — trust / nature / safety */
  --brand-primary-600: #0A7D45; /* brighter emerald for gradient sheen */
  --accent: #F5A623;          /* gold/amber — CTA / highlight (classic pairing with emerald) */
  --accent-600: #E0940D;

  /* Safety semantic colors */
  --safe: #12A150;            /* pass — distinct brighter green vs deep emerald brand */
  --warning: #F59E0B;
  --danger: #DC2626;
  --neutral: #64726B;

  /* Neutrals (subtle green tint to harmonize) */
  --bg: #F1F5F2;
  --surface: #FFFFFF;
  --line: #DCE6E0;
  --ink: #10251A;             /* deep forest text */
  --muted: #566B60;           /* green-gray muted */

  /* Fonts */
  --font-display: 'Kanit', 'Sarabun', sans-serif;
  --font-body: 'Inter', 'Sarabun', -apple-system, BlinkMacSystemFont, sans-serif;

  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(4,106,56,.08), 0 6px 20px rgba(4,106,56,.06);
  --ease: cubic-bezier(0.16, 1, 0.3, 1);

  --sidebar-w: 260px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: var(--font-body);
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  /* ไทยไม่มีเว้นวรรค — กันข้อความล้นจอ (บทเรียน AI-Commerce) */
  overflow-wrap: anywhere;
  word-break: break-word;
}

.screen { display: none; }
.screen.active { display: block; }
#login-screen.active { display: grid; }   /* login แสดงเฉพาะตอน active เท่านั้น */

/* ============================ LOGIN ============================ */
.login-screen {
  min-height: 100dvh;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(1200px 500px at 50% -10%, rgba(245,166,35,.12), transparent 60%),
    linear-gradient(160deg, #046A38 0%, #02502A 100%);
}
.login-card {
  width: 100%;
  max-width: 400px;
  background: var(--surface);
  border-radius: 18px;
  padding: 32px 28px;
  box-shadow: 0 20px 60px rgba(0,0,0,.28);
}
.login-brand { text-align: center; margin-bottom: 24px; }
.brand-mark-lg { width: 56px; height: 56px; }
.login-title {
  font-family: var(--font-display);
  font-size: 26px; font-weight: 700; margin-top: 12px; color: var(--brand-primary);
}
.login-subtitle { color: var(--muted); font-size: 14px; margin-top: 4px; }

.divider { display: flex; align-items: center; gap: 12px; margin: 18px 0; color: var(--muted); font-size: 13px; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--line); }

.login-lang { display: flex; gap: 8px; justify-content: center; margin-top: 18px; }
.lang-pill {
  border: 1px solid var(--line); background: #fff; color: var(--muted);
  padding: 6px 14px; border-radius: 999px; font-size: 13px; cursor: pointer;
}
.lang-pill.active { border-color: var(--brand-primary); color: var(--brand-primary); font-weight: 600; }

/* ============================ BUTTONS ============================ */
.btn-primary, .btn-secondary, .btn-danger, .btn-accent, .btn-oauth {
  font-family: var(--font-body); font-size: 15px; font-weight: 600;
  border-radius: var(--radius-sm); padding: 12px 18px; cursor: pointer;
  border: 1px solid transparent; transition: all .18s var(--ease);
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 46px; white-space: nowrap;
}
.btn-primary { background: var(--brand-primary); color: #fff; }
.btn-primary:hover { background: var(--brand-primary-600); }
.btn-secondary { background: #fff; color: var(--ink); border-color: var(--line); }
.btn-secondary:hover { background: var(--bg); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-accent { background: var(--accent); color: #201400; }
.btn-oauth { background: #fff; color: var(--ink); border-color: var(--line); }
.btn-oauth:hover { background: var(--bg); }
.btn-apple { background: #000; color: #fff; border-color: #000; }
.btn-full { width: 100%; margin-bottom: 10px; }
button:disabled { opacity: .55; cursor: not-allowed; }

/* ============================ FORMS ============================ */
.form-field { margin-bottom: 14px; }
.form-field label { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 6px; }
.form-field input, .form-field select, .form-field textarea {
  width: 100%; padding: 12px 14px; font-size: 15px; font-family: var(--font-body);
  border: 1px solid var(--line); border-radius: var(--radius-sm); background: #fff; color: var(--ink);
}
.form-field input:focus, .form-field select:focus, .form-field textarea:focus {
  outline: none; border-color: var(--brand-primary); box-shadow: 0 0 0 3px rgba(4,106,56,.14);
}
.form-field textarea { min-height: 110px; resize: vertical; }
.required { color: var(--danger); }

/* ============================ APP LAYOUT ============================ */
#app-screen.active { display: block; min-height: 100dvh; padding-left: var(--sidebar-w); }

.sidebar {
  background: var(--brand-primary); color: #C4D8CC;
  display: flex; flex-direction: column; padding: 18px 14px;
  position: fixed; top: 0; left: 0; width: var(--sidebar-w); height: 100dvh; z-index: 40;
}
.sidebar-brand { display: flex; align-items: center; gap: 10px; padding: 8px 8px 18px; }
.brand-mark { width: 34px; height: 34px; }
.brand-name { font-family: var(--font-display); font-weight: 700; color: #fff; font-size: 16px; }
.brand-tagline { font-size: 11px; color: #8FB09C; }
.sidebar-nav {
  flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 2px; overflow-y: auto;
  /* scroll hint: เงาบน/ล่างบอกว่ายังเลื่อนได้ */
  background:
    linear-gradient(var(--brand-primary) 30%, rgba(4,106,56,0)) top / 100% 22px no-repeat,
    linear-gradient(rgba(4,106,56,0), var(--brand-primary) 70%) bottom / 100% 22px no-repeat,
    radial-gradient(farthest-side at 50% 0, rgba(245,166,35,.55), rgba(0,0,0,0)) top / 100% 12px no-repeat,
    radial-gradient(farthest-side at 50% 100%, rgba(245,166,35,.55), rgba(0,0,0,0)) bottom / 100% 12px no-repeat;
  background-attachment: local, local, scroll, scroll;
}

.nav-item {
  display: flex; align-items: center; gap: 12px; width: 100%;
  padding: 11px 12px; border-radius: var(--radius-sm); background: transparent;
  border: none; color: #C4D8CC; font-size: 14.5px; font-family: var(--font-body);
  cursor: pointer; text-align: left; transition: all .15s var(--ease);
}
.nav-item:hover { background: rgba(255,255,255,.07); color: #fff; }
.nav-item.active { background: var(--accent); color: #201400; font-weight: 600; }
.nav-item svg { flex-shrink: 0; }
.nav-group-label { font-size: 11px; text-transform: uppercase; letter-spacing: .5px; color: #7FA089; padding: 14px 12px 6px; }

.sidebar-footer { border-top: 1px solid rgba(255,255,255,.10); padding-top: 12px; margin-top: 12px; }
.user-info { display: flex; align-items: center; gap: 10px; padding: 6px 8px 12px; }
.user-avatar { width: 36px; height: 36px; border-radius: 50%; background: var(--accent); color: #201400; display: grid; place-items: center; font-weight: 700; }
.user-name { color: #fff; font-size: 14px; font-weight: 600; }
.user-role { color: #8FB09C; font-size: 12px; }

/* Topbar + bottom nav (mobile only) */
.topbar, .bottom-nav { display: none; }

/* ============================ MAIN CONTENT ============================ */
.main-content { padding: 28px 32px; max-width: 1200px; min-width: 0; overflow-x: clip; }

.page-header { margin-bottom: 22px; }
.page-title { font-family: var(--font-display); font-size: 24px; font-weight: 700; color: var(--brand-primary); }
.page-subtitle { color: var(--muted); font-size: 14px; margin-top: 3px; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 18px; min-width: 0; }
.card-header { padding: 16px 18px; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; align-items: center; }
.card-title { font-size: 16px; font-weight: 600; }
.card-body { padding: 18px; }

/* Stat grid */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(200px,100%), 1fr)); gap: 14px; margin-bottom: 18px; }
.stat-card { background: var(--surface); border: 1px solid var(--line); border-left: 4px solid var(--brand-primary); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); }
.stat-card.amber { border-left-color: var(--accent); }
.stat-card.safe { border-left-color: var(--safe); }
.stat-card.danger { border-left-color: var(--danger); }
.stat-label { font-size: 13px; color: var(--muted); }
.stat-value { font-family: var(--font-display); font-size: 30px; font-weight: 700; color: var(--brand-primary); margin-top: 4px; }

/* Status badges (safety colors) */
.status-badge { display: inline-flex; align-items: center; gap: 5px; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; word-break: normal; overflow-wrap: normal; flex-shrink: 0; }
.status-badge.pass { background: #dcfce7; color: #166534; }
.status-badge.fail { background: #fee2e2; color: #991b1b; }
.status-badge.warn { background: #fef3c7; color: #92400e; }
.status-badge.na { background: #eef1f5; color: var(--neutral); }

/* ============================ MODAL ============================ */
.modal-backdrop { position: fixed; inset: 0; background: rgba(3,50,27,.55); display: grid; place-items: center; padding: 16px; z-index: 1000; opacity: 0; transition: opacity .2s; }
.modal-backdrop.open { opacity: 1; }
.modal-content { background: #fff; border-radius: 16px; width: 100%; max-width: 460px; max-height: 92dvh; overflow-y: auto; box-shadow: 0 30px 80px rgba(0,0,0,.35); }
.modal-large { max-width: 720px; }
.modal-header { display: flex; justify-content: space-between; align-items: center; padding: 18px 20px; border-bottom: 1px solid var(--line); }
.modal-title { font-family: var(--font-display); font-size: 18px; font-weight: 700; color: var(--brand-primary); }
.modal-close-btn { background: none; border: none; font-size: 26px; line-height: 1; color: var(--muted); cursor: pointer; }
.modal-body { padding: 20px; }
.modal-footer { display: flex; justify-content: flex-end; gap: 10px; padding: 16px 20px; border-top: 1px solid var(--line); }

/* ============================ TOAST ============================ */
#toast-container { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 2000; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.toast { padding: 12px 18px; border-radius: 10px; color: #fff; font-size: 14px; font-weight: 500; box-shadow: 0 8px 24px rgba(0,0,0,.2); opacity: 0; transform: translateY(12px); transition: all .3s var(--ease); max-width: 90vw; }
.toast.show { opacity: 1; transform: translateY(0); }
.toast-success { background: var(--safe); }
.toast-error { background: var(--danger); }
.toast-warning { background: var(--warning); color: #201400; }
.toast-info { background: var(--brand-primary); }

.icon-btn { background: none; border: none; color: var(--brand-primary); cursor: pointer; padding: 6px; display: grid; place-items: center; }

/* ============================ RESPONSIVE (mobile) ============================ */
@media (max-width: 860px) {
  #app-screen.active { padding-left: 0; }
  .sidebar { display: none; }

  .topbar {
    display: flex; align-items: center; gap: 8px;
    position: sticky; top: 0; z-index: 50;
    padding: 10px 12px; padding-top: max(10px, env(safe-area-inset-top));
    background: #fff; border-bottom: 1px solid var(--line);
  }
  .topbar-title { flex: 1; font-family: var(--font-display); font-weight: 700; color: var(--brand-primary); font-size: 17px; text-align: center; }

  .main-content { padding: 16px 14px 90px; max-width: 100%; }

  .bottom-nav {
    display: flex; position: fixed; bottom: 0; left: 0; right: 0; z-index: 50;
    background: #fff; border-top: 1px solid var(--line);
    padding: 6px 4px; padding-bottom: max(6px, env(safe-area-inset-bottom));
    justify-content: space-around;
  }
  .bottom-nav .nav-item {
    flex-direction: column; gap: 3px; font-size: 10.5px; padding: 6px 4px;
    color: var(--muted); border-radius: 10px; flex: 1; text-align: center; align-items: center;
  }
  .bottom-nav .nav-item.active { background: transparent; color: var(--brand-primary); font-weight: 600; }
  .bottom-nav .nav-item.active svg { color: var(--accent); }

  .page-title { font-size: 21px; }
  .stat-value { font-size: 26px; }
}

@media (min-width: 861px) {
  .topbar { display: none !important; }
}

/* ============================ CHECKLIST ENGINE ============================ */
.chk-toolbar { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; }
.chk-meta { font-size: 13px; color: var(--muted); margin-top: 4px; }
.chk-item { border: 1px solid var(--line); border-radius: 10px; padding: 12px; margin-bottom: 12px; background: #fff; }
.chk-item-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; font-weight: 600; color: var(--muted); }
.chk-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(160px, 100%), 1fr)); gap: 10px; }
.chk-flags { display: flex; gap: 16px; flex-wrap: wrap; margin-top: 6px; font-size: 14px; }
.chk-flags label { display: flex; align-items: center; gap: 6px; cursor: pointer; }
.icon-sm { background: none; border: 1px solid var(--line); border-radius: 8px; padding: 4px 10px; cursor: pointer; color: var(--muted); font-size: 14px; }
.icon-sm:hover:not(:disabled) { background: var(--bg); }
.tpl-row { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 12px; border: 1px solid var(--line); border-radius: 10px; margin-bottom: 8px; cursor: pointer; }
.tpl-row:hover { background: var(--bg); }

/* ============================ TOPBAR BRAND + LANG LABEL ============================ */
.topbar-brand { flex: 1; display: flex; align-items: center; justify-content: center; gap: 8px; min-width: 0; }
.topbar-logo { width: 26px; height: 26px; border-radius: 6px; object-fit: contain; background: #fff; }
.topbar .topbar-title { flex: none; max-width: 60vw; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lang-label { font-weight: 700; font-size: 13px; color: var(--brand-primary); border: 1.5px solid var(--line); border-radius: 999px; padding: 3px 10px; }

/* ============================ DRAWER (เมนู "เพิ่มเติม" แบบ tile grid) ============================ */
.drawer-backdrop { position: fixed; inset: 0; background: rgba(3,50,27,.5); z-index: 900; opacity: 0; transition: opacity .2s; }
.drawer-backdrop.open { opacity: 1; }
.drawer {
  position: absolute; inset: 0; background: var(--bg);
  display: flex; flex-direction: column;
  transform: translateY(100%); transition: transform .28s var(--ease);
  padding: 18px 16px;
  padding-top: max(18px, env(safe-area-inset-top));
  padding-bottom: max(14px, env(safe-area-inset-bottom));
}
.drawer-backdrop.open .drawer { transform: translateY(0); }
.drawer-head { display: flex; align-items: center; gap: 12px; padding-bottom: 14px; border-bottom: 1px solid var(--line); }
.drawer-logo { width: 46px; height: 46px; border-radius: 12px; object-fit: contain; background: #fff; border: 1px solid var(--line); }
.drawer-brand { font-family: var(--font-display); font-size: 20px; font-weight: 700; color: var(--brand-primary); min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.drawer-scroll { flex: 1; min-height: 0; overflow-y: auto; padding: 6px 2px 12px;
  background:
    radial-gradient(farthest-side at 50% 0, rgba(4,106,56,.18), rgba(0,0,0,0)) top / 100% 12px no-repeat,
    radial-gradient(farthest-side at 50% 100%, rgba(4,106,56,.18), rgba(0,0,0,0)) bottom / 100% 12px no-repeat;
  background-attachment: scroll, scroll;
}
.drawer-group-label { font-size: 14px; font-weight: 700; color: var(--muted); margin: 16px 4px 10px; }
.drawer-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 18px 8px; }
.drawer-tile { background: none; border: none; cursor: pointer; display: flex; flex-direction: column; align-items: center; gap: 8px; font-family: var(--font-body); padding: 0; }
.tile-icon { width: 64px; height: 64px; border-radius: 16px; background: linear-gradient(160deg, var(--brand-primary), var(--brand-primary-600)); color: #fff; display: grid; place-items: center; border: 2.5px solid transparent; box-shadow: var(--shadow); }
.tile-icon svg { width: 26px; height: 26px; }
.drawer-tile.active .tile-icon { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(245,166,35,.25); }
.tile-label { font-size: 13px; color: var(--ink); text-align: center; }
.drawer-actions { display: flex; gap: 10px; justify-content: space-between; padding: 12px 0; }
.drawer-pill { display: inline-flex; align-items: center; gap: 8px; padding: 12px 18px; border-radius: 999px; border: 1px solid var(--line); background: #fff; font-size: 15px; font-weight: 600; color: var(--brand-primary); cursor: pointer; font-family: var(--font-body); }
.drawer-user { display: flex; align-items: center; gap: 12px; background: #fff; border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; }
.drawer-user-name { font-weight: 700; color: var(--ink); }
.drawer-user-role { font-size: 13px; color: var(--muted); }

/* template picker rows: คอลัมน์คงที่ ไม่แตกแถว */
.tpl-row > div:first-child { min-width: 0; flex: 1; }

/* ============================ USERS & ADMINS LIST ============================ */
.users-table { display: flex; flex-direction: column; gap: 8px; }
.user-row { display: flex; align-items: center; gap: 12px; padding: 12px 14px; background: #fff; border: 1px solid var(--line); border-radius: 12px; }
.user-row .user-avatar { width: 40px; height: 40px; flex-shrink: 0; }
.user-row-name { font-weight: 600; color: var(--ink); overflow: hidden; text-overflow: ellipsis; }

/* Users table (scrollable, wider email column) */
.users-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; max-width: 100%; border: 1px solid var(--line); border-radius: 12px; }
.users-table2 { width: 100%; border-collapse: collapse; min-width: 640px; }
.users-table2 th { text-align: left; font-size: 12px; font-weight: 600; color: var(--muted); padding: 12px 14px; border-bottom: 1px solid var(--line); background: var(--bg); white-space: nowrap; }
.users-table2 td { padding: 12px 14px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.users-table2 tbody tr:last-child td { border-bottom: none; }
.ut-user { display: flex; align-items: center; gap: 10px; min-width: 0; }
.ut-user .user-avatar { width: 38px; height: 38px; flex-shrink: 0; }
.ut-name { font-weight: 600; color: var(--ink); white-space: nowrap; }
.ut-email { color: var(--muted); font-size: 14px; min-width: 220px; white-space: nowrap; }
.ut-actions { display: flex; gap: 8px; flex-wrap: nowrap; align-items: center; }
.ut-actions .btn-sm { white-space: nowrap; flex-shrink: 0; }
.users-table2 th:last-child, .users-table2 td:last-child { min-width: 190px; }

/* ============================ INSPECTION RUN ============================ */
.run-item { transition: border-color .18s var(--ease); }
.run-item.is-pass { border-left: 4px solid var(--safe); }
.run-item.is-fail { border-left: 4px solid var(--danger); background: #fff7f7; }
.run-item-head { display: flex; gap: 10px; margin-bottom: 10px; }
.run-item-num { font-size: 12px; font-weight: 700; color: var(--muted); }
.run-item-text { font-size: 15.5px; font-weight: 600; color: var(--ink); line-height: 1.4; margin-top: 2px; }
.run-choices { display: grid; grid-template-columns: repeat(3, minmax(0,1fr)); gap: 8px; }
.choice-btn { min-height: 46px; border-radius: 10px; border: 1.5px solid var(--line); background: #fff; color: var(--ink); font-family: var(--font-body); font-size: 14.5px; font-weight: 600; cursor: pointer; }
.choice-btn.active-pass { background: var(--safe); color: #fff; border-color: var(--safe); }
.choice-btn.active-fail { background: var(--danger); color: #fff; border-color: var(--danger); }
.choice-btn.active-na { background: var(--neutral); color: #fff; border-color: var(--neutral); }
.run-photos { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.thumb { position: relative; width: 84px; height: 84px; border-radius: 10px; overflow: hidden; border: 1px solid var(--line); background: #fff; display: grid; place-items: center; }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb-x { position: absolute; top: 4px; right: 4px; width: 22px; height: 22px; border-radius: 50%; border: none; background: rgba(0,0,0,.65); color: #fff; font-size: 16px; line-height: 1; cursor: pointer; }
.thumb-add { flex-direction: column; gap: 4px; color: var(--muted); font-size: 11px; cursor: pointer; border-style: dashed; background: var(--bg); }
.run-fab { position: sticky; bottom: 8px; padding: 8px 0; margin-top: 16px; }
@media (max-width: 860px) { .run-fab { bottom: 74px; } }   /* เผื่อ bottom-nav */

/* ============================ CAPA ============================ */
.capa-filters { display: flex; gap: 8px; overflow-x: auto; margin-bottom: 14px; padding-bottom: 4px; }
.pill { flex-shrink: 0; padding: 8px 16px; border-radius: 999px; border: 1.5px solid var(--line); background: #fff; color: var(--muted); font-size: 14px; font-weight: 600; cursor: pointer; font-family: var(--font-body); }
.pill.active { background: var(--brand-primary); color: #fff; border-color: var(--brand-primary); }
.capa-card { cursor: pointer; padding: 14px 16px; margin-bottom: 12px; transition: transform .12s var(--ease); }
.capa-card:active { transform: scale(.99); }
.capa-card.is-overdue { border-left: 4px solid var(--danger); }
.capa-card-head { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; }
.capa-card-title { font-weight: 600; color: var(--ink); font-size: 15px; line-height: 1.4; margin-bottom: 6px; }
.capa-card-foot { display: flex; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin-top: 10px; font-size: 13px; color: var(--muted); }
.capa-kv { display: flex; justify-content: space-between; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
.capa-kv:last-child { border: none; }
.capa-kv strong { text-align: right; color: var(--ink); }
.capa-actions { display: flex; gap: 10px; flex-wrap: wrap; margin: 8px 0 20px; }
.capa-actions button { flex: 1; min-width: 140px; }
.tl-row { display: flex; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.tl-row:last-child { border: none; }
.tl-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--accent); flex-shrink: 0; margin-top: 6px; }
.tl-title { font-size: 14px; color: var(--ink); }

/* ============================ DASHBOARD ============================ */
.dash-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(320px, 100%), 1fr)); gap: 14px; margin-bottom: 18px; }
.kpi-sub { display: block; font-size: 12px; color: var(--muted); font-weight: 500; font-family: var(--font-body); margin-top: 4px; }
.risk-row { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.risk-row:last-child { border: none; }
.risk-bar-wrap { flex: 1; height: 8px; background: var(--bg); border-radius: 999px; overflow: hidden; min-width: 60px; max-width: 180px; }
.risk-bar { height: 100%; border-radius: 999px; transition: width .3s var(--ease); }
.risk-bar.fail { background: var(--danger); }
.risk-bar.warn { background: var(--warning); }
.risk-bar.na { background: var(--safe); }

/* ============================ NAV BADGE ============================ */
.nav-item { position: relative; }
.nav-badge {
  position: absolute; top: 4px; right: 8px;
  min-width: 18px; height: 18px; padding: 0 5px; border-radius: 999px;
  background: var(--warning); color: #201400; font-size: 11px; font-weight: 700;
  display: grid; place-items: center;
}
.nav-badge.is-overdue { background: var(--danger); color: #fff; }
@media (max-width: 860px) {
  .bottom-nav .nav-badge { top: 2px; right: 20%; }
}

/* ============================ AI COPILOT ============================ */
.cop-chat { display: flex; flex-direction: column; gap: 12px; min-height: 300px; max-height: calc(100dvh - 260px); overflow-y: auto; padding: 8px 2px 14px; }
.cop-empty { text-align: center; color: var(--muted); padding: 30px 10px; }
.cop-empty p { margin: 12px 0; }
.cop-suggests { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.cop-bubble { max-width: 85%; padding: 11px 14px; border-radius: 14px; font-size: 15px; line-height: 1.5; white-space: pre-wrap; }
.cop-user { align-self: flex-end; background: var(--brand-primary); color: #fff; border-bottom-right-radius: 4px; }
.cop-assistant { align-self: flex-start; background: #fff; border: 1px solid var(--line); color: var(--ink); border-bottom-left-radius: 4px; }
.cop-typing .cop-body { color: var(--muted); }
.cop-badge { font-size: 11px; color: var(--muted); margin-top: 6px; }
.cop-input { display: flex; gap: 8px; align-items: flex-end; position: sticky; bottom: 0; background: var(--bg); padding: 8px 0; }
@media (max-width: 860px) { .cop-input { bottom: 66px; } .cop-chat { max-height: calc(100dvh - 300px); } }
.cop-input textarea { flex: 1; resize: none; padding: 12px 14px; border: 1px solid var(--line); border-radius: 12px; font-family: var(--font-body); font-size: 15px; max-height: 120px; }
.cop-input .btn-primary { min-width: 46px; padding: 12px; border-radius: 12px; align-self: stretch; }

/* ============================ COPILOT header + panel (B) ============================ */
.cop-header { display: flex; align-items: flex-start; gap: 12px; }
.cop-kb-btn { flex-shrink: 0; display: inline-flex; align-items: center; gap: 6px; }
.cop-panel { border: 1px solid var(--line); border-radius: 16px; background: #fbfcfe; box-shadow: var(--shadow); overflow: hidden; display: flex; flex-direction: column; }
.cop-panel-label { display: flex; align-items: center; gap: 7px; padding: 10px 16px; border-bottom: 1px solid var(--line); background: #fff; font-size: 13px; font-weight: 600; color: var(--muted); flex-shrink: 0; }
.cop-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--safe); box-shadow: 0 0 0 3px rgba(22,163,74,.15); }
.cop-panel .cop-chat { flex: 1 1 auto; min-height: 0; max-height: none; padding: 14px; margin: 0; overflow-y: auto; }
.cop-panel .cop-input { padding: 10px 12px; border-top: 1px solid var(--line); background: #fff; position: static; bottom: auto; flex-shrink: 0; }
@media (min-width: 861px) { .cop-panel { height: calc(100dvh - 240px); min-height: 420px; } }
@media (max-width: 860px) { .cop-panel { height: calc(100dvh - 205px); } .cop-panel .cop-input { position: static; bottom: auto; } }
.kb-upload { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.line-code { font-family: var(--font-display); font-size: 34px; font-weight: 700; letter-spacing: 6px; color: var(--brand-primary); margin: 10px 0; }

/* ============================ COMPLIANCE REPORT preview ============================ */
.rep-frame { overflow-x: auto; -webkit-overflow-scrolling: touch; border: 1px solid var(--line); border-radius: 10px; padding: 12px; background: #fff; max-width: 100%; }
.rep-frame .rep-doc { min-width: 620px; font-size: 13px; }
.rep-frame .rep-head { display: flex; align-items: center; gap: 12px; border-bottom: 3px solid var(--brand-primary); padding-bottom: 10px; }
.rep-frame .rep-logo { width: 48px; height: 48px; object-fit: contain; }
.rep-frame .rep-org { font-size: 18px; font-weight: 700; color: var(--brand-primary); }
.rep-frame .rep-title { font-size: 14px; color: var(--muted); }
.rep-frame .rep-meta { display: flex; gap: 16px; flex-wrap: wrap; font-size: 12px; color: var(--muted); margin: 10px 0; }
.rep-frame .rep-iso { background: #f0f4f9; border-left: 4px solid var(--accent); padding: 8px 12px; font-size: 12px; margin: 10px 0; }
.rep-frame h3 { color: var(--brand-primary); font-size: 14px; margin: 16px 0 6px; border-bottom: 1px solid var(--line); padding-bottom: 4px; }
.rep-frame .rep-kpis { display: grid; grid-template-columns: repeat(4,1fr); gap: 8px; margin: 10px 0; }
.rep-frame .rep-kpi { border: 1px solid var(--line); border-radius: 8px; padding: 10px; text-align: center; }
.rep-frame .rep-kpi-v { font-size: 20px; font-weight: 700; color: var(--brand-primary); }
.rep-frame .rep-kpi-l { font-size: 10px; color: var(--muted); }
.rep-frame .rep-table { width: 100%; border-collapse: collapse; font-size: 12px; margin: 6px 0; }
.rep-frame .rep-table th, .rep-frame .rep-table td { border: 1px solid var(--line); padding: 6px 8px; text-align: left; }
.rep-frame .rep-table th { background: var(--brand-primary); color: #fff; }
.rep-frame .rep-sign { display: flex; gap: 30px; margin: 30px 0 10px; }
.rep-frame .rep-sign-box { flex: 1; text-align: center; font-size: 12px; color: var(--muted); }
.rep-frame .rep-sign-line { border-top: 1px solid var(--ink); margin-bottom: 6px; height: 30px; }
.rep-frame .rep-foot { text-align: center; font-size: 10px; color: #9aa7bd; margin-top: 16px; border-top: 1px solid var(--line); padding-top: 6px; }

/* ============================ CHAT MONITOR (console) ============================ */
.con-row { display: flex; align-items: center; gap: 12px; }
.con-avatar { width: 44px; height: 44px; border-radius: 50%; background: linear-gradient(160deg, var(--brand-primary), var(--brand-primary-600)); color: #fff; display: grid; place-items: center; font-weight: 700; flex-shrink: 0; }
.con-last { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 46vw; }
/* หน้าห้องแชท: คอลัมน์เดียว bound ความสูง — แถวปุ่มบน / ข้อมูลบรรทัดเดียว / แชทเลื่อน / input ล่าง */
.con-room { display: flex; flex-direction: column; background: #fbfcfe; border: 1px solid var(--line); border-radius: 16px; overflow: hidden; box-shadow: var(--shadow); }
.con-room-bar { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 12px 14px; border-bottom: 1px solid var(--line); background: #fff; flex-shrink: 0; }
.con-room-bar #con-back, .con-room-bar #con-takeover { flex-shrink: 0; white-space: nowrap; }
.con-room-info { display: flex; align-items: center; gap: 10px; padding: 10px 14px; border-bottom: 1px solid var(--line); background: #fff; flex-shrink: 0; min-width: 0; }
.con-room-name { font-family: var(--font-display); font-weight: 700; color: var(--brand-primary); font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.con-room-info .status-badge { flex-shrink: 0; }
.con-room-chan { flex-shrink: 0; }
.con-room .cop-chat { flex: 1 1 auto; min-height: 0; max-height: none; overflow-y: auto; padding: 14px; margin: 0; }
.con-room .cop-input { flex-shrink: 0; border-top: 1px solid var(--line); background: #fff; position: static; bottom: auto; padding: 10px 12px; }
@media (min-width: 861px) { .con-room { height: calc(100dvh - 56px); } }
@media (max-width: 860px) {
  .con-room {
    position: fixed; left: 0; right: 0;
    top: calc(52px + max(10px, env(safe-area-inset-top, 0px)));
    bottom: calc(36px + max(6px, env(safe-area-inset-bottom, 0px)));
    height: auto; border-radius: 0; border-left: none; border-right: none; z-index: 30;
  }
}
/* บับเบิลในหน้า monitor: ลูกค้าซ้ายขาว · AI ขวาน้ำเงิน · แอดมินขวาส้ม (ตัวอักษรเข้มอ่านง่าย) */
.cop-bubble.con-user { align-self: flex-start; background: #fff; border: 1px solid var(--line); color: var(--ink); border-bottom-left-radius: 4px; }
.cop-bubble.con-ai { align-self: flex-end; background: var(--brand-primary); color: #fff; border-bottom-right-radius: 4px; }
.cop-bubble.con-agent { align-self: flex-end; background: #FFF3E0; border: 1px solid var(--accent); color: #7A4A00; border-bottom-right-radius: 4px; }
.cop-bubble.con-agent .cop-badge { color: #B26A00; }
/* มือถือ: กล่องพิมพ์อยู่ล่างสุดชิด bottom-nav */
@media (max-width: 860px) {
  .cop-header.page-header { margin-bottom: 12px; }
}

/* Photo AI button in copilot */
.cop-photo-btn { min-width: 46px; padding: 12px; border-radius: 12px; border: 1px solid var(--line); background: #fff; color: var(--brand-primary); cursor: pointer; align-self: stretch; display: grid; place-items: center; }
.cop-photo-btn:hover { background: var(--bg); }

/* ============================ PREDICTIVE AI (Phase 3) ============================ */
.pred-select { padding: 8px 12px; border: 1px solid var(--line); border-radius: 10px; font-family: var(--font-body); font-size: 14px; background: #fff; }
.pred-kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-bottom: 16px; }
.pred-kpi { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 14px 10px; text-align: center; box-shadow: var(--shadow); }
.pred-kpi-v { font-size: 26px; font-weight: 700; color: var(--brand-primary); font-family: var(--font-display); }
.pred-kpi-l { font-size: 11px; color: var(--muted); margin-top: 2px; }
@media (max-width: 560px) { .pred-kpis { grid-template-columns: repeat(2, 1fr); } }
.pred-insight { border-left: 4px solid var(--accent); }
.pred-insight-body { font-size: 14px; line-height: 1.65; color: var(--ink); }
.pred-hotspot { display: flex; align-items: center; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.pred-hotspot:last-child { border-bottom: none; }
.pred-rank { width: 26px; height: 26px; border-radius: 50%; background: var(--brand-primary); color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 13px; flex-shrink: 0; }
.pred-zone { font-weight: 600; color: var(--ink); margin-bottom: 6px; }
.pred-bar { height: 8px; background: var(--bg); border-radius: 6px; overflow: hidden; margin-bottom: 6px; }
.pred-bar-fill { height: 100%; border-radius: 6px; transition: width .5s; }
.pred-tags { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; font-size: 12px; }
.pred-score { font-size: 24px; font-weight: 700; font-family: var(--font-display); flex-shrink: 0; min-width: 40px; text-align: right; }
.pred-recur { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
.pred-recur:last-child { border-bottom: none; }

/* ============================ IoT / CCTV Bridge ============================ */
.iot-field { margin-top: 8px; }
.iot-field label { display: block; font-size: 11px; color: var(--muted); margin-bottom: 3px; font-weight: 600; }
.iot-field input { width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; font-family: monospace; font-size: 12px; background: var(--bg); color: var(--ink); }
.btn-sm { padding: 6px 12px !important; font-size: 13px !important; }

/* ============================ SCROLL HINT (เมนูเลื่อนได้) ============================ */
/* (drawer/sidebar ใช้ position เดิม absolute/fixed ซึ่งเป็น containing block ให้ scroll-hint อยู่แล้ว) */
.scroll-hint {
  position: absolute; left: 0; right: 0; height: 26px; display: none;
  align-items: center; justify-content: center; cursor: pointer; z-index: 5;
}
.scroll-hint.show { display: flex; }
.scroll-hint svg { animation: hintBounce 1.4s ease-in-out infinite; }
.sidebar .scroll-hint { color: var(--accent); }
.sidebar .scroll-hint-up { background: linear-gradient(var(--brand-primary), rgba(4,106,56,0)); }
.sidebar .scroll-hint-down { background: linear-gradient(rgba(4,106,56,0), var(--brand-primary)); }
.drawer .scroll-hint { color: var(--brand-primary); }
.drawer .scroll-hint-up { background: linear-gradient(#fff, rgba(255,255,255,0)); }
.drawer .scroll-hint-down { background: linear-gradient(rgba(255,255,255,0), #fff); }
@keyframes hintBounce { 0%,100% { transform: translateY(0); opacity: .55; } 50% { transform: translateY(3px); opacity: 1; } }
.scroll-hint-up svg { animation-name: hintBounceUp; }
@keyframes hintBounceUp { 0%,100% { transform: translateY(0); opacity: .55; } 50% { transform: translateY(-3px); opacity: 1; } }

/* ============================ PHOTO SOURCE PICKER ============================ */
.photo-src-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.photo-src-btn { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; padding: 24px 12px; border: 1.5px solid var(--line); border-radius: 14px; background: #fff; color: var(--brand-primary); font-family: var(--font-body); font-size: 15px; font-weight: 600; cursor: pointer; transition: all .15s var(--ease); }
.photo-src-btn:hover { background: var(--bg); border-color: var(--brand-primary); }
.photo-src-btn:active { transform: scale(.97); }

/* ============================ COPILOT ROOM (ผู้ช่วย AI — header fix, chat scroll, input bottom) ============================ */
.cop-room { display: flex; flex-direction: column; background: #fbfcfe; border: 1px solid var(--line); border-radius: 16px; overflow: hidden; box-shadow: var(--shadow); }
.cop-room-head { display: flex; align-items: flex-start; gap: 12px; padding: 12px 16px; border-bottom: 1px solid var(--line); background: #fff; flex-shrink: 0; }
.cop-room-head .page-title { font-size: 20px; }
.cop-room-head .page-subtitle { font-size: 12.5px; margin-top: 2px; }
.cop-room-head .cop-kb-btn { flex-shrink: 0; white-space: nowrap; }
.cop-room .cop-chat { flex: 1 1 auto; min-height: 0; max-height: none; overflow-y: auto; padding: 14px; margin: 0; }
.cop-room .cop-input { flex-shrink: 0; border-top: 1px solid var(--line); background: #fff; position: static; bottom: auto; padding: 10px 12px; }
@media (min-width: 861px) { .cop-room { height: calc(100dvh - 56px); } }
@media (max-width: 860px) {
  .cop-room {
    position: fixed; left: 0; right: 0;
    top: calc(52px + max(10px, env(safe-area-inset-top, 0px)));
    bottom: calc(36px + max(6px, env(safe-area-inset-bottom, 0px)));
    height: auto; border-radius: 0; border-left: none; border-right: none; z-index: 30;
  }
  .cop-room-head { flex-wrap: wrap; }
}

/* ============ Super-admin + Onboarding wizard ============ */
.sa-list .table-wrap { overflow-x: auto; }
.ob-dots { display: flex; align-items: center; gap: 6px; margin-bottom: 14px; }
.ob-dot { width: 28px; height: 28px; border-radius: 50%; display: flex; align-items: center; justify-content: center;
  background: var(--bg); border: 2px solid var(--line); color: var(--muted); font-weight: 700; font-size: .85rem; }
.ob-dot.on { background: var(--brand-primary); border-color: var(--brand-primary); color: #fff; }
.ob-dot.done { background: var(--safe); border-color: var(--safe); color: #fff; }
.ob-line { flex: 1; height: 2px; background: var(--line); }
.ob-row { display: grid; grid-template-columns: minmax(0,1fr) auto; gap: 8px; margin-bottom: 8px; }
.ob-check { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border: 1px solid var(--line);
  border-radius: 10px; margin-bottom: 8px; cursor: pointer; }
.ob-check input { width: 18px; height: 18px; accent-color: var(--brand-primary); }
.ob-pass { background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px;
  font-size: .85rem; white-space: pre-wrap; overflow-wrap: anywhere; cursor: copy; }

/* ============ Super-admin: ตาราง responsive เลื่อนซ้ายขวาได้ ============ */
.sa-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; border-radius: 0 0 14px 14px; }
.sa-table { min-width: 680px; width: 100%; }          /* แคบกว่านี้ = เลื่อนแนวนอน */
.sa-table th, .sa-table td { white-space: nowrap; padding: 10px 12px; }
.sa-table td:first-child { white-space: normal; min-width: 150px; }  /* ชื่อบริษัทตัดบรรทัดได้ */
.sa-tid { font-size: .72rem; overflow-wrap: anywhere; }
.sa-actions { display: flex; gap: 6px; }
.sa-actions .btn { white-space: nowrap; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
@media (max-width: 860px) {
  .sa-table { min-width: 620px; font-size: .85rem; }
  .sa-table th, .sa-table td { padding: 8px 9px; }
}

/* ============ จัดการผู้ดูแลแพลตฟอร์ม ============ */
.sa-admin-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.sa-admin-row { display: flex; align-items: center; justify-content: space-between; gap: 10px;
  border: 1px solid var(--line); border-radius: 10px; padding: 9px 12px; }
.sa-admin-info { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; min-width: 0; }
.sa-admin-info .muted { font-size: .82rem; overflow-wrap: anywhere; }
.sa-admin-row .btn { white-space: nowrap; flex-shrink: 0; }
.sa-admin-add { display: grid; grid-template-columns: minmax(0,1fr) auto; gap: 8px; }
.sa-admin-add input { min-width: 0; }
@media (max-width: 520px) { .sa-admin-add { grid-template-columns: 1fr; } }

/* ============ Wizard/Super-admin: input มาตรฐาน + ปุ่มขนาดเดียวกัน ============ */
/* input/select ใน wizard และช่องเพิ่ม admin ให้หน้าตาเหมือน .form-field ทุกส่วนของแอป */
.ob-row input, .ob-row select, .sa-admin-add input {
  width: 100%; padding: 12px 14px; border: 1.5px solid var(--line); border-radius: 12px;
  font: inherit; font-size: 16px; background: #fff; color: var(--ink); min-width: 0;
}
.ob-row input:focus, .ob-row select:focus, .sa-admin-add input:focus {
  outline: none; border-color: var(--brand-primary);
  box-shadow: 0 0 0 3px rgba(4, 106, 56, .12);
}
.ob-row { grid-template-columns: minmax(0,1.6fr) minmax(0,1fr); }
/* แถวปุ่มนำทาง wizard: ปุ่มสูงเท่ากัน กว้างเท่ากัน */
.ob-nav { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 16px; }
.ob-btn { padding: 12px 16px; border-radius: 12px; font-size: 15px; font-weight: 600; white-space: nowrap; }
/* ช่องเพิ่มผู้ดูแลแพลตฟอร์ม: input เต็ม + ปุ่มบรรทัดถัดไปบนมือถือ */
.sa-admin-add { display: grid; grid-template-columns: minmax(0,1fr) auto; gap: 10px; align-items: stretch; }
.sa-admin-add .btn { white-space: nowrap; padding: 12px 18px; border-radius: 12px; }
@media (max-width: 520px) {
  .sa-admin-add { grid-template-columns: 1fr; }
  .ob-row { grid-template-columns: 1fr; }
}
/* การ์ดแพ็กเกจในหน้าตั้งค่า: แถวห่างอ่านง่าย */
.plan-info { display: flex; flex-direction: column; gap: 8px; }
.plan-row { display: flex; align-items: baseline; gap: 10px; }
.plan-label { color: var(--muted); min-width: 120px; flex-shrink: 0; }
.plan-label::after { content: ':'; }
