:root {
  --nav: #12324a;
  --primary: #0f766e;
  --primary-dark: #0a5c56;
  --ink: #17202a;
  --muted: #667085;
  --line: #d8dee5;
  --bg: #f5f7f9;
  --panel: #ffffff;
  --success: #087443;
  --warning: #b54708;
  --danger: #b42318;
  --info: #175cd3;
  --locked: #344054;
  --focus: #84c5bd;
}

* { box-sizing: border-box; }

html { min-height: 100%; }

body {
  min-height: 100%;
  margin: 0;
  font-family: Arial, "Noto Sans Thai", "Tahoma", sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 14px;
  letter-spacing: 0;
}

a { color: inherit; }

button,
input,
select,
textarea {
  font: inherit;
}

button:focus,
input:focus,
select:focus,
textarea:focus,
a:focus {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

.app-frame {
  display: grid;
  grid-template-columns: 244px minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  background: var(--nav);
  color: #fff;
  padding: 16px 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 4px 8px 18px;
  color: #fff;
  text-decoration: none;
  font-weight: 700;
}

.brand-mark {
  display: inline-grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: #e6fffb;
  color: var(--primary-dark);
  font-weight: 700;
}

.brand-text { line-height: 1.25; }

.primary-nav {
  display: grid;
  gap: 4px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 38px;
  padding: 8px 10px;
  border-radius: 6px;
  color: #d8e6ef;
  text-decoration: none;
}

.nav-item.active,
.nav-item:hover {
  background: rgba(255, 255, 255, .14);
  color: #fff;
}

.nav-icon {
  display: inline-grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border: 1px solid rgba(255, 255, 255, .22);
  border-radius: 6px;
  font-size: 12px;
}

.workspace {
  min-width: 0;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  min-height: 58px;
  padding: 10px 18px;
  background: #fff;
  border-bottom: 1px solid var(--line);
}

.context-strip,
.user-strip,
.header-actions,
.pagination-actions,
.modal-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.context-chip,
.counter-chip {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 6px;
  padding: 5px 8px;
  color: #344054;
  text-decoration: none;
}

.counter-chip {
  background: #f8fafc;
  font-weight: 700;
}

.status-open {
  color: var(--success);
  border-color: #abefc6;
  background: #ecfdf3;
}

.main-content {
  padding: 18px;
}

.page-header,
.document-header,
.section-heading,
.pagination-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: flex-start;
}

.page-header,
.document-header {
  margin-bottom: 14px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-transform: none;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 4px;
  font-size: 26px;
}

h2 {
  margin-bottom: 0;
  font-size: 16px;
}

.page-description {
  margin-bottom: 0;
  color: var(--muted);
}

.panel,
.kpi-card,
.login-card,
.confirm-dialog {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.panel {
  padding: 14px;
  margin-bottom: 14px;
}

.kpi-strip {
  display: grid;
  grid-template-columns: repeat(6, minmax(132px, 1fr));
  gap: 10px;
  margin-bottom: 14px;
}

.kpi-card {
  padding: 12px;
  min-height: 96px;
}

.kpi-label,
.kpi-note,
.table-count,
.timeline-time,
.attachment-item small {
  color: var(--muted);
  font-size: 12px;
}

.kpi-value {
  margin-top: 6px;
  font-size: 20px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.kpi-note { margin-top: 5px; }

.dashboard-grid,
.detail-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.45fr) 360px;
  gap: 14px;
}

.two-column,
.report-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}

.filter-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: flex-end;
  padding: 12px 0;
}

.filter-field {
  display: grid;
  gap: 4px;
  min-width: 164px;
  color: var(--muted);
  font-size: 12px;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  padding: 7px 9px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 7px 10px;
  background: #fff;
  color: var(--ink);
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
}

.btn-small {
  min-height: 28px;
  padding: 4px 8px;
  font-size: 12px;
}

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

.btn-primary:hover {
  background: var(--primary-dark);
}

.btn-secondary {
  background: #f8fafc;
  color: #344054;
}

.btn-ghost {
  background: transparent;
}

.btn-danger {
  background: #fff5f5;
  border-color: #fecdca;
  color: var(--danger);
}

.btn:disabled {
  cursor: not-allowed;
  opacity: .55;
}

.table-wrap {
  width: 100%;
  overflow-x: auto;
}

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

.data-table th,
.data-table td {
  border-bottom: 1px solid var(--line);
  padding: 8px;
  text-align: left;
  white-space: nowrap;
  vertical-align: middle;
}

.data-table th {
  background: #f8fafc;
  color: var(--muted);
  font-weight: 700;
}

.amount {
  text-align: right !important;
  font-variant-numeric: tabular-nums;
}

.table-link {
  color: var(--primary-dark);
  font-weight: 700;
  text-decoration: none;
}

.actions-cell {
  display: flex;
  gap: 6px;
}

.status-badge,
.approval-badge {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  border: 1px solid currentColor;
  border-radius: 999px;
  padding: 3px 8px;
  font-size: 12px;
  font-weight: 700;
}

.status-draft,
.approval-draft,
.approval-none {
  color: var(--locked);
  background: #f8fafc;
}

.status-pending,
.approval-pending {
  color: var(--warning);
  background: #fffaeb;
}

.status-approved,
.approval-approved {
  color: var(--info);
  background: #eff8ff;
}

.status-posted,
.status-filed {
  color: var(--success);
  background: #ecfdf3;
}

.status-closed,
.status-locked {
  color: #1d2939;
  background: #eaecf0;
}

.status-cancelled,
.status-reversed,
.approval-rejected {
  color: var(--danger);
  background: #fff5f5;
}

.blocked-alert {
  margin-bottom: 14px;
  border: 1px solid #fecdca;
  border-left: 4px solid var(--danger);
  border-radius: 8px;
  background: #fff5f5;
  color: #7a271a;
  padding: 10px 12px;
}

.blocked-alert ul {
  margin: 6px 0 0;
  padding-left: 20px;
}

.action-bar {
  position: sticky;
  top: 0;
  z-index: 3;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin-bottom: 12px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, .96);
}

.tab-group {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  margin-bottom: 12px;
  border-bottom: 1px solid var(--line);
}

.tab-item {
  padding: 9px 10px;
  color: var(--muted);
  border-bottom: 3px solid transparent;
  text-decoration: none;
  white-space: nowrap;
}

.tab-item.active {
  color: var(--primary-dark);
  border-color: var(--primary);
  font-weight: 700;
}

.facts-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.fact-item {
  display: grid;
  gap: 3px;
  padding: 10px;
  background: #f8fafc;
  border-radius: 6px;
}

.fact-item span {
  color: var(--muted);
  font-size: 12px;
}

.compact-facts {
  display: grid;
  grid-template-columns: minmax(120px, .8fr) 1fr;
  gap: 8px 12px;
  margin: 0;
}

.compact-facts dt {
  color: var(--muted);
}

.compact-facts dd {
  margin: 0;
  font-weight: 700;
}

.document-status {
  display: grid;
  justify-items: end;
  gap: 6px;
}

.document-total {
  font-size: 24px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.timeline {
  display: grid;
  gap: 12px;
  margin: 0;
  padding-left: 18px;
}

.timeline p {
  margin-bottom: 0;
  color: var(--muted);
}

.attachment-list {
  display: grid;
  gap: 8px;
}

.attachment-item,
.empty-state,
.upload-control {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 9px 10px;
  background: #f8fafc;
}

.upload-control {
  cursor: pointer;
  color: var(--primary-dark);
  font-weight: 700;
}

.upload-control input {
  max-width: 220px;
}

.task-list {
  display: grid;
  gap: 8px;
  margin: 12px 0 0;
  padding: 0;
  list-style: none;
}

.task-list li {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
}

.report-card p {
  min-height: 40px;
  color: var(--muted);
}

.login-page {
  display: grid;
  place-items: center;
  min-height: 100vh;
  padding: 24px;
  background: #eef4f7;
}

.login-card {
  width: min(420px, 100%);
  padding: 24px;
}

.brand-login {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 18px;
}

.brand-login h1 {
  font-size: 22px;
}

.login-form {
  display: grid;
  gap: 12px;
}

.login-form label {
  display: grid;
  gap: 5px;
}

.confirm-modal {
  display: none;
}

.confirm-dialog {
  width: min(460px, 92vw);
  padding: 16px;
}

.empty-row {
  padding: 22px !important;
  text-align: center !important;
  color: var(--muted);
}

@media (max-width: 1180px) {
  .kpi-strip {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .dashboard-grid,
  .detail-layout,
  .two-column,
  .report-grid {
    grid-template-columns: 1fr;
  }

  .activity-panel {
    order: 2;
  }
}

@media (max-width: 820px) {
  .app-frame {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: static;
  }

  .primary-nav {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .topbar,
  .page-header,
  .document-header,
  .pagination-row {
    align-items: stretch;
    flex-direction: column;
  }

  .kpi-strip,
  .facts-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .main-content {
    padding: 12px;
  }
}

@media (max-width: 520px) {
  .primary-nav,
  .kpi-strip,
  .facts-grid {
    grid-template-columns: 1fr;
  }

  .filter-field,
  .btn {
    width: 100%;
  }

  .header-actions,
  .filter-toolbar,
  .action-bar {
    align-items: stretch;
    flex-direction: column;
  }
}
