:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface2: #334155;
  --text: #f1f5f9;
  --muted: #94a3b8;
  --accent: #14b8a6;
  --accent-dim: #0f766e;
  --danger: #f87171;
  --income: #4ade80;
  --income-dim: #166534;
  --radius: 12px;
  --nav-h: 56px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-tap-highlight-color: transparent;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  padding-bottom: calc(var(--nav-h) + var(--safe-bottom) + 8px);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

.app-header {
  padding: 1rem 1.25rem 0.5rem;
  background: linear-gradient(180deg, #134e4a 0%, var(--bg) 100%);
}

.app-header h1 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.login-screen {
  position: fixed;
  inset: 0;
  z-index: 100;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  background: linear-gradient(180deg, #134e4a 0%, var(--bg) 50%);
}

body.authenticated .login-screen,
body.authenticated #view-login {
  display: none !important;
  visibility: hidden;
  pointer-events: none;
}

body.guest #appShell {
  display: none !important;
}

body.authenticated #appShell {
  display: block !important;
}

body.authenticated {
  padding-bottom: calc(var(--nav-h) + var(--safe-bottom) + 8px);
}

body.guest {
  padding-bottom: 0;
}

.login-card {
  width: 100%;
  max-width: 400px;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1.5rem;
  border: 1px solid var(--surface2);
}

.login-card h1 {
  margin: 0 0 0.25rem;
  font-size: 1.75rem;
}

.login-lead {
  margin: 0 0 0.5rem;
  color: var(--muted);
  font-size: 0.9rem;
}

.login-hint {
  margin: 0 0 1.25rem;
  font-size: 0.8rem;
  line-height: 1.4;
  color: var(--muted);
}

.login-hint a {
  color: var(--accent);
}

.login-form .field-label:first-of-type {
  margin-top: 0;
}

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

body.guest .bottom-nav,
body.guest .app-header {
  display: none;
}

.back-link {
  display: inline-block;
  margin-bottom: 0.5rem;
  padding: 0;
  border: none;
  background: none;
  color: var(--accent);
  font-size: 0.9rem;
  cursor: pointer;
}

.view-title {
  margin: 0 0 1rem;
  font-size: 1.25rem;
}

.dash-period {
  margin-bottom: 1.25rem;
}

.dash-heading {
  margin: 0 0 0.5rem;
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  color: var(--muted);
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.35rem;
}

.dash-day {
  text-transform: uppercase;
}

.dash-date {
  font-size: 0.85rem;
  font-weight: 600;
  text-transform: none;
  letter-spacing: 0;
  color: var(--accent);
}

.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
}

.stat-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 0.75rem;
  border: 1px solid var(--surface2);
}

.stat-label {
  display: block;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.stat-value {
  display: block;
  font-size: 1.15rem;
  font-weight: 700;
  margin-top: 0.25rem;
}

.stat-paid .stat-value {
  color: var(--danger);
}

.stat-received .stat-value {
  color: var(--income);
}

.stat-net {
  margin: 0.5rem 0 0;
  font-size: 0.85rem;
  color: var(--muted);
}

.stat-net.net-positive {
  color: var(--income);
}

.stat-net.net-negative {
  color: var(--danger);
}

.direction-toggle {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
}

.direction-btn {
  padding: 0.85rem;
  font-size: 1rem;
  font-weight: 600;
  border: 2px solid var(--surface2);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}

.direction-btn.active[data-direction="paid"] {
  border-color: var(--danger);
  background: rgba(248, 113, 113, 0.12);
  color: var(--danger);
}

.direction-btn.active[data-direction="received"] {
  border-color: var(--income);
  background: rgba(74, 222, 128, 0.12);
  color: var(--income);
}

.dir-badge {
  display: inline-block;
  font-size: 0.65rem;
  font-weight: 700;
  text-transform: uppercase;
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
  margin-right: 0.35rem;
  vertical-align: middle;
}

.dir-paid {
  background: rgba(248, 113, 113, 0.2);
  color: var(--danger);
}

.dir-received {
  background: rgba(74, 222, 128, 0.2);
  color: var(--income);
}

.history-amount.amount-paid {
  color: var(--danger);
}

.history-amount.amount-received {
  color: var(--income);
}

.subtitle {
  margin: 0.25rem 0 0;
  font-size: 0.875rem;
  color: var(--muted);
}

.storage-banner {
  margin: 0.75rem 1rem 0;
  padding: 0.75rem 1rem;
  font-size: 0.85rem;
  line-height: 1.4;
  background: #7f1d1d;
  color: #fecaca;
  border-radius: var(--radius);
  max-width: 480px;
  margin-left: auto;
  margin-right: auto;
}

.storage-banner code {
  font-size: 0.8em;
}

main {
  padding: 0 1rem 1rem;
  max-width: 480px;
  margin: 0 auto;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.field-label {
  display: block;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin: 1rem 0 0.5rem;
}

.optional {
  font-weight: 400;
  text-transform: none;
  letter-spacing: 0;
}

#amount {
  width: 100%;
  font-size: 2.5rem;
  font-weight: 700;
  padding: 0.75rem 1rem;
  border: 2px solid var(--surface2);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  text-align: center;
}

#amount:focus {
  outline: none;
  border-color: var(--accent);
}

input[type="text"],
input[type="password"],
input[type="date"],
input[type="month"],
input[type="number"],
select,
.custom-input,
.login-card input {
  width: 100%;
  padding: 0.75rem 1rem;
  font-size: 1rem;
  line-height: 1.25;
  border: 1px solid var(--surface2);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  -webkit-appearance: none;
  appearance: none;
  box-sizing: border-box;
}

input[type="password"] {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  letter-spacing: 0.02em;
}

input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.chip {
  padding: 0.5rem 0.85rem;
  border-radius: 999px;
  border: 1px solid var(--surface2);
  background: var(--surface);
  color: var(--text);
  font-size: 0.875rem;
  cursor: pointer;
}

.chip.active {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: #fff;
}

.chip-custom {
  border-style: dashed;
}

.hidden {
  display: none !important;
}

.method-toggle {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
}

.method-btn {
  padding: 0.85rem;
  font-size: 1rem;
  font-weight: 600;
  border: 2px solid var(--surface2);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}

.method-btn.active {
  border-color: var(--accent);
  background: var(--accent-dim);
  color: #fff;
}

.btn-primary {
  width: 100%;
  margin-top: 1.5rem;
  padding: 1rem;
  font-size: 1.1rem;
  font-weight: 700;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: #042f2e;
  cursor: pointer;
}

.btn-primary:active {
  transform: scale(0.98);
}

.btn-secondary {
  width: 100%;
  margin-top: 1rem;
  padding: 0.75rem;
  font-size: 0.95rem;
  border: 1px solid var(--surface2);
  border-radius: var(--radius);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}

.toast {
  position: fixed;
  bottom: calc(var(--nav-h) + var(--safe-bottom) + 1rem);
  left: 50%;
  transform: translateX(-50%) translateY(1rem);
  background: var(--accent-dim);
  color: #fff;
  padding: 0.6rem 1.2rem;
  border-radius: 999px;
  font-size: 0.9rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 50;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.toolbar {
  margin-bottom: 1rem;
}

.toolbar-stack {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

input[type="search"] {
  width: 100%;
  padding: 0.65rem 0.85rem;
  font-size: 0.95rem;
  border: 1px solid var(--surface2);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-sizing: border-box;
}

input[type="search"]::placeholder {
  color: var(--muted);
}

.toolbar input[type="month"] {
  width: 100%;
}

.total-card {
  background: linear-gradient(135deg, var(--accent-dim), #115e59);
  border-radius: var(--radius);
  padding: 1.25rem;
  margin-bottom: 1.25rem;
  text-align: center;
}

.total-label {
  display: block;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  opacity: 0.85;
}

.total-amount {
  display: block;
  font-size: 2.25rem;
  font-weight: 800;
  margin: 0.25rem 0;
}

.total-meta {
  font-size: 0.85rem;
  opacity: 0.9;
}

.breakdown-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.breakdown-item {
  margin-bottom: 1rem;
}

.breakdown-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 0.35rem;
}

.breakdown-cat {
  font-weight: 600;
}

.breakdown-amt {
  font-variant-numeric: tabular-nums;
}

.pct {
  font-size: 0.8rem;
  color: var(--muted);
  font-weight: 400;
}

.bar-track {
  height: 6px;
  background: var(--surface2);
  border-radius: 3px;
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 3px;
  transition: width 0.3s ease;
}

.history-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.history-item {
  position: relative;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 0.85rem 4.5rem 0.85rem 1rem;
  margin-bottom: 0.5rem;
}

.history-item--editable {
  cursor: pointer;
}

.history-item--editable:active {
  background: var(--surface2);
}

.history-hint {
  margin: 0.25rem 0 0;
  font-size: 0.75rem;
  color: var(--muted);
  text-align: center;
}

.history-actions {
  position: absolute;
  right: 0.35rem;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.btn-edit,
.btn-delete {
  width: 2rem;
  height: 2rem;
  border: none;
  border-radius: 6px;
  background: transparent;
  font-size: 0.85rem;
  cursor: pointer;
  color: var(--muted);
}

.btn-edit {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--accent);
}

.btn-delete {
  font-size: 1.25rem;
  line-height: 1;
}

.btn-edit:hover {
  background: rgba(20, 184, 166, 0.15);
}

.btn-delete:hover {
  color: var(--danger);
}

.edit-modal {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

body.edit-open .bottom-nav {
  z-index: 50;
}

.edit-modal.hidden {
  display: none !important;
}

.edit-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
}

.edit-sheet {
  position: relative;
  width: 100%;
  max-width: 480px;
  max-height: 92dvh;
  overflow-y: auto;
  background: var(--bg);
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 1rem 1rem 0;
  border-top: 1px solid var(--surface2);
}

.edit-sheet .add-form {
  padding-bottom: 0.5rem;
}

.edit-title {
  margin: 0 0 0.75rem;
  font-size: 1.05rem;
  font-weight: 700;
}

.edit-footer {
  position: sticky;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.65rem 0 calc(0.65rem + var(--safe-bottom));
  margin-top: 0.75rem;
  background: linear-gradient(transparent, var(--bg) 20%);
  border-top: 1px solid var(--surface2);
}

.btn-text {
  padding: 0.5rem 0.25rem;
  border: none;
  background: none;
  color: var(--muted);
  font-size: 0.85rem;
  cursor: pointer;
}

.btn-save-compact {
  padding: 0.55rem 1.25rem;
  min-width: 5.5rem;
  font-size: 0.875rem;
  font-weight: 600;
  border: none;
  border-radius: 999px;
  background: var(--accent);
  color: #042f2e;
  cursor: pointer;
}

.btn-save-compact:disabled {
  opacity: 0.6;
}

.btn-save-compact:active:not(:disabled) {
  transform: scale(0.97);
}

.edit-sheet .field-label {
  margin-top: 0.75rem;
}

.edit-sheet .field-label:first-of-type {
  margin-top: 0;
}

.edit-sheet .chips {
  max-height: 9rem;
  overflow-y: auto;
}

#editAmount {
  width: 100%;
  font-size: 1.5rem;
  font-weight: 700;
  padding: 0.6rem 0.85rem;
  border: 2px solid var(--surface2);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  text-align: center;
  box-sizing: border-box;
}

.history-main {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.history-cat {
  font-weight: 600;
}

.history-amount {
  font-weight: 700;
  color: var(--accent);
}

.history-sub {
  font-size: 0.8rem;
  color: var(--muted);
  margin-top: 0.25rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.history-payee::before {
  content: "· ";
}

.empty-state {
  text-align: center;
  color: var(--muted);
  padding: 2rem 1rem;
}

.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: calc(var(--nav-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--surface2);
  z-index: 40;
}

.nav-btn {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.1rem;
  border: none;
  background: transparent;
  color: var(--muted);
  padding: 0.35rem 0.15rem;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.nav-btn.active {
  color: var(--accent);
}

.nav-btn-logout {
  color: var(--muted);
}

.nav-btn-logout:active {
  color: var(--danger);
}

.nav-icon {
  font-size: 1.15rem;
  line-height: 1;
}

.nav-label {
  font-size: 0.58rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f8fafc;
    --surface: #fff;
    --surface2: #e2e8f0;
    --text: #0f172a;
    --muted: #64748b;
  }

  .app-header {
    background: linear-gradient(180deg, #ccfbf1 0%, var(--bg) 100%);
  }

  .method-btn.active,
  .chip.active {
    color: #fff;
  }
}
