:root {
  color-scheme: light;
  --bg: #f5f7f8;
  --panel: #ffffff;
  --ink: #182026;
  --muted: #66737d;
  --line: #d8e0e6;
  --line-strong: #c4ced6;
  --brand: #0f8b6d;
  --brand-dark: #0b6f57;
  --danger: #b64a3d;
  --blue-bg: #edf5fc;
  --blue-line: #cfe0f0;
  --green-bg: #eaf6f1;
  --green-line: #c7e3d8;
  --amber-bg: #fff6e6;
  --amber-line: #ecd6a8;
  --rose-bg: #fbefef;
  --rose-line: #e7cbcb;
  --radius: 8px;
  --shadow: 0 12px 32px rgba(24, 32, 38, 0.08);
  font-family: Inter, "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  width: 100%;
  min-height: 100vh;
  overflow-x: hidden;
  background: var(--bg);
  color: var(--ink);
}

[hidden] {
  display: none !important;
}

button,
input {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: 22px;
  line-height: 1.2;
}

h2 {
  margin-bottom: 0;
  font-size: 20px;
  line-height: 1.25;
}

svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.app-shell {
  min-height: 100vh;
  overflow-x: hidden;
}

.auth-view {
  display: grid;
  place-items: center;
  min-height: 100vh;
  padding: 20px;
}

.login-panel {
  width: min(440px, 100%);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
  padding: 22px;
}

.login-brand {
  margin-bottom: 22px;
}

.login-form {
  display: grid;
  gap: 14px;
}

.login-error {
  min-height: 20px;
  margin: 0;
  color: var(--danger);
  font-size: 14px;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  border-bottom: 1px solid var(--line);
  background: rgba(245, 247, 248, 0.94);
  backdrop-filter: blur(10px);
}

.topbar-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  width: min(1180px, calc(100vw - 32px));
  min-height: 72px;
  margin: 0 auto;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand > div:last-child {
  min-width: 0;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: var(--radius);
  background: var(--brand);
  color: #fff;
  flex: 0 0 auto;
}

.eyebrow {
  margin-bottom: 3px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.2;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
}

.status-badge {
  display: inline-flex;
  align-items: center;
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  color: var(--muted);
  padding: 0 12px;
  font-size: 13px;
  white-space: nowrap;
}

.status-badge.ok {
  border-color: var(--green-line);
  background: var(--green-bg);
  color: var(--brand-dark);
}

.status-badge.warn {
  border-color: var(--amber-line);
  background: var(--amber-bg);
  color: #8a6418;
}

.icon-button {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  color: var(--ink);
}

.icon-button:hover {
  border-color: var(--line-strong);
}

.icon-button.danger {
  color: var(--danger);
}

.layout {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  gap: 20px;
  width: min(1180px, calc(100vw - 32px));
  margin: 22px auto 48px;
}

.sidebar {
  align-self: start;
  position: sticky;
  top: 94px;
  display: grid;
  gap: 16px;
}

.main {
  display: grid;
  gap: 16px;
  min-width: 0;
}

.panel {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
}

.settlement-panel,
.members-panel,
.input-panel,
.split-panel,
.table-panel {
  padding: 18px;
}

.section-heading {
  margin-bottom: 16px;
}

.row-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.form-grid,
.expense-form {
  display: grid;
  gap: 12px;
}

.expense-form {
  grid-template-columns: 150px minmax(140px, 1fr) 150px 92px minmax(160px, 1.2fr) minmax(150px, 1fr) 112px;
  align-items: end;
}

.field {
  display: grid;
  gap: 6px;
  min-width: 0;
}

.field span {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.2;
}

.field input {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fafbfc;
  color: var(--ink);
  padding: 0 12px;
  outline: none;
}

.field input[type="file"] {
  display: flex;
  align-items: center;
  padding: 8px 10px;
  line-height: 1.4;
}

.field input:focus {
  border-color: var(--brand);
  background: #fff;
  box-shadow: 0 0 0 3px rgba(15, 139, 109, 0.12);
}

.field input.invalid {
  border-color: var(--danger);
  background: #fff8f7;
}

.field input.invalid:focus {
  border-color: var(--danger);
  box-shadow: 0 0 0 3px rgba(182, 74, 61, 0.12);
}

.field input:disabled {
  color: var(--muted);
  cursor: not-allowed;
}

.field-error {
  display: none;
  margin: 0;
  border: 1px solid var(--rose-line);
  border-radius: var(--radius);
  background: var(--rose-bg);
  color: var(--danger);
  padding: 10px 12px;
  font-size: 14px;
  line-height: 1.45;
}

.field-error.show {
  display: block;
}

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 42px;
  border-radius: var(--radius);
  background: var(--brand);
  color: #fff;
  padding: 0 16px;
  font-weight: 700;
}

.button:hover {
  background: var(--brand-dark);
}

.button.full {
  width: 100%;
}

.formula {
  margin-top: 18px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

.formula strong {
  display: block;
  margin-bottom: 8px;
}

.formula p {
  margin-bottom: 7px;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.45;
}

.member-list,
.balance-list {
  display: grid;
  gap: 10px;
}

.member-chip,
.balance-row {
  display: grid;
  gap: 4px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fafbfc;
}

.member-chip {
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
}

.member-chip span,
.balance-row span {
  color: var(--muted);
  font-size: 13px;
}

.member-chip strong,
.balance-row strong {
  min-width: 0;
  overflow-wrap: anywhere;
}

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

.stat-card {
  display: grid;
  gap: 8px;
  min-height: 88px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
}

.stat-card span {
  color: var(--muted);
  font-size: 13px;
}

.stat-card strong {
  font-size: 24px;
  line-height: 1.1;
  overflow-wrap: anywhere;
}

.stat-card.blue {
  background: var(--blue-bg);
  border-color: var(--blue-line);
}

.stat-card.green {
  background: var(--green-bg);
  border-color: var(--green-line);
}

.stat-card.amber {
  background: var(--amber-bg);
  border-color: var(--amber-line);
}

.stat-card.rose {
  background: var(--rose-bg);
  border-color: var(--rose-line);
}

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fafbfc;
  color: var(--muted);
  padding: 0 10px;
  font-size: 13px;
  white-space: nowrap;
}

.balance-row {
  grid-template-columns: minmax(120px, 1fr) repeat(3, minmax(110px, 0.8fr));
  align-items: center;
}

.balance-row .positive {
  color: var(--brand-dark);
}

.balance-row .negative {
  color: var(--danger);
}

.balance-row .neutral {
  color: var(--muted);
}

.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

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

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

th {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

tbody tr:hover {
  background: #fafbfc;
}

.right {
  text-align: right;
}

.money {
  color: var(--brand-dark);
  font-weight: 700;
}

.muted-text {
  color: var(--muted);
}

.delete-button {
  display: inline-grid;
  place-items: center;
  width: 34px;
  height: 34px;
  margin-left: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  color: var(--danger);
}

.file-link {
  display: inline-flex;
  align-items: center;
  max-width: 160px;
  min-height: 30px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  color: var(--brand-dark);
  padding: 0 10px;
  font-size: 13px;
  font-weight: 700;
  text-decoration: none;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.empty {
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius);
  padding: 18px;
  color: var(--muted);
  text-align: center;
  line-height: 1.5;
}

.toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 10;
  max-width: min(360px, calc(100vw - 36px));
  transform: translateY(16px);
  border-radius: var(--radius);
  background: var(--ink);
  color: #fff;
  padding: 12px 14px;
  opacity: 0;
  pointer-events: none;
  transition: 0.18s ease;
}

.toast.show {
  transform: translateY(0);
  opacity: 1;
}

@media (max-width: 980px) {
  .layout {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: static;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  }

  .expense-form {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .note-field {
    grid-column: 1 / -1;
  }

  .file-field {
    grid-column: 1 / -1;
  }

  .add-button {
    grid-column: 1 / -1;
  }
}

@media (max-width: 720px) {
  .topbar-inner {
    width: 100%;
    padding: 0 12px;
  }

  .layout {
    width: 100%;
    margin-top: 12px;
    padding: 0 8px;
  }

  h1 {
    font-size: 18px;
  }

  .brand-mark {
    width: 38px;
    height: 38px;
  }

  .sidebar {
    grid-template-columns: 1fr;
  }

  .summary-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .expense-form {
    grid-template-columns: 1fr;
  }

  .balance-row {
    grid-template-columns: 1fr 1fr;
  }

  .stat-card {
    min-height: 82px;
    padding: 14px;
  }

  .stat-card strong {
    font-size: 20px;
  }

  .table-wrap {
    overflow: visible;
  }

  table {
    min-width: 0;
  }

  thead {
    display: none;
  }

  tbody,
  tr,
  td {
    display: block;
    width: 100%;
  }

  tbody {
    display: grid;
    gap: 10px;
  }

  tbody tr {
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: #fafbfc;
    padding: 8px 10px;
  }

  td {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    border-bottom: 1px solid var(--line);
    padding: 9px 0;
    text-align: right;
  }

  td:last-child {
    border-bottom: 0;
  }

  td::before {
    content: attr(data-label);
    color: var(--muted);
    font-size: 13px;
    font-weight: 700;
    text-align: left;
  }

  td[data-label="項目"],
  td[data-label="檔案"] {
    align-items: flex-start;
  }

  .delete-button {
    margin-left: 0;
  }
}

@media (max-width: 460px) {
  .topbar-inner {
    align-items: flex-start;
    flex-direction: column;
    gap: 10px;
    padding: 12px 0;
  }

  .top-actions {
    width: 100%;
    flex-wrap: wrap;
  }

  .status-badge {
    flex: 1 1 100%;
    justify-content: center;
  }

  .icon-button {
    width: 44px;
    flex: 0 0 44px;
  }

  .summary-grid {
    grid-template-columns: 1fr;
  }

  .row-heading {
    align-items: flex-start;
    flex-direction: column;
  }

  .balance-row {
    grid-template-columns: 1fr;
  }
}
