:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --ink: #1f2533;
  --muted: #6b7280;
  --line: #e3e6eb;
  --brand: #e8741e;
  --brand-dark: #c95f12;
  --blue: #2563eb;
  --green: #15a34a;
  --red: #dc2626;
  --amber: #f59e0b;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: "Segoe UI", "Sarabun", "Tahoma", system-ui, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 15px;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
}

/* Header */
.app-header {
  background: linear-gradient(120deg, var(--brand), var(--brand-dark));
  color: #fff;
  padding: 14px 16px;
}
.app-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.app-header__title { display: flex; align-items: center; gap: 12px; }
.logout-btn { flex: 0 0 auto; background: rgba(255,255,255,.18); border: 1px solid rgba(255,255,255,.35); color: #fff; padding: 7px 14px; border-radius: 9px; font-size: 13.5px; cursor: pointer; font-family: inherit; }
.logout-btn:hover { background: rgba(255,255,255,.28); }

/* role-based visibility: พนักงาน(operator) เห็นแค่ สถานะ + เปิดเที่ยว, ไม่เห็นปุ่มแก้ไข/Export */
body[data-role="operator"] .tab[data-view="history"],
body[data-role="operator"] .tab[data-view="customers"],
body[data-role="operator"] .tab[data-view="analytics"],
body[data-role="operator"] .tab[data-view="settings"],
body[data-role="operator"] .js-edit,
body[data-role="operator"] #f-export { display: none !important; }
/* จัดการบัญชีผู้ใช้: เฉพาะ admin */
body:not([data-role="admin"]) #set-users { display: none !important; }

/* login screen */
.login-screen { position: fixed; inset: 0; z-index: 100; background: linear-gradient(135deg, var(--brand), var(--brand-dark)); display: flex; align-items: center; justify-content: center; padding: 20px; }
.login-screen[hidden] { display: none; }
.login-box { background: #fff; border-radius: 16px; padding: 30px 26px; width: 100%; max-width: 360px; text-align: center; box-shadow: 0 20px 50px rgba(0,0,0,.3); }
.login-logo { font-size: 46px; }
.login-box h1 { margin: 6px 0 2px; font-size: 22px; }
.login-box p { margin: 0 0 18px; color: var(--muted); font-size: 14px; }
.login-box input { width: 100%; padding: 12px 14px; font-size: 16px; border: 1px solid #d4d8df; border-radius: 10px; margin-bottom: 12px; font-family: inherit; }
.login-err { color: var(--red); font-size: 13.5px; margin-top: 10px; }
.login-err[hidden] { display: none; }
.pw-warning { background: #fef2f2; border: 1px solid #fecaca; color: var(--red); border-radius: 9px; padding: 8px 12px; font-size: 13px; margin-bottom: 10px; }
.pw-warning[hidden] { display: none; }
.app-header .logo { font-size: 30px; }
.app-header h1 { margin: 0; font-size: 19px; }
.app-header p { margin: 0; font-size: 12.5px; opacity: .9; }

/* Tabs */
.tabs {
  display: flex; gap: 4px; overflow-x: auto;
  background: #fff; padding: 6px 8px;
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.tab {
  flex: 0 0 auto; border: none; background: transparent;
  padding: 9px 16px; border-radius: 999px; font-size: 14.5px;
  color: var(--muted); cursor: pointer; font-family: inherit; white-space: nowrap;
}
.tab.is-active { background: var(--brand); color: #fff; font-weight: 600; }

/* Layout */
main { max-width: 1000px; margin: 0 auto; padding: 16px; }
.view { display: none; }
.view.is-active { display: block; animation: fade .15s ease; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
h2 { font-size: 18px; margin: 4px 0 14px; }
.hint { color: var(--muted); font-size: 13px; margin-top: -6px; }
.section-head { display: flex; justify-content: space-between; align-items: center; margin: 18px 0 10px; }
.section-head h2 { margin: 0; }

/* Cards */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.cards { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }

/* Summary chips */
.summary { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); margin-bottom: 8px; }
.chip { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow); }
.chip .num { font-size: 24px; font-weight: 700; }
.chip .lbl { font-size: 12.5px; color: var(--muted); }
.chip.accent .num { color: var(--brand); }

/* Trip card */
.trip { border-left: 4px solid var(--blue); }
.trip.done { border-left-color: var(--green); }
.trip.is-delivered { border-left-color: var(--amber); }
.trip.overdue { border-left-color: var(--red); }
.overdue-warn { color: var(--red); font-weight: 600; font-size: 13px; }
.trip__top { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.trip__driver { font-weight: 700; font-size: 16px; }
.trip__plate { color: var(--muted); font-size: 13.5px; }
.badge { font-size: 12px; padding: 3px 10px; border-radius: 999px; font-weight: 600; white-space: nowrap; }
.badge.out { background: #e0edff; color: var(--blue); }
.badge.done { background: #dcf5e6; color: var(--green); }
.badge.slow { background: #fde6e6; color: var(--red); }
.badge.fast { background: #dcf5e6; color: var(--green); }
.badge.normal { background: #eef0f3; color: var(--muted); }
.badge.delivered { background: #fef3c7; color: #b45309; }
.trip__rows { margin: 10px 0; font-size: 14px; }
.trip__rows .row { display: flex; gap: 6px; padding: 2px 0; }
.trip__rows .k { color: var(--muted); min-width: 78px; }
.bills { display: flex; flex-wrap: wrap; gap: 5px; }
.bill-tag { background: #f1f3f6; border: 1px solid var(--line); border-radius: 6px; padding: 1px 8px; font-size: 13px; }
.trip__actions { display: flex; gap: 8px; margin-top: 6px; }
.elapsed { font-variant-numeric: tabular-nums; }

/* Forms */
.form label { display: block; margin-bottom: 12px; font-size: 13.5px; color: #374151; font-weight: 600; }
.form label small { font-weight: 400; color: var(--muted); }
.form input, .form select, .form textarea, .filters input, .filters select {
  width: 100%; margin-top: 5px; padding: 10px 12px; font-size: 15px;
  border: 1px solid #d4d8df; border-radius: 9px; font-family: inherit; background: #fff;
}
.form textarea { resize: vertical; }
.grid2 { display: grid; gap: 0 14px; grid-template-columns: 1fr 1fr; }
fieldset.optional { border: 1px dashed var(--line); border-radius: var(--radius); padding: 12px 14px; margin: 4px 0 14px; }
fieldset.optional legend { font-size: 12.5px; color: var(--muted); padding: 0 6px; }
.req { color: var(--red); }
.form-actions { display: flex; gap: 10px; margin-top: 6px; }
.form-actions.space-between { justify-content: space-between; }

/* Buttons */
.btn { border: 1px solid var(--line); background: #fff; color: var(--ink); padding: 9px 16px; border-radius: 9px; font-size: 14.5px; cursor: pointer; font-family: inherit; }
.btn:hover { background: #f7f8fa; }
.btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-dark); }
.btn-success { background: var(--green); border-color: var(--green); color: #fff; }
.btn-danger { background: #fff; border-color: var(--red); color: var(--red); }
.btn-danger:hover { background: #fef2f2; }
.btn-lg { padding: 12px 22px; font-size: 16px; width: 100%; }
.btn-sm { padding: 6px 12px; font-size: 13px; }

/* Filters */
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; }
.filters input, .filters select { width: auto; flex: 1 1 130px; margin-top: 0; }
.filters #f-q { flex: 2 1 220px; }

/* Tables */
.tbl { width: 100%; border-collapse: collapse; font-size: 14px; }
.tbl th, .tbl td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); }
.tbl th { color: var(--muted); font-weight: 600; font-size: 12.5px; }
.tbl td.num, .tbl th.num { text-align: right; font-variant-numeric: tabular-nums; }
.block { margin-bottom: 22px; }
.block h3 { font-size: 15px; margin: 0 0 8px; }

/* per-day bars */
.bars { display: flex; align-items: flex-end; gap: 6px; height: 90px; padding-top: 6px; }
.bars .bar { flex: 1; background: var(--brand); border-radius: 4px 4px 0 0; min-height: 3px; position: relative; }
.bars .bar span { position: absolute; bottom: -18px; left: 0; right: 0; text-align: center; font-size: 10px; color: var(--muted); }
.bars .bar em { position: absolute; top: -16px; left: 0; right: 0; text-align: center; font-size: 10px; font-style: normal; }

/* Settings */
.settings-grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }
.list-row { display: flex; align-items: center; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid var(--line); gap: 8px; }
.list-row.inactive { opacity: .45; }
.list-row .nm { font-weight: 500; }
.list-row .sub { color: var(--muted); font-size: 12.5px; }
.inline-form { display: flex; gap: 8px; margin-top: 12px; }
.inline-form input { flex: 1; margin-top: 0; padding: 9px 11px; border: 1px solid #d4d8df; border-radius: 9px; font-family: inherit; font-size: 14.5px; }

/* Modal */
.modal { position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center; padding: 14px; }
.modal[hidden] { display: none; }
.modal__backdrop { position: absolute; inset: 0; background: rgba(15,20,30,.5); }
.modal__box { position: relative; background: #fff; border-radius: 14px; width: 100%; max-width: 560px; max-height: 92vh; overflow: auto; padding: 18px; box-shadow: 0 20px 50px rgba(0,0,0,.3); }
.modal__head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.modal__head h3 { margin: 0; font-size: 17px; }
.icon-btn { border: none; background: #f1f3f6; width: 32px; height: 32px; border-radius: 8px; cursor: pointer; font-size: 15px; }

.empty { text-align: center; color: var(--muted); padding: 30px 10px; }

/* customer autofill + import */
.cust-ref { background: #eef5ff; border: 1px solid #d6e4ff; border-radius: 9px; padding: 8px 12px; font-size: 13px; color: #374151; margin-bottom: 12px; }
.cust-ref b { color: var(--blue); }
.map-grid { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
.map-grid label { display: block; font-size: 13px; font-weight: 600; color: #374151; }
.map-grid select { width: 100%; margin-top: 4px; padding: 8px 10px; border: 1px solid #d4d8df; border-radius: 8px; font-family: inherit; }
.preview-tbl { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.preview-tbl th, .preview-tbl td { border: 1px solid var(--line); padding: 5px 8px; text-align: left; white-space: nowrap; }
.preview-tbl th { background: #f7f8fa; color: var(--muted); }
.cust-card { border-left: 4px solid var(--brand); }
.cust-card .code { font-weight: 700; }
.cust-card .actions { display: flex; gap: 8px; margin-top: 8px; }

/* loader picker (พนักงานยกของ) */
.loader-pick { margin-bottom: 12px; }
.loader-pick__head { display: flex; justify-content: space-between; align-items: center; font-size: 13.5px; font-weight: 600; color: #374151; margin-bottom: 6px; }
.loader-pick__head small { font-weight: 400; color: var(--muted); }
.loader-count { background: var(--brand); color: #fff; font-size: 12.5px; padding: 2px 10px; border-radius: 999px; font-weight: 600; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip-btn { border: 1px solid #d4d8df; background: #fff; color: var(--ink); padding: 7px 14px; border-radius: 999px; font-size: 14px; cursor: pointer; font-family: inherit; }
.chip-btn:hover { background: #f7f8fa; }
.chip-btn.on { background: var(--brand); border-color: var(--brand); color: #fff; font-weight: 600; }
.chips .empty-mini { color: var(--muted); font-size: 13px; padding: 4px 0; }

/* employee settings rows */
.emp-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 9px 0; border-bottom: 1px solid var(--line); }
.emp-row.inactive { opacity: .45; }
.emp-row .emp-nm { font-weight: 500; }
.emp-row .emp-sub { color: var(--muted); font-size: 12px; }
.emp-roles { display: flex; gap: 10px; align-items: center; }
.emp-roles label { font-size: 12.5px; font-weight: 500; display: inline-flex; align-items: center; gap: 4px; cursor: pointer; }
.emp-add-roles { display: flex; gap: 14px; margin: 8px 0; font-size: 13.5px; }
.emp-add-roles label { display: inline-flex; align-items: center; gap: 5px; cursor: pointer; }

/* Toast */
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #1f2533; color: #fff; padding: 11px 20px; border-radius: 10px; font-size: 14px; z-index: 80; box-shadow: 0 6px 20px rgba(0,0,0,.25); }
.toast[hidden] { display: none; }
.toast.err { background: var(--red); }

@media (max-width: 560px) {
  .grid2 { grid-template-columns: 1fr; }
  main { padding: 12px; }
}
