* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  font-family: "Segoe UI", "Sarabun", "Tahoma", sans-serif;
  background: #f4f6fb; color: #1f2937;
}
.hidden { display: none !important; }
.screen { min-height: 100vh; }
.muted { color: #6b7280; font-size: 13px; }
.small { font-size: 11px; }
.mt { margin-top: 12px; display: block; }
code { background: #f1f5f9; padding: 1px 5px; border-radius: 3px; font-size: 12px; }

/* ===== LOGIN ===== */
#loginScreen {
  display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #1e3a8a 0%, #4f46e5 100%);
}
.login-card {
  background: #fff; padding: 32px; border-radius: 14px;
  width: 460px; max-width: 92vw;
  box-shadow: 0 20px 50px rgba(0,0,0,.25);
}
.login-card h1 { margin: 0 0 4px; font-size: 22px; color: #1e3a8a; }
.login-card .subtitle { margin: 0 0 20px; color: #6b7280; font-size: 13px; }
.login-tabs { display: flex; gap: 4px; margin: 0 0 14px; border-bottom: 2px solid #e5e7eb; }
.login-tabs .lt {
  flex: 1; background: transparent; border: 0; padding: 8px;
  font-weight: 600; color: #6b7280; cursor: pointer; border-bottom: 3px solid transparent;
}
.login-tabs .lt.active { color: #4f46e5; border-bottom-color: #4f46e5; }
.login-card label { display: block; font-size: 13px; margin: 12px 0 4px; color: #374151; }
.login-card select, .login-card input {
  width: 100%; padding: 10px; border: 1px solid #d1d5db; border-radius: 8px; font-size: 14px;
}
.login-card button.primary {
  width: 100%; margin-top: 16px; padding: 12px;
  background: #4f46e5; color: #fff; border: 0; border-radius: 8px;
  font-size: 15px; font-weight: 600; cursor: pointer;
}
.login-card button.primary:hover { background: #4338ca; }
.user-mgmt { margin-top: 18px; border-top: 1px solid #e5e7eb; padding-top: 12px; }
.user-mgmt summary { cursor: pointer; color: #4f46e5; font-size: 13px; }
.user-mgmt .row { display: grid; grid-template-columns: 1fr 1fr 1fr 1fr auto; gap: 6px; margin-top: 10px; }
.user-mgmt .row input, .user-mgmt .row select {
  padding: 6px 8px; border: 1px solid #d1d5db; border-radius: 6px; font-size: 12px;
}
.user-mgmt .row button {
  background: #10b981; color: #fff; border: 0; padding: 0 10px; border-radius: 6px; cursor: pointer;
}

/* ===== TOPBAR ===== */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  background: #1e293b; color: #fff; padding: 10px 16px; flex-wrap: wrap; gap: 8px;
}
.topbar .brand { font-weight: 700; font-size: 16px; }
.topbar nav { display: flex; gap: 4px; flex-wrap: wrap; }
.topbar nav .tab {
  background: transparent; color: #cbd5e1; border: 0; padding: 8px 12px;
  border-radius: 6px; cursor: pointer; font-size: 13px;
}
.topbar nav .tab:hover { background: #334155; color: #fff; }
.topbar nav .tab.active { background: #4f46e5; color: #fff; }
.topbar nav .tab.admin-only { background: #7c3aed; color: #fff; }
.topbar nav .tab.admin-only.active { background: #6d28d9; }
.topbar .userbox { display: flex; gap: 6px; align-items: center; font-size: 13px; }
.topbar .userbox button {
  background: #334155; color: #fff; border: 0; padding: 6px 10px; border-radius: 6px; cursor: pointer; font-size: 12px;
}
.topbar .userbox button:hover { background: #475569; }

main { padding: 20px; max-width: 1380px; margin: 0 auto; }
.tabpanel { display: none; }
.tabpanel.active { display: block; }
h2 { margin: 0 0 16px; color: #1e3a8a; }
h3 { margin: 24px 0 10px; color: #374151; }

/* ===== CARDS ===== */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; }
.card {
  background: #fff; border-radius: 10px; padding: 16px;
  box-shadow: 0 2px 6px rgba(0,0,0,.06);
  border-left: 4px solid #4f46e5;
}
.card .num { font-size: 26px; font-weight: 700; color: #1e3a8a; }
.card.warn { border-left-color: #f59e0b; }
.card.warn .num { color: #b45309; }
.card.big .num { color: #16a34a; }
.card.big { border-left-color: #16a34a; }
.card .hint { font-size: 11px; color: #4f46e5; margin-top: 4px; }
.card.link { cursor: pointer; transition: transform .12s, box-shadow .12s; }
.card.link:hover { transform: translateY(-2px); box-shadow: 0 6px 16px rgba(0,0,0,.1); }

/* ===== DASH BLOCKS ===== */
.dash-row { display: grid; grid-template-columns: 2fr 1fr; gap: 14px; margin-top: 22px; }
.dash-block { background: #fff; padding: 16px; border-radius: 10px; box-shadow: 0 1px 3px rgba(0,0,0,.05); }
.trend { display: flex; align-items: flex-end; gap: 8px; height: 160px; padding-top: 8px; }
.trend .bar {
  flex: 1; background: linear-gradient(180deg, #6366f1 0%, #4f46e5 100%);
  border-radius: 4px 4px 0 0; position: relative;
  display: flex; flex-direction: column; align-items: center; justify-content: flex-end;
  min-height: 4px; color: #fff; font-size: 11px; padding-bottom: 2px;
  transition: background .15s;
}
.trend .bar:hover { background: linear-gradient(180deg, #818cf8 0%, #6366f1 100%); }
.trend .bar .lab { position: absolute; bottom: -22px; color: #6b7280; font-size: 10px; white-space: nowrap; }
.trend .bar .val { font-size: 10px; opacity: .9; }
.catlist { display: flex; flex-direction: column; gap: 6px; }
.catlist .cat-row {
  display: flex; justify-content: space-between; cursor: pointer; padding: 6px 8px;
  border-radius: 4px; font-size: 13px;
}
.catlist .cat-row:hover { background: #eef2ff; }
.catlist .cat-row b { color: #4f46e5; }

@media (max-width: 900px) { .dash-row { grid-template-columns: 1fr; } }

/* ===== FILTERS ===== */
.filters { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.filters select, .filters input {
  padding: 8px 10px; border: 1px solid #d1d5db; border-radius: 6px; font-size: 13px; background: #fff;
}
.filters input { flex: 1; min-width: 180px; }
.filters button {
  background: #e5e7eb; color: #374151; border: 0; padding: 8px 12px;
  border-radius: 6px; cursor: pointer; font-size: 13px;
}
.filters button:hover { background: #d1d5db; }
.filters button.primary { background: #4f46e5; color: #fff; }
.filters button.primary:hover { background: #4338ca; }
.filter-banner {
  background: #eef2ff; border: 1px solid #c7d2fe; padding: 8px 12px;
  border-radius: 6px; margin-bottom: 10px; color: #3730a3; font-size: 13px;
}

/* ===== REQ LIST ===== */
.reqlist { display: flex; flex-direction: column; gap: 10px; }
.reqcard {
  background: #fff; border-radius: 10px; padding: 14px 16px;
  box-shadow: 0 1px 3px rgba(0,0,0,.05);
  display: grid; grid-template-columns: 1fr auto; gap: 8px;
  cursor: pointer; transition: transform .1s;
  border-left: 4px solid #cbd5e1;
}
.reqcard:hover { transform: translateY(-1px); box-shadow: 0 4px 10px rgba(0,0,0,.08); }
.reqcard.s-requested    { border-left-color: #6366f1; }
.reqcard.s-booked       { border-left-color: #0ea5e9; }
.reqcard.s-transferred  { border-left-color: #14b8a6; }
.reqcard.s-completed    { border-left-color: #22c55e; }
.reqcard.s-advance_open { border-left-color: #f59e0b; }
.reqcard.s-cleared      { border-left-color: #16a34a; }
.reqcard.s-rejected     { border-left-color: #ef4444; opacity: .7; }
.reqcard .ttl { font-weight: 600; color: #111827; }
.reqcard .meta { font-size: 12px; color: #6b7280; margin-top: 4px; }
.reqcard .amt { font-weight: 700; color: #1e3a8a; font-size: 16px; text-align: right; }

.stbadge {
  display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px;
  background: #e0e7ff; color: #3730a3; margin-left: 6px;
}
.stbadge.requested { background: #e0e7ff; color: #3730a3; }
.stbadge.booked { background: #e0f2fe; color: #075985; }
.stbadge.transferred { background: #ccfbf1; color: #0f766e; }
.stbadge.completed { background: #dcfce7; color: #166534; }
.stbadge.advance_open { background: #fef3c7; color: #92400e; }
.stbadge.cleared { background: #d1fae5; color: #065f46; }
.stbadge.rejected { background: #fee2e2; color: #991b1b; }

.flow-track { display: flex; gap: 6px; margin-top: 6px; }
.flow-step {
  flex: 1; padding: 4px; font-size: 11px; text-align: center;
  background: #f3f4f6; color: #6b7280; border-radius: 4px;
}
.flow-step.done { background: #dcfce7; color: #166534; }

/* ===== FORM ===== */
.form {
  background: #fff; padding: 20px; border-radius: 10px;
  box-shadow: 0 1px 3px rgba(0,0,0,.05); max-width: 900px;
}
.form label { display: block; font-size: 13px; color: #374151; margin: 10px 0 4px; }
.form input, .form select, .form textarea {
  width: 100%; padding: 8px 10px; border: 1px solid #d1d5db; border-radius: 6px;
  font-family: inherit; font-size: 14px;
}
.form .grid2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px 16px; }
.form .req { color: #ef4444; }
.form button.big { padding: 12px 20px; font-size: 15px; }
button.primary {
  background: #4f46e5; color: #fff; border: 0; padding: 10px 18px;
  border-radius: 8px; cursor: pointer; font-weight: 600;
}
button.primary:hover { background: #4338ca; }
button.success { background: #16a34a; color:#fff; border:0; padding: 8px 14px; border-radius: 6px; cursor: pointer; }
button.success:hover { background: #15803d; }
button.danger { background: #ef4444; color:#fff; border:0; padding: 8px 14px; border-radius: 6px; cursor: pointer; }
button.danger:hover { background: #dc2626; }

/* ===== SIGNATURE ===== */
.sigblock { margin-top: 16px; }
.sigblock canvas { border: 2px dashed #94a3b8; border-radius: 8px; background: #fafbff; cursor: crosshair; display: block; touch-action: none; max-width: 100%; }
.sigbtns { display: flex; gap: 8px; margin-top: 6px; }
.sigbtns button { background: #e5e7eb; border: 0; padding: 6px 12px; border-radius: 6px; cursor: pointer; }
.sigbtns button.primary { background: #4f46e5; color: #fff; }

/* ===== MODAL ===== */
.modal {
  position: fixed; inset: 0; background: rgba(15, 23, 42, 0.55);
  display: flex; align-items: center; justify-content: center; z-index: 100; padding: 20px;
}
.modal-content {
  background: #fff; border-radius: 12px; padding: 24px;
  width: 760px; max-width: 100%; max-height: 90vh; overflow-y: auto; position: relative;
}
.modal-content.small { width: 560px; }
.modal-content .close {
  position: absolute; top: 8px; right: 12px;
  background: transparent; border: 0; font-size: 26px; cursor: pointer; color: #6b7280;
}
.modal-content canvas { max-width: 100%; }

/* ===== AUDIT LIST ===== */
.auditlist { background: #fff; border-radius: 10px; padding: 8px; box-shadow: 0 1px 3px rgba(0,0,0,.05); }
.auditrow {
  border-bottom: 1px solid #e5e7eb; padding: 10px;
  display: grid; grid-template-columns: 160px 1fr 100px; gap: 10px; align-items: center;
}
.auditrow:last-child { border-bottom: 0; }
.auditrow .ts { font-family: "Consolas", monospace; font-size: 12px; color: #6b7280; }
.auditrow .desc { font-size: 13px; }
.auditrow .desc b { color: #1e3a8a; }
.auditrow a.linkno { color: #4f46e5; cursor: pointer; text-decoration: underline; font-weight: 600; }
.auditrow .sigthumb { width: 90px; height: 36px; object-fit: contain; border: 1px solid #e5e7eb; border-radius: 4px; background: #fafbff; cursor: zoom-in; }

/* ===== DETAIL MODAL ===== */
.det-section { margin-top: 16px; padding-top: 12px; border-top: 1px dashed #e5e7eb; }
.det-section h4 { margin: 0 0 8px; color: #1e3a8a; font-size: 14px; }
.det-row { display: grid; grid-template-columns: 160px 1fr; gap: 8px; padding: 4px 0; font-size: 13px; }
.det-row .k { color: #6b7280; }
.det-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.evidence-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
.evidence-grid .ev {
  border: 1px solid #e5e7eb; border-radius: 6px; padding: 6px; text-align: center; font-size: 11px; position: relative;
}
.evidence-grid .ev img { max-width: 100%; max-height: 80px; display: block; margin: 0 auto; }
.evidence-grid .ev .delx {
  position: absolute; top: 2px; right: 4px; background: #ef4444; color: #fff;
  width: 20px; height: 20px; border-radius: 50%; border: 0; cursor: pointer; font-size: 12px; line-height: 1;
}
.timeline { border-left: 3px solid #c7d2fe; padding-left: 14px; margin-left: 8px; }
.timeline .tline { margin-bottom: 14px; position: relative; }
.timeline .tline:before {
  content: ""; position: absolute; left: -20px; top: 4px; width: 11px; height: 11px;
  background: #4f46e5; border-radius: 50%;
}
.timeline .tline .when { font-size: 11px; color: #6b7280; }
.timeline .tline .what { font-weight: 600; color: #111827; }
.timeline .tline .who  { font-size: 12px; color: #374151; }
.timeline .tline img { width: 140px; border: 1px solid #e5e7eb; border-radius: 4px; margin-top: 4px; background: #fafbff; }

/* ===== ADMIN ===== */
.admin-tabs { display: flex; gap: 6px; margin-bottom: 14px; flex-wrap: wrap; border-bottom: 2px solid #e5e7eb; }
.admin-tabs .atab {
  background: transparent; border: 0; padding: 8px 14px; cursor: pointer;
  font-size: 13px; color: #6b7280; border-bottom: 3px solid transparent;
}
.admin-tabs .atab.active { color: #4f46e5; border-bottom-color: #4f46e5; }
.apanel { background: #fff; padding: 14px; border-radius: 10px; box-shadow: 0 1px 3px rgba(0,0,0,.05); }
.atable {
  width: 100%; border-collapse: collapse; font-size: 13px;
}
.atable th, .atable td {
  padding: 8px 10px; border-bottom: 1px solid #e5e7eb; text-align: left; vertical-align: top;
}
.atable th { background: #f8fafc; color: #374151; font-weight: 600; }
.atable tr.linkrow { cursor: pointer; }
.atable tr.linkrow:hover { background: #f5f3ff; }
.atable .actions { display: flex; gap: 6px; }
.atable .actions button {
  background: #e5e7eb; border: 0; padding: 4px 8px; border-radius: 4px; cursor: pointer; font-size: 12px;
}
.atable .actions button.danger { background: #fee2e2; color: #991b1b; }
.atable .actions button.primary { background: #4f46e5; color: #fff; }
.atable .linkno {
  color: #4f46e5; cursor: pointer; text-decoration: underline; font-weight: 600;
}
.export-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 10px; margin-top: 10px; }
.ex-btn {
  background: #4f46e5; color: #fff; padding: 12px; border-radius: 8px;
  text-decoration: none; text-align: center; font-weight: 600;
}
.ex-btn:hover { background: #4338ca; }
.ex-btn.db { background: #16a34a; }
.ex-btn.db:hover { background: #15803d; }

/* ===== TOAST ===== */
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: #1e293b; color: #fff; padding: 12px 20px; border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0,0,0,.2); z-index: 200; font-size: 14px;
}
.toast.err { background: #dc2626; }
.toast.ok { background: #16a34a; }

@media (max-width: 720px) {
  .form .grid2 { grid-template-columns: 1fr; }
  .auditrow { grid-template-columns: 1fr; }
  .user-mgmt .row { grid-template-columns: 1fr; }
}

/* ===== ATTACHMENT TOOLS ===== */
.att-item { text-align: center; }
.att-tools {
  display: flex; gap: 4px; justify-content: center; margin-top: 4px;
}
.att-tools button {
  font-size: 11px; padding: 3px 8px; border-radius: 4px; cursor: pointer; border: 0;
  background: #6366f1; color: #fff;
}
.att-tools button.danger { background: #ef4444; }
.att-tools button:hover { opacity: .85; }
.att-add {
  font-size: 12px; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 0;
  background: #4f46e5; color: #fff; font-weight: 600;
}
.att-add:hover { background: #4338ca; }
.ev { position: relative; }
