:root {
  --bg: #f5f7f8;
  --panel: #ffffff;
  --ink: #1b2528;
  --muted: #657276;
  --line: #d9e0e2;
  --accent: #0f766e;
  --accent-2: #2563eb;
  --danger: #b42318;
  --warn: #b45309;
  --good: #047857;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  color: var(--ink);
  background: var(--bg);
  font-family: "Microsoft JhengHei", "PingFang TC", system-ui, sans-serif;
}

body.locked .topbar,
body.locked .app-shell {
  display: none;
}

.auth-screen {
  display: none;
  min-height: 100vh;
  padding: 24px;
  place-items: center;
  background: #12373a;
}

body.locked .auth-screen {
  display: grid;
}

.auth-box {
  display: grid;
  gap: 14px;
  width: min(100%, 420px);
  padding: 24px;
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 16px 40px rgba(0, 0, 0, .18);
}

.auth-box.compact {
  box-shadow: none;
}

.auth-box h1,
.auth-box h2 {
  color: var(--ink);
}

.auth-box p {
  color: var(--muted);
}

.password-dialog {
  width: min(92vw, 460px);
  border: 0;
  border-radius: 8px;
  padding: 0;
}

.password-dialog::backdrop {
  background: rgba(18, 55, 58, .55);
}

.receipt-dialog {
  width: min(96vw, 920px);
  max-height: 92vh;
  border: 0;
  border-radius: 8px;
  padding: 0;
}

.receipt-dialog::backdrop {
  background: rgba(18, 55, 58, .55);
}

.receipt-form {
  max-height: 92vh;
  overflow: auto;
  border: 0;
  border-radius: 8px;
}

.receipt-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
}

.receipt-items-editor {
  display: grid;
  gap: 8px;
}

.receipt-item-rows {
  display: grid;
  gap: 8px;
}

.receipt-item-row {
  display: grid;
  grid-template-columns: minmax(160px, 1fr) 120px 90px auto;
  gap: 8px;
  align-items: center;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfd;
}

.receipt-assets {
  display: grid;
  grid-template-columns: 1fr auto 1fr auto;
  gap: 10px;
  align-items: end;
}

.receipt-preview-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.receipt-preview-row div {
  display: grid;
  gap: 6px;
  min-height: 120px;
  padding: 10px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  font-size: 13px;
}

.receipt-preview-row img {
  max-width: 100%;
  max-height: 110px;
  object-fit: contain;
}

.receipt-records,
.payment-summary {
  margin-top: 14px;
}

.payment-summary {
  margin-bottom: 14px;
}

.average-income-panel {
  display: grid;
  gap: 8px;
  margin-bottom: 12px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfd;
}

.average-income-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
}

.average-income-actions input,
.average-income-actions select {
  width: auto;
  min-width: 130px;
}

.row-check {
  display: inline-flex;
  gap: 8px;
  align-items: center;
}

.row-check input {
  width: auto;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 24px;
  align-items: center;
  padding: 24px clamp(18px, 4vw, 48px);
  color: white;
  background: #12373a;
}

h1, h2, p { margin: 0; }
h1 { font-size: clamp(24px, 4vw, 38px); letter-spacing: 0; }
h2 { font-size: 18px; }
.topbar p { margin-top: 6px; color: #d8eeee; }

.section-title {
  margin-top: 20px;
}

button, .file-btn {
  min-height: 38px;
  border: 0;
  border-radius: 6px;
  padding: 8px 14px;
  color: white;
  background: var(--accent);
  font: inherit;
  cursor: pointer;
  white-space: nowrap;
}

button:hover, .file-btn:hover { filter: brightness(.96); }
.ghost { color: var(--ink); background: #e8eef0; }
.ghost.light { color: #12373a; background: #d8eeee; }
.danger { background: var(--danger); }
button:disabled {
  cursor: not-allowed;
  opacity: .55;
}
.file-btn input { display: none; }
.top-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
  align-items: center;
}

.backup-status {
  max-width: 260px;
  padding: 8px 10px;
  border: 1px solid rgba(216, 238, 238, .45);
  border-radius: 6px;
  color: #e9fbfb;
  font-size: 13px;
  line-height: 1.3;
  overflow-wrap: anywhere;
}

.app-shell { padding: 18px clamp(14px, 3vw, 36px) 36px; }

.tabs {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 12px;
}

.tab {
  color: var(--ink);
  background: #e5ecee;
}

.tab.active {
  color: white;
  background: var(--accent-2);
}

.view { display: none; }
.view.active { display: block; }

.summary-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 14px;
}

.metric {
  padding: 18px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.metric span {
  display: block;
  color: var(--muted);
  font-size: 13px;
}

.metric strong {
  display: block;
  margin-top: 8px;
  font-size: 26px;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
  min-width: 0;
}

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

.query-panel {
  margin-bottom: 14px;
}

.search-filter {
  display: flex;
  grid-template-columns: auto;
  align-items: center;
  flex-direction: row;
  gap: 8px;
  min-width: min(100%, 280px);
}

.search-filter input {
  width: min(320px, 52vw);
}

.split {
  display: grid;
  grid-template-columns: minmax(280px, 390px) minmax(0, 1fr);
  gap: 14px;
  align-items: start;
}

.dual {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

.detail-panel {
  display: none;
  margin-top: 14px;
}

.detail-panel.active {
  display: block;
}

.detail-head {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  align-items: flex-start;
  margin-bottom: 14px;
}

.detail-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
  color: var(--muted);
  font-size: 13px;
}

.detail-sections {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

.subpanel {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: #fbfcfd;
}

.subpanel h3 {
  margin: 0 0 10px;
  font-size: 15px;
}

.group-info-sections {
  margin-bottom: 14px;
}

.section-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
}

.section-head h3 {
  margin: 0;
  color: var(--ink);
  font-size: 15px;
}

.collapsible-section {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
}

.collapsible-section > summary {
  position: relative;
  padding: 12px 40px 12px 14px;
  color: var(--ink);
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  list-style: none;
  user-select: none;
}

.collapsible-section > summary::-webkit-details-marker {
  display: none;
}

.collapsible-section > summary::after {
  content: "展開";
  position: absolute;
  right: 14px;
  color: var(--accent);
  font-size: 13px;
  font-weight: 600;
}

.collapsible-section[open] > summary::after {
  content: "收合";
}

.collapsible-section[open] > summary {
  border-bottom: 1px solid var(--line);
}

.collapsible-content {
  display: grid;
  gap: 10px;
  padding: 12px 14px 14px;
}

.subpanel.collapsible-section {
  padding: 0;
}

.payment-summary.collapsible-section .table-wrap {
  margin: 0;
}

.closed-groups-section { margin-top: 18px; min-width: 0; }
.closed-groups-section > summary { padding-right: 54px; }
.closed-groups-section .collapsible-content { min-width: 0; }

.quote-editor {
  display: grid;
  gap: 8px;
}

.quote-rows {
  display: grid;
  gap: 8px;
}

.participant-fee-editor,
.participant-fee-rows {
  display: grid;
  gap: 8px;
}

.participant-fee-row {
  display: grid;
  grid-template-columns: 1fr 100px;
  gap: 8px;
  align-items: center;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfd;
}

.participant-fee-row [data-participant-fee-field="note"],
.participant-fee-row button {
  grid-column: 1 / -1;
}

.participant-income-entry {
  display: grid;
  grid-template-columns: 120px 120px 90px auto;
  gap: 6px;
  min-width: 460px;
}

.participant-income-entry input,
.participant-income-entry select {
  min-width: 0;
}

.quick-picks {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.quick-picks button {
  min-height: 30px;
  padding: 5px 9px;
  font-size: 13px;
}

.quote-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  align-items: center;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfd;
}

.quote-row [data-quote-field="item"],
.quote-row [data-quote-field="note"],
.quote-row button {
  grid-column: 1 / -1;
}

.quote-row .quote-fee-check {
  grid-column: 1 / -1;
}

.quote-targets { grid-column: 1 / -1; min-width: 0; }
.quote-targets[hidden] { display: none; }
.quote-target-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.quote-target-grid > .hint { grid-column: 1 / -1; }
.quote-targets .checkline { min-height: 44px; min-width: 0; gap: 4px; overflow-wrap: anywhere; }
.quote-targets .checkline input { flex-shrink: 0; }
.quote-targets .checkline span { min-width: 0; }
.quote-targets > summary { padding-right: 54px; }

.estimated-expense-form {
  display: grid;
  grid-template-columns: minmax(150px, 1.5fr) 90px 130px minmax(150px, 1fr) auto;
  gap: 8px;
  align-items: end;
  margin-bottom: 12px;
}

.estimated-expense-form input,
.estimated-expense-form button {
  min-width: 0;
}

.participant-list {
  display: grid;
  grid-template-columns: repeat(4, minmax(100px, 1fr));
  gap: 4px 28px;
  margin: 0;
  padding-left: 26px;
  line-height: 1.8;
}

.participant-list li {
  padding-right: 8px;
}

.form {
  display: grid;
  gap: 12px;
}

label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 13px;
}

input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 9px 10px;
  color: var(--ink);
  background: white;
  font: inherit;
}

textarea { resize: vertical; }

.checkline {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--ink);
}
.checkline input { width: auto; }

.inline-filter {
  display: flex;
  grid-template-columns: auto;
  align-items: center;
  flex-direction: row;
  gap: 8px;
}
.inline-filter input { width: 160px; }

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

.hint {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.5;
}

.table-wrap {
  overflow-x: auto;
  margin-top: 12px;
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 760px;
}

.detail-income-table {
  table-layout: fixed;
  min-width: 1060px;
}

.detail-income-table .income-date-col { width: 96px; }
.detail-income-table .income-payer-col { width: 120px; }
.detail-income-table .income-amount-col { width: 96px; }
.detail-income-table .income-method-col { width: 82px; }
.detail-income-table .income-effective-col { width: 96px; }
.detail-income-table .income-status-col { width: 74px; }
.detail-income-table .income-receipt-col { width: 100px; }
.detail-income-table .income-note-col { width: 180px; }
.detail-income-table .income-action-col { width: 100px; }

th, td {
  padding: 10px 9px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
  font-size: 14px;
}

th {
  color: var(--muted);
  background: #f0f4f5;
  font-weight: 700;
}

.note-cell {
  white-space: pre-wrap;
  word-break: break-word;
}

.table-money-input {
  width: 92px;
  min-height: 32px;
  padding: 4px 6px;
  text-align: right;
}

.table-note-input {
  width: 120px;
  min-height: 32px;
  padding: 4px 6px;
}

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

.actions button {
  min-height: 30px;
  padding: 5px 9px;
  font-size: 13px;
}

.pill {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  border-radius: 999px;
  padding: 2px 9px;
  font-size: 12px;
  background: #edf2f7;
}

.paid { color: var(--good); background: #dcfce7; }
.pending { color: var(--warn); background: #fef3c7; }
.gap-bad { color: var(--danger); }
.gap-good { color: var(--good); }

.empty {
  padding: 24px;
  color: var(--muted);
  text-align: center;
  border: 1px dashed var(--line);
  border-radius: 8px;
}

.alert-list {
  display: grid;
  gap: 10px;
  margin-top: 12px;
}

.alert {
  padding: 12px;
  border-left: 4px solid var(--accent-2);
  background: #f7fafc;
  border-radius: 6px;
}

.alert.check-alert {
  border-left-color: var(--warn);
  background: #fff7ed;
  color: #7c2d12;
}

.alert.departure-alert {
  border: 2px solid #dc2626;
  border-left-width: 8px;
  background: #fef2f2;
  color: #7f1d1d;
  font-weight: 700;
  font-size: 16px;
  box-shadow: 0 8px 18px rgba(220, 38, 38, .12);
}

@media (max-width: 900px) {
  .topbar { align-items: flex-start; flex-direction: column; }
  .summary-grid, .split, .dual, .detail-sections { grid-template-columns: 1fr; }
  .panel-head { align-items: flex-start; flex-direction: column; }
  .participant-fee-row, .participant-income-entry { grid-template-columns: 1fr; }
  .participant-income-entry { min-width: 240px; }
  .participant-list { grid-template-columns: repeat(2, minmax(100px, 1fr)); }
  .quote-row { grid-template-columns: 1fr 1fr; }
  .quote-row button { grid-column: 1 / -1; }
  .estimated-expense-form { grid-template-columns: 1fr 1fr; }
  .estimated-expense-form input[name="item"],
  .estimated-expense-form input[name="note"],
  .estimated-expense-form button { grid-column: 1 / -1; }
  .receipt-grid, .receipt-assets, .receipt-preview-row, .receipt-item-row { grid-template-columns: 1fr; }
  table { min-width: 680px; }
}
