:root {
  --bg: #f4f7f8;
  --surface: #ffffff;
  --surface-soft: #f8fafb;
  --border: #e3e9ec;
  --text: #1c2b33;
  --muted: #6b7f88;
  --primary: #0f766e;
  --primary-dark: #0b5c56;
  --primary-soft: #e6f5f2;
  --blue: #2563eb;
  --blue-soft: #eaf1ff;
  --amber: #b45309;
  --amber-soft: #fff4e4;
  --red: #be123c;
  --red-soft: #fff0f3;
  --shadow: 0 2px 10px rgba(19, 51, 61, .06);
  --radius: 14px;
}

* { box-sizing: border-box; }

html, body { margin: 0; min-height: 100%; }

body {
  font-family: "Segoe UI", "Noto Sans Thai", "Sarabun", Tahoma, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
a { color: var(--primary); }

.app-shell { display: flex; min-height: 100vh; }

/* ---------- Sidebar ---------- */
.sidebar {
  width: 246px;
  flex: 0 0 246px;
  background: #103f3c;
  color: #e8f5f3;
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  padding: 22px 14px 16px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  background: none;
  border: 0;
  color: inherit;
  text-align: left;
  padding: 4px 8px 22px;
}

.brand-mark {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: #2dd4bf;
  color: #08312e;
  font-weight: 800;
  font-size: 20px;
  flex: none;
}

.brand-text strong { display: block; font-size: 18px; letter-spacing: .2px; }
.brand-text small { display: block; color: #9fc9c4; font-size: 11px; }

.nav-list { display: flex; flex-direction: column; gap: 4px; }

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: #c3dfdb;
  text-align: left;
  transition: background .15s, color .15s;
}

.nav-item:hover { background: rgba(255, 255, 255, .08); color: #fff; }
.nav-item.active { background: #2dd4bf; color: #08312e; font-weight: 700; }
.nav-item svg { width: 19px; height: 19px; flex: none; }
.nav-item .nav-label { display: block; font-size: 14px; }
.nav-item .nav-en { display: block; font-size: 10px; opacity: .7; }

.sidebar-footer {
  margin-top: auto;
  padding: 14px 10px 0;
  border-top: 1px solid rgba(255, 255, 255, .12);
  font-size: 12px;
  color: #b9d8d4;
}
.sidebar-footer small { display: block; color: #86b3ae; }

.dot {
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #34d399;
  margin-right: 5px;
  vertical-align: 1px;
}

/* ---------- Content / topbar ---------- */
.content { flex: 1; min-width: 0; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 14px;
  min-height: 72px;
  padding: 12px 28px;
  background: rgba(255, 255, 255, .94);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}

.page-heading { min-width: 0; }
.page-heading h1 { margin: 0; font-size: 20px; line-height: 1.3; }
.page-heading p { margin: 2px 0 0; color: var(--muted); font-size: 12px; }

.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 14px; }
.today-label { color: var(--muted); font-size: 12px; white-space: nowrap; }
.connection {
  display: inline-flex;
  align-items: center;
  background: var(--primary-soft);
  color: var(--primary-dark);
  padding: 5px 10px;
  border-radius: 20px;
  font-size: 12px;
  white-space: nowrap;
}
.connection.offline { background: var(--red-soft); color: var(--red); }
.connection.offline .dot { background: var(--red); }
.mobile-brand { display: none; }

.view { padding: 24px 28px 48px; max-width: 1440px; margin: 0 auto; }

/* ---------- Cards & layout ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  margin-bottom: 18px;
}
.card-head {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}
.card-head h2 { margin: 0; font-size: 16px; }
.card-head p { margin: 0; color: var(--muted); font-size: 12px; }
.card-head .spacer { flex: 1; }

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 14px;
  margin-bottom: 18px;
}
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
}
.stat-top { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 12px; }
.stat-icon {
  width: 34px; height: 34px; border-radius: 10px;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--primary-soft); color: var(--primary);
}
.stat-icon.blue { background: var(--blue-soft); color: var(--blue); }
.stat-icon.amber { background: var(--amber-soft); color: var(--amber); }
.stat-icon.red { background: var(--red-soft); color: var(--red); }
.stat-icon svg { width: 18px; height: 18px; }
.stat-value { font-size: 26px; font-weight: 750; margin-top: 10px; line-height: 1.15; }
.stat-note { color: var(--muted); font-size: 11px; margin-top: 4px; }

.two-col { display: grid; grid-template-columns: minmax(0, 1.35fr) minmax(260px, .65fr); gap: 18px; align-items: start; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 40px;
  padding: 8px 15px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-soft); }
.btn svg { width: 16px; height: 16px; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.blue { background: var(--blue); border-color: var(--blue); color: #fff; }
.btn.danger { color: var(--red); border-color: #f2c9d3; }
.btn.danger:hover { background: var(--red-soft); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.icon { width: 36px; min-height: 36px; padding: 0; }
.btn.icon.danger { color: var(--red); }
.btn.small { min-height: 32px; padding: 5px 10px; font-size: 12px; }

.icon-btn {
  width: 34px; height: 34px;
  display: inline-flex; align-items: center; justify-content: center;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  color: var(--muted);
}
.icon-btn:hover { color: var(--primary); border-color: var(--primary); }
.icon-btn.danger:hover { color: var(--red); border-color: var(--red); }
.icon-btn svg { width: 16px; height: 16px; }

/* ---------- Forms ---------- */
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field.full { grid-column: 1 / -1; }
.field label { font-size: 13px; font-weight: 600; }
.field .en { color: var(--muted); font-weight: 400; font-size: 11px; }
.field input, .field select, .field textarea {
  width: 100%;
  min-height: 42px;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface);
  color: var(--text);
  outline: none;
}
.field textarea { min-height: 78px; resize: vertical; }
.field input:focus, .field select:focus, .field textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}
.hint { margin: 0; font-size: 12px; color: var(--muted); }
.hint strong { color: var(--primary); }
.form-actions { grid-column: 1 / -1; display: flex; gap: 10px; flex-wrap: wrap; }
.form-actions .btn { min-height: 44px; }

.filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: end; margin-bottom: 16px; }
.filters .field { flex: 1 1 170px; max-width: 280px; }
.filters .btn { min-height: 42px; }
.check-label { display: inline-flex; align-items: center; gap: 7px; font-size: 13px; min-height: 42px; }
.check-label input { width: 17px; height: 17px; accent-color: var(--primary); }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 11px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; min-width: 620px; }
thead th {
  background: var(--surface-soft);
  text-align: left;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .4px;
  color: var(--muted);
  padding: 11px 14px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
tbody td { padding: 12px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--surface-soft); }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.cell-title { font-weight: 650; }
.cell-sub { color: var(--muted); font-size: 11px; }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; }

.badge {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 3px 9px; border-radius: 20px;
  font-size: 11px; font-weight: 700; white-space: nowrap;
}
.badge.in { background: var(--primary-soft); color: var(--primary-dark); }
.badge.out { background: var(--blue-soft); color: var(--blue); }
.badge.ok { background: var(--primary-soft); color: var(--primary-dark); }
.badge.low { background: var(--amber-soft); color: var(--amber); }
.badge.empty { background: var(--red-soft); color: var(--red); }
.badge.muted { background: #eef2f4; color: var(--muted); }

.empty-state { text-align: center; padding: 40px 16px; color: var(--muted); }
.empty-state svg { width: 36px; height: 36px; opacity: .5; margin-bottom: 8px; }
.empty-state p { margin: 4px 0; }
.loading { text-align: center; padding: 50px; color: var(--muted); }

.summary-line {
  display: flex; gap: 18px; flex-wrap: wrap;
  padding: 12px 14px; background: var(--surface-soft);
  border-radius: 10px; font-size: 13px;
}
.summary-line b { color: var(--primary-dark); }

/* ---------- Modal & toast ---------- */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(12, 32, 36, .55);
  display: flex; align-items: center; justify-content: center;
  padding: 18px;
}
.modal {
  background: var(--surface);
  border-radius: 16px;
  width: min(600px, 100%);
  max-height: calc(100vh - 36px);
  overflow-y: auto;
  padding: 22px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .2);
}
.modal-head { display: flex; align-items: center; margin-bottom: 18px; }
.modal-head h2 { margin: 0; font-size: 18px; }
.modal-head .icon-btn { margin-left: auto; }

.toast-root {
  position: fixed; right: 20px; bottom: 24px; z-index: 200;
  display: flex; flex-direction: column; gap: 8px; max-width: min(360px, calc(100vw - 32px));
}
.toast {
  background: #153b38; color: #fff;
  padding: 12px 16px; border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .2);
  font-size: 13px;
  animation: toast-in .2s ease-out;
}
.toast.error { background: var(--red); }
.toast.success { background: var(--primary); }
@keyframes toast-in { from { transform: translateY(8px); opacity: 0; } }

/* ---------- Bottom nav (mobile) ---------- */
.bottom-nav { display: none; }

@media (max-width: 900px) {
  .sidebar { display: none; }
  .topbar { padding: 10px 16px; min-height: 64px; }
  .mobile-brand { display: inline-flex; }
  .mobile-brand .brand-mark { width: 34px; height: 34px; font-size: 17px; }
  .page-heading h1 { font-size: 17px; }
  .page-heading p { display: none; }
  .view { padding: 16px 14px 100px; }
  .card { padding: 16px; border-radius: 12px; }
  .two-col { grid-template-columns: 1fr; }
  .bottom-nav {
    display: flex;
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 50;
    background: #fff;
    border-top: 1px solid var(--border);
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
    box-shadow: 0 -4px 16px rgba(19, 51, 61, .07);
  }
  .bottom-nav .nav-item {
    flex: 1;
    flex-direction: column;
    gap: 2px;
    padding: 5px 2px;
    min-height: 56px;
    border-radius: 8px;
    text-align: center;
    color: var(--muted);
  }
  .bottom-nav .nav-item svg { width: 21px; height: 21px; }
  .bottom-nav .nav-item .nav-label { font-size: 10px; }
  .bottom-nav .nav-item .nav-en { display: none; }
  .bottom-nav .nav-item.active { background: var(--primary-soft); color: var(--primary-dark); }
  .toast-root { right: 12px; bottom: 84px; }
}

@media (max-width: 620px) {
  .today-label { display: none; }
  .stat-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .stat { padding: 13px; }
  .stat-value { font-size: 21px; }
  .stat-top { font-size: 10px; }
  .stat-icon { width: 28px; height: 28px; }
  .form-grid { grid-template-columns: 1fr; }
  .filters .field { max-width: none; flex-basis: 100%; }
  .filters .btn { width: 100%; }
  .card-head h2 { font-size: 15px; }

  /* Turn wide tables into readable cards */
  .table-wrap { border: 0; }
  table, thead, tbody, tr, td { display: block; width: 100%; min-width: 0; }
  thead { display: none; }
  tbody tr {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 11px;
    margin-bottom: 10px;
    padding: 8px 12px;
  }
  tbody td {
    border: 0;
    padding: 7px 0;
    display: flex;
    justify-content: space-between;
    gap: 12px;
    text-align: right;
  }
  tbody td::before {
    content: attr(data-label);
    color: var(--muted);
    font-size: 11px;
    font-weight: 600;
    text-align: left;
    flex: none;
  }
  tbody td.cell-main { font-size: 15px; padding-top: 10px; }
  tbody td.cell-main::before { display: none; }
  tbody td.row-actions-cell { border-top: 1px solid var(--border); margin-top: 4px; padding-top: 10px; }
  tbody td.row-actions-cell::before { display: none; }
  .row-actions { justify-content: flex-start; }
  .summary-line { gap: 10px 18px; font-size: 12px; }
  .modal { padding: 16px; }
}

@media print {
  .sidebar, .topbar, .bottom-nav, .filters, .form-actions, .toast-root, .row-actions-cell { display: none !important; }
  body { background: #fff; }
  .card { box-shadow: none; border: 0; }
  .view { padding: 0; }
}
