:root {
  color-scheme: light;
  --bg: #f4f6f7;
  --surface: #ffffff;
  --surface-soft: #f0f4f3;
  --ink: #17211f;
  --muted: #64716e;
  --line: #dce4e2;
  --green: #10735a;
  --green-dark: #075c47;
  --yellow: #f4c34f;
  --red: #b42318;
  --blue: #2563a6;
  --amber: #9a6700;
  --shadow: 0 12px 32px rgba(23, 33, 31, 0.07);
  font-family: "Segoe UI", "Noto Sans TC", Arial, sans-serif;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body { margin: 0; min-width: 320px; background: var(--bg); color: var(--ink); }
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--muted); }

.app-shell { display: grid; grid-template-columns: 248px minmax(0, 1fr); min-height: 100vh; }
.sidebar { background: #152320; color: #f7fbfa; display: flex; flex-direction: column; padding: 26px 16px; position: sticky; top: 0; height: 100vh; }
.brand { display: flex; align-items: center; gap: 12px; padding: 0 8px; margin-bottom: 32px; }
.brand-mark { display: grid; width: 42px; height: 42px; flex: 0 0 42px; place-items: center; border-radius: 8px; background: var(--yellow); color: #17211f; font-size: 19px; font-weight: 800; }
.brand strong, .brand span { display: block; }
.brand span { color: #bdd0cc; font-size: 13px; margin-top: 3px; }
.nav-list { display: grid; gap: 6px; }
.nav-item { width: 100%; border: 0; border-radius: 7px; background: transparent; color: #dce8e5; padding: 12px 14px; text-align: left; }
.nav-item:hover, .nav-item.active { background: rgba(255,255,255,.1); color: #fff; }
.nav-item.active { box-shadow: inset 3px 0 var(--yellow); }
.sidebar-footer { display: grid; gap: 10px; margin-top: auto; padding: 16px 8px 0; border-top: 1px solid rgba(255,255,255,.1); }
.backend-badge { color: #d5e1df; font-size: 12px; }
.backend-badge::before { content: ""; display: inline-block; width: 7px; height: 7px; margin-right: 7px; border-radius: 50%; background: #9aa6a4; }
.backend-badge.demo::before { background: var(--yellow); }
.backend-badge.firebase::before { background: #45c69b; }
.text-button { border: 0; background: transparent; color: #d5e1df; padding: 0; text-align: left; }
.current-admin { color: #f7fbfa; font-size: 12px; line-height: 1.45; }

main { min-width: 0; padding: 28px; }
.topbar { display: flex; align-items: flex-start; justify-content: space-between; gap: 24px; margin-bottom: 22px; }
h1, h2, p { margin: 0; }
h1 { font-size: 29px; line-height: 1.25; letter-spacing: 0; }
h2 { font-size: 19px; line-height: 1.3; letter-spacing: 0; }
.eyebrow { color: var(--green); font-size: 11px; font-weight: 800; letter-spacing: 0; margin-bottom: 5px; text-transform: uppercase; }
.page-description { color: var(--muted); font-size: 14px; margin-top: 7px; }
.toolbar { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 9px; }
button { border: 1px solid var(--line); border-radius: 7px; background: var(--surface); color: var(--ink); min-height: 40px; padding: 9px 13px; }
button:hover { border-color: #aebcb8; }
button.primary { background: var(--green); border-color: var(--green); color: #fff; }
button.primary:hover { background: var(--green-dark); }
button.full { width: 100%; }
.link-button, .table-action { min-height: auto; border: 0; background: transparent; color: var(--green); font-weight: 700; padding: 6px; }
.icon-button { width: 36px; height: 36px; min-height: 36px; padding: 0; font-size: 24px; line-height: 1; }

.notice { border: 1px solid #9ed8c4; border-radius: 7px; background: #ecf9f4; color: var(--green-dark); margin-bottom: 18px; padding: 12px 14px; }
.notice.error { border-color: #efb1ac; background: #fff1f0; color: var(--red); }
.notice.info { border-color: #b6cce5; background: #f0f6fd; color: var(--blue); }
.view { display: none; }
.view.active { display: block; }

.metric-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 14px; margin-bottom: 16px; }
.metric-card, .panel, .mini-metric { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; box-shadow: var(--shadow); }
.metric-card { display: grid; gap: 7px; min-height: 128px; padding: 19px; }
.metric-card span, .metric-card small { color: var(--muted); }
.metric-card strong { font-size: 26px; font-variant-numeric: tabular-nums; }
.dashboard-grid { display: grid; grid-template-columns: minmax(0, 1.6fr) minmax(300px, .8fr); gap: 16px; }
.panel { min-width: 0; padding: 19px; }
.span-2 { min-width: 0; }
.panel-heading { display: flex; align-items: center; justify-content: space-between; gap: 14px; margin-bottom: 17px; }

.site-overview { display: grid; gap: 10px; }
.site-row { display: grid; grid-template-columns: 180px minmax(100px, 1fr) 150px; align-items: center; gap: 16px; border: 1px solid var(--line); border-radius: 7px; padding: 13px; }
.site-row strong, .site-row span { display: block; }
.site-row span { color: var(--muted); font-size: 13px; margin-top: 3px; }
.progress { height: 9px; overflow: hidden; border-radius: 999px; background: #e4ebe9; }
.progress span { display: block; height: 100%; background: linear-gradient(90deg, var(--green), var(--yellow)); }
.site-balance { text-align: right; }
.cash-flow { display: grid; gap: 17px; }
.flow-number { display: grid; gap: 6px; }
.flow-number > span, .net-result span { color: var(--muted); font-size: 13px; }
.flow-number strong, .net-result strong { font-size: 22px; font-variant-numeric: tabular-nums; }
.flow-bar { height: 7px; border-radius: 999px; background: var(--surface-soft); overflow: hidden; }
.flow-bar span { display: block; height: 100%; }
.flow-number.positive .flow-bar span { background: var(--green); }
.flow-number.negative .flow-bar span { background: var(--red); }
.net-result { display: flex; justify-content: space-between; align-items: baseline; border-top: 1px solid var(--line); padding-top: 15px; }
.status-summary { display: grid; gap: 12px; }
.status-summary > div { display: grid; grid-template-columns: 12px 1fr auto; align-items: center; gap: 9px; border-bottom: 1px solid var(--line); padding: 8px 0; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: #8b9895; }
.status-dot.rented { background: var(--green); } .status-dot.vacant { background: var(--blue); } .status-dot.maintenance { background: var(--amber); } .status-dot.inactive { background: #8b9895; }
.empty-card { color: var(--muted); padding: 28px; text-align: center; }

.filter-bar { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 14px; }
.filter-bar input { flex: 1 1 260px; }
.filter-bar select { flex: 0 1 190px; }
.ledger-filter-bar input[type="month"] { flex: 0 1 170px; }
.ledger-filter-bar input[type="search"] { flex: 1 1 240px; }
input, select, textarea { width: 100%; border: 1px solid #cbd6d3; border-radius: 7px; background: #fff; color: var(--ink); min-height: 42px; padding: 9px 11px; }
input:focus, select:focus, textarea:focus { outline: 3px solid rgba(16,115,90,.12); border-color: var(--green); }
textarea { min-height: 92px; resize: vertical; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { border-bottom: 1px solid var(--line); padding: 12px 10px; text-align: left; vertical-align: middle; white-space: nowrap; }
th { color: var(--muted); font-size: 12px; font-weight: 800; }
td { font-size: 14px; }
td strong, td small { display: block; }
td small { color: var(--muted); margin-top: 4px; }
tbody tr:last-child td { border-bottom: 0; }
.number { text-align: right; font-variant-numeric: tabular-nums; }
.actions { text-align: right; }
.empty-state { color: var(--muted); padding: 42px 12px; text-align: center; }
.money-positive { color: var(--green-dark); font-weight: 700; }
.money-negative, .money-due { color: var(--red); font-weight: 700; }
.status-badge { display: inline-block; border-radius: 999px; background: #eef2f1; color: #56615f; font-size: 12px; font-weight: 700; padding: 5px 8px; }
.status-badge.active, .status-badge.rented, .status-badge.paid, .status-badge.income { background: #e5f5ef; color: var(--green-dark); }
.status-badge.pending, .status-badge.maintenance { background: #fff5d8; color: var(--amber); }
.status-badge.overdue, .status-badge.expense { background: #ffebe9; color: var(--red); }
.status-badge.vacant { background: #eaf2fb; color: var(--blue); }
.ledger-metrics { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; margin-bottom: 14px; }
.mini-metric { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px 18px; }
.mini-metric span { color: var(--muted); font-size: 13px; }
.mini-metric strong { font-size: 20px; }
.mini-metric.positive { border-left: 4px solid var(--green); } .mini-metric.negative { border-left: 4px solid var(--red); } .mini-metric.warning { border-left: 4px solid var(--yellow); }
.chart-panel { margin-bottom: 14px; }
.chart-heading { align-items: flex-start; }
.rent-history-heading select { width: min(280px, 100%); }
.segmented-control { display: inline-flex; flex-wrap: wrap; gap: 3px; border: 1px solid var(--line); border-radius: 7px; background: var(--surface-soft); padding: 3px; }
.segmented-control button { min-height: 32px; border: 0; background: transparent; padding: 6px 10px; }
.segmented-control button[aria-pressed="true"] { background: var(--green); color: #fff; }
.ledger-chart, .rent-history-chart { width: 100%; min-height: 260px; }
.line-chart svg { display: block; width: 100%; height: auto; }
.line-chart .chart-grid { stroke: var(--line); stroke-width: 1; }
.line-chart .chart-axis { fill: var(--muted); font-size: 12px; }
.line-chart .chart-zero { stroke: #9aa6a4; stroke-width: 1.5; }
.line-chart .chart-line { fill: none; stroke-width: 3; stroke-linejoin: round; stroke-linecap: round; }
.line-chart .chart-point { stroke: var(--surface); stroke-width: 2; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 10px 18px; margin-top: 10px; color: var(--muted); font-size: 12px; }
.chart-legend span { display: inline-flex; align-items: center; gap: 7px; }
.chart-legend i { width: 18px; height: 3px; border-radius: 2px; }

dialog { width: min(720px, calc(100vw - 30px)); max-height: calc(100vh - 30px); overflow-y: auto; border: 0; border-radius: 8px; box-shadow: 0 24px 70px rgba(0,0,0,.24); padding: 0; }
dialog::backdrop { background: rgba(8,18,16,.55); }
#recordForm { padding: 22px; }
.dialog-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 20px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 15px; }
.form-grid label { display: grid; align-content: start; gap: 6px; color: #394541; font-size: 13px; font-weight: 700; }
.form-grid .wide { grid-column: 1 / -1; }
.site-map-field { display: grid; gap: 8px; }
.map-field-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; color: #394541; font-size: 13px; }
.map-field-heading button { min-height: 34px; padding: 6px 10px; }
.site-map-picker { width: 100%; height: 340px; border: 1px solid var(--line); border-radius: 7px; background: var(--surface-soft); overflow: hidden; }
.map-status { min-height: 18px; color: var(--muted); font-size: 12px; }
.map-status.success { color: var(--green-dark); }
.map-status.error { color: var(--red); }
.form-error { color: var(--red); font-size: 13px; min-height: 18px; margin-top: 12px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 9px; margin-top: 4px; }

.login-screen { display: grid; min-height: 100vh; place-items: center; padding: 20px; background: #152320; }
.login-panel { width: min(420px, 100%); display: grid; gap: 18px; border-radius: 8px; background: #fff; box-shadow: 0 24px 70px rgba(0,0,0,.25); padding: 28px; }
.brand-login { padding: 0; margin: 0; }
.brand-login span:not(.brand-mark) { color: var(--muted); }
.login-panel label { display: grid; gap: 6px; font-size: 13px; font-weight: 700; }
.login-copy { margin-top: 8px; }
.login-reset { min-height: auto; border: 0; background: transparent; color: var(--green); font-weight: 700; padding: 4px; }
.form-error.success { color: var(--green-dark); }
.registration-fields { display: grid; gap: 18px; }
.admin-panel + .admin-panel { margin-top: 16px; }

@media (min-width: 1180px) { .dashboard-grid { grid-template-columns: minmax(0, 1.55fr) minmax(300px, .75fr); } }
@media (max-width: 1050px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; padding: 16px; }
  .brand { margin-bottom: 16px; }
  .nav-list { grid-template-columns: repeat(6, minmax(120px, 1fr)); overflow-x: auto; }
  .nav-item { text-align: center; white-space: nowrap; }
  .nav-item.active { box-shadow: inset 0 -3px var(--yellow); }
  .sidebar-footer { display: flex; justify-content: space-between; padding-top: 12px; }
  .metric-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 760px) {
  main { padding: 18px 14px; }
  .topbar { flex-direction: column; }
  .rent-history-heading { align-items: stretch; flex-direction: column; }
  .toolbar { width: 100%; justify-content: flex-start; }
  h1 { font-size: 24px; }
  .metric-grid, .dashboard-grid, .ledger-metrics { grid-template-columns: 1fr; }
  .site-row { grid-template-columns: 1fr; }
  .site-balance { text-align: left; }
  .form-grid { grid-template-columns: 1fr; }
  .site-map-picker { height: 280px; }
  .filter-bar { display: grid; grid-template-columns: 1fr; }
  .filter-bar input, .filter-bar select { width: 100%; }
  .mini-metric { min-height: 70px; }
  .panel { padding: 15px; }
}
