:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-muted: #f8fafc;
  --text: #0f172a;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --primary: #0f766e;
  --primary-dark: #0d5c56;
  --primary-soft: #ccfbf1;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --radius-lg: 20px;
  --radius-md: 14px;
  --radius-sm: 10px;
  --shadow-sm: 0 2px 8px rgba(15, 23, 42, 0.05);
  --shadow-md: 0 10px 25px rgba(15, 118, 110, 0.16);
}

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

[hidden] {
  display: none !important;
}

html {
  -webkit-text-size-adjust: 100%;
}

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

.app-shell {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100dvh;
  padding: 16px 16px calc(28px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* Header */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 4px 2px;
}

.app-kicker {
  margin: 0;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--primary);
}

.app-title {
  margin: 2px 0 0;
  font-size: 1.5rem;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.app-main {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* Summary Card */
.summary-card {
  background: linear-gradient(145deg, #0f766e 0%, #115e59 100%);
  color: #ffffff;
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: var(--shadow-md);
}

.summary-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.summary-label {
  font-size: 0.9rem;
  font-weight: 600;
  opacity: 0.9;
}

.summary-badge {
  background: rgba(255, 255, 255, 0.18);
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
}

.summary-amount {
  font-size: 2.2rem;
  font-weight: 800;
  letter-spacing: -0.03em;
  margin: 8px 0 4px;
  word-break: break-word;
}

.summary-sub {
  font-size: 0.84rem;
  opacity: 0.88;
  margin-bottom: 16px;
}

.summary-amount > span {
  display: block;
}

#all-time-total > span {
  display: inline-block;
  margin-inline-start: 0.6em;
}

.period-tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  background: rgba(0, 0, 0, 0.16);
  padding: 4px;
  border-radius: 12px;
}

.period-tab {
  appearance: none;
  border: none;
  background: transparent;
  color: rgba(255, 255, 255, 0.85);
  font-size: 0.85rem;
  font-weight: 600;
  padding: 8px 6px;
  border-radius: 9px;
  cursor: pointer;
  min-height: 36px;
  transition: background-color 0.15s ease, color 0.15s ease;
}

.period-tab.is-active {
  background: #ffffff;
  color: var(--primary-dark);
}

/* Generic Card */
.card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 18px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
}

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

.section-title {
  margin: 0 0 12px;
  font-size: 1.05rem;
  font-weight: 700;
}

.section-header .section-title {
  margin-bottom: 0;
}

/* Form */
.form-fields {
  border: 0;
  padding: 0;
  margin: 0;
  min-width: 0;
}

.field-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 14px;
  min-width: 0;
}

.field-label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text);
}

.optional-tag {
  font-weight: 400;
  color: var(--text-muted);
}

.amount-input-wrap {
  display: flex;
  align-items: center;
  background: var(--surface-muted);
  border: 1.5px solid var(--border);
  border-radius: var(--radius-md);
  padding: 0 14px;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.amount-input-wrap:focus-within {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.14);
  background: #ffffff;
}

#amount-input {
  width: 100%;
  border: none;
  background: transparent;
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--text);
  padding: 12px 0;
  outline: none;
}

#note-input,
input[type="date"],
select {
  width: 100%;
  border: 1.5px solid var(--border);
  background: var(--surface-muted);
  border-radius: var(--radius-sm);
  padding: 11px 12px;
  font-size: 0.95rem;
  font-family: inherit;
  color: var(--text);
  outline: none;
  min-height: 44px;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

#note-input:focus,
input[type="date"]:focus,
select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.14);
  background: #ffffff;
}

.field-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 140px);
  gap: 10px;
}

.amount-row select {
  min-height: 54px;
}

.field-error {
  margin: 0;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--danger);
}

/* Category Selector Grid */
.category-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.category-option {
  position: relative;
}

.category-option input {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: 0;
  opacity: 0;
}

.category-chip {
  appearance: none;
  border: 1.5px solid var(--border);
  background: var(--surface-muted);
  border-radius: var(--radius-md);
  padding: 10px 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  cursor: pointer;
  min-height: 64px;
  font-family: inherit;
  color: var(--text);
  transition: border-color 0.15s ease, background-color 0.15s ease, transform 0.1s ease;
}

.category-chip:active {
  transform: scale(0.97);
}

.category-option input:checked + .category-chip {
  border-color: var(--primary);
  background: var(--primary-soft);
  color: var(--primary-dark);
  font-weight: 700;
}

.category-option input:focus-visible + .category-chip {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

.category-chip-icon {
  font-size: 1.25rem;
  line-height: 1;
}

.category-chip-label {
  font-size: 0.78rem;
  font-weight: 600;
  text-align: center;
}

.btn-primary {
  width: 100%;
  appearance: none;
  border: none;
  background: var(--primary);
  color: #ffffff;
  font-family: inherit;
  font-size: 1rem;
  font-weight: 700;
  padding: 14px 18px;
  border-radius: var(--radius-md);
  cursor: pointer;
  min-height: 48px;
  transition: background-color 0.15s ease, transform 0.1s ease;
}

.btn-primary:hover {
  background: var(--primary-dark);
}

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

/* Breakdown */
.breakdown-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.breakdown-currency {
  margin: 0;
  font-size: 0.9rem;
}

.breakdown-item {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.breakdown-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.86rem;
}

.breakdown-name {
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 600;
}

.breakdown-amount {
  font-weight: 700;
}

.breakdown-track {
  width: 100%;
  height: 7px;
  background: var(--surface-muted);
  border-radius: 999px;
  overflow: hidden;
}

.breakdown-fill {
  height: 100%;
  border-radius: 999px;
  background: var(--primary);
}

/* Filter Bar */
.filter-bar {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 10px;
  margin-bottom: 6px;
  scrollbar-width: none;
}

.filter-bar::-webkit-scrollbar {
  display: none;
}

.filter-pill {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-muted);
  color: var(--text-muted);
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  padding: 7px 12px;
  border-radius: 999px;
  white-space: nowrap;
  cursor: pointer;
  min-height: 34px;
}

.filter-pill.is-active {
  background: var(--primary);
  border-color: var(--primary);
  color: #ffffff;
}

.text-btn {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--primary);
  font-family: inherit;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  padding: 6px;
  min-height: 36px;
}

.feedback {
  margin: 10px 0 0;
  color: var(--text-muted);
  font-size: 0.85rem;
}

.feedback:empty {
  margin: 0;
}

.feedback.is-error {
  color: var(--danger);
}

.backup-actions,
.expense-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 4px;
}

button:disabled {
  opacity: 0.55;
  cursor: default;
}

/* Expense List */
.expense-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.expense-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding: 12px;
  background: var(--surface-muted);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}

.expense-left {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
  flex: 1 1 120px;
}

.expense-icon {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  flex-shrink: 0;
  background: #ffffff;
  border: 1px solid var(--border);
}

.expense-info {
  min-width: 0;
}

.expense-title {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 700;
  overflow-wrap: anywhere;
}

.expense-meta {
  margin: 2px 0 0;
  font-size: 0.78rem;
  color: var(--text-muted);
}

.expense-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 0 1 auto;
  flex-wrap: wrap;
  justify-content: flex-end;
  margin-inline-start: auto;
  min-width: 0;
}

.expense-amount {
  font-size: 0.98rem;
  font-weight: 800;
  color: var(--text);
  overflow-wrap: anywhere;
}

.delete-btn {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--text-muted);
  width: 44px;
  height: 44px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 1.1rem;
}

.delete-btn:hover,
.delete-btn:focus-visible {
  background: var(--danger-soft);
  color: var(--danger);
}

/* Empty State */
.empty-state {
  text-align: center;
  padding: 26px 12px 12px;
}

.empty-icon {
  font-size: 2rem;
  margin-bottom: 6px;
}

.empty-title {
  margin: 0;
  font-weight: 700;
  font-size: 0.98rem;
}

.empty-copy {
  margin: 4px 0 0;
  font-size: 0.85rem;
  color: var(--text-muted);
}

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(18px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  width: calc(100% - 32px);
  max-width: 448px;
  background: #0f172a;
  color: #ffffff;
  padding: 12px 16px;
  border-radius: 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  box-shadow: 0 12px 30px rgba(15, 23, 42, 0.25);
  z-index: 50;
  font-size: 0.9rem;
}

.toast-action {
  appearance: none;
  border: none;
  background: transparent;
  color: #5eead4;
  font-family: inherit;
  font-weight: 700;
  font-size: 0.9rem;
  cursor: pointer;
  padding: 4px 8px;
}

@media (max-width: 360px) {
  .field-row {
    grid-template-columns: 1fr;
  }

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