:root {
  --ink: #1f2933;
  --muted: #667085;
  --line: #d8e2dd;
  --surface: #ffffff;
  --canvas: #f3f7f5;
  --teal: #0f766e;
  --teal-dark: #115e59;
  --red: #c1121f;
  --yellow: #fff200;
  --focus: #2563eb;
}

* { box-sizing: border-box; }

html { min-height: 100%; font-family: Arial, "Microsoft JhengHei", sans-serif; }

body {
  background: var(--canvas);
  color: var(--ink);
  margin: 0;
  min-height: 100vh;
}

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

a { color: inherit; }

.topbar {
  background: var(--surface);
  border-top: 4px solid var(--teal);
  border-bottom: 1px solid var(--line);
}

.topbar-inner,
.page-shell {
  margin: 0 auto;
  max-width: 1440px;
  padding-left: 20px;
  padding-right: 20px;
}

.topbar-inner {
  align-items: center;
  display: flex;
  gap: 24px;
  min-height: 64px;
}

.brand {
  color: var(--teal);
  font-size: 20px;
  font-weight: 700;
  text-decoration: none;
  white-space: nowrap;
}

.navigation { display: flex; flex: 1; gap: 4px; }
.navigation a { border-bottom: 3px solid transparent; color: #475467; padding: 20px 12px 17px; text-decoration: none; white-space: nowrap; }
.navigation a:hover, .navigation a:focus { border-bottom-color: var(--teal); color: var(--teal-dark); }
.user-area { align-items: center; color: var(--muted); display: flex; gap: 12px; white-space: nowrap; }
.link-button { background: transparent; border: 0; color: #344054; cursor: pointer; padding: 5px; text-decoration: underline; }

.page-shell { padding-bottom: 32px; padding-top: 24px; }
.page-title { align-items: center; display: flex; gap: 16px; justify-content: space-between; margin-bottom: 18px; }
.page-title h1 { font-size: 26px; font-weight: 700; margin: 0; }
.page-title h2 { font-size: 20px; margin: 0; }

.button-row { align-items: center; display: flex; flex-wrap: wrap; gap: 8px; }
.button {
  align-items: center;
  background: #ffffff;
  border: 1px solid #aeb9b4;
  border-radius: 5px;
  color: #24302b;
  cursor: pointer;
  display: inline-flex;
  font-weight: 700;
  justify-content: center;
  line-height: 1.25;
  min-height: 36px;
  padding: 8px 13px;
  text-decoration: none;
}
.button:hover, .button:focus { border-color: #7c8b84; filter: brightness(0.98); }
.button.primary { background: var(--teal); border-color: var(--teal); color: #ffffff; }
.button.dark { background: #344054; border-color: #344054; color: #ffffff; }
.button.secondary { background: #ffffff; }
.button.danger { background: #fff6f6; border-color: #efb0b6; color: #a3101b; }
.button.small { font-size: 13px; min-height: 30px; padding: 5px 8px; }
.full-button { width: 100%; }

.button:focus-visible,
.link-button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
.photo-preview-button:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

.filter-bar,
.entry-form,
.settings-section,
.report-section,
.login-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.filter-bar { align-items: end; display: grid; gap: 12px; grid-template-columns: repeat(6, minmax(120px, 1fr)); margin-bottom: 16px; padding: 16px; }
.filter-actions { align-items: center; display: flex; flex-wrap: wrap; gap: 8px; min-height: 61px; }
.form-grid { display: grid; gap: 16px; grid-template-columns: repeat(3, minmax(0, 1fr)); }
.field-row { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field-row.full { grid-column: 1 / -1; }
.field-row label, .field-label { color: #344054; font-size: 14px; font-weight: 700; }
.field-row input:not([type="checkbox"]), .field-row select, .field-row textarea {
  background: #ffffff;
  border: 1px solid #aeb9b4;
  border-radius: 4px;
  color: #17212b;
  min-height: 38px;
  padding: 8px 10px;
  width: 100%;
}
.field-row textarea { min-height: 128px; resize: vertical; }
.field-row input[type="file"] { background: #fff; border: 1px solid #aeb9b4; border-radius: 4px; padding: 7px; }
.transaction-type { border: 1px solid #aeb9b4; border-radius: 4px; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); overflow: hidden; width: 100%; }
.transaction-type > span { min-width: 0; position: relative; }
.transaction-type > span + span { border-left: 1px solid #aeb9b4; }
.transaction-type input { height: 1px; margin: -1px; opacity: 0; padding: 0; position: absolute; width: 1px; }
.transaction-type label { align-items: center; color: #344054; cursor: pointer; display: flex; justify-content: center; min-height: 38px; padding: 8px 10px; text-align: center; width: 100%; }
.transaction-type .income-option input:checked + label { background: var(--teal); color: #ffffff; }
.transaction-type .expense-option input:checked + label { background: var(--red); color: #ffffff; }
.transaction-type input:focus-visible + label { outline: 3px solid #7cc7c0; outline-offset: -3px; }
.field-help, .muted { color: var(--muted); font-size: 13px; }
.check-row { justify-content: end; padding-top: 26px; }
.check-row label { align-items: center; display: inline-flex; gap: 7px; }
.entry-form { padding: 20px; }
.settings-form { max-width: 900px; }
.category-editor { border-bottom: 1px solid var(--line); padding-bottom: 20px; }
.category-editor h2 { font-size: 18px; margin: 0 0 16px; }
.category-types { align-items: center; display: flex; flex-wrap: wrap; gap: 20px; min-height: 38px; }
.category-types > span { align-items: center; display: inline-flex; gap: 6px; }
.category-types input[type="checkbox"] { width: 18px; height: 18px; margin: 0; }
.category-table { min-width: 480px; }
.top-gap { margin-top: 20px; }
.validation-message { color: var(--red); display: block; font-weight: 700; margin-bottom: 12px; min-height: 0; }

.summary-strip { display: grid; gap: 12px; grid-template-columns: repeat(3, minmax(0, 1fr)); margin-bottom: 16px; }
.summary-strip > div { background: var(--surface); border: 1px solid var(--line); border-left: 4px solid var(--teal); border-radius: 6px; padding: 14px 16px; }
.summary-strip > div:nth-child(2) { border-left-color: var(--red); }
.summary-strip > div:nth-child(3) { border-left-color: #b7791f; }
.summary-strip span { color: var(--muted); display: block; font-size: 13px; }
.summary-strip strong { display: block; font-size: 21px; margin-top: 4px; }

.table-wrap { background: var(--surface); border: 1px solid var(--line); overflow-x: auto; }
.ledger-table, .settings-table { border-collapse: collapse; min-width: 720px; width: 100%; }
.ledger-table th, .ledger-table td, .settings-table th, .settings-table td { border-bottom: 1px solid #dde5e1; padding: 11px 10px; text-align: left; vertical-align: middle; }
.ledger-table th, .settings-table th { background: #eef2f7; color: #344054; font-weight: 700; white-space: nowrap; }
.ledger-table tbody tr:hover, .settings-table tbody tr:hover { background: #f8fbf9; }
.numeric { text-align: right !important; white-space: nowrap; }
.ledger-date-column, .ledger-date-cell { min-width: 132px; width: 132px; }
.ledger-account-column, .ledger-account-cell { min-width: 176px; width: 176px; }
.ledger-balance-column { min-width: 120px; }
.ledger-photo-column, .ledger-photo-cell { text-align: center; width: 70px; }
.summary-cell { max-width: 560px; white-space: pre-wrap; }
.actions { white-space: nowrap; }
.actions .button + .button { margin-left: 5px; }

.expense-amount, .expense-summary { color: var(--red) !important; font-weight: 700; }
.balance-amount { color: var(--teal); font-weight: 700; }
.account-list-cell { align-items: center; display: flex; gap: 7px; }
.account-badge { border-radius: 999px; display: inline-block; font-size: 14px; font-weight: 700; line-height: 1.2; min-width: 64px; padding: 4px 9px; text-align: center; }
.account-cash { background: #e8f1ff; color: #0b63ce; }
.account-esun { background: #e7f6ec; color: #137333; }
.account-land { background: #e8f3eb; border: 2px solid #14532d; color: #14532d; }
.account-other { background: #f1f3f5; color: #374151; }
.status-badge, .role-badge { border-radius: 999px; display: inline-block; font-size: 12px; font-weight: 700; padding: 4px 9px; white-space: nowrap; }
.status-active { background: #e7f6ec; color: #137333; }
.status-inactive, .status-failed { background: #fde8ea; color: #b4232d; }
.permission-write { background: #fff2d9; color: #9a5d00; }
.permission-read { background: #e9eefc; color: #3157a5; }

.photo-preview-button { background: transparent; border: 0; cursor: zoom-in; padding: 0; position: relative; }
.account-list-photo img { border: 1px solid rgba(15, 118, 110, 0.32); display: block; height: 30px; object-fit: cover; width: 30px; }
.ledger-photo-thumbnail img { border: 1px solid #bfd0c9; display: block; height: 42px; object-fit: cover; width: 42px; }
.photo-count { align-items: center; background: var(--teal); border: 2px solid #fff; border-radius: 999px; color: #fff; display: flex; font-size: 11px; font-weight: 700; height: 21px; justify-content: center; min-width: 21px; padding: 0 3px; position: absolute; right: -8px; top: -8px; }
.photo-gallery { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); margin-top: 20px; }
.photo-tile { background: #f8faf9; border: 1px solid var(--line); margin: 0; padding: 8px; }
.photo-tile img { background: #fff; display: block; height: 118px; object-fit: cover; width: 100%; }
.photo-tile figcaption { font-size: 12px; margin: 7px 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.photo-modal { align-items: center; background: rgba(16, 24, 40, 0.88); display: none; inset: 0; justify-content: center; padding: 28px; position: fixed; z-index: 1000; }
.photo-modal.is-open { display: flex; }
.photo-modal img { max-height: calc(100vh - 56px); max-width: 100%; object-fit: contain; }
.photo-modal-close { background: #fff; border: 0; border-radius: 4px; cursor: pointer; font-weight: 700; padding: 8px 12px; position: fixed; right: 20px; top: 20px; }

.account-total-banner { align-items: center; background: var(--yellow); color: #3d3300; display: flex; justify-content: space-between; margin-bottom: 16px; padding: 16px 18px; }
.account-total-banner span { font-weight: 700; }
.account-total-banner strong { color: #3d3300; font-size: 24px; }
.account-total-banner .expense-amount { color: #a3101b !important; }
.account-balance-grid { display: grid; gap: 16px; grid-template-columns: repeat(3, minmax(0, 1fr)); }
.account-total-card { background: #fff; border: 1px solid var(--line); border-radius: 8px; min-height: 210px; padding: 16px; }
.account-card-header { align-items: center; display: flex; gap: 8px; justify-content: space-between; }
.account-card-header h2 { font-size: 17px; margin: 0; }
.account-total-card > strong { display: block; font-size: 24px; margin: 22px 0; }
.account-total-card .field-help { overflow-wrap: anywhere; }
.summary-strip strong { overflow-wrap: anywhere; }
.account-total-card dl { display: grid; gap: 8px; margin: 0; }
.account-total-card dl div { display: flex; justify-content: space-between; }
.account-total-card dt { color: var(--muted); font-size: 13px; }
.account-total-card dd { margin: 0; }

.settings-section { margin-bottom: 16px; padding: 18px; }
.settings-section h2, .report-section h2 { font-size: 18px; margin: 0 0 14px; }
.section-heading { align-items: center; display: flex; justify-content: space-between; }
.section-heading span { color: var(--muted); font-size: 13px; }
.report-grid { display: grid; gap: 16px; grid-template-columns: repeat(3, minmax(0, 1fr)); }
.report-section { min-width: 0; overflow-x: auto; padding: 18px; }
.report-section .settings-table { min-width: 0; }
.account-current-balance-table { margin-top: 14px; table-layout: fixed; }
.account-current-balance-table th:first-child,
.account-current-balance-table td:first-child { width: 42%; }
.account-current-balance-table th,
.account-current-balance-table td { overflow-wrap: anywhere; white-space: normal; }
.account-current-balance-table .account-badge { max-width: 100%; min-width: 0; }
.account-current-balance-table .field-help { display: inline-block; max-width: 100%; }
.account-grand-total th { background: var(--yellow) !important; color: #3d3300; }

.login-body { align-items: flex-start; display: flex; justify-content: center; padding: 8vh 20px 20px; }
.login-panel { max-width: 420px; padding: 28px; width: 100%; }
.login-panel h1 { color: var(--teal); font-size: 27px; margin: 0 0 8px; }
.login-panel .field-row { margin-top: 16px; }
.login-panel .button { margin-top: 20px; width: 100%; }

@media (max-width: 1040px) {
  .filter-bar { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .report-grid { grid-template-columns: 1fr; }
}

@media (max-width: 760px) {
  .topbar-inner { align-items: flex-start; flex-wrap: wrap; gap: 0; padding-bottom: 8px; padding-top: 12px; }
  .navigation { flex-basis: 100%; order: 3; overflow-x: auto; }
  .navigation a { padding: 12px 9px; }
  .user-area { margin-left: auto; }
  .page-shell, .topbar-inner { padding-left: 12px; padding-right: 12px; }
  .page-title { align-items: stretch; flex-direction: column; }
  .filter-bar, .form-grid, .summary-strip, .account-balance-grid { grid-template-columns: 1fr; }
  .filter-actions { min-height: 0; }
  .field-row.full { grid-column: auto; }
  .check-row { padding-top: 0; }
  .account-total-banner { align-items: flex-start; flex-direction: column; gap: 7px; }
}
