:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --orange: #ea580c;
  --emerald: #059669;
  --blue: #2563eb;
  --slate: #1e293b;
  --danger: #dc2626;
  --warning: #d97706;
  --radius: 12px;
  --shadow: 0 4px 16px rgba(15, 23, 42, 0.06);
}

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

body {
  font-family: 'Noto Sans Thai', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.55;
  min-height: 100vh;
}

.hidden { display: none !important; }
.mb-16 { margin-bottom: 16px; }
.full-width { width: 100%; }

/* Topbar */
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 40;
  box-shadow: 0 1px 4px rgba(15, 23, 42, 0.04);
}

.topbar-inner {
  max-width: 1200px;
  margin: 0 auto;
  padding: 12px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-logo {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: linear-gradient(135deg, #4f46e5, #2563eb);
  color: #fff;
  font-weight: 700;
  display: grid;
  place-items: center;
  font-size: 16px;
}

.brand h1 {
  font-size: 17px;
  font-weight: 700;
  line-height: 1.2;
}

.brand-sub {
  font-size: 12px;
  color: var(--muted);
}

.nav-tabs {
  display: flex;
  gap: 6px;
  background: #f8fafc;
  padding: 4px;
  border-radius: 10px;
  border: 1px solid var(--border);
}

.nav-btn {
  border: none;
  background: transparent;
  padding: 8px 14px;
  border-radius: 8px;
  font-family: inherit;
  font-size: 13.5px;
  font-weight: 500;
  color: var(--muted);
  cursor: pointer;
  transition: all 0.15s;
}

.nav-btn:hover { color: var(--text); background: #eef2ff; }
.nav-btn.active { background: var(--primary); color: #fff; }

.mode-pill {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 12px;
  font-weight: 600;
  padding: 6px 12px;
  border-radius: 999px;
  background: #fef3c7;
  color: #92400e;
  border: 1px solid #fde68a;
}

.mode-pill .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #f59e0b;
}

.mode-pill.live {
  background: #dcfce7;
  color: #166534;
  border-color: #bbf7d0;
}

.mode-pill.live .dot { background: #16a34a; }

/* Container & Views */
.container {
  max-width: 1200px;
  margin: 24px auto 60px;
  padding: 0 20px;
}

.view { display: none; }
.view.active { display: block; animation: fadeIn 0.2s ease; }

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.form-wrapper {
  max-width: 820px;
  margin: 0 auto;
}

/* Stepper */
.stepper {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
  background: var(--surface);
  padding: 14px 20px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
}

.step-item {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 500;
}

.step-num {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: #e2e8f0;
  color: #475569;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 12px;
}

.step-item.active { color: var(--primary); font-weight: 600; }
.step-item.active .step-num { background: var(--primary); color: #fff; }
.step-item.done .step-num { background: var(--emerald); color: #fff; }

.step-line {
  flex: 1;
  height: 2px;
  background: var(--border);
  margin: 0 12px;
}

/* Cards */
.card {
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.card-header {
  padding: 18px 24px;
  color: #fff;
}

.card-header h2, .card-header h3 {
  font-size: 17px;
  font-weight: 600;
  margin-bottom: 2px;
}

.card-header p {
  font-size: 13px;
  opacity: 0.9;
}

.accent-indigo { background: linear-gradient(135deg, #4f46e5, #6366f1); }
.accent-orange { background: linear-gradient(135deg, #ea580c, #f97316); }
.accent-emerald { background: linear-gradient(135deg, #059669, #10b981); }
.accent-blue { background: linear-gradient(135deg, #2563eb, #3b82f6); }
.accent-slate { background: linear-gradient(135deg, #1e293b, #334155); }

.card-body { padding: 22px 24px; }
.card-footer {
  padding: 14px 24px;
  background: #f8fafc;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
}
.card-footer.right { justify-content: flex-end; }

/* Form Elements */
.form-step { display: none; }
.form-step.active { display: block; animation: fadeIn 0.2s ease; }

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 16px;
}

.field {
  margin-bottom: 16px;
}

.field:last-child { margin-bottom: 0; }

.field label {
  display: block;
  font-size: 13.5px;
  font-weight: 600;
  margin-bottom: 6px;
  color: #1e293b;
}

.req { color: var(--danger); }

input[type="text"],
input[type="url"],
input[type="password"],
select,
textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  font-family: inherit;
  font-size: 14px;
  color: var(--text);
  background: #fff;
  transition: border-color 0.15s, box-shadow 0.15s;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.14);
}

.divider {
  height: 1px;
  background: var(--border);
  margin: 18px 0;
}

/* Department Selector Cards */
.dept-cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin-top: 8px;
}

.dept-card { cursor: pointer; }
.dept-card input { display: none; }

.dept-card-inner {
  border: 2px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  height: 100%;
  display: flex;
  flex-direction: column;
  gap: 10px;
  transition: all 0.15s;
  background: #fff;
}

.dept-card:hover .dept-card-inner { border-color: #94a3b8; }
.dept-card input:checked + .dept-card-inner {
  border-color: var(--primary);
  background: #eef2ff;
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.12);
}

.dept-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  font-size: 20px;
}

.bg-orange { background: #ffedd5; }
.bg-emerald { background: #d1fae5; }
.bg-blue { background: #dbeafe; }

.dept-title { font-weight: 700; font-size: 14px; color: var(--text); }
.dept-desc { font-size: 12px; color: var(--muted); margin-top: 2px; }
.dept-db-tag {
  display: inline-block;
  margin-top: 8px;
  font-size: 11px;
  font-weight: 600;
  color: #475569;
  background: #f1f5f9;
  padding: 2px 8px;
  border-radius: 6px;
}

/* Radio Pills */
.radio-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.radio-list.grid-2 {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
}

.radio-pill {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 14px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  background: #fff;
  transition: all 0.15s;
}

.radio-pill:hover { border-color: #94a3b8; background: #f8fafc; }
.radio-pill:has(input:checked) {
  border-color: var(--primary);
  background: #eef2ff;
  color: #312e81;
}
.radio-pill.urgency-high:has(input:checked) {
  border-color: var(--danger);
  background: #fef2f2;
  color: #991b1b;
}

.step2-actions {
  display: flex;
  justify-content: space-between;
  margin-top: 16px;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 18px;
  border-radius: 8px;
  border: 1px solid transparent;
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
}

.btn-sm { padding: 7px 12px; font-size: 13px; }
.btn-lg { padding: 12px 24px; font-size: 15px; }

.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }

.btn-secondary { background: #fff; color: var(--text); border-color: #cbd5e1; }
.btn-secondary:hover { background: #f8fafc; border-color: #94a3b8; }

.btn-ghost { background: transparent; color: var(--muted); }
.btn-ghost:hover { background: #e2e8f0; color: var(--text); }

.btn-outline-blue { background: #eff6ff; color: #1d4ed8; border-color: #bfdbfe; }
.btn-outline-blue:hover { background: #dbeafe; }

/* Success Card */
.success-card {
  padding: 36px 28px;
  text-align: center;
}

.success-icon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: #dcfce7;
  color: #16a34a;
  font-size: 30px;
  font-weight: 700;
  display: grid;
  place-items: center;
  margin: 0 auto 14px;
}

.muted { color: var(--muted); font-size: 14px; }

.receipt-box {
  max-width: 520px;
  margin: 20px auto;
  text-align: left;
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px 18px;
  font-size: 13.5px;
}

.receipt-row {
  display: flex;
  justify-content: space-between;
  padding: 5px 0;
  border-bottom: 1px dashed #e2e8f0;
  gap: 12px;
}
.receipt-row:last-child { border-bottom: none; }
.receipt-row span:first-child { color: var(--muted); }
.receipt-row span:last-child { font-weight: 600; text-align: right; }

.success-actions {
  display: flex;
  justify-content: center;
  gap: 12px;
  margin-top: 20px;
}

/* Track View */
.search-bar {
  display: flex;
  gap: 10px;
  margin-bottom: 18px;
}

.ticket-cards-list {
  display: grid;
  gap: 12px;
}

.track-card {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
  background: #fff;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  flex-wrap: wrap;
}

/* Admin */
.login-card {
  max-width: 440px;
  margin: 40px auto;
}

.hint-text {
  margin-top: 12px;
  font-size: 12.5px;
  color: var(--muted);
  text-align: center;
}

.admin-header-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.admin-subtabs {
  display: flex;
  gap: 8px;
}

.subtab-btn {
  padding: 10px 16px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
}

.subtab-btn.active {
  background: var(--slate);
  color: #fff;
  border-color: var(--slate);
}

.admin-panel { display: none; }
.admin-panel.active { display: block; }

.dept-filter-tabs {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}

.dept-tab {
  padding: 8px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: #fff;
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  color: #475569;
  cursor: pointer;
}

.dept-tab.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

.db-info-banner {
  background: #eff6ff;
  border: 1px solid #bfdbfe;
  color: #1e3a8a;
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 13px;
  margin-bottom: 16px;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  margin-bottom: 16px;
}

.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 18px;
  border-left: 4px solid var(--primary);
}

.kpi-pending { border-left-color: #f59e0b; }
.kpi-progress { border-left-color: #2563eb; }
.kpi-done { border-left-color: #16a34a; }

.kpi-label { font-size: 12.5px; color: var(--muted); font-weight: 500; }
.kpi-value { font-size: 26px; font-weight: 700; margin-top: 2px; }

.toolbar {
  display: flex;
  gap: 10px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}

.toolbar input { flex: 1; min-width: 220px; }
.toolbar select { width: 180px; }

.table-card { overflow: hidden; }
.table-responsive { overflow-x: auto; }

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13.5px;
}

.data-table th, .data-table td {
  padding: 12px 14px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

.data-table th {
  background: #f8fafc;
  font-weight: 600;
  color: #475569;
  white-space: nowrap;
}

.data-table tr:hover td { background: #f8fafc; }

/* Badges */
.badge {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 600;
  white-space: nowrap;
}

.badge-machine { background: #ffedd5; color: #9a3412; }
.badge-facility { background: #d1fae5; color: #065f46; }
.badge-it { background: #dbeafe; color: #1e40af; }

.status-pending { background: #fef3c7; color: #92400e; }
.status-progress { background: #dbeafe; color: #1e40af; }
.status-done { background: #dcfce7; color: #166534; }
.status-cancel { background: #f1f5f9; color: #475569; }

.urg-high { color: #dc2626; font-weight: 600; }
.urg-mid { color: #d97706; font-weight: 600; }
.urg-low { color: #059669; font-weight: 600; }

/* Settings Grid */
.settings-dept-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}

.input-with-btn {
  display: flex;
  gap: 8px;
}

.help {
  display: block;
  margin-top: 5px;
  font-size: 12px;
  color: var(--muted);
}

.toggle-row {
  margin-bottom: 12px;
}

.switch-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
}

.settings-save-bar {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  display: grid;
  place-items: center;
  z-index: 100;
  padding: 16px;
}

.modal {
  background: var(--surface);
  width: 100%;
  max-width: 620px;
  border-radius: 14px;
  overflow: hidden;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.25);
  max-height: 92vh;
  display: flex;
  flex-direction: column;
}

.modal-header {
  padding: 16px 20px;
  background: var(--slate);
  color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-close {
  background: transparent;
  border: none;
  color: #fff;
  font-size: 24px;
  cursor: pointer;
  line-height: 1;
}

.modal-body {
  padding: 20px;
  overflow-y: auto;
}

.modal-footer {
  padding: 14px 20px;
  background: #f8fafc;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.detail-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px 16px;
  font-size: 13.5px;
}

.detail-item label {
  display: block;
  font-size: 11.5px;
  color: var(--muted);
}

.detail-item span {
  font-weight: 600;
}

/* Toast */
.toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 200;
}

.toast {
  background: var(--slate);
  color: #fff;
  padding: 12px 16px;
  border-radius: 10px;
  font-size: 13.5px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  max-width: 360px;
  animation: fadeIn 0.2s ease;
}

.toast.success { background: #15803d; }
.toast.error { background: #b91c1c; }

/* Mobile Ticket Card List (hidden on desktop) */
.mobile-ticket-list {
  display: none;
  flex-direction: column;
  gap: 12px;
}

.mobile-ticket-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.mtc-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.mtc-id-wrap {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.mtc-title {
  font-weight: 700;
  font-size: 14.5px;
  color: var(--text);
}

.mtc-detail {
  font-size: 13px;
  color: var(--muted);
}

.mtc-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 12px;
  font-size: 12.5px;
  color: #475569;
  padding: 8px 0;
  border-top: 1px dashed var(--border);
  border-bottom: 1px dashed var(--border);
}

@media (max-width: 960px) {
  .settings-dept-grid { grid-template-columns: 1fr; }
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 768px) {
  .desktop-only-table { display: none; }
  .mobile-ticket-list { display: flex; }
}

@media (max-width: 720px) {
  body {
    padding-bottom: 76px;
  }

  .topbar-inner {
    padding: 10px 14px;
    gap: 8px;
  }

  .brand h1 { font-size: 15px; }
  .brand-sub { font-size: 11px; }
  .mode-pill { font-size: 11px; padding: 4px 9px; }

  /* Sticky Bottom App Navigation on Mobile */
  .nav-tabs {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 60;
    border-radius: 0;
    border: none;
    border-top: 1px solid var(--border);
    background: rgba(255, 255, 255, 0.96);
    backdrop-filter: blur(8px);
    padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
    box-shadow: 0 -4px 16px rgba(15, 23, 42, 0.08);
  }

  .nav-btn {
    padding: 10px 6px;
    font-size: 12.5px;
    text-align: center;
    white-space: nowrap;
  }

  .container {
    margin: 14px auto 24px;
    padding: 0 12px;
  }

  /* Compact Horizontal Stepper on Mobile */
  .stepper {
    padding: 10px 12px;
    gap: 6px;
  }

  .step-item {
    font-size: 12px;
    gap: 6px;
  }

  .step-item:not(.active) .step-label {
    display: none;
  }

  .step-line { margin: 0 6px; }

  .card-header { padding: 14px 16px; }
  .card-header h2, .card-header h3 { font-size: 15.5px; }
  .card-body { padding: 16px; }
  .card-footer { padding: 12px 16px; flex-wrap: wrap; }

  /* Prevent iOS Safari zoom on focus & comfortable tap targets */
  input[type="text"],
  input[type="url"],
  input[type="password"],
  select,
  textarea {
    font-size: 16px;
    padding: 11px 12px;
  }

  .btn { min-height: 44px; }
  .radio-pill { padding: 12px 14px; font-size: 14.5px; }

  .form-grid, .dept-cards, .radio-list.grid-2, .detail-grid {
    grid-template-columns: 1fr;
  }

  .dept-card-inner {
    flex-direction: row;
    align-items: flex-start;
  }

  .dept-icon { flex-shrink: 0; }

  .step2-actions, .success-actions, .search-bar, .input-with-btn {
    flex-direction: column;
  }

  .step2-actions .btn,
  .success-actions .btn,
  .search-bar .btn,
  .input-with-btn .btn,
  .toolbar select,
  .toolbar .btn,
  .settings-save-bar .btn {
    width: 100%;
  }

  .admin-header-bar {
    flex-direction: column;
    align-items: stretch;
  }

  .admin-subtabs {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }

  .subtab-btn {
    font-size: 12.5px;
    padding: 10px 8px;
    text-align: center;
  }

  .admin-header-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
  }

  .dept-filter-tabs {
    flex-wrap: nowrap;
    overflow-x: auto;
    padding-bottom: 4px;
    -webkit-overflow-scrolling: touch;
  }

  .dept-tab {
    flex-shrink: 0;
  }

  .modal-backdrop {
    padding: 8px;
    align-items: flex-end;
  }

  .modal {
    max-height: 90dvh;
    border-radius: 16px 16px 8px 8px;
  }

  .toast-container {
    bottom: 78px;
    left: 12px;
    right: 12px;
  }

  .toast { max-width: 100%; }
}
