:root {
  --green: #2f7d5b;
  --green-d: #1f5c42;
  --green-l: #e3f1ea;
  --head-bg: #dfece5;
  --head-tx: #183528;
  --sidebar-bg: #1c3a2e;
  --sidebar-hover: #264a3a;
  --sidebar-tx: #cfe3d9;
  --bg: #f4f6f5;
  --card: #ffffff;
  --line: #dce4e0;
  --tx: #1d2b25;
  --mut: #5f6f67;
  --warn: #9a5500;
  --warn-bg: #fef3c7;
  --bad: #c8402f;
  --bad-bg: #fdeceb;
  --blue: #2a5f98;
  --blue-bg: #e0ebf7;
  --out-tx: #1f6a46;
  --out-bg: #dff1e7;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  font-family: "Microsoft JhengHei", "PingFang TC", "Noto Sans TC", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--tx);
  font-size: 14px;
  line-height: 1.45;
  overflow-x: clip;
}

.hidden {
  display: none !important;
}

.mut {
  color: var(--mut);
  font-size: 12px;
}

.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* 版面結構 */
.app-shell {
  display: grid;
  grid-template-columns: 224px minmax(0, 1fr);
  min-height: 100vh;
}

/* 側邊欄 */
.sidebar {
  background: var(--sidebar-bg);
  color: var(--sidebar-tx);
  display: flex;
  flex-direction: column;
  padding: 18px 0 14px;
  border-right: 1px solid #152e24;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 18px 16px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  margin-bottom: 10px;
}

.brand-mark {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  background: var(--green);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.brand-name {
  font-size: 16px;
  font-weight: 700;
  color: #fff;
  line-height: 1.2;
}

.brand-sub {
  font-size: 11px;
  color: #96b8a8;
  margin-top: 2px;
}

.nav-list {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.nav-group-label {
  margin: 14px 18px 5px;
  font-size: 11px;
  letter-spacing: 0.04em;
  color: #82a796;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 9px;
  width: 100%;
  border: 0;
  background: transparent;
  color: var(--sidebar-tx);
  padding: 9px 18px;
  font: inherit;
  font-size: 13.5px;
  text-align: left;
  cursor: pointer;
  transition: background-color 0.12s ease;
}

.nav-item:hover {
  background: var(--sidebar-hover);
  color: #fff;
}

.nav-item.active {
  background: var(--green);
  color: #fff;
  font-weight: 600;
}

.nav-item svg {
  flex-shrink: 0;
  opacity: 0.92;
}

.sidebar-footer {
  margin-top: 16px;
  padding: 12px 18px 0;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  font-size: 11.5px;
  color: #8eb2a1;
  line-height: 1.6;
}

/* 手機版頂列 */
.mobile-header {
  display: none;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 14px;
  background: var(--sidebar-bg);
  color: #fff;
  position: sticky;
  top: 0;
  z-index: 25;
}

.mobile-title {
  font-weight: 700;
  font-size: 15px;
}

.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 28;
}

/* 主內容區 */
.main-content {
  padding: 18px 24px 28px;
  min-width: 0;
}

.userbar {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 12px;
  margin-bottom: 10px;
  min-height: 24px;
  font-size: 12.5px;
  color: var(--mut);
}

.userbar-user {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.page-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}

.page-head h1 {
  margin: 0;
  font-size: 20px;
  font-weight: 700;
  display: flex;
  align-items: baseline;
  gap: 10px;
  flex-wrap: wrap;
}

.page-head h1 small {
  font-size: 12.5px;
  font-weight: 400;
  color: var(--mut);
}

.head-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

/* 按鈕與輸入元件 */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--tx);
  padding: 7px 13px;
  border-radius: 6px;
  font: inherit;
  font-size: 13.5px;
  cursor: pointer;
  white-space: nowrap;
  transition: border-color 0.12s ease, background-color 0.12s ease;
}

.btn:hover:not(:disabled) {
  border-color: var(--green);
}

.btn:disabled {
  opacity: 0.42;
  cursor: default;
}

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

.btn-primary:hover:not(:disabled) {
  background: var(--green-d);
  border-color: var(--green-d);
}

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

.btn-danger:hover:not(:disabled) {
  background: #ab3324;
  border-color: #ab3324;
}

.btn-sm {
  padding: 5px 10px;
  font-size: 12.5px;
}

.btn-xs {
  padding: 3px 8px;
  font-size: 12px;
}

.btn-text-danger {
  color: var(--bad);
}

.icon-btn {
  border: 0;
  background: transparent;
  color: inherit;
  padding: 4px;
  border-radius: 6px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.icon-btn:hover {
  background: rgba(0, 0, 0, 0.08);
}

.link-btn {
  border: 0;
  background: none;
  padding: 0;
  color: var(--green-d);
  font: inherit;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 2px;
}

input[type="text"],
input[type="number"],
input[type="date"],
input[type="month"],
input[type="password"],
select {
  font: inherit;
  font-size: 13.5px;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--tx);
  min-width: 0;
}

input:focus,
select:focus {
  outline: 2px solid rgba(47, 125, 91, 0.2);
  border-color: var(--green);
}

/* KPI 卡片 */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 14px;
}

.kpi-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px 16px;
}

.kpi-label {
  font-size: 12px;
  color: var(--mut);
}

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

.kpi-value.out {
  color: var(--out-tx);
}

.kpi-value.in {
  color: var(--blue);
}

.kpi-value.warn {
  color: var(--warn);
}

.kpi-sub {
  margin-top: 3px;
  font-size: 12px;
  color: var(--mut);
}

/* 卡片與篩選區 */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.filter-panel {
  padding: 12px;
  border-bottom: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.stats-toolbar {
  margin-bottom: 14px;
  border-bottom: 1px solid var(--line);
}

.filter-row {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.period-row {
  padding-top: 2px;
}

.filter-row-right {
  margin-left: auto;
  display: flex;
  gap: 6px;
}

.search-field {
  position: relative;
  flex: 1 1 250px;
  min-width: 220px;
}

.search-field svg {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--mut);
  pointer-events: none;
}

.search-field input {
  width: 100%;
  padding-left: 32px;
}

.stats-search {
  max-width: 260px;
}

.seg-group {
  display: inline-flex;
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow: hidden;
  background: #f8faf9;
}

.seg-btn {
  border: 0;
  border-right: 1px solid var(--line);
  background: transparent;
  padding: 6px 11px;
  font: inherit;
  font-size: 13px;
  color: var(--mut);
  cursor: pointer;
  white-space: nowrap;
}

.seg-btn:last-child {
  border-right: 0;
}

.seg-btn:hover:not(.active) {
  background: #edf3ef;
  color: var(--tx);
}

.seg-btn.active {
  background: var(--green);
  color: #fff;
  font-weight: 600;
}

.period-controls {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

/* 可搜尋下拉選單 Combobox */
.combobox {
  position: relative;
  min-width: 155px;
}

.modal .combobox {
  width: 100%;
}

.cb-input-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

.cb-input {
  width: 100%;
  padding-right: 30px !important;
}

.cb-toggle {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  border: 0;
  background: transparent;
  color: var(--mut);
  padding: 4px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.cb-menu {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  min-width: 220px;
  max-height: 240px;
  overflow-y: auto;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 6px;
  box-shadow: 0 8px 20px rgba(17, 36, 26, 0.14);
  z-index: 35;
  display: none;
}

.cb-menu.open {
  display: block;
}

.cb-option {
  padding: 8px 10px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  border-bottom: 1px solid #f2f5f3;
  font-size: 13px;
}

.cb-option:last-child {
  border-bottom: 0;
}

.cb-option:hover,
.cb-option.active {
  background: var(--green-l);
  color: var(--head-tx);
  font-weight: 600;
}

.cb-empty {
  padding: 10px;
  color: var(--mut);
  font-size: 12.5px;
  text-align: center;
}

/* 表格 */
.table-wrap {
  width: 100%;
  overflow-x: auto;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
}

.data-table th {
  background: var(--head-bg);
  color: var(--head-tx);
  text-align: left;
  padding: 9px 9px;
  font-size: 12.5px;
  font-weight: 700;
  white-space: nowrap;
  border-bottom: 1px solid #c9dcd1;
}

.data-table th.num {
  text-align: right;
}

.data-table td {
  padding: 8px 9px;
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
  font-size: 13.5px;
}

#view-ledger .data-table td:nth-child(2) {
  white-space: nowrap;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

#view-ledger .data-table td:nth-child(4) {
  white-space: nowrap;
}

.data-table tbody tr:hover {
  background: #f6faf8;
}

.data-table tfoot td {
  background: #edf5f0;
  font-weight: 700;
  border-top: 2px solid #bfd7c9;
  border-bottom: 0;
}

.actions-col {
  text-align: right;
  white-space: nowrap;
}

.row-actions {
  display: inline-flex;
  gap: 4px;
  justify-content: flex-end;
}

.empty-cell {
  text-align: center;
  padding: 42px 16px !important;
  color: var(--mut);
}

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}

.badge-out {
  background: var(--out-bg);
  color: var(--out-tx);
}

.badge-in {
  background: var(--blue-bg);
  color: var(--blue);
}

.badge-warn {
  background: var(--warn-bg);
  color: var(--warn);
}

.badge-ok {
  background: var(--green-l);
  color: var(--green-d);
}

.sub-line {
  font-size: 11.5px;
  color: var(--mut);
  margin-top: 2px;
}

.sub-warn {
  color: var(--warn);
}

.sub-ok {
  color: var(--green-d);
}

.date-cell {
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* 本頁小計與跨頁總計列 */
.summary-strip {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 9px 14px;
  flex-wrap: wrap;
  font-size: 13px;
  border-top: 1px solid var(--line);
}

.summary-strip.page-summary {
  background: #fafcfb;
}

.summary-strip.total-summary {
  background: var(--green-l);
  color: var(--head-tx);
  border-top: 1px solid #c3ddd0;
  font-weight: 600;
}

.summary-title {
  font-weight: 700;
}

.summary-metrics {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  font-variant-numeric: tabular-nums;
}

.pager {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-top: 1px solid var(--line);
  flex-wrap: wrap;
}

.pager-nums {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}

.pager button {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 5px;
  padding: 4px 10px;
  font: inherit;
  font-size: 12.5px;
  cursor: pointer;
}

.pager button.active {
  background: var(--green);
  color: #fff;
  border-color: var(--green);
  font-weight: 600;
}

.pager button:disabled {
  opacity: 0.4;
  cursor: default;
}

.pager select {
  padding: 3px 6px;
  font-size: 12.5px;
}

/* 統計圖表卡 */
.chart-card {
  padding: 14px 16px;
  margin-bottom: 14px;
}

.card-header-bar {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 12px;
  gap: 8px;
  flex-wrap: wrap;
}

.card-header-bar h2 {
  margin: 0;
  font-size: 15px;
}

.bar-chart {
  display: flex;
  flex-direction: column;
  gap: 9px;
}

.bar-row {
  display: grid;
  grid-template-columns: 160px minmax(0, 1fr) 170px;
  align-items: center;
  gap: 10px;
  font-size: 13px;
}

.bar-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 600;
}

.bar-track {
  height: 12px;
  background: #eef2ef;
  border-radius: 6px;
  overflow: hidden;
  display: flex;
}

.bar-fill-out {
  background: var(--green);
  height: 100%;
}

.bar-fill-in {
  background: var(--blue);
  height: 100%;
}

.bar-val {
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-size: 12.5px;
}

/* 彈窗 */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 30, 22, 0.45);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 40;
}

.modal.open {
  display: flex;
}

.modal-box {
  background: #fff;
  border-radius: 10px;
  width: 640px;
  max-width: 100%;
  max-height: 92vh;
  overflow-y: auto;
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.2);
}

.modal-sm {
  width: 430px;
}

.modal-lg {
  width: 660px;
}

.modal-xl {
  width: 920px;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 13px 18px;
  border-bottom: 1px solid var(--line);
  font-weight: 700;
  font-size: 15.5px;
}

.modal-body {
  padding: 16px 18px;
}

.modal-footer {
  padding: 12px 0 0;
  margin-top: 14px;
  border-top: 1px solid var(--line);
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.form-grid.single-col {
  grid-template-columns: 1fr;
}

.form-grid .full {
  grid-column: 1 / -1;
}

.form-grid label {
  display: block;
  font-size: 12px;
  color: var(--mut);
  margin-bottom: 4px;
}

.form-grid input,
.form-grid select {
  width: 100%;
}

.label-with-action {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
}

.label-with-action label {
  margin-bottom: 0;
}

.manual-badge {
  font-size: 11.5px;
  color: var(--warn);
  font-weight: 600;
}

.inline-quick-add {
  display: flex;
  gap: 6px;
  margin-top: 6px;
  padding: 8px;
  background: #f6faf8;
  border: 1px solid #c9dfd3;
  border-radius: 6px;
  flex-wrap: wrap;
}

.inline-quick-add input {
  flex: 1 1 160px;
}

.inline-quick-add select {
  width: auto;
  flex: 0 0 auto;
}

.inline-quick-add .w-short {
  flex: 0 1 110px;
}

.tx-preview-bar {
  margin-top: 14px;
  padding: 10px 12px;
  border-radius: 6px;
  background: #f5f8f6;
  border: 1px solid var(--line);
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.prev-balance {
  margin-left: auto;
  color: var(--green-d);
}

.form-error {
  color: var(--bad);
  font-size: 12.5px;
  min-height: 18px;
  margin-top: 8px;
}

.confirm-msg {
  margin: 0;
  font-size: 14px;
  line-height: 1.6;
}

/* 對帳單明細表預覽（仿點陣單機報表風格） */
.modal-toolbar {
  padding: 12px 16px;
  background: #f6f8f7;
  border-bottom: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 8px;
  position: sticky;
  top: 0;
  z-index: 5;
}

.toolbar-controls,
.toolbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.print-sheet {
  padding: 26px 30px;
  background: #fff;
  color: #111;
  font-size: 13px;
}

.stmt-top {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: end;
  margin-bottom: 12px;
}

.stmt-company-brand {
  font-size: 15px;
  font-weight: 700;
}

.stmt-title {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-align: center;
  border-bottom: 2px solid #111;
  padding-bottom: 3px;
}

.stmt-page-no {
  text-align: right;
  font-size: 12.5px;
}

.stmt-meta {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px 20px;
  margin-bottom: 12px;
  font-size: 12.5px;
}

.stmt-table {
  width: 100%;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
}

.stmt-table th {
  border-top: 1.5px solid #111;
  border-bottom: 1.5px solid #111;
  padding: 6px 6px;
  text-align: left;
  font-size: 12.5px;
  background: #fff;
  color: #111;
}

.stmt-table th.num,
.stmt-table td.num {
  text-align: right;
}

.stmt-table td {
  padding: 6px 6px;
  border-bottom: 1px dashed #ccc;
  font-size: 12.5px;
}

.stmt-summary-grid {
  margin-top: 14px;
  border-top: 1.5px solid #111;
  border-bottom: 1.5px solid #111;
  padding: 10px 4px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px 24px;
  font-variant-numeric: tabular-nums;
  font-size: 12.5px;
}

.stmt-sum-item {
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

.stmt-sum-item.strong {
  font-weight: 700;
}

/* 登入閘門 */
.login-overlay {
  position: fixed;
  inset: 0;
  background: linear-gradient(135deg, #1c3a2e, #2f7d5b);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 60;
}

.login-overlay.off {
  display: none;
}

.login-box {
  background: #fff;
  border-radius: 12px;
  padding: 28px 28px 22px;
  width: 360px;
  max-width: 100%;
  text-align: center;
  box-shadow: 0 20px 44px rgba(0, 0, 0, 0.25);
}

.login-logo {
  width: 52px;
  height: 52px;
  border-radius: 12px;
  background: var(--green-l);
  color: var(--green-d);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 8px;
}

.login-box h2 {
  margin: 4px 0 4px;
  font-size: 19px;
}

.login-sub {
  margin-bottom: 14px;
}

.login-fields {
  text-align: left;
}

.login-fields label {
  display: block;
  font-size: 12px;
  color: var(--mut);
  margin: 10px 0 4px;
}

.login-fields input {
  width: 100%;
  padding: 9px 10px;
}

.login-submit {
  width: 100%;
  padding: 10px;
  margin-top: 8px;
}

.login-links {
  margin-top: 12px;
}

.login-hint {
  margin-top: 8px;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--sidebar-bg);
  color: #fff;
  padding: 10px 18px;
  border-radius: 6px;
  font-size: 13px;
  opacity: 0;
  transition: opacity 0.2s ease;
  pointer-events: none;
  z-index: 70;
}

.toast.open {
  opacity: 1;
}

/* RWD */
@media (max-width: 1120px) {
  .kpi-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 860px) {
  .mobile-header {
    display: flex;
  }

  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 240px;
    z-index: 32;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-backdrop.open {
    display: block;
  }

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

  .filter-row-right {
    margin-left: 0;
  }

  .form-grid {
    grid-template-columns: 1fr;
  }

  .bar-row {
    grid-template-columns: 110px minmax(0, 1fr) 115px;
    font-size: 12px;
  }

  .stmt-summary-grid {
    grid-template-columns: 1fr;
  }
}

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

  .stmt-meta {
    grid-template-columns: 1fr;
  }
}

/* 列印樣式：只輸出對帳單明細表 */
@media print {
  .no-print {
    display: none !important;
  }

  body {
    background: #fff !important;
  }

  .modal {
    position: static !important;
    display: block !important;
    background: transparent !important;
    padding: 0 !important;
  }

  .modal-box {
    width: 100% !important;
    max-width: none !important;
    max-height: none !important;
    box-shadow: none !important;
    border-radius: 0 !important;
    overflow: visible !important;
  }

  .print-sheet {
    padding: 8mm 10mm !important;
  }
}
