/* ==========================================================
   ROOT / DESIGN TOKENS
   ========================================================== */
:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --primary-light: #dbeafe;
  --emerald: #10b981;
  --emerald-dark: #059669;
  --emerald-light: #d1fae5;
  --danger: #ef4444;
  --danger-light: #fee2e2;
  --warning: #f59e0b;
  --warning-light: #fef3c7;

  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e5e9f0;
  --border-strong: #d7dce5;

  --text: #1e293b;
  --text-soft: #64748b;
  --text-faint: #94a3b8;

  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 18px;

  --shadow-sm: 0 1px 3px rgba(30, 41, 59, 0.06), 0 1px 2px rgba(30, 41, 59, 0.04);
  --shadow-md: 0 4px 16px rgba(30, 41, 59, 0.08);
  --shadow-lg: 0 12px 32px rgba(30, 41, 59, 0.12);

  --sidebar-w: 268px;
  --sidebar-w-collapsed: 78px;
  --topbar-h: 64px;

  --font: "Inter", -apple-system, BlinkMacSystemFont, sans-serif;
}

[data-theme="dark"] {
  --bg: #0f172a;
  --surface: #1a2436;
  --surface-2: #16202f;
  --border: #2b374a;
  --border-strong: #384457;
  --text: #e6ebf3;
  --text-soft: #9aa9bd;
  --text-faint: #6b7a90;
  --primary-light: #17305f;
  --emerald-light: #0d3327;
  --danger-light: #4a1414;
  --warning-light: #45330a;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

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

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 10px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-faint); }

/* ==========================================================
   APP SHELL
   ========================================================== */
.app-shell {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ==========================================================
   SIDEBAR
   ========================================================== */
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: width 0.25s ease;
  z-index: 40;
}

.app-shell.collapsed .sidebar { width: var(--sidebar-w-collapsed); }

.sidebar-brand {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 18px;
  border-bottom: 1px solid var(--border);
  position: relative;
}

.brand-mark {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--primary), var(--emerald));
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 15px;
}

.brand-text {
  font-weight: 700;
  font-size: 16px;
  white-space: nowrap;
  overflow: hidden;
}
.brand-text em { color: var(--primary); font-style: normal; }

.app-shell.collapsed .brand-text,
.app-shell.collapsed .sidebar-search,
.app-shell.collapsed .nav-item span,
.app-shell.collapsed .nav-parent span,
.app-shell.collapsed .nav-parent .chev,
.app-shell.collapsed .nav-children,
.app-shell.collapsed .user-meta {
  display: none;
}

.sidebar-collapse-btn {
  margin-left: auto;
  border: none;
  background: transparent;
  color: var(--text-soft);
  width: 28px;
  height: 28px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}
.sidebar-collapse-btn:hover { background: var(--surface-2); color: var(--text); }
.app-shell.collapsed .sidebar-collapse-btn i { transform: rotate(180deg); }

.sidebar-search {
  margin: 14px 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  color: var(--text-faint);
}
.sidebar-search input {
  border: none;
  outline: none;
  background: transparent;
  color: var(--text);
  font-size: 13px;
  width: 100%;
}

.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  padding: 4px 12px 12px;
}

.nav-item, .nav-parent {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 10px 12px;
  border: none;
  background: transparent;
  color: var(--text-soft);
  font-size: 13.5px;
  font-weight: 500;
  border-radius: var(--radius-sm);
  cursor: pointer;
  text-decoration: none;
  margin-bottom: 2px;
  transition: background 0.15s, color 0.15s;
}
.nav-item i, .nav-parent i:first-child { width: 18px; text-align: center; font-size: 14px; }

.nav-item:hover, .nav-parent:hover { background: var(--surface-2); color: var(--text); }
.nav-item.active-item { background: var(--primary-light); color: var(--primary-dark); font-weight: 600; }

.chev { margin-left: auto; font-size: 11px; transition: transform 0.2s; }
.nav-group.open .chev { transform: rotate(180deg); }

.nav-children {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.25s ease;
  padding-left: 8px;
}
.nav-group.open .nav-children { max-height: 500px; }

.nav-child {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px 8px 24px;
  font-size: 13px;
  color: var(--text-soft);
  text-decoration: none;
  border-radius: var(--radius-sm);
  margin-bottom: 1px;
}
.nav-child i { font-size: 6px; }
.nav-child:hover { background: var(--surface-2); color: var(--text); }
.nav-child.active { background: var(--emerald-light); color: var(--emerald-dark); font-weight: 600; }
.nav-child.active i { color: var(--emerald-dark); }

.sidebar-footer {
  padding: 14px 16px;
  border-top: 1px solid var(--border);
}
.user-chip { display: flex; align-items: center; gap: 10px; }
.avatar {
  width: 36px; height: 36px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--primary), var(--emerald));
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-size: 12.5px; font-weight: 700;
  flex-shrink: 0;
}
.user-meta strong { display: block; font-size: 13px; }
.user-meta small { color: var(--text-faint); font-size: 11.5px; }

.sidebar-backdrop {
  display: none;
  position: fixed; inset: 0;
  background: rgba(15, 23, 42, 0.5);
  z-index: 35;
}

/* ==========================================================
   MAIN AREA
   ========================================================== */
.main-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.topbar {
  height: var(--topbar-h);
  flex-shrink: 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  position: sticky;
  top: 0;
  z-index: 30;
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 12px; }

.icon-btn {
  width: 36px; height: 36px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-soft);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  position: relative;
  transition: background 0.15s, color 0.15s;
}
.icon-btn:hover { background: var(--surface-2); color: var(--primary); }
.icon-btn .dot {
  position: absolute; top: 7px; right: 8px;
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--danger);
  border: 1.5px solid var(--surface);
}

#mobileMenuBtn { display: none; }

.breadcrumb {
  display: flex; align-items: center; gap: 8px;
  color: var(--text-faint); font-size: 13px;
}
.breadcrumb i { font-size: 10px; }
.breadcrumb .current { color: var(--text); font-weight: 600; }

.topbar-user .avatar { width: 34px; height: 34px; }

.content-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 20px 24px 40px;
}

.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  padding: 20px 24px 0;
  flex-wrap: wrap;
}
.page-header h1 { margin: 0 0 4px; font-size: 22px; font-weight: 700; }
.page-header p { margin: 0; color: var(--text-soft); font-size: 13.5px; }
.page-header-actions { display: flex; gap: 10px; flex-wrap: wrap; }

/* ==========================================================
   BUTTONS
   ========================================================== */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 9px 16px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 600;
  border: 1px solid transparent;
  cursor: pointer;
  transition: transform 0.08s ease, box-shadow 0.15s, background 0.15s, opacity 0.15s;
  white-space: nowrap;
  position: relative;
  overflow: hidden;
}
.btn:active { transform: scale(0.97); }
.btn-sm { padding: 6px 11px; font-size: 12px; }

.btn-primary { background: var(--primary); color: #fff; box-shadow: 0 2px 8px rgba(37, 99, 235, 0.35); }
.btn-primary:hover { background: var(--primary-dark); }

.btn-secondary { background: var(--emerald); color: #fff; box-shadow: 0 2px 8px rgba(16, 185, 129, 0.3); }
.btn-secondary:hover { background: var(--emerald-dark); }

.btn-success { background: var(--emerald-light); color: var(--emerald-dark); }
.btn-success:hover { background: var(--emerald); color: #fff; }

.btn-warning { background: var(--warning-light); color: #92400e; }
.btn-warning:hover { background: var(--warning); color: #fff; }

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

.btn-light { background: var(--surface-2); color: var(--text); border-color: var(--border); }
.btn-light:hover { background: var(--border); }

/* ==========================================================
   CARDS
   ========================================================== */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  padding: 20px 22px;
  margin: 20px 24px 0;
  transition: box-shadow 0.2s;
}
.card:hover { box-shadow: var(--shadow-md); }

.card-title-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.card-title { font-size: 15px; font-weight: 700; display: flex; align-items: center; gap: 8px; }
.card-title i { color: var(--primary); }

.badge {
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 11.5px;
  font-weight: 700;
  letter-spacing: 0.3px;
  text-transform: uppercase;
}
.badge-draft { background: var(--warning-light); color: #92400e; }
.badge-posted { background: var(--emerald-light); color: var(--emerald-dark); }

/* ==========================================================
   FORM GRID
   ========================================================== */
.form-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
}
.form-group { display: flex; flex-direction: column; gap: 6px; }
.form-group.full-width { grid-column: 1 / -1; }

.form-group label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-soft);
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.form-group input, .form-group select, .form-group textarea {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 12px;
  font-size: 13.5px;
  background: var(--surface-2);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
  width: 100%;
}
.form-group input:focus, .form-group select:focus, .form-group textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-light);
  background: var(--surface);
}
.form-group input[readonly] { color: var(--text-soft); cursor: not-allowed; }
.form-group textarea { resize: vertical; }

/* ==========================================================
   GRID CARD / TOOLBAR
   ========================================================== */
.grid-card { padding: 20px 0 0; }
.table-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  padding: 0 22px 16px;
}
.table-header h3 { margin: 0; font-size: 15px; display: flex; align-items: center; gap: 8px; }
.table-header h3 i { color: var(--primary); }
.table-header small { color: var(--text-faint); font-size: 12px; }

.grid-toolbar { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.toolbar-sep { width: 1px; height: 22px; background: var(--border); margin: 0 4px; }

/* ==========================================================
   EXCEL-LIKE JOURNAL GRID
   ========================================================== */
.grid-wrapper {
  overflow: auto;
  max-height: 480px;
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  position: relative;
}

table.journal-grid {
  border-collapse: separate;
  border-spacing: 0;
  width: 100%;
  min-width: 1400px;
  font-size: 13px;
}

.journal-grid thead th {
  position: sticky;
  top: 0;
  z-index: 5;
  background: var(--surface-2);
  border-bottom: 2px solid var(--border-strong);
  border-right: 1px solid var(--border);
  padding: 10px 12px;
  font-weight: 700;
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
  color: var(--text-soft);
  text-align: left;
  white-space: nowrap;
}

.journal-grid td {
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  padding: 0;
  height: 40px;
}

.journal-grid .freeze-col {
  position: sticky;
  left: 0;
  z-index: 3;
  background: var(--surface);
}
.journal-grid thead .freeze-col { z-index: 6; background: var(--surface-2); }

.journal-grid .freeze-col-2 {
  position: sticky;
  left: 48px;
  z-index: 3;
  background: var(--surface);
  box-shadow: 2px 0 4px rgba(0,0,0,0.03);
}
.journal-grid thead .freeze-col-2 { z-index: 6; background: var(--surface-2); }

.col-num { width: 48px; text-align: center; }
.col-code { width: 110px; }
.col-account { width: 190px; }
.col-desc { width: 190px; }
.col-cc { width: 140px; }
.col-proj { width: 130px; }
.col-dept { width: 130px; }
.col-money { width: 140px; }
.col-ref { width: 120px; }
.col-tax { width: 90px; }
.col-remarks { width: 150px; }
.col-action { width: 70px; text-align: center; }

.row-num-cell {
  text-align: center;
  color: var(--text-faint);
  font-weight: 600;
  font-size: 12px;
  user-select: none;
}

.cell-input, .cell-select {
  width: 100%;
  height: 40px;
  border: none;
  outline: none;
  background: transparent;
  padding: 0 10px;
  font-size: 13px;
  color: var(--text);
  font-family: inherit;
}
.cell-input:focus, .cell-select:focus {
  background: var(--primary-light);
  box-shadow: inset 0 0 0 2px var(--primary);
}

.cell-input.money { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
.cell-input.money.debit { color: var(--primary-dark); }
.cell-input.money.credit { color: var(--emerald-dark); }
.cell-input.money::placeholder { color: var(--text-faint); font-weight: 400; }

.grid-row.selected-row td { background: var(--primary-light); }
.grid-row.selected-row .freeze-col,
.grid-row.selected-row .freeze-col-2 { background: var(--primary-light); }

.grid-row.error-row td { background: var(--danger-light); }

.cell-active {
  outline: 2px solid var(--primary) !important;
  outline-offset: -2px;
  z-index: 2;
  position: relative;
}

.row-delete-btn {
  width: 28px; height: 28px;
  border: none;
  background: transparent;
  color: var(--text-faint);
  border-radius: 6px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  margin: 0 auto;
  transition: background 0.15s, color 0.15s;
}
.row-delete-btn:hover { background: var(--danger-light); color: var(--danger); }

/* account autocomplete dropdown */
.autocomplete-list {
  position: absolute;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg);
  z-index: 100;
  max-height: 220px;
  overflow-y: auto;
  min-width: 220px;
}
.autocomplete-item {
  padding: 9px 12px;
  font-size: 13px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  gap: 10px;
}
.autocomplete-item:hover, .autocomplete-item.highlighted { background: var(--primary-light); }
.autocomplete-item .code { color: var(--text-faint); font-size: 11.5px; }

/* context menu */
.context-menu {
  position: fixed;
  display: none;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  padding: 6px;
  z-index: 200;
  min-width: 200px;
}
.context-menu.open { display: flex; }
.context-menu button {
  display: flex; align-items: center; gap: 10px;
  border: none; background: transparent;
  padding: 9px 10px; font-size: 13px;
  border-radius: 6px; cursor: pointer;
  color: var(--text); text-align: left;
}
.context-menu button:hover { background: var(--surface-2); }
.context-menu button.danger { color: var(--danger); }
.context-menu button.danger:hover { background: var(--danger-light); }
.context-menu hr { border: none; border-top: 1px solid var(--border); margin: 4px 0; }

/* ==========================================================
   SUMMARY
   ========================================================== */
.summary-card {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 16px;
  margin: 20px 24px 0;
  position: sticky;
  bottom: 0;
}
.summary-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 16px 18px;
  box-shadow: var(--shadow-sm);
  transition: transform 0.15s, box-shadow 0.15s;
}
.summary-box:hover { transform: translateY(-2px); box-shadow: var(--shadow-md); }
.summary-label { font-size: 11.5px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.4px; color: var(--text-soft); }
.summary-box h2 { margin: 6px 0 0; font-size: 21px; font-weight: 700; font-variant-numeric: tabular-nums; }

.summary-box.status { background: var(--emerald-light); border-color: var(--emerald); }
#balanceStatus.balanced { color: var(--emerald-dark); display: flex; align-items: center; gap: 8px; }
#balanceStatus.unbalanced { color: var(--danger); display: flex; align-items: center; gap: 8px; }
.summary-box.status.error { background: var(--danger-light); border-color: var(--danger); }

/* ==========================================================
   ACTION BAR / FOOTER
   ========================================================== */
.action-bar {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin: 20px 24px 0;
  padding: 18px 22px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
}

.footer {
  display: flex;
  justify-content: space-between;
  padding: 24px;
  color: var(--text-faint);
  font-size: 12px;
}

/* ==========================================================
   TOASTS
   ========================================================== */
.toast-container {
  position: fixed;
  top: 20px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  z-index: 500;
}
.toast {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-left: 4px solid var(--primary);
  box-shadow: var(--shadow-lg);
  border-radius: var(--radius-sm);
  padding: 12px 16px;
  font-size: 13px;
  font-weight: 500;
  min-width: 260px;
  animation: toastIn 0.25s ease;
}
.toast.success { border-left-color: var(--emerald); }
.toast.success i { color: var(--emerald); }
.toast.error { border-left-color: var(--danger); }
.toast.error i { color: var(--danger); }
.toast.info i { color: var(--primary); }
.toast.hide { animation: toastOut 0.2s ease forwards; }

@keyframes toastIn { from { opacity: 0; transform: translateX(30px); } to { opacity: 1; transform: translateX(0); } }
@keyframes toastOut { to { opacity: 0; transform: translateX(30px); } }

/* ==========================================================
   MODAL
   ========================================================== */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(15, 23, 42, 0.55);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 400;
  backdrop-filter: blur(2px);
}
.modal-overlay.open { display: flex; }
.modal-box {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 28px;
  max-width: 380px;
  width: 90%;
  text-align: center;
  box-shadow: var(--shadow-lg);
  animation: modalIn 0.2s ease;
}
@keyframes modalIn { from { opacity: 0; transform: scale(0.94); } to { opacity: 1; transform: scale(1); } }
.modal-icon {
  width: 52px; height: 52px;
  border-radius: 50%;
  background: var(--warning-light);
  color: var(--warning);
  display: flex; align-items: center; justify-content: center;
  font-size: 22px;
  margin: 0 auto 14px;
}
.modal-box h3 { margin: 0 0 8px; font-size: 16px; }
.modal-box p { margin: 0 0 20px; color: var(--text-soft); font-size: 13px; }
.modal-actions { display: flex; gap: 10px; justify-content: center; }

/* ==========================================================
   RESPONSIVE
   ========================================================== */
@media (max-width: 1366px) {
  .form-grid { grid-template-columns: repeat(3, 1fr); }
  .summary-card { grid-template-columns: repeat(3, 1fr); }
}

@media (max-width: 1024px) {
  .sidebar {
    position: fixed;
    left: 0; top: 0; bottom: 0;
    transform: translateX(-100%);
    box-shadow: var(--shadow-lg);
  }
  .app-shell.mobile-open .sidebar { transform: translateX(0); }
  .app-shell.mobile-open .sidebar-backdrop { display: block; }
  .sidebar-collapse-btn { display: none; }
  #mobileMenuBtn { display: flex; }
  .form-grid { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 768px) {
  .page-header { padding: 16px 16px 0; }
  .content-scroll { padding: 16px 12px 32px; }
  .card { margin: 16px 12px 0; padding: 16px; }
  .summary-card { grid-template-columns: repeat(2, 1fr); margin: 16px 12px 0; }
  .action-bar { margin: 16px 12px 0; }
  .form-grid { grid-template-columns: 1fr; }
  .page-header-actions .btn span { display: none; }
}

@media (max-width: 480px) {
  .summary-card { grid-template-columns: 1fr; }
  .breadcrumb span:not(.current) { display: none; }
}

/* Skeleton / loading */
.skeleton {
  background: linear-gradient(90deg, var(--surface-2) 25%, var(--border) 37%, var(--surface-2) 63%);
  background-size: 400% 100%;
  animation: skeletonShine 1.4s ease infinite;
  border-radius: 6px;
}
@keyframes skeletonShine { 0% { background-position: 100% 50%; } 100% { background-position: 0 50%; } }