:root {
  --green-900: #0f3d22;
  --green-800: #14532d;
  --green-700: #166534;
  --green-600: #15803d;
  --green-50: #f0fdf4;
  --bg: #f5f7f6;
  --card: #ffffff;
  --text: #1a2420;
  --muted: #5b6b63;
  --line: #d9e1dc;
  --danger: #b42318;
  --warn-bg: #fff4e5;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 40, 24, .06), 0 2px 8px rgba(16, 40, 24, .05);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", Arial, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { font: 15px/1.5 var(--font); color: var(--text); background: var(--bg); -webkit-text-size-adjust: 100%; }
[hidden] { display: none !important; }
h1 { font-size: 1.35rem; margin: 0 0 .25rem; }
h2 { font-size: 1.3rem; margin: 0; }
h3 { font-size: 1.1rem; margin: 0; }
h4 { font-size: .95rem; margin: 1rem 0 .5rem; color: var(--green-800); }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
code { font-family: ui-monospace, Consolas, monospace; }

/* Butang */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .35rem;
  min-height: 42px; padding: .5rem .95rem; border-radius: 8px; border: 1px solid var(--line);
  background: #fff; color: var(--text); font: inherit; font-weight: 500; cursor: pointer; text-decoration: none;
}
.btn:hover { background: #f1f5f3; }
.btn:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, .icon-btn:focus-visible {
  outline: 3px solid #86efac; outline-offset: 1px;
}
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--green-700); border-color: var(--green-700); color: #fff; }
.btn-primary:hover { background: var(--green-800); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger:hover { background: #8f1c13; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--green-800); }
.btn-light { background: #fff; color: var(--green-900); }
.btn-sm { min-height: 36px; padding: .3rem .7rem; font-size: .88rem; }
.btn-block { width: 100%; }
.icon-btn { border: 0; background: transparent; font-size: 1.3rem; width: 42px; height: 42px; border-radius: 8px; cursor: pointer; color: inherit; }
.icon-btn:hover { background: rgba(0,0,0,.06); }

/* Medan */
.field { display: flex; flex-direction: column; gap: .3rem; margin-bottom: .8rem; min-width: 0; }
.field > span, .field-row > span { font-weight: 600; font-size: .9rem; }
.field-row { display: flex; justify-content: space-between; align-items: center; gap: .5rem; flex-wrap: wrap; }
input, select, textarea {
  font: inherit; color: inherit; width: 100%; min-height: 42px; padding: .5rem .65rem;
  border: 1px solid #b8c6be; border-radius: 8px; background: #fff;
}
textarea { resize: vertical; min-height: 84px; }
input[type=checkbox], input[type=radio] { width: 20px; height: 20px; min-height: 0; accent-color: var(--green-700); }
input[type=range] { padding: 0; min-height: 30px; accent-color: var(--green-700); }
.req { color: var(--danger); }
.hint { font-size: .85rem; color: var(--muted); }
.hint.warn { color: #9a3412; background: var(--warn-bg); padding: .45rem .6rem; border-radius: 6px; }
.counter { font-size: .78rem; color: var(--muted); text-align: right; }
.form-msg { color: var(--danger); margin: .3rem 0; min-height: 1.2em; font-weight: 500; white-space: pre-line; }
.form-msg:empty { min-height: 0; margin: 0; }
.check { display: inline-flex; align-items: center; gap: .45rem; cursor: pointer; min-height: 36px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 1rem; }
.field-inline { display: flex; align-items: center; gap: .5rem; }
.field-inline select { width: auto; }

/* Kad & paparan berpusat */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.center-view { min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 1rem; gap: .5rem; }
.auth-card { width: 100%; max-width: 400px; padding: 1.5rem; }
.brand-block { display: flex; gap: .8rem; align-items: center; margin-bottom: 1.2rem; }
.brand-block img { border-radius: 12px; }
.brand-block p { margin: 0; }
.spinner { width: 36px; height: 36px; border: 4px solid #cfe3d6; border-top-color: var(--green-700); border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Banner */
.banner { position: sticky; top: 0; z-index: 60; padding: .55rem 1rem; display: flex; gap: .8rem; align-items: center; justify-content: center; flex-wrap: wrap; font-weight: 500; }
.banner-warn { background: #7c2d12; color: #fff; }
.banner-info { background: var(--green-900); color: #fff; }

/* Susun atur aplikasi */
.app { display: grid; grid-template-columns: 250px 1fr; min-height: 100vh; }
.topbar { display: none; }
.sidebar { background: var(--green-900); color: #e6f4ea; padding: 1rem .8rem; display: flex; flex-direction: column; gap: 1rem; position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.side-brand { display: flex; align-items: center; gap: .6rem; font-weight: 700; font-size: 1.1rem; padding: 0 .3rem; }
.side-brand img { border-radius: 8px; }
.side-nav { display: flex; flex-direction: column; gap: .2rem; }
.side-nav a { color: #e6f4ea; text-decoration: none; padding: .65rem .7rem; border-radius: 8px; font-weight: 500; }
.side-nav a:hover { background: rgba(255,255,255,.08); }
.side-nav a.active { background: var(--green-700); color: #fff; }
.side-user, .side-app { border-top: 1px solid rgba(255,255,255,.15); padding-top: .9rem; display: flex; flex-direction: column; gap: .4rem; }
.user-name { font-weight: 700; }
.user-role { font-size: .85rem; opacity: .85; margin-bottom: .3rem; }
.btn-side { background: transparent; color: #e6f4ea; border-color: rgba(255,255,255,.25); justify-content: flex-start; }
.btn-side:hover { background: rgba(255,255,255,.08); }
.side-note { opacity: .8; }
.main { padding: 1.25rem; min-width: 0; }
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 39; }

.page-head { display: flex; align-items: center; justify-content: space-between; gap: .8rem; flex-wrap: wrap; margin-bottom: 1rem; }
.head-actions { display: flex; gap: .5rem; flex-wrap: wrap; }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: .8rem; margin-bottom: 1rem; }
.stat { background: #fff; border: 1px solid var(--line); border-left: 4px solid var(--green-600); border-radius: var(--radius); padding: .75rem 1rem; }
.stat-num { font-size: 1.6rem; font-weight: 700; color: var(--green-800); }
.stat-lbl { color: var(--muted); font-size: .88rem; }

.filters { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 0 .8rem; padding: .9rem .9rem 0; margin-bottom: 1rem; }
.filter-actions { grid-column: 1 / -1; display: flex; gap: .5rem; flex-wrap: wrap; padding-bottom: .9rem; }

/* Jadual */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.grid { width: 100%; border-collapse: collapse; min-width: 980px; }
.grid th, .grid td { padding: .6rem .65rem; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.grid th { background: #eef4f0; font-size: .85rem; color: #2a3b31; white-space: nowrap; position: sticky; top: 0; }
.grid tbody tr:hover { background: #fafcfb; }
.c-bil { width: 56px; text-align: right !important; }
.c-date { width: 110px; white-space: nowrap; }
.c-days { width: 90px; text-align: center !important; }
.c-title { min-width: 220px; }
.c-sender { min-width: 140px; }
.c-wf { min-width: 180px; }
.c-act { width: 130px; }
.act-col .btn { white-space: nowrap; }
.cell-title { white-space: pre-wrap; word-break: break-word; overflow-wrap: anywhere; }
.cell-ref { color: var(--muted); font-size: .85rem; margin-top: .25rem; word-break: break-word; }
.cell-sender { word-break: break-word; overflow-wrap: anywhere; }
.wf-meta { font-size: .84rem; color: var(--muted); margin-top: .3rem; }
.wf-meta b { color: var(--text); font-weight: 600; }
.act-col { display: flex; flex-direction: column; gap: .35rem; align-items: stretch; }
.grid-state { padding: 2rem 1rem; text-align: center; color: var(--muted); }
.grid-state .btn { margin-top: .6rem; }
.not-yet { color: var(--muted); font-style: italic; }
.missing { color: #9a3412; font-weight: 500; }

.badge { display: inline-block; padding: .18rem .55rem; border-radius: 999px; font-size: .8rem; font-weight: 700; border: 1px solid transparent; white-space: nowrap; }
.st-menunggu_minit { background: #fff1d6; color: #8a4b00; border-color: #f5c46b; }
.st-menunggu_edaran { background: #e0ecff; color: #1e429f; border-color: #a4c2f4; }
.st-dalam_tindakan { background: #f1e6ff; color: #5b21b6; border-color: #c9b0f2; }
.st-selesai { background: #dcfce7; color: #14532d; border-color: #86d6a3; }

.pager { display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; margin-top: 1rem; }
.pg-select select { width: auto; display: inline-block; min-height: 38px; }
.pg-info { color: var(--muted); font-size: .9rem; margin-left: auto; }

/* Dialog */
.dlg { border: 0; border-radius: 14px; padding: 0; width: min(560px, calc(100vw - 1rem)); max-height: calc(100vh - 1rem); box-shadow: 0 20px 50px rgba(0,0,0,.25); color: var(--text); }
.dlg-wide { width: min(820px, calc(100vw - 1rem)); }
.dlg-xl { width: min(1100px, calc(100vw - 1rem)); }
.dlg::backdrop { background: rgba(10, 25, 15, .45); }
.dlg > form, .dlg { display: flex; flex-direction: column; }
.dlg:not([open]) { display: none; }
.dlg-head { display: flex; justify-content: space-between; align-items: center; padding: .8rem 1rem; border-bottom: 1px solid var(--line); background: var(--green-50); }
.dlg-body { padding: 1rem; overflow-y: auto; }
.dlg-foot { display: flex; justify-content: flex-end; gap: .5rem; padding: .8rem 1rem; border-top: 1px solid var(--line); flex-wrap: wrap; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: .4rem .8rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-weight: 600; word-break: break-word; }
.pw { font-size: 1.15rem; background: #f1f5f3; padding: .25rem .5rem; border-radius: 6px; user-select: all; letter-spacing: .03em; }

.tabs { display: flex; gap: .3rem; border-bottom: 2px solid var(--line); margin-bottom: 1rem; }
.tab { border: 0; background: transparent; padding: .6rem .9rem; font: inherit; font-weight: 600; color: var(--muted); cursor: pointer; border-bottom: 3px solid transparent; margin-bottom: -2px; min-height: 44px; }
.tab.active { color: var(--green-800); border-bottom-color: var(--green-700); }
.tab-panel { border: 0; padding: 0; margin: 0; min-width: 0; }
.officer-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: .3rem .8rem; margin-bottom: .8rem; }
.officer-grid .check { padding: .2rem .4rem; border: 1px solid var(--line); border-radius: 8px; }
.officer-grid .check:has(input:checked) { background: var(--green-50); border-color: #86d6a3; }
.officer-grid .check:has(input:disabled) { opacity: .55; }
.radio-row { display: flex; gap: 1rem; flex-wrap: wrap; }
.status-preview { padding: .6rem .8rem; background: #f6f8f7; border-radius: 8px; }

/* Imbasan */
.scan-src { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; margin-bottom: .5rem; }
.file-btn { position: relative; }
.scan-stage { position: relative; display: inline-block; max-width: 100%; background: #333; border-radius: 8px; overflow: hidden; line-height: 0; }
.scan-stage canvas { display: block; max-width: 100%; height: auto; max-height: 60vh; }
#scan-overlay { position: absolute; left: 0; top: 0; width: 100%; height: 100%; touch-action: none; cursor: crosshair; max-height: none; }
.scan-tools { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: .8rem; margin: .8rem 0; }
.scan-tools fieldset { border: 1px solid var(--line); border-radius: 8px; padding: .5rem .7rem; margin: 0; min-width: 0; }
.scan-tools legend { font-weight: 700; font-size: .88rem; color: var(--green-800); padding: 0 .3rem; }
.slider { display: grid; grid-template-columns: 5.5rem 1fr 3rem; align-items: center; gap: .4rem; font-size: .88rem; }
.slider output { text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); }
.btn-row { display: flex; gap: .4rem; flex-wrap: wrap; margin-bottom: .3rem; }
.scan-result { background: #f1f1f1; border: 1px dashed #aaa; border-radius: 8px; padding: .4rem; overflow: auto; max-height: 240px; }
.scan-result canvas { max-width: 100%; height: auto; display: block; background: #fff; }
.ocr-status { margin-top: .8rem; padding: .7rem .9rem; border-radius: 8px; background: var(--green-50); border: 1px solid #b7e1c3; font-weight: 500; }
.ocr-status.err { background: #fdecea; border-color: #f3b3ad; color: var(--danger); }

/* Toast */
.toast-area { position: fixed; right: 1rem; bottom: 1rem; z-index: 80; display: flex; flex-direction: column; gap: .5rem; max-width: min(420px, calc(100vw - 2rem)); }
.toast { background: var(--green-900); color: #fff; padding: .75rem 1rem; border-radius: 10px; box-shadow: var(--shadow); font-weight: 500; }
.toast.err { background: var(--danger); }
.toast.warn { background: #9a3412; }

.print-head { display: none; }

/* Responsif */
@media (max-width: 1100px) {
  .filters { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .scan-tools { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .topbar { display: flex; align-items: center; gap: .5rem; position: sticky; top: 0; z-index: 30; background: var(--green-900); color: #fff; padding: .35rem .6rem; }
  .topbar-title { display: flex; align-items: center; gap: .5rem; font-weight: 700; }
  .topbar-title img { border-radius: 6px; }
  .sidebar { position: fixed; left: 0; top: 0; bottom: 0; width: 270px; z-index: 40; transform: translateX(-100%); transition: transform .2s; }
  .sidebar.open { transform: none; }
  .main { padding: .8rem; }
  .officer-grid { grid-template-columns: repeat(2, 1fr); }
  .scan-tools { grid-template-columns: 1fr; }
}
@media (max-width: 600px) {
  .stats { grid-template-columns: 1fr 1fr 1fr; gap: .4rem; }
  .stat { padding: .5rem .6rem; }
  .stat-num { font-size: 1.25rem; }
  .stat-lbl { font-size: .75rem; }
  .filters { grid-template-columns: 1fr 1fr; }
  .grid-2 { grid-template-columns: 1fr; }
  .pg-info { margin-left: 0; width: 100%; }
  .dlg { max-height: 100vh; width: 100vw; border-radius: 0; }
}

/* Cetakan */
@media print {
  @page { size: A4 landscape; margin: 10mm; }
  body { background: #fff; font-size: 10pt; }
  .sidebar, .topbar, .scrim, .filters, .pager, .head-actions, .banner, .toast-area, .stats, .no-print, dialog, .page-head { display: none !important; }
  .app { display: block; }
  .main { padding: 0; }
  .card { box-shadow: none; border: 0; }
  .table-wrap { overflow: visible; }
  .print-head { display: block; margin-bottom: 6pt; }
  .print-head h1 { font-size: 13pt; }
  .print-head p { margin: 0; font-size: 9pt; }
  .grid { min-width: 0; width: 100%; table-layout: fixed; }
  .grid th { position: static; white-space: normal; background: #eee !important; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .grid th, .grid td { padding: 4pt 5pt; border: 1px solid #999; font-size: 9pt; }
  .grid tr { break-inside: avoid; page-break-inside: avoid; }
  .grid thead { display: table-header-group; }
  .c-bil { width: 7%; } .c-date { width: 11%; } .c-days { width: 8%; } .c-title { width: 30%; } .c-sender { width: 17%; } .c-wf { width: 16%; }
  .cell-title, .cell-sender { white-space: pre-wrap; overflow-wrap: anywhere; }
  .cell-ref, .wf-meta { font-size: 8pt; }
  .badge { font-size: 8pt; border: 1px solid #555; background: none !important; color: #000 !important; }
  .wf-btn { display: none !important; }
}
