:root {
  --bg: #f4f6f9;
  --card: #ffffff;
  --border: #e6e9ee;
  --text: #14181f;
  --muted: #6b7280;
  --primary: #2454ff;
  --primary-dark: #173aa6;
  --owes-th: #2454ff;
  --owes-ms: #e08a2e;
  --danger: #e5484d;
  --success: #1f9d55;
  --shadow: 0 8px 24px -8px rgba(20, 24, 31, 0.16);
  --shadow-sm: 0 2px 8px -2px rgba(20, 24, 31, 0.12);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --card: #181b22;
    --border: #2a2e38;
    --text: #eceef1;
    --muted: #9aa1ac;
    --owes-ms: #f0a04b;
    --shadow: 0 8px 24px -8px rgba(0, 0, 0, 0.5);
    --shadow-sm: 0 2px 8px -2px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

/* The [hidden] attribute is how app.js shows/hides the login vs. app screen.
   Author rules (e.g. `.login-screen { display: flex }` below) normally beat
   the browser's built-in `[hidden] { display: none }` rule regardless of
   specificity, so without this override a "hidden" element stays visible. */
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

h1, h2 { margin: 0; letter-spacing: -0.01em; }

.screen { width: 100%; }

/* ---------- Login ---------- */

.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.login-card {
  width: 100%;
  max-width: 320px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 32px 24px;
  box-shadow: var(--shadow);
}

.brand { display: flex; flex-direction: column; align-items: center; gap: 6px; margin-bottom: 8px; }
.brand-mark {
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.14em;
  color: var(--primary);
  text-transform: uppercase;
}
.brand-mark.small { font-size: 0.7rem; }
.brand h1 { font-size: 1.3rem; font-weight: 700; }

/* ---------- App shell ---------- */

.app-screen {
  max-width: 480px;
  margin: 0 auto;
  padding: 16px 16px 100px;
  min-height: 100vh;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 4px 20px;
}

.icon-btn {
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--muted);
  width: 38px;
  height: 38px;
  border-radius: 12px;
  font-size: 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
}

/* ---------- Balance hero ---------- */

.balance-hero {
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: #fff;
  border-radius: 20px;
  padding: 28px 24px;
  text-align: center;
  box-shadow: var(--shadow);
  margin-bottom: 24px;
}

.balance-hero.settled {
  background: linear-gradient(135deg, #1f9d55, #147a41);
}

.balance-hero.owes-ms {
  background: linear-gradient(135deg, var(--owes-ms), #b96b1a);
}

.balance-label {
  margin: 0 0 6px;
  font-size: 0.85rem;
  opacity: 0.85;
  font-weight: 500;
}

.balance-amount {
  margin: 0;
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.settle-btn {
  margin-top: 16px;
  background: rgba(255, 255, 255, 0.14);
  border: 1px solid rgba(255, 255, 255, 0.35);
  color: #fff;
  font-size: 0.82rem;
  font-weight: 600;
  padding: 8px 16px;
  border-radius: 999px;
  cursor: pointer;
}
.settle-btn:active { background: rgba(255, 255, 255, 0.24); }

/* ---------- History ---------- */

.history-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; padding: 0 4px; }
.history-header h2 { font-size: 1rem; color: var(--muted); font-weight: 600; }

.tx-list {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}

.tx-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.tx-row:last-child { border-bottom: none; }

.tx-main { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.tx-main strong { font-size: 0.95rem; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx-meta { font-size: 0.78rem; color: var(--muted); }

.tx-side { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.tx-owed { font-weight: 700; font-size: 0.95rem; white-space: nowrap; }
.tx-owed.dir-th { color: var(--owes-th); }
.tx-owed.dir-ms { color: var(--owes-ms); }

.tx-row.voided { opacity: 0.45; }
.tx-row.voided .tx-main strong { text-decoration: line-through; }
.tx-row.voided .void-btn { display: none; }

.tx-row.settlement { background: rgba(31, 157, 85, 0.08); }
.tx-row.settlement .tx-main strong { color: var(--success); }

.void-btn {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 0.72rem;
  padding: 4px 6px;
  cursor: pointer;
  text-decoration: underline;
}
.void-btn:hover { color: var(--danger); }

.empty-state {
  padding: 32px 16px;
  text-align: center;
  color: var(--muted);
  font-size: 0.9rem;
}

/* ---------- FAB ---------- */

.fab {
  position: fixed;
  right: 20px;
  bottom: calc(20px + env(safe-area-inset-bottom));
  width: 58px;
  height: 58px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  font-size: 1.8rem;
  line-height: 1;
  border: none;
  box-shadow: 0 10px 24px -6px rgba(36, 84, 255, 0.55);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.fab:active { transform: scale(0.95); }

/* ---------- Bottom sheet ---------- */

.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(10, 12, 16, 0.45);
  z-index: 20;
}

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 21;
  max-width: 480px;
  margin: 0 auto;
  background: var(--card);
  border-radius: 22px 22px 0 0;
  padding: 10px 20px calc(24px + env(safe-area-inset-bottom));
  box-shadow: 0 -12px 32px -8px rgba(20, 24, 31, 0.25);
  max-height: 88vh;
  overflow-y: auto;
}

.sheet-handle {
  width: 36px;
  height: 4px;
  border-radius: 2px;
  background: var(--border);
  margin: 6px auto 14px;
}

.sheet h2 { font-size: 1.1rem; margin-bottom: 16px; }

/* ---------- Form ---------- */

form { display: flex; flex-direction: column; gap: 14px; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: 0.82rem; color: var(--muted); font-weight: 500; }

input[type="text"],
input[type="number"],
input[type="date"],
input[type="password"] {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  color: var(--text);
  font-size: 1rem;
  width: 100%;
}
input:focus { outline: 2px solid var(--primary); outline-offset: 1px; }

.segmented {
  display: flex;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 3px;
  gap: 3px;
}
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented label {
  flex: 1;
  text-align: center;
  padding: 9px 8px;
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.segmented input:checked + label {
  background: var(--primary);
  color: #fff;
}

.btn {
  border: none;
  border-radius: 12px;
  padding: 14px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:active { background: var(--primary-dark); }

.error { color: var(--danger); font-size: 0.85rem; margin: 0; }
