:root {
    color-scheme: dark;
    --bg: #080d18;
    --panel: #111a2a;
    --panel-soft: #162135;
    --border: #263550;
    --text: #f4f7fb;
    --muted: #91a0b8;
    --income: #35d49a;
    --expense: #ff6b7a;
    --accent: #7c8cff;
    --warning: #ffc857;
    --shadow: 0 18px 45px rgba(0, 0, 0, 0.25);
  }

  * { box-sizing: border-box; }
  [hidden] { display: none !important; }

  html, body { max-width: 100%; overflow-x: hidden; }

  body {
    margin: 0;
    min-width: 320px;
    min-height: 100vh;
    background:
      radial-gradient(circle at 85% 5%, rgba(124, 140, 255, 0.14), transparent 28rem),
      var(--bg);
    color: var(--text);
    font-family: Inter, "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
  }

  button, select, input { font: inherit; }

  .page-shell {
    width: min(1480px, calc(100% - 40px));
    margin: 0 auto;
    padding: 38px 0 28px;
  }

  .dashboard-tabs {
    display: inline-flex;
    gap: 4px;
    margin-bottom: 24px;
    padding: 4px;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: rgba(17, 26, 42, 0.78);
  }

  .dashboard-tab {
    height: 36px;
    border: 0;
    color: var(--muted);
    background: transparent;
  }

  .dashboard-tab.active {
    color: var(--text);
    background: var(--panel-soft);
    box-shadow: 0 5px 16px rgba(0, 0, 0, 0.22);
  }

  .finance-entry-nav {
    min-height: 36px;
    padding: 8px 14px;
    border-radius: 8px;
    color: var(--muted);
    text-decoration: none;
  }

  .finance-entry-nav:hover,
  .finance-entry-nav:focus-visible {
    color: var(--text);
    background: var(--panel-soft);
  }

  .topbar {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    gap: 24px;
    margin-bottom: 24px;
  }

  .eyebrow, .panel-kicker {
    margin: 0 0 8px;
    color: var(--accent);
    font-size: 0.72rem;
    font-weight: 800;
    letter-spacing: 0.18em;
  }

  h1, h2, p { margin-top: 0; }
  h1 { margin-bottom: 7px; font-size: clamp(1.75rem, 3vw, 2.55rem); }
  h2 { margin-bottom: 0; font-size: 1.05rem; }
  .subtitle, .legend-note, footer { color: var(--muted); }
  .subtitle { margin-bottom: 0; }

  .controls {
    display: flex;
    align-items: flex-end;
    gap: 12px;
    flex-wrap: wrap;
  }

  .controls label { display: grid; gap: 6px; }
  .controls label span { color: var(--muted); font-size: 0.78rem; font-weight: 700; }

  select, button {
    height: 42px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--panel);
    color: var(--text);
  }

  input {
    height: 42px;
    padding: 0 12px;
    border: 1px solid var(--border);
    border-radius: 10px;
    color: var(--text);
    background: var(--panel);
    font-variant-numeric: tabular-nums;
  }

  input:focus { border-color: var(--accent); outline: 2px solid rgba(124, 140, 255, 0.2); }
  input.input-invalid { border-color: var(--expense); }

  select { min-width: 118px; padding: 0 34px 0 12px; }
  button { padding: 0 17px; cursor: pointer; font-weight: 750; }
  button:hover:not(:disabled) { border-color: var(--accent); background: var(--panel-soft); }
  button:disabled, select:disabled { cursor: not-allowed; opacity: 0.55; }

  .status {
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 48px;
    margin-bottom: 18px;
    padding: 12px 16px;
    border: 1px solid var(--border);
    border-radius: 12px;
    color: var(--muted);
    background: rgba(17, 26, 42, 0.75);
  }

  .status-ready { display: none; }
  .status-error { border-color: rgba(255, 107, 122, 0.5); color: #ffabb4; }

  .spinner {
    width: 18px;
    height: 18px;
    border: 2px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
  }

  @keyframes spin { to { transform: rotate(360deg); } }

  .dashboard { display: grid; min-width: 0; max-width: 100%; gap: 18px; }
  .kpi-grid { display: grid; min-width: 0; max-width: 100%; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; }
  .finance-kpis { grid-template-columns: repeat(12, minmax(0, 1fr)); }
  .finance-kpis .kpi-card { grid-column: span 3; }
  .finance-kpis .finance-secondary-card { grid-column: span 4; }
  .activity-kpis { grid-template-columns: repeat(6, minmax(0, 1fr)); }
  .kpi-card, .panel {
    min-width: 0;
    max-width: 100%;
    border: 1px solid var(--border);
    border-radius: 16px;
    background: linear-gradient(145deg, rgba(22, 33, 53, 0.94), rgba(13, 21, 35, 0.96));
    box-shadow: var(--shadow);
  }

  .kpi-card { position: relative; overflow: hidden; padding: 20px; }
  .kpi-card::before { content: ""; position: absolute; inset: 0 auto 0 0; width: 4px; background: var(--accent); }
  .income-card::before { background: var(--income); }
  .expense-card::before { background: var(--expense); }
  .balance-card::before { background: var(--warning); }
  .muted-card::before { background: var(--muted); }
  .kpi-label { display: block; margin-bottom: 14px; color: var(--muted); font-size: 0.83rem; font-weight: 700; }
  .kpi-card strong { display: block; margin-bottom: 8px; font-size: clamp(1.35rem, 2vw, 2rem); font-variant-numeric: tabular-nums; }
  .kpi-card small { color: var(--muted); }
  .value-positive { color: var(--income); }
  .value-negative { color: var(--expense); }

  .settlement-panel { display: grid; gap: 18px; }
  .settlement-heading { margin-bottom: 0; }
  .settlement-actions { display: flex; align-items: center; justify-content: flex-end; gap: 12px; }
  .settlement-actions button { height: 36px; }
  .settlement-inputs { display: flex; flex-wrap: wrap; gap: 14px; }
  .settlement-inputs label { display: grid; flex: 1 1 220px; gap: 7px; }
  .settlement-inputs label span { color: var(--muted); font-size: 0.78rem; font-weight: 700; }
  .number-stepper { display: grid; grid-template-columns: 42px minmax(80px, 1fr) 42px; gap: 7px; }
  .number-stepper button { width: 42px; padding: 0; font-size: 1.15rem; }
  .number-stepper input { width: 100%; text-align: center; }
  .settlement-validation { margin: -6px 0 0; color: #ffabb4; font-size: 0.8rem; }
  .settlement-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
  .settlement-card {
    min-width: 0;
    padding: 15px;
    border: 1px solid rgba(38, 53, 80, 0.78);
    border-radius: 12px;
    background: rgba(8, 13, 24, 0.34);
  }
  .settlement-card > span { display: block; margin-bottom: 9px; color: var(--muted); font-size: 0.76rem; font-weight: 750; }
  .settlement-card strong { display: block; margin-bottom: 7px; font-size: clamp(1.15rem, 2vw, 1.55rem); font-variant-numeric: tabular-nums; }
  .settlement-card small { color: var(--muted); line-height: 1.45; }
  .income-summary strong { color: var(--income); }
  .expense-summary strong { color: var(--expense); }
  .total-card { background: rgba(124, 140, 255, 0.075); }
  .net-summary { grid-column: 1 / -1; }

  .finance-entry-panel { display: grid; gap: 18px; }
  .finance-entry-heading { margin-bottom: 0; }
  .finance-entry-meta { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; }
  .finance-entry-meta label { display: grid; gap: 7px; }
  .finance-entry-meta label span { color: var(--muted); font-size: 0.78rem; font-weight: 700; }
  .finance-entry-list-heading,
  .finance-entry-form-actions,
  .finance-entry-summary { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
  .finance-entry-list-heading button { height: 36px; }
  .finance-entry-lines { display: grid; gap: 10px; }
  .finance-entry-line {
    display: grid;
    grid-template-columns: 110px minmax(150px, 0.8fr) minmax(120px, 0.6fr) minmax(220px, 1.6fr) auto;
    gap: 9px;
    padding: 11px;
    border: 1px solid rgba(38, 53, 80, 0.78);
    border-radius: 12px;
    background: rgba(8, 13, 24, 0.3);
  }
  .finance-entry-line select,
  .finance-entry-line input { width: 100%; min-width: 0; }
  .finance-entry-remove,
  .danger-button { color: #ffabb4; border-color: rgba(255, 107, 122, 0.45); }
  .finance-entry-summary {
    justify-content: flex-end;
    flex-wrap: wrap;
    padding: 14px 16px;
    border-radius: 12px;
    background: rgba(124, 140, 255, 0.075);
  }
  .finance-entry-summary span { color: var(--muted); font-size: 0.82rem; }
  .finance-entry-summary strong { margin-left: 7px; color: var(--text); font-size: 1rem; }
  .finance-entry-form-actions { justify-content: flex-end; }
  .finance-batch-table { min-width: 980px; }
  .finance-batch-actions { display: flex; gap: 7px; }
  .finance-batch-actions button { height: 34px; padding: 0 11px; font-size: 0.78rem; }
  .finance-batch-filters { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
  .finance-batch-filters input { width: 145px; height: 36px; }
  .finance-batch-filters button { height: 36px; padding: 0 11px; }
  .finance-batch-filters > span { color: var(--muted); font-size: 0.78rem; }

  .chart-grid { display: grid; grid-template-columns: 1.25fr 1fr; gap: 18px; }
  .activity-chart-grid { grid-template-columns: minmax(300px, 0.75fr) minmax(0, 1.5fr); }
  .history-only { grid-column: 1 / -1; }
  .panel { padding: 20px; }
  .panel-heading { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 20px; }
  .panel-kicker { margin-bottom: 5px; font-size: 0.65rem; }
  .legend-note, .record-count { font-size: 0.78rem; }
  .chart-wrap { position: relative; height: 320px; }

  .transaction-actions,
  .panel-actions {
    display: flex;
    align-items: center;
    gap: 12px;
  }

  .collapse-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    height: 34px;
    padding: 0 11px;
    color: var(--muted);
    font-size: 0.78rem;
  }

  .collapse-icon {
    display: inline-block;
    font-size: 1rem;
    transition: transform 0.2s ease;
  }

  .collapse-button[aria-expanded="false"] .collapse-icon {
    transform: rotate(180deg);
  }

  .warning-box {
    padding: 14px 18px;
    border: 1px solid rgba(255, 200, 87, 0.45);
    border-radius: 12px;
    color: #ffe29a;
    background: rgba(255, 200, 87, 0.08);
    font-size: 0.86rem;
  }

  .warning-box ul { margin: 0; padding-left: 20px; }
  .warning-box li + li { margin-top: 5px; }

  .roster-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 18px;
    align-items: start;
  }

  .roster-list {
    display: grid;
    gap: 8px;
  }

  .roster-list[hidden] { display: none; }

  .roster-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    min-height: 43px;
    padding: 9px 11px;
    border: 1px solid rgba(38, 53, 80, 0.75);
    border-radius: 10px;
    background: rgba(8, 13, 24, 0.34);
  }

  .roster-item time {
    flex-shrink: 0;
    color: var(--muted);
    font-size: 0.72rem;
  }

  .roster-identity {
    display: flex;
    align-items: center;
    gap: 9px;
    min-width: 0;
  }

  .status-badge {
    padding: 4px 8px;
    border-radius: 999px;
    font-size: 0.68rem;
    font-weight: 800;
  }

  .status-active { color: #83efc8; background: rgba(53, 212, 154, 0.13); }
  .status-leave { color: #ffbdc4; background: rgba(255, 107, 122, 0.13); }

  .waitlist-position {
    display: inline-grid;
    place-items: center;
    width: 24px;
    height: 24px;
    flex-shrink: 0;
    border-radius: 50%;
    color: #ffbdc4;
    background: rgba(255, 107, 122, 0.13);
    font-size: 0.72rem;
    font-weight: 800;
  }

  .table-wrap { width: 100%; min-width: 0; max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  table { width: 100%; border-collapse: collapse; min-width: 790px; }
  th, td { padding: 13px 12px; border-bottom: 1px solid rgba(38, 53, 80, 0.72); text-align: left; }
  th { color: var(--muted); font-size: 0.74rem; letter-spacing: 0.04em; }
  td { font-size: 0.88rem; }
  td.note { max-width: 430px; white-space: pre-line; }
  .number { text-align: right; font-variant-numeric: tabular-nums; }
  tbody tr:hover { background: rgba(124, 140, 255, 0.055); }
  .empty-message { margin: 22px 0 6px; color: var(--muted); text-align: center; }
  #transactionContent[hidden] { display: none; }

  footer { display: flex; justify-content: space-between; gap: 16px; padding: 3px 4px; font-size: 0.76rem; }
  .site-credit {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 6px;
    margin-top: 26px;
    padding: 22px 12px 6px;
    border-top: 1px solid var(--border);
    text-align: center;
  }
  .site-credit p { margin: 0; letter-spacing: 0.04em; }
  .site-credit strong { color: var(--accent); font-weight: 850; }

  @media (max-width: 980px) {
    .topbar { align-items: flex-start; flex-direction: column; }
    .kpi-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .finance-kpis { grid-template-columns: repeat(12, minmax(0, 1fr)); }
    .finance-kpis .kpi-card,
    .finance-kpis .finance-secondary-card { grid-column: span 6; }
    .activity-kpis { grid-template-columns: repeat(3, minmax(0, 1fr)); }
    .chart-grid { grid-template-columns: 1fr; }
    .history-only { grid-column: auto; }
    .roster-grid { grid-template-columns: 1fr; }
    .settlement-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .finance-entry-line { grid-template-columns: 110px 1fr 1fr; }
    .finance-entry-note { grid-column: 1 / 3; }
  }

  @media (max-width: 600px) {
    .page-shell { width: min(100% - 24px, 1480px); padding-top: 24px; }
    .dashboard-tabs { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); width: 100%; }
    .dashboard-tab,
    .finance-entry-nav {
      display: grid;
      place-items: center;
      width: 100%;
      min-width: 0;
      height: auto;
      min-height: 48px;
      padding: 8px;
      text-align: center;
      white-space: normal;
    }
    .controls { display: grid; grid-template-columns: 1fr 1fr; width: 100%; }
    .controls button { grid-column: 1 / -1; }
    select { width: 100%; }
    .kpi-grid { grid-template-columns: 1fr; }
    .finance-kpis { grid-template-columns: 1fr; }
    .finance-kpis .kpi-card,
    .finance-kpis .finance-secondary-card { grid-column: auto; }
    .kpi-card { width: 100%; }
    .activity-kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .settlement-heading { flex-direction: column; }
    .settlement-actions { width: 100%; align-items: flex-start; flex-direction: column; }
    .settlement-actions button { width: 100%; }
    .settlement-grid { grid-template-columns: 1fr; }
    .finance-entry-meta,
    .finance-entry-form-actions { align-items: stretch; flex-direction: column; }
    .finance-entry-form-actions button { width: 100%; }
    .finance-entry-line { grid-template-columns: 1fr; }
    .finance-entry-note { grid-column: auto; }
    .finance-entry-summary { align-items: flex-start; flex-direction: column; }
    .finance-batch-heading { flex-direction: column; }
    .finance-batch-filters { width: 100%; }
    .finance-batch-filters input { flex: 1 1 130px; }
    .net-summary { grid-column: auto; }
    #financeDashboard > .chart-grid { display: none; }
    .transactions-panel { overflow: hidden; }
    .chart-wrap { height: 275px; }
    .panel { padding: 16px; }
    .panel-heading { align-items: flex-start; }
    .roster-item { align-items: flex-start; flex-direction: column; }
    .roster-item time { padding-left: 33px; }
    footer { flex-direction: column; }
  }
