:root {
  --bg: #f6e6dc;
  --bg-2: #fdf4ee;
  --card: #fffdfa;
  --kpi: #f8d4c6;
  --line: #ead3c6;
  --text: #3f2e24;
  --muted: #8a6d5d;
  --title: #c45911;
  --ying: #f4a0b8;
  --ying-2: #e56b8a;
  --ching: #5b9bd5;
  --ching-2: #2e75b6;
  --income: #6ba3d6;
  --profit: #f3b4c4;
  --orange: #ed7d31;
  --purple: #8c6fd1;
  --green: #70ad47;
  --green-2: #548235;
  --neg: #c00000;
  --shadow: 0 1px 8px rgba(80, 40, 20, 0.08);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Noto Sans TC", "Microsoft JhengHei", "PingFang TC", sans-serif;
}
code { font-size: 12px; }

#loading {
  position: fixed; inset: 0; z-index: 50;
  display: flex; flex-direction: column; gap: 12px;
  align-items: center; justify-content: center;
  background: var(--bg); color: var(--title); font-weight: 700;
}
.spinner {
  width: 36px; height: 36px; border-radius: 50%;
  border: 3px solid #e8c4b0; border-top-color: var(--title);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; background: #4a2c23; color: #fff;
  position: sticky; top: 0; z-index: 20;
}
.brand .logo { font-weight: 800; letter-spacing: 0.04em; }
.brand .sub { font-size: 11px; opacity: 0.75; }
.menu-btn {
  display: none; border: 1px solid #f3d2c4; background: transparent; color: #fff;
  border-radius: 8px; padding: 8px 12px; font-size: 14px;
}
.tabs {
  display: flex; flex-wrap: wrap; gap: 6px; flex: 1;
}
.tabs button {
  border: 0; background: transparent; color: #f3d2c4;
  padding: 8px 10px; border-radius: 8px; cursor: pointer; font-size: 13px;
}
.tabs button.on, .tabs button:hover { background: #6b4033; color: #fff; }
.tools { display: flex; align-items: center; gap: 8px; }
.tools .who { font-size: 12px; opacity: 0.8; }
.tools .upload, .tools .logout {
  background: #c45911; color: #fff; padding: 8px 10px; border-radius: 8px;
  cursor: pointer; font-size: 12px; text-decoration: none;
}
.tools .logout { background: #6b4033; }
.tools input { display: none; }
body.static-host .upload { display: none; }

.filters {
  display: flex; flex-wrap: wrap; gap: 12px 18px;
  align-items: end; padding: 10px 16px;
  background: var(--bg-2); border-bottom: 1px solid var(--line);
}
.filters label { font-size: 12px; color: var(--muted); display: flex; flex-direction: column; gap: 4px; }
.filters select, .filters input[type="text"], .filters input:not([type="checkbox"]) {
  min-width: 130px; padding: 6px 8px; border: 1px solid var(--line);
  border-radius: 6px; background: #fff;
}
.doc-group { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.doc-group button {
  border: 1px solid var(--line); background: #fff; border-radius: 16px;
  padding: 5px 10px; cursor: pointer;
}
.doc-group button.on { background: #4a2c23; color: #fff; border-color: #4a2c23; }
.chk { flex-direction: row !important; align-items: center; gap: 6px !important; }
.hint { font-size: 12px; color: var(--muted); margin-left: auto; }

.kpis {
  display: grid;
  grid-template-columns: repeat(8, minmax(110px, 1fr));
  gap: 8px; padding: 12px 16px;
  background: var(--kpi);
}
.kpi {
  background: transparent; padding: 4px 6px;
}
.kpi .v { font-size: 22px; font-weight: 800; color: var(--title); line-height: 1.1; }
.kpi .v.neg { color: var(--neg); }
.kpi .l { font-size: 12px; color: var(--text); margin-top: 2px; }
.kpis.patient { grid-template-columns: repeat(7, minmax(110px, 1fr)); }

.page { padding: 12px 16px 32px; min-height: 70vh; }
.grid-1 { display: grid; grid-template-columns: 1fr; gap: 12px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid-4 { display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; gap: 12px; }
.card {
  background: var(--card); border-radius: 10px; box-shadow: var(--shadow);
  padding: 10px 12px;
}
.card h3 {
  margin: 0 0 8px; text-align: center; color: var(--title);
  font-size: 16px; font-weight: 800;
}
.chart { width: 100%; height: 420px; }
.chart.tall { height: 520px; }
.chart.short { height: 280px; }
.seg {
  display: flex; gap: 8px; margin-bottom: 8px;
}
.seg button {
  border: 1px solid var(--line); background: #fff; border-radius: 8px;
  padding: 6px 12px; cursor: pointer;
}
.seg button.on { background: var(--ching); color: #fff; border-color: var(--ching); }
.table-wrap { max-height: 420px; overflow: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 6px 8px; border-bottom: 1px solid var(--line); text-align: left; }
th { color: var(--muted); font-weight: 600; position: sticky; top: 0; background: var(--card); }
td.num { text-align: right; font-variant-numeric: tabular-nums; }
.note { font-size: 12px; color: var(--muted); margin: 8px 0 0; }
.foot { padding: 10px 16px 20px; color: var(--muted); font-size: 12px; }

@media (max-width: 1100px) {
  .kpis, .kpis.patient { grid-template-columns: repeat(4, 1fr); }
  .grid-2, .grid-4 { grid-template-columns: 1fr; }
  .chart { height: 340px; }
  .chart.tall { height: 380px; }
}

@media (max-width: 720px) {
  .topbar { flex-wrap: wrap; }
  .menu-btn { display: inline-block; margin-left: auto; }
  .tabs {
    display: none; flex-basis: 100%; flex-direction: column; gap: 4px;
    background: #3a221b; padding: 8px; border-radius: 10px;
  }
  .tabs.open { display: flex; }
  .tabs button { text-align: left; padding: 12px; font-size: 15px; }
  .tools { width: 100%; justify-content: flex-end; }
  .brand .sub { display: none; }
  .filters { gap: 10px; }
  .filters select, .filters input:not([type="checkbox"]) { min-width: 0; width: 42vw; font-size: 16px; }
  .kpis, .kpis.patient { grid-template-columns: repeat(2, 1fr); }
  .kpi .v { font-size: 18px; }
  .hint { display: none; }
  .chart, .chart.tall { height: 280px; }
  .chart.short { height: 220px; }
  .page { padding: 8px 10px 40px; }
}
