/* ===================================================================
   Moto Care - ธีมหลัก
   โทนสีสดใส ทันสมัย รองรับทั้งจอคอมพิวเตอร์และมือถือ + โหมดมืด
   =================================================================== */

:root {
  color-scheme: light;

  /* --- สีแบรนด์ (ไล่เฉดม่วง-น้ำเงิน) --- */
  --brand-50:  #f2eeff;
  --brand-100: #e4dcff;
  --brand-200: #c9b9ff;
  --brand-400: #8b6bff;
  --brand-500: #6d4aff;
  --brand-600: #5a35e8;
  --brand-700: #4725bd;
  --brand-grad: linear-gradient(135deg, #6d4aff 0%, #9b5cff 50%, #c45ce8 100%);
  --hero-grad: linear-gradient(135deg, #6d4aff 0%, #8d55ff 45%, #b85ce8 100%);

  /* --- สีประกอบที่สดใส --- */
  --cyan:   #00b8d4;
  --cyan-soft: #e0f7fb;
  --teal:   #0fb98a;
  --teal-soft: #e2f8f1;
  --amber:  #f59e0b;
  --amber-soft: #fef3e2;
  --orange: #ff7a45;
  --orange-soft: #ffeee7;
  --rose:   #f43f6c;
  --rose-soft: #ffe9ef;
  --violet: #8b5cf6;
  --violet-soft: #f1e9ff;
  --blue:   #2a78d6;
  --blue-soft: #e6f0fd;

  /* --- สถานะ (ใช้คู่กับไอคอนและข้อความเสมอ ไม่ใช้สีสื่อความหมายลำพัง) --- */
  --st-good:     #0ca30c;
  --st-good-bg:  #e7f8e7;
  --st-warning:  #d98700;
  --st-warning-bg: #fff4e0;
  --st-serious:  #e0632f;
  --st-serious-bg: #ffeee6;
  --st-critical: #d03b3b;
  --st-critical-bg: #fdeaea;
  --st-info:     #2a78d6;
  --st-info-bg:  #e6f0fd;

  /* --- พื้นหลังและตัวอักษร --- */
  --page:      #f5f6fb;
  --surface:   #ffffff;
  --surface-2: #fafaff;
  --surface-3: #f2f3f9;
  --ink:        #15161c;
  --ink-2:      #4d4f5c;
  --ink-muted:  #80838f;
  --line:       #e7e8f0;
  --line-strong: #d4d6e2;

  /* --- กราฟ (ชุดสีที่ผ่านการตรวจความเข้าถึงสำหรับผู้มีภาวะตาบอดสี) --- */
  --series-1: #2a78d6;  /* ซ่อมบำรุง */
  --series-2: #eb6834;  /* ประกัน/ภาษี */
  --series-3: #1baf7a;  /* ค่าใช้จ่ายอื่น */
  --series-4: #eda100;  /* อุปกรณ์ / หมวดที่ 4 */
  --grid:     #e7e8f0;
  --axis:     #c3c2b7;

  /* --- เงาและมุมโค้ง --- */
  --r-sm: 8px;
  --r:    14px;
  --r-lg: 20px;
  --r-xl: 26px;
  --sh-1: 0 1px 2px rgba(21, 22, 28, .06), 0 1px 3px rgba(21, 22, 28, .04);
  --sh-2: 0 4px 10px rgba(21, 22, 28, .06), 0 2px 4px rgba(21, 22, 28, .04);
  --sh-3: 0 12px 28px rgba(21, 22, 28, .10), 0 4px 10px rgba(21, 22, 28, .05);
  --sh-brand: 0 8px 20px rgba(109, 74, 255, .28);

  --sidebar-w: 248px;
  --topbar-h: 64px;
  --font: 'Noto Sans Thai', 'IBM Plex Sans Thai', system-ui, -apple-system, 'Segoe UI', sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --brand-50:  #221a3d;
    --brand-100: #2c2150;
    --brand-200: #3d2d6e;
    --brand-400: #a98bff;
    --brand-500: #8b6bff;
    --brand-600: #7a55ff;
    --brand-700: #6d4aff;

    --cyan-soft: #10313a;
    --teal-soft: #0f3029;
    --amber-soft: #3a2c10;
    --orange-soft: #3a2118;
    --rose-soft: #3a1522;
    --violet-soft: #281b45;
    --blue-soft: #122540;

    --st-good: #3cc83c; --st-good-bg: #12301a;
    --st-warning: #fab219; --st-warning-bg: #332611;
    --st-serious: #ec835a; --st-serious-bg: #3a2318;
    --st-critical: #ff6b6b; --st-critical-bg: #381a1c;
    --st-info: #5598e7; --st-info-bg: #142a45;

    --page:      #0c0d12;
    --surface:   #171b24;
    --surface-2: #1c212c;
    --surface-3: #222836;
    --ink:        #f0f1f6;
    --ink-2:      #b9bcc9;
    --ink-muted:  #878b99;
    --line:       #2a3040;
    --line-strong: #3a4154;

    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --grid: #2a3040;
    --axis: #3a4154;

    --sh-1: 0 1px 2px rgba(0,0,0,.4);
    --sh-2: 0 4px 12px rgba(0,0,0,.45);
    --sh-3: 0 14px 32px rgba(0,0,0,.55);
    --sh-brand: 0 8px 22px rgba(109, 74, 255, .35);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --brand-50:  #221a3d;
  --brand-100: #2c2150;
  --brand-200: #3d2d6e;
  --brand-400: #a98bff;
  --brand-500: #8b6bff;
  --brand-600: #7a55ff;
  --brand-700: #6d4aff;

  --cyan-soft: #10313a;
  --teal-soft: #0f3029;
  --amber-soft: #3a2c10;
  --orange-soft: #3a2118;
  --rose-soft: #3a1522;
  --violet-soft: #281b45;
  --blue-soft: #122540;

  --st-good: #3cc83c; --st-good-bg: #12301a;
  --st-warning: #fab219; --st-warning-bg: #332611;
  --st-serious: #ec835a; --st-serious-bg: #3a2318;
  --st-critical: #ff6b6b; --st-critical-bg: #381a1c;
  --st-info: #5598e7; --st-info-bg: #142a45;

  --page:      #0c0d12;
  --surface:   #171b24;
  --surface-2: #1c212c;
  --surface-3: #222836;
  --ink:        #f0f1f6;
  --ink-2:      #b9bcc9;
  --ink-muted:  #878b99;
  --line:       #2a3040;
  --line-strong: #3a4154;

  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --grid: #2a3040;
  --axis: #3a4154;

  --sh-1: 0 1px 2px rgba(0,0,0,.4);
  --sh-2: 0 4px 12px rgba(0,0,0,.45);
  --sh-3: 0 14px 32px rgba(0,0,0,.55);
  --sh-brand: 0 8px 22px rgba(109, 74, 255, .35);
}

/* =================== พื้นฐาน =================== */

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.6;
  color: var(--ink);
  background: var(--page);
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4 { margin: 0; font-weight: 700; line-height: 1.3; letter-spacing: -.01em; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
p { margin: 0; }
a { color: var(--brand-600); text-decoration: none; }
a:hover { text-decoration: underline; }

button, input, select, textarea { font: inherit; color: inherit; }

:focus-visible {
  outline: 3px solid var(--brand-400);
  outline-offset: 2px;
  border-radius: 4px;
}

.hidden { display: none !important; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.nowrap { white-space: nowrap; }
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--ink-muted); }
.small { font-size: .82rem; }
.center { text-align: center; }
.right { text-align: right; }
.row { display: flex; gap: 10px; align-items: center; }
.row-wrap { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1 1 auto; }
.stack { display: flex; flex-direction: column; gap: 6px; }

/* =================== หน้าเข้าสู่ระบบ =================== */

#login-view {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
  background:
    radial-gradient(1100px 600px at 10% -10%, rgba(109,74,255,.22), transparent 60%),
    radial-gradient(900px 600px at 95% 10%, rgba(0,184,212,.18), transparent 55%),
    radial-gradient(800px 500px at 50% 110%, rgba(244,63,108,.14), transparent 60%),
    var(--page);
}

.login-card {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-xl);
  box-shadow: var(--sh-3);
  padding: 34px 30px 30px;
}

.login-logo {
  width: 62px; height: 62px;
  border-radius: 18px;
  box-shadow: 0 8px 20px rgba(244, 82, 63, .3);
  display: block;
  margin: 0 auto 16px;
}
.login-title { text-align: center; font-size: 1.6rem; margin-bottom: 4px; }
.login-sub { text-align: center; color: var(--ink-muted); font-size: .9rem; margin-bottom: 24px; }
.login-hint {
  margin-top: 18px; padding: 12px 14px;
  background: var(--brand-50); border: 1px dashed var(--brand-200);
  border-radius: var(--r); font-size: .82rem; color: var(--ink-2);
}

/* =================== โครงหน้าหลัก =================== */

#app-view { min-height: 100dvh; }

.shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: 100dvh;
}

/* --- แถบเมนูด้านข้าง --- */
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--line);
  display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100dvh;
  z-index: 30;
}
.sidebar-brand {
  display: flex; align-items: center; gap: 11px;
  padding: 18px 18px 16px;
  border-bottom: 1px solid var(--line);
}
.brand-mark {
  width: 40px; height: 40px; flex: none;
  border-radius: 12px;
  box-shadow: 0 5px 14px rgba(244, 82, 63, .28);
  display: block;
}
.brand-name { font-weight: 800; font-size: 1.08rem; letter-spacing: -.02em; }
.brand-co { font-size: .72rem; color: var(--ink-muted); line-height: 1.2;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 150px; }

.nav { padding: 12px 10px; overflow-y: auto; flex: 1; }
.nav-label {
  font-size: .68rem; font-weight: 700; letter-spacing: .09em;
  text-transform: uppercase; color: var(--ink-muted);
  padding: 14px 10px 6px;
}
.nav-item {
  display: flex; align-items: center; gap: 11px;
  padding: 10px 12px; margin-bottom: 2px;
  border-radius: 11px;
  color: var(--ink-2); font-weight: 600; font-size: .92rem;
  cursor: pointer; border: 0; background: none; width: 100%; text-align: left;
  transition: background .13s, color .13s;
}
.nav-item:hover { background: var(--surface-3); color: var(--ink); text-decoration: none; }
.nav-item.active {
  background: var(--brand-grad); color: #fff;
  box-shadow: var(--sh-brand);
}
.nav-item.active .nav-ico { filter: none; }
.nav-ico { font-size: 1.1rem; width: 22px; text-align: center; flex: none; }
.nav-badge {
  margin-left: auto; min-width: 22px; height: 22px; padding: 0 6px;
  border-radius: 11px; background: var(--st-critical); color: #fff;
  font-size: .72rem; font-weight: 800; display: grid; place-items: center;
}
.nav-item.active .nav-badge { background: rgba(255,255,255,.28); }

.sidebar-foot { padding: 12px; border-top: 1px solid var(--line); }
.user-chip {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 10px; border-radius: 12px;
  background: var(--surface-3); cursor: pointer;
  border: 0; width: 100%; text-align: left;
}
.user-chip:hover { background: var(--brand-50); }
.avatar {
  width: 34px; height: 34px; flex: none; border-radius: 50%;
  background: var(--brand-grad); color: #fff;
  display: grid; place-items: center; font-weight: 800; font-size: .85rem;
}
.user-name { font-weight: 700; font-size: .86rem; line-height: 1.25;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-role { font-size: .72rem; color: var(--ink-muted); line-height: 1.2; }

/* --- พื้นที่เนื้อหา --- */
.main { display: flex; flex-direction: column; min-width: 0; }

.topbar {
  height: var(--topbar-h);
  display: flex; align-items: center; gap: 12px;
  padding: 0 22px;
  background: color-mix(in srgb, var(--surface) 86%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
}
.topbar h1 { font-size: 1.22rem; }
.topbar-sub { font-size: .78rem; color: var(--ink-muted); line-height: 1.2; }

.content { padding: 22px; max-width: 1480px; width: 100%; }

/* =================== ปุ่ม =================== */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  padding: 10px 16px; border-radius: 11px;
  border: 1px solid transparent;
  font-weight: 700; font-size: .89rem;
  cursor: pointer; white-space: nowrap;
  transition: transform .1s, box-shadow .13s, background .13s, border-color .13s;
  text-decoration: none;
}
.btn:hover { text-decoration: none; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .55; cursor: not-allowed; transform: none; }

.btn-primary { background: var(--brand-grad); color: #fff; box-shadow: var(--sh-brand); }
.btn-primary:hover { box-shadow: 0 10px 26px rgba(109,74,255,.38); }

.btn-ghost { background: var(--surface); color: var(--ink-2); border-color: var(--line-strong); box-shadow: var(--sh-1); }
.btn-ghost:hover { background: var(--surface-3); color: var(--ink); }

.btn-soft { background: var(--brand-50); color: var(--brand-700); border-color: var(--brand-100); }
.btn-soft:hover { background: var(--brand-100); }

.btn-danger { background: var(--st-critical); color: #fff; }
.btn-danger:hover { filter: brightness(1.07); }

.btn-sm { padding: 6px 11px; font-size: .81rem; border-radius: 9px; }
.btn-icon { padding: 7px; width: 34px; height: 34px; border-radius: 10px; }
.btn-block { width: 100%; }
.btn-lg { padding: 13px 20px; font-size: .96rem; border-radius: 13px; }

.icon-btn {
  width: 38px; height: 38px; flex: none;
  display: grid; place-items: center;
  border-radius: 11px; border: 1px solid var(--line-strong);
  background: var(--surface); cursor: pointer; font-size: 1.05rem;
  color: var(--ink-2); position: relative;
}
.icon-btn:hover { background: var(--surface-3); color: var(--ink); }
.icon-btn .dot {
  position: absolute; top: 6px; right: 6px;
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--st-critical); border: 2px solid var(--surface);
}

/* =================== การ์ด =================== */

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  box-shadow: var(--sh-1);
}
.card-pad { padding: 18px 20px; }
.card-head {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 16px 20px; border-bottom: 1px solid var(--line);
}
.card-head h2 { font-size: 1.02rem; }
.card-head .card-sub { font-size: .78rem; color: var(--ink-muted); line-height: 1.25; }
.card-body { padding: 18px 20px; }
.card-foot { padding: 13px 20px; border-top: 1px solid var(--line); background: var(--surface-2);
  border-radius: 0 0 var(--r-lg) var(--r-lg); }

.grid { display: grid; gap: 16px; }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-sidebar { grid-template-columns: minmax(0, 1.9fr) minmax(0, 1fr); }

/* =================== กล่องตัวเลข (stat tile) =================== */

.stat {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  box-shadow: var(--sh-1);
  padding: 17px 18px;
  display: flex; flex-direction: column; gap: 3px;
  position: relative; overflow: hidden;
  min-width: 0;
}
.stat::after {
  content: ''; position: absolute; inset: 0 0 auto 0; height: 4px;
  background: var(--stat-accent, var(--brand-500));
}
.stat-top { display: flex; align-items: center; gap: 9px; margin-bottom: 4px; }
.stat-ico {
  width: 36px; height: 36px; flex: none; border-radius: 11px;
  display: grid; place-items: center; font-size: 1.02rem;
  background: var(--stat-soft, var(--brand-50));
}
.stat-label { font-size: .8rem; color: var(--ink-2); font-weight: 600; line-height: 1.25; }
.stat-value { font-size: 1.75rem; font-weight: 800; letter-spacing: -.02em; line-height: 1.15; }
.stat-value .unit { font-size: .85rem; font-weight: 600; color: var(--ink-muted); margin-left: 3px; }
.stat-note { font-size: .76rem; color: var(--ink-muted); }

.stat.s-brand  { --stat-accent: var(--brand-500); --stat-soft: var(--brand-50); }
.stat.s-cyan   { --stat-accent: var(--cyan);   --stat-soft: var(--cyan-soft); }
.stat.s-teal   { --stat-accent: var(--teal);   --stat-soft: var(--teal-soft); }
.stat.s-amber  { --stat-accent: var(--amber);  --stat-soft: var(--amber-soft); }
.stat.s-rose   { --stat-accent: var(--rose);   --stat-soft: var(--rose-soft); }
.stat.s-orange { --stat-accent: var(--orange); --stat-soft: var(--orange-soft); }
.stat.s-violet { --stat-accent: var(--violet); --stat-soft: var(--violet-soft); }
.stat.s-blue   { --stat-accent: var(--blue);   --stat-soft: var(--blue-soft); }

/* กล่องยอดรวมใหญ่ (hero) */
.hero {
  background: var(--hero-grad);
  border-radius: var(--r-lg);
  padding: 22px 24px;
  color: #fff;
  box-shadow: var(--sh-brand);
  position: relative; overflow: hidden;
}
.hero::before {
  content: ''; position: absolute; width: 230px; height: 230px;
  right: -60px; top: -90px; border-radius: 50%;
  background: rgba(255,255,255,.14);
}
.hero::after {
  content: ''; position: absolute; width: 150px; height: 150px;
  right: 60px; bottom: -90px; border-radius: 50%;
  background: rgba(255,255,255,.1);
}
.hero-label { font-size: .84rem; opacity: .9; font-weight: 600; }
.hero-value { font-size: 2.6rem; font-weight: 800; letter-spacing: -.03em; line-height: 1.1; }
.hero-note { font-size: .8rem; opacity: .88; }
.hero-split { display: flex; gap: 22px; flex-wrap: wrap; margin-top: 14px;
  padding-top: 14px; border-top: 1px solid rgba(255,255,255,.25); position: relative; }
.hero-split > div { min-width: 92px; }
.hero-split .k { font-size: .74rem; opacity: .85; }
.hero-split .v { font-size: 1.02rem; font-weight: 700; }

/* =================== ป้ายสถานะ =================== */

.badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 9px; border-radius: 999px;
  font-size: .76rem; font-weight: 700; white-space: nowrap;
  background: var(--surface-3); color: var(--ink-2);
  border: 1px solid transparent;
}
.badge.good     { background: var(--st-good-bg);     color: var(--st-good); }
.badge.warning  { background: var(--st-warning-bg);  color: var(--st-warning); }
.badge.serious  { background: var(--st-serious-bg);  color: var(--st-serious); }
.badge.critical { background: var(--st-critical-bg); color: var(--st-critical); }
.badge.info     { background: var(--st-info-bg);     color: var(--st-info); }
.badge.muted    { background: var(--surface-3);      color: var(--ink-muted); }
.badge.brand    { background: var(--brand-50);       color: var(--brand-700); }

.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 5px 11px; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--line-strong);
  font-size: .82rem; font-weight: 600; color: var(--ink-2); cursor: pointer;
}
.chip:hover { background: var(--surface-3); }
.chip.on { background: var(--brand-grad); color: #fff; border-color: transparent; box-shadow: var(--sh-brand); }
.chip .n { font-variant-numeric: tabular-nums; opacity: .8; }

/* =================== ตาราง =================== */

.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table.tbl { width: 100%; border-collapse: collapse; font-size: .88rem; }
table.tbl th {
  text-align: left; font-size: .74rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: .05em;
  color: var(--ink-muted); padding: 10px 12px;
  border-bottom: 1px solid var(--line); white-space: nowrap;
  background: var(--surface-2); position: sticky; top: 0; z-index: 1;
}
table.tbl td { padding: 11px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
table.tbl tbody tr:hover { background: var(--surface-2); }
table.tbl tbody tr:last-child td { border-bottom: 0; }
table.tbl td.num, table.tbl th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.tbl .plate {
  font-weight: 800; font-variant-numeric: tabular-nums;
}
.row-link { cursor: pointer; }

/* =================== ฟอร์ม =================== */

.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field > label { font-size: .8rem; font-weight: 700; color: var(--ink-2); }
.field .req { color: var(--st-critical); }
.field .help { font-size: .74rem; color: var(--ink-muted); }

.input, .select, .textarea {
  width: 100%; padding: 10px 12px;
  background: var(--surface);
  border: 1.5px solid var(--line-strong);
  border-radius: 11px;
  font-size: .9rem; color: var(--ink);
  transition: border-color .13s, box-shadow .13s;
  min-width: 0;
}
.input:hover, .select:hover, .textarea:hover { border-color: var(--brand-200); }
.input:focus, .select:focus, .textarea:focus {
  outline: none; border-color: var(--brand-500);
  box-shadow: 0 0 0 3.5px rgba(109,74,255,.15);
}
.input::placeholder, .textarea::placeholder { color: var(--ink-muted); }
.textarea { min-height: 78px; resize: vertical; line-height: 1.55; }
.select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1.5 6 6.5l5-5' stroke='%2380838f' stroke-width='1.8' fill='none' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 12px center;
  padding-right: 34px;
}
.input.invalid, .select.invalid, .textarea.invalid { border-color: var(--st-critical); }
.input[type="date"] { min-height: 42px; }

.form-grid { display: grid; gap: 14px; grid-template-columns: repeat(2, minmax(0, 1fr)); }
.form-grid .span-2 { grid-column: 1 / -1; }
.form-section-title {
  grid-column: 1 / -1; font-size: .78rem; font-weight: 800;
  letter-spacing: .07em; text-transform: uppercase; color: var(--brand-600);
  padding-top: 6px; display: flex; align-items: center; gap: 8px;
}
.form-section-title::after { content: ''; flex: 1; height: 1px; background: var(--line); }

.search-box { position: relative; flex: 1 1 220px; min-width: 0; max-width: 420px; }
.search-box .input { padding-left: 38px; }
.search-box::before {
  content: '🔍'; position: absolute; left: 13px; top: 50%;
  transform: translateY(-50%); font-size: .86rem; opacity: .6; pointer-events: none;
}

/* แถวรายการอะไหล่ */
.parts-table { width: 100%; border-collapse: collapse; font-size: .86rem; }
.parts-table th {
  font-size: .72rem; text-transform: uppercase; letter-spacing: .05em;
  color: var(--ink-muted); text-align: left; padding: 6px 6px; font-weight: 700;
}
.parts-table td { padding: 3px 4px; }
.parts-table .input { padding: 7px 9px; font-size: .85rem; border-radius: 9px; }
.parts-total {
  display: flex; justify-content: flex-end; gap: 10px; align-items: baseline;
  padding: 10px 6px 0; border-top: 1px dashed var(--line-strong); margin-top: 8px;
  font-weight: 700;
}

/* =================== แท็บ =================== */

.tabs {
  display: flex; gap: 4px; overflow-x: auto;
  border-bottom: 1px solid var(--line); padding: 0 4px;
}
.tab {
  padding: 11px 15px; border: 0; background: none; cursor: pointer;
  font-weight: 700; font-size: .89rem; color: var(--ink-muted);
  border-bottom: 3px solid transparent; white-space: nowrap;
  display: flex; align-items: center; gap: 7px;
}
.tab:hover { color: var(--ink); }
.tab.active { color: var(--brand-600); border-bottom-color: var(--brand-500); }
.tab .n {
  font-size: .72rem; padding: 1px 7px; border-radius: 999px;
  background: var(--surface-3); color: var(--ink-muted); font-weight: 800;
}
.tab.active .n { background: var(--brand-50); color: var(--brand-700); }

/* =================== กราฟ =================== */

.chart-wrap { width: 100%; position: relative; }
.chart-wrap svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart-grid line { stroke: var(--grid); stroke-width: 1; }
.chart-axis line { stroke: var(--axis); stroke-width: 1; }
.chart-tick {
  font-size: 10.5px; fill: var(--ink-muted);
  font-variant-numeric: tabular-nums; font-family: var(--font);
}
.chart-label { font-size: 11px; fill: var(--ink-2); font-family: var(--font); font-weight: 600; }
.chart-bar { cursor: pointer; }
.chart-bar:hover { opacity: .82; }
.chart-hit { fill: transparent; cursor: pointer; }

.legend { display: flex; gap: 14px; flex-wrap: wrap; align-items: center; }
.legend-item { display: inline-flex; align-items: center; gap: 6px; font-size: .8rem; color: var(--ink-2); font-weight: 600; }
.legend-swatch { width: 11px; height: 11px; border-radius: 3px; flex: none; }

.chart-tip {
  position: absolute; pointer-events: none; z-index: 10;
  background: var(--surface); border: 1px solid var(--line-strong);
  border-radius: 11px; box-shadow: var(--sh-3);
  padding: 9px 12px; font-size: .82rem; min-width: 150px;
  transform: translate(-50%, -100%); opacity: 0; transition: opacity .1s;
}
.chart-tip.show { opacity: 1; }
.chart-tip .t { font-weight: 800; margin-bottom: 5px; }
.chart-tip .r { display: flex; align-items: center; gap: 7px; justify-content: space-between; }
.chart-tip .r .lbl { display: inline-flex; align-items: center; gap: 6px; color: var(--ink-2); }
.chart-tip .r .v { font-weight: 700; font-variant-numeric: tabular-nums; }
.chart-tip .tot {
  margin-top: 5px; padding-top: 5px; border-top: 1px solid var(--line);
  display: flex; justify-content: space-between; font-weight: 800;
}

/* แถบแนวนอนสำหรับสัดส่วนตามหมวด */
.hbar-row { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; align-items: center; margin-bottom: 11px; }
.hbar-name { font-size: .85rem; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hbar-val { font-size: .85rem; font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.hbar-track { grid-column: 1 / -1; height: 9px; border-radius: 5px; background: var(--surface-3); overflow: hidden; }
.hbar-fill { height: 100%; border-radius: 5px; background: var(--series-1); transition: width .4s; }

/* =================== การแจ้งเตือน =================== */

.alert-item {
  display: flex; gap: 13px; align-items: flex-start;
  padding: 14px 16px;
  border-radius: var(--r);
  border: 1px solid var(--line);
  border-left: 5px solid var(--al-color, var(--ink-muted));
  background: var(--surface);
  margin-bottom: 10px;
}
.alert-item.sev-overdue  { --al-color: var(--st-critical); background: var(--st-critical-bg); }
.alert-item.sev-critical { --al-color: var(--st-serious);  background: var(--st-serious-bg); }
.alert-item.sev-warning  { --al-color: var(--st-warning);  background: var(--st-warning-bg); }
.alert-item.sev-notice   { --al-color: var(--st-info);     background: var(--st-info-bg); }
.alert-item.is-snoozed { opacity: .62; }
.alert-ico {
  width: 38px; height: 38px; flex: none; border-radius: 11px;
  display: grid; place-items: center; font-size: 1.1rem;
  background: var(--surface); box-shadow: var(--sh-1);
}
.alert-body { flex: 1; min-width: 0; }
.alert-title { font-weight: 700; font-size: .93rem; }
.alert-meta { font-size: .79rem; color: var(--ink-2); margin-top: 2px; word-break: break-word; }
.alert-actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 9px; }

.alert-strip {
  display: flex; gap: 11px; align-items: center;
  padding: 13px 16px; border-radius: var(--r);
  background: var(--st-critical-bg); color: var(--ink);
  border: 1px solid color-mix(in srgb, var(--st-critical) 28%, transparent);
  margin-bottom: 16px;
}
.alert-strip.ok { background: var(--st-good-bg); border-color: color-mix(in srgb, var(--st-good) 28%, transparent); }

/* =================== Modal =================== */

.modal-backdrop {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(14, 15, 22, .55);
  backdrop-filter: blur(3px);
  display: grid; place-items: center;
  padding: 18px; overflow-y: auto;
  animation: fade .14s ease;
}
@keyframes fade { from { opacity: 0 } to { opacity: 1 } }
@keyframes pop { from { opacity: 0; transform: translateY(14px) scale(.98) } to { opacity: 1; transform: none } }

.modal {
  background: var(--surface);
  border-radius: var(--r-xl);
  box-shadow: var(--sh-3);
  width: 100%; max-width: 620px;
  max-height: calc(100dvh - 36px);
  display: flex; flex-direction: column;
  animation: pop .18s cubic-bezier(.2,.8,.3,1);
  margin: auto;
}
.modal.wide { max-width: 900px; }
.modal.narrow { max-width: 440px; }
.modal-head {
  display: flex; align-items: flex-start; gap: 12px;
  padding: 19px 22px 15px; border-bottom: 1px solid var(--line);
}
.modal-head h2 { font-size: 1.1rem; }
.modal-head .sub { font-size: .79rem; color: var(--ink-muted); margin-top: 2px; }
.modal-body { padding: 20px 22px; overflow-y: auto; flex: 1; }
.modal-foot {
  display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap;
  padding: 15px 22px; border-top: 1px solid var(--line);
  background: var(--surface-2); border-radius: 0 0 var(--r-xl) var(--r-xl);
}
.modal-close {
  margin-left: auto; width: 34px; height: 34px; flex: none;
  border: 0; background: var(--surface-3); border-radius: 10px;
  cursor: pointer; font-size: 1.05rem; color: var(--ink-2);
}
.modal-close:hover { background: var(--st-critical-bg); color: var(--st-critical); }

.form-error {
  background: var(--st-critical-bg); color: var(--st-critical);
  border: 1px solid color-mix(in srgb, var(--st-critical) 30%, transparent);
  padding: 10px 13px; border-radius: 11px; font-size: .85rem; font-weight: 600;
  margin-bottom: 14px; display: flex; gap: 8px; align-items: flex-start;
}

/* =================== Toast =================== */

#toasts {
  position: fixed; bottom: 20px; right: 20px; z-index: 200;
  display: flex; flex-direction: column; gap: 9px; max-width: min(380px, calc(100vw - 32px));
}
.toast {
  display: flex; gap: 10px; align-items: flex-start;
  padding: 13px 15px; border-radius: var(--r);
  background: var(--surface); border: 1px solid var(--line-strong);
  box-shadow: var(--sh-3); font-size: .87rem; font-weight: 600;
  animation: slideIn .22s cubic-bezier(.2,.8,.3,1);
}
@keyframes slideIn { from { opacity: 0; transform: translateX(26px) } to { opacity: 1; transform: none } }
.toast.out { animation: slideOut .2s forwards; }
@keyframes slideOut { to { opacity: 0; transform: translateX(26px) } }
.toast.ok    { border-left: 5px solid var(--st-good); }
.toast.err   { border-left: 5px solid var(--st-critical); }
.toast.info  { border-left: 5px solid var(--st-info); }
.toast .ti { font-size: 1.05rem; line-height: 1.3; }

/* =================== สถานะว่าง / โหลด =================== */

.empty { text-align: center; padding: 48px 22px; color: var(--ink-muted); }
.empty-ico { font-size: 2.8rem; margin-bottom: 10px; opacity: .55; }
.empty-title { font-weight: 700; color: var(--ink-2); font-size: 1rem; margin-bottom: 5px; }
.empty-text { font-size: .86rem; max-width: 420px; margin: 0 auto 16px; }

.skeleton {
  background: linear-gradient(90deg, var(--surface-3) 25%, var(--surface-2) 50%, var(--surface-3) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.3s infinite; border-radius: 9px;
}
@keyframes shimmer { 0% { background-position: 200% 0 } 100% { background-position: -200% 0 } }
.sk-card { height: 104px; }
.sk-line { height: 13px; margin-bottom: 9px; }

.spinner {
  width: 17px; height: 17px; border-radius: 50%;
  border: 2.5px solid rgba(255,255,255,.35); border-top-color: #fff;
  animation: spin .7s linear infinite; display: inline-block;
}
.spinner.dark { border-color: var(--line-strong); border-top-color: var(--brand-500); }
@keyframes spin { to { transform: rotate(360deg) } }

/* =================== การ์ดรถ =================== */

.veh-card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--r-lg); box-shadow: var(--sh-1);
  padding: 16px 17px; cursor: pointer;
  transition: transform .13s, box-shadow .13s, border-color .13s;
  display: flex; flex-direction: column; gap: 11px;
  position: relative; overflow: hidden;
}
.veh-card:hover { transform: translateY(-3px); box-shadow: var(--sh-3); border-color: var(--brand-200); }
.veh-card::before {
  content: ''; position: absolute; inset: 0 0 auto 0; height: 4px;
  background: var(--veh-accent, var(--brand-grad));
}
.veh-card.lv-overdue  { --veh-accent: var(--st-critical); }
.veh-card.lv-critical { --veh-accent: var(--st-serious); }
.veh-card.lv-warning  { --veh-accent: var(--st-warning); }
.veh-card.lv-notice   { --veh-accent: var(--st-info); }

.veh-plate {
  font-size: 1.14rem; font-weight: 800; letter-spacing: -.01em;
  font-variant-numeric: tabular-nums;
}
.veh-type { font-size: .78rem; color: var(--ink-muted); }
.veh-ico {
  width: 46px; height: 46px; flex: none; border-radius: 13px;
  display: grid; place-items: center; font-size: 1.4rem;
  background: var(--brand-50);
}
.veh-rows { display: flex; flex-direction: column; gap: 5px; font-size: .82rem; }
.veh-row { display: flex; justify-content: space-between; gap: 10px; }
.veh-row .k { color: var(--ink-muted); }
.veh-row .v { font-weight: 600; text-align: right; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* บัตรข้อมูลเอกสาร 4 ช่อง */
.doc-cards { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
.doc-card {
  border: 1px solid var(--line); border-radius: var(--r);
  padding: 14px 15px; background: var(--surface);
  border-left: 5px solid var(--dc, var(--ink-muted));
}
.doc-card.ok       { --dc: var(--st-good); }
.doc-card.warn     { --dc: var(--st-warning); }
.doc-card.over     { --dc: var(--st-critical); }
.doc-card.none     { --dc: var(--line-strong); }
.doc-card .dt { font-size: .78rem; color: var(--ink-muted); font-weight: 600; display: flex; align-items: center; gap: 6px; }
.doc-card .dd { font-size: 1.02rem; font-weight: 800; margin: 4px 0 2px; }
.doc-card .dn { font-size: .76rem; color: var(--ink-2); }

/* รายละเอียดแบบคู่ key/value */
.kv { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 14px 18px; }
.kv-item { min-width: 0; }
.kv-k { font-size: .74rem; color: var(--ink-muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.kv-v { font-size: .93rem; font-weight: 600; word-break: break-word; }
.kv-v.mono { font-variant-numeric: tabular-nums; letter-spacing: .02em; }

/* ไทม์ไลน์ประวัติ */
.tl { position: relative; padding-left: 26px; }
.tl::before { content: ''; position: absolute; left: 7px; top: 6px; bottom: 6px; width: 2px; background: var(--line); }
.tl-item { position: relative; padding-bottom: 18px; }
.tl-item::before {
  content: ''; position: absolute; left: -23px; top: 5px;
  width: 11px; height: 11px; border-radius: 50%;
  background: var(--brand-500); border: 2.5px solid var(--surface);
  box-shadow: 0 0 0 2px var(--brand-100);
}
.tl-date { font-size: .76rem; color: var(--ink-muted); font-weight: 600; }
.tl-title { font-weight: 700; font-size: .92rem; }
.tl-note { font-size: .82rem; color: var(--ink-2); }

/* =================== เมนูล่างสำหรับมือถือ =================== */

.mobile-nav { display: none; }
.sidebar-overlay { display: none; }

/* =================== Responsive =================== */

@media (max-width: 1240px) {
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-sidebar { grid-template-columns: 1fr; }
}

@media (max-width: 1024px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; left: 0; top: 0; width: 282px;
    transform: translateX(-100%); transition: transform .22s cubic-bezier(.2,.8,.3,1);
    box-shadow: var(--sh-3); z-index: 90;
  }
  body.nav-open .sidebar { transform: none; }
  body.nav-open .sidebar-overlay {
    display: block; position: fixed; inset: 0; z-index: 80;
    background: rgba(14,15,22,.5); backdrop-filter: blur(2px);
  }
  .grid-3, .grid-2 { grid-template-columns: 1fr; }
  .content { padding-bottom: 86px; }

  .mobile-nav {
    display: flex; position: fixed; bottom: 0; left: 0; right: 0; z-index: 70;
    background: color-mix(in srgb, var(--surface) 94%, transparent);
    backdrop-filter: blur(14px);
    border-top: 1px solid var(--line);
    padding: 7px 5px calc(7px + env(safe-area-inset-bottom));
  }
  .mobile-nav button {
    flex: 1; border: 0; background: none; cursor: pointer;
    display: flex; flex-direction: column; align-items: center; gap: 2px;
    padding: 5px 2px; border-radius: 11px;
    font-size: .66rem; font-weight: 700; color: var(--ink-muted);
    position: relative; min-width: 0;
  }
  .mobile-nav button .mi { font-size: 1.22rem; }
  .mobile-nav button.active { color: var(--brand-600); background: var(--brand-50); }
  .mobile-nav button .mb {
    position: absolute; top: 2px; right: 50%; margin-right: -22px;
    min-width: 17px; height: 17px; padding: 0 4px;
    border-radius: 9px; background: var(--st-critical); color: #fff;
    font-size: .62rem; display: grid; place-items: center;
  }
}

@media (max-width: 768px) {
  body { font-size: 14.5px; }
  .topbar { padding: 0 14px; gap: 9px; }
  .topbar h1 { font-size: 1.05rem; }
  .content { padding: 15px 14px 86px; }
  .grid { gap: 12px; }
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .form-grid { grid-template-columns: 1fr; }
  .card-pad, .card-body { padding: 15px 16px; }
  .card-head { padding: 14px 16px; }
  .hero { padding: 18px; }
  .hero-value { font-size: 2.1rem; }
  .stat-value { font-size: 1.5rem; }
  .modal { max-width: 100%; border-radius: var(--r-lg); }
  .modal-foot { border-radius: 0 0 var(--r-lg) var(--r-lg); }
  .modal-foot .btn { flex: 1; }
  #toasts { left: 14px; right: 14px; bottom: 90px; max-width: none; }
  .login-card { padding: 26px 20px 22px; }
  .kv { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }
}

@media (max-width: 520px) {
  .grid-4 { grid-template-columns: 1fr; }
  .hero-split { gap: 14px; }
  .parts-table { font-size: .8rem; }
}

@media print {
  .sidebar, .topbar, .mobile-nav, .btn, .icon-btn, #toasts { display: none !important; }
  .shell { grid-template-columns: 1fr; }
  .card { box-shadow: none; border-color: #ccc; break-inside: avoid; }
  body { background: #fff; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

/* ===================================================================
   ปรับละเอียดสำหรับมือถือและแท็บเล็ต
   =================================================================== */

/* --- แท็บเล็ต (768-1024px): ใช้ 2 คอลัมน์ ไม่ยุบเป็นคอลัมน์เดียว --- */
@media (min-width: 769px) and (max-width: 1024px) {
  .grid-2, .grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .content { padding: 20px 20px 92px; }
  .doc-cards { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* --- แท็บเล็ตแนวนอนขนาดใหญ่: โชว์เมนูข้างถาวร --- */
@media (min-width: 1025px) {
  #nav-toggle { display: none !important; }
}

/* --- iOS: ตัวอักษรในช่องกรอกต้องไม่เล็กกว่า 16px ไม่งั้นหน้าจอจะซูมเองตอนแตะ --- */
@media (max-width: 1024px) {
  .input, .select, .textarea,
  .parts-table .input {
    font-size: 16px;
  }
  /* ปุ่มและเมนูให้ใหญ่พอสำหรับการแตะด้วยนิ้ว (>= 44px ตามแนวทาง iOS/Android) */
  .btn { min-height: 44px; padding: 11px 17px; }
  .btn-sm { min-height: 38px; }
  .btn-icon, .icon-btn { min-height: 42px; min-width: 42px; }
  .nav-item { padding: 13px 13px; }
  .tab { padding: 13px 16px; }
  table.tbl td { padding: 13px 12px; }
  .chip { padding: 8px 14px; }
  .modal-close { width: 42px; height: 42px; }
}

/* --- บอกผู้ใช้ว่าตารางเลื่อนแนวนอนได้ (เงาบอกขอบ) --- */
@media (max-width: 1024px) {
  .table-wrap {
    background:
      linear-gradient(to right, var(--surface) 30%, transparent),
      linear-gradient(to left,  var(--surface) 30%, transparent) 100% 0,
      radial-gradient(farthest-side at 0 50%, rgba(0,0,0,.14), transparent),
      radial-gradient(farthest-side at 100% 50%, rgba(0,0,0,.14), transparent) 100% 0;
    background-repeat: no-repeat;
    background-size: 36px 100%, 36px 100%, 14px 100%, 14px 100%;
    background-attachment: local, local, scroll, scroll;
  }
  /* หัวตารางไม่ต้องติดด้านบนบนจอเล็ก เพราะเลื่อนทั้งหน้าอยู่แล้ว */
  table.tbl th { position: static; }
}

/* --- มือถือแนวนอน (จอเตี้ย): ลดความสูง modal และแถบบน --- */
@media (max-height: 460px) and (orientation: landscape) {
  :root { --topbar-h: 52px; }
  .modal { max-height: calc(100dvh - 16px); }
  .modal-head { padding: 12px 18px 10px; }
  .modal-body { padding: 14px 18px; }
  .modal-foot { padding: 10px 18px; }
  .mobile-nav { padding: 3px 4px calc(3px + env(safe-area-inset-bottom)); }
  .mobile-nav button .mi { font-size: 1.05rem; }
}

/* --- รองรับรอยบากและขอบโค้งของจอ (safe area) --- */
@supports (padding: max(0px)) {
  .topbar { padding-left: max(14px, env(safe-area-inset-left)); padding-right: max(14px, env(safe-area-inset-right)); }
  .content { padding-left: max(14px, env(safe-area-inset-left)); padding-right: max(14px, env(safe-area-inset-right)); }
  .mobile-nav { padding-left: max(5px, env(safe-area-inset-left)); padding-right: max(5px, env(safe-area-inset-right)); }
}

/* --- อุปกรณ์สัมผัส: ไม่ต้องมีเอฟเฟกต์ hover ที่ค้าง --- */
@media (hover: none) {
  .veh-card:hover { transform: none; box-shadow: var(--sh-1); border-color: var(--line); }
  .veh-card:active { transform: scale(.985); background: var(--surface-2); }
  table.tbl tbody tr:hover { background: transparent; }
  table.tbl tbody tr.row-link:active { background: var(--brand-50); }
  .btn:active { transform: scale(.97); }
}

/* --- ป้องกันการเลื่อนแนวนอนทั้งหน้า --- */
html, body { max-width: 100%; overflow-x: hidden; }
.modal-backdrop { overscroll-behavior: contain; }

/* แถวสองคอลัมน์ที่ไม่ต้องการให้การ์ดยืดสูงเท่ากัน ใส่คลาส .top เพิ่ม */
.grid-sidebar.top { align-items: start; }

/* ===================================================================
   แถบเครื่องมือ (ค้นหา + ตัวกรอง) ให้ใช้งานง่ายบนมือถือ
   =================================================================== */
@media (max-width: 768px) {
  .toolbar {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 9px;
    align-items: stretch;
  }
  .toolbar > .search-box { grid-column: 1 / -1; max-width: none; }
  .toolbar > .select { max-width: none !important; width: 100%; }
  .toolbar > .spacer { display: none; }
  .toolbar > .btn { width: 100%; }
  .toolbar > .btn-primary { grid-column: 1 / -1; }
  .toolbar > .small { grid-column: 1 / -1; text-align: right; margin: -3px 0; }
  .toolbar > .row { grid-column: 1 / -1; }
  .toolbar > .row > .input[type="date"] { flex: 1; max-width: none; }
  /* กลุ่มปุ่มกรอง (หน้าแจ้งเตือน) ให้เลื่อนแนวนอนแทนการตัดบรรทัด */
  .toolbar > .row-wrap {
    grid-column: 1 / -1;
    flex-wrap: nowrap;
    overflow-x: auto;
    padding-bottom: 4px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .toolbar > .row-wrap::-webkit-scrollbar { display: none; }
  .toolbar > .row-wrap > .chip { flex: none; }

  /* แถบแจ้งเตือนด้านบน: วางปุ่มไว้บรรทัดล่างแทนการบีบข้อความ */
  .alert-strip { flex-wrap: wrap; }
  .alert-strip > div { flex: 1 1 100%; }
  .alert-strip > .btn { width: 100%; }
}

/* ===================================================================
   จอมือถือเล็ก: กล่องตัวเลขสรุปวาง 2 คอลัมน์แบบกระชับ
   (ไม่ต้องเลื่อนผ่านกล่องสรุปยาวๆ กว่าจะถึงเนื้อหาจริง)
   =================================================================== */
@media (max-width: 520px) {
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .stat { padding: 13px 12px; }
  .stat-top { gap: 7px; margin-bottom: 2px; }
  .stat-ico { width: 30px; height: 30px; font-size: .9rem; border-radius: 9px; }
  .stat-label { font-size: .73rem; line-height: 1.3; }
  .stat-value { font-size: 1.32rem; }
  .stat-value .unit { font-size: .72rem; }
  .stat-note { font-size: .69rem; line-height: 1.35; }
  .hero-value { font-size: 1.9rem; }
  .hero-split { gap: 12px 18px; }
  .hero-split > div { min-width: 78px; }
}

/* ปุ่มลัดบนแดชบอร์ด: เรียงต่อกันเมื่อมีพื้นที่กว้าง (แท็บเล็ต) และซ้อนกันเมื่อแคบ */
.quick-actions {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 9px;
}
.quick-actions .btn { width: 100%; }

/* ===================================================================
   หัวการ์ดบนจอเล็ก: ให้ชื่อเรื่องอยู่เต็มบรรทัด แล้วปุ่มลงมาข้างล่าง
   (ไม่งั้นชื่อยาวๆ จะถูกบีบจนตัดคำทีละตัวอักษร)
   =================================================================== */
@media (max-width: 1024px) {
  .card-head {
    flex-wrap: wrap;
    row-gap: 10px;
  }
  .card-head > div:first-child {
    flex: 1 1 100% !important;
    min-width: 0;
    max-width: 100%;
  }
  .card-head > .spacer { display: none; }
  .card-head > .btn { flex: 1 1 auto; }
  .card-head > .badge { order: -1; }
  .card-head h2 { word-break: normal; overflow-wrap: anywhere; }
}

@media (max-width: 560px) {
  /* ปุ่มไอคอนเดี่ยว (แก้ไข/ลบ) ไม่ต้องยืดเต็มแถว */
  .card-head > .btn-sm { flex: 0 1 auto; min-width: 46px; }
}

/* ===================================================================
   หน้ารายงานตอนสั่งพิมพ์: ซ่อนแถบเครื่องมือ ใช้พื้นขาว ตารางไม่ขาดกลางหน้า
   =================================================================== */
@media print {
  .toolbar, .card-foot { display: none !important; }
  .stat { break-inside: avoid; border: 1px solid #ccc; }
  table.tbl { font-size: 11px; }
  table.tbl th { background: #f3f3f3 !important; }
  table.tbl tr { break-inside: avoid; }
  .chart-wrap { break-inside: avoid; }
  .content { padding: 0 !important; max-width: none; }
  .card { margin-bottom: 10px; }
}

/* ===================================================================
   ตารางรายงานย้อนหลัง (เมทริกซ์ รถ × ช่วงเวลา)
   =================================================================== */
.sparkline { display: block; }

.matrix td.cell {
  text-align: right;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  position: relative;
}
/* แรเงาอ่อนๆ ตามความสูงของยอด - ตัวเลขยังใช้สีตัวอักษรปกติเพื่อให้อ่านชัดเสมอ */
.matrix td.cell[data-heat] { background: color-mix(in srgb, var(--series-1) calc(var(--heat) * 1%), transparent); }
.matrix th.period { text-align: right; }
.matrix th.period .pm { display: block; font-size: .64rem; font-weight: 600; color: var(--ink-muted); }
.matrix th.partial { color: var(--st-warning); }
.matrix td.cell.zero { color: var(--ink-muted); }
.matrix td.spark { padding: 4px 10px; width: 120px; }

.heat-scale { display: inline-flex; align-items: center; gap: 5px; font-size: .74rem; color: var(--ink-muted); }
.heat-scale .box { width: 17px; height: 11px; border-radius: 3px; border: 1px solid var(--line); }

.trend-up   { color: var(--st-critical); font-weight: 700; }
.trend-down { color: var(--st-good); font-weight: 700; }
.trend-flat { color: var(--ink-muted); }

/* ชื่อรถในตารางย้อนหลัง: จำกัดความกว้างไม่ให้ดันแถวสูงจนตารางอ่านยาก */
.matrix td.vname { min-width: 150px; max-width: 190px; }
.matrix td.vname .plate { white-space: nowrap; }
.matrix .vmodel {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 180px;
}
.matrix tbody td, .matrix tfoot th { padding-top: 8px; padding-bottom: 8px; }

/* ตารางย้อนหลังมีคอลัมน์เยอะ บีบระยะห่างให้พอดีจอมากขึ้น */
.matrix th.period, .matrix td.cell { padding-left: 8px; padding-right: 8px; }
.matrix thead th { font-size: .68rem; }
.matrix td.cell { font-size: .84rem; }

/* =================== ตัวเลือกบริษัทในเครือ (แถบบน) =================== */

.company-switch {
  flex: none;
  max-width: 220px;
  height: 38px;
  padding: 0 32px 0 12px;
  border-radius: 11px;
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-size: .86rem;
  font-weight: 600;
  cursor: pointer;
  text-overflow: ellipsis;
  /* ลูกศรวาดเอง เพื่อให้หน้าตาเหมือนกันทุกเบราว์เซอร์และตามธีมมืด/สว่าง */
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%),
                    linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: calc(100% - 17px) 17px, calc(100% - 12px) 17px;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
}
.company-switch:hover { background-color: var(--surface-3); }
.company-switch:focus-visible { outline: 2px solid var(--brand-500); outline-offset: 2px; }

/* กำลังกรองดูบริษัทเดียว ไม่ใช่ภาพรวมทั้งเครือ - เน้นให้เห็นชัด */
.company-switch.filtered {
  border-color: var(--brand-400);
  color: var(--brand-600);
  background-color: var(--brand-50);
}

@media (max-width: 720px) {
  .company-switch { max-width: 132px; font-size: .8rem; padding-right: 28px; }
}
