/* ==========================================================================
   ระบบนับปริมาณจราจร — Shared Styles
   ========================================================================== */

:root {
  --primary: #1e40af;
  --primary-dark: #1e3a8a;
  --primary-light: #3b82f6;
  --accent: #f59e0b;
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-alt: #f1f5f9;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-mute: #64748b;
  --success: #16a34a;
  --danger: #dc2626;
  --warning: #f59e0b;
  --shadow-sm: 0 1px 2px rgba(15,23,42,0.05);
  --shadow: 0 4px 12px rgba(15,23,42,0.08);
  --radius: 12px;
}

* { box-sizing: border-box; }
html { font-size: 16px; -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Sarabun", "IBM Plex Sans Thai",
               "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: contain;
}

h1, h2, h3, h4 { margin: 0 0 .4em; font-weight: 700; letter-spacing: -0.01em; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1.05rem; }

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

button {
  font-family: inherit;
  cursor: pointer;
  border: 1px solid transparent;
  background: var(--surface);
  color: var(--text);
  padding: 9px 16px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 500;
  transition: transform 0.05s, background 0.15s, box-shadow 0.15s;
}
button:active { transform: scale(0.98); }
button:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover { background: var(--primary-dark); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-success { background: var(--success); color: #fff; }
.btn-warn { background: var(--warning); color: #fff; }
.btn-ghost { background: transparent; }
.btn-outline { background: transparent; border-color: var(--border); }
.btn-block { display: block; width: 100%; }
.btn-sm { padding: 5px 10px; font-size: 12px; }

input[type=text], input[type=number], input[type=datetime-local], input[type=date],
input[type=time], input[type=email], select, textarea {
  font-family: inherit;
  font-size: 14px;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  width: 100%;
  color: var(--text);
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--primary-light);
  outline-offset: -1px;
  border-color: var(--primary-light);
}
label {
  display: block;
  font-weight: 500;
  font-size: 13px;
  color: var(--text-mute);
  margin-bottom: 4px;
}
.field { margin-bottom: 12px; }
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.row > .grow { flex: 1 1 auto; min-width: 0; }
.grid { display: grid; gap: 12px; }
.grid-2 { grid-template-columns: 1fr 1fr; }
.grid-3 { grid-template-columns: 1fr 1fr 1fr; }
@media (max-width: 640px) {
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
}

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 50;
  background: var(--primary);
  color: #fff;
  padding: 10px 16px;
  display: flex; align-items: center; gap: 12px;
  box-shadow: var(--shadow);
}
.topbar .logo {
  font-weight: 800;
  font-size: 16px;
  display: flex; align-items: center; gap: 8px;
}
.topbar .logo .badge {
  background: rgba(255,255,255,.2);
  padding: 2px 8px; border-radius: 4px; font-size: 11px;
}
.topbar nav { margin-left: auto; display: flex; gap: 4px; }
.topbar nav a {
  color: #fff;
  padding: 6px 12px; border-radius: 6px;
  font-size: 13px; font-weight: 500;
}
.topbar nav a:hover { background: rgba(255,255,255,.15); text-decoration: none; }
.topbar nav a.active { background: rgba(255,255,255,.2); }
.topbar .mode-pill {
  margin-left: 12px;
  padding: 2px 8px; border-radius: 999px;
  font-size: 11px; background: rgba(255,255,255,.15);
}

main {
  max-width: 1400px;
  margin: 0 auto;
  padding: 16px;
}
.container-sm { max-width: 720px; margin: 0 auto; padding: 16px; }
.container-md { max-width: 1000px; margin: 0 auto; padding: 16px; }

.card {
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  padding: 16px;
  box-shadow: var(--shadow-sm);
}
.card + .card { margin-top: 12px; }
.card-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 12px;
}
.card-title { font-weight: 700; font-size: 1.05rem; margin: 0; }

.muted { color: var(--text-mute); }
.small { font-size: 12px; }
.tiny { font-size: 11px; }
.bold { font-weight: 700; }
.mono { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; }
.center { text-align: center; }
.right { text-align: right; }

.pill {
  display: inline-block; padding: 2px 8px; border-radius: 999px;
  font-size: 11px; font-weight: 600; background: var(--surface-alt);
  color: var(--text-mute);
}
.pill-success { background: #dcfce7; color: #166534; }
.pill-danger  { background: #fee2e2; color: #991b1b; }
.pill-info    { background: #dbeafe; color: #1e40af; }
.pill-warn    { background: #fef3c7; color: #92400e; }

/* Counter grid (vehicle buttons) */
.counter-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
}
.veh-btn {
  position: relative;
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: 14px;
  padding: 14px 10px 10px;
  text-align: center;
  cursor: pointer;
  transition: transform 0.06s, box-shadow 0.15s;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.veh-btn:active {
  transform: scale(0.96);
}
.veh-btn .veh-icon {
  display: block; margin: 4px auto 6px;
  width: 60px; height: 30px;
  pointer-events: none;
}
.veh-btn .veh-icon svg { width: 100%; height: 100%; display: block; }
.veh-btn .veh-name { font-weight: 700; font-size: 14px; }
.veh-btn .veh-full { color: var(--text-mute); font-size: 10.5px; margin-top: 2px; line-height: 1.25; }
.veh-btn .veh-count {
  font-size: 28px; font-weight: 800;
  margin-top: 8px;
  font-variant-numeric: tabular-nums;
}
.veh-btn .veh-stripe {
  position: absolute; top: 0; left: 0; right: 0; height: 6px;
  border-radius: 14px 14px 0 0;
}
.veh-btn.flash {
  box-shadow: 0 0 0 4px rgba(59,130,246,.4);
}

/* Movement tabs (TMC) */
.movement-tabs {
  display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px;
}
.mv-tab {
  padding: 10px 16px;
  border-radius: 10px;
  border: 2px solid var(--border);
  background: var(--surface);
  cursor: pointer;
  font-weight: 600;
  font-size: 14px;
  display: inline-flex; align-items: center; gap: 6px;
}
.mv-tab.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
.mv-tab .mv-cnt {
  background: rgba(0,0,0,.1);
  padding: 1px 7px; border-radius: 999px; font-size: 11px;
}
.mv-tab.active .mv-cnt {
  background: rgba(255,255,255,.25);
}

/* Bin badge */
.bin-badge {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 6px 12px;
  background: var(--surface-alt);
  border-radius: 8px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.bin-badge .dot {
  width: 8px; height: 8px; border-radius: 50%; background: var(--success);
  animation: pulse 2s infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 1; } 50% { opacity: 0.4; }
}

/* Tables */
table.tvc {
  width: 100%; border-collapse: collapse; font-size: 13px;
}
table.tvc th, table.tvc td {
  border: 1px solid var(--border);
  padding: 6px 8px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}
table.tvc th {
  background: var(--surface-alt);
  font-weight: 700;
  position: sticky; top: 0; z-index: 2;
  font-size: 12px;
}
table.tvc td.label, table.tvc th.label {
  text-align: left; font-weight: 600;
}
table.tvc .total-row td { background: var(--surface-alt); font-weight: 700; }
table.tvc tbody tr:hover td { background: #f8fafc; }
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }

/* Modal */
.modal-bg {
  position: fixed; inset: 0;
  background: rgba(15,23,42,.5);
  display: none;
  align-items: center; justify-content: center;
  z-index: 100;
  padding: 16px;
}
.modal-bg.show { display: flex; }
.modal {
  background: #fff; border-radius: var(--radius);
  padding: 20px; max-width: 600px; width: 100%;
  max-height: 90vh; overflow: auto;
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
}

/* Empty state */
.empty {
  text-align: center; padding: 48px 16px;
  color: var(--text-mute);
}
.empty .big { font-size: 48px; margin-bottom: 12px; opacity: 0.5; }

/* Floating action button */
.fab {
  position: fixed; bottom: 24px; right: 24px;
  width: 56px; height: 56px; border-radius: 50%;
  background: var(--primary); color: #fff;
  font-size: 26px; font-weight: 800;
  border: none; box-shadow: 0 6px 16px rgba(30,64,175,.4);
  display: flex; align-items: center; justify-content: center;
  z-index: 30;
}

/* Mobile tweaks */
@media (max-width: 640px) {
  body { font-size: 14px; }
  main { padding: 10px; }
  .card { padding: 12px; }
  .topbar { padding: 8px 12px; }
  .topbar .logo { font-size: 14px; }
  .topbar nav a { padding: 6px 8px; font-size: 12px; }
  .counter-grid { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .veh-btn { padding: 10px 6px 8px; }
  .veh-btn .veh-count { font-size: 22px; }
}

@media (max-width: 380px) {
  .counter-grid { grid-template-columns: 1fr; }
}

/* Progress bar */
.progress {
  height: 6px; background: var(--surface-alt); border-radius: 999px; overflow: hidden;
}
.progress > div {
  height: 100%; background: var(--success);
  transition: width 0.3s;
}

/* Connection indicator */
.connection-dot {
  display: inline-block; width: 8px; height: 8px; border-radius: 50%;
  background: var(--success); margin-right: 4px;
}
.connection-dot.offline { background: var(--danger); }

/* Print */
@media print {
  .topbar, .fab, button { display: none !important; }
  body { background: #fff; }
  .card { box-shadow: none; border: 1px solid #ccc; break-inside: avoid; }
}
