/* ============================================================
   CSS Custom Properties
   ============================================================ */
:root {
  --color-bg: #f5f7fa;
  --color-surface: #ffffff;
  --color-border: #e2e8f0;
  --color-text: #1a202c;
  --color-text-muted: #718096;
  --color-primary: #4a90e2;
  --color-primary-hover: #357abd;
  --color-error: #e53e3e;
  --color-empty: #a0aec0;

  /* Category badge colours */
  --color-food: #FF6384;
  --color-transport: #36A2EB;
  --color-fun: #FFCE56;

  --radius: 8px;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  --gap: 1rem;
  --max-width: 1440px;
}

/* ============================================================
   Dark Mode
   ============================================================ */
body.dark {
  --color-bg: #0f1117;
  --color-surface: #1a1d27;
  --color-border: #2d3148;
  --color-text: #e2e8f0;
  --color-text-muted: #8892a4;
  --color-primary: #6aa3f5;
  --color-primary-hover: #4a90e2;
  --color-empty: #4a5568;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
}

body.dark input,
body.dark select {
  background: #252836;
  color: var(--color-text);
  border-color: var(--color-border);
}

/* ============================================================
   Reset / Base
   ============================================================ */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

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

body {
  font-family: system-ui, -apple-system, sans-serif;
  background: var(--color-bg);
  color: var(--color-text);
  min-height: 100vh;
  padding: var(--gap);
  transition: background 0.25s, color 0.25s;
}

/* ============================================================
   Layout
   ============================================================ */
header {
  max-width: var(--max-width);
  margin: 0 auto var(--gap);
  background: var(--color-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem 1.5rem;
  text-align: center;
  position: relative;
}

header h1 {
  font-size: clamp(1.1rem, 4vw, 1.75rem);
  margin-bottom: 0.5rem;
}

.balance-label {
  font-size: 0.85rem;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

#balance {
  font-size: clamp(1.5rem, 6vw, 2.5rem);
  font-weight: 700;
  color: var(--color-primary);
}

#balance::before {
  content: '$';
}

main {
  max-width: var(--max-width);
  margin: 0 auto;
  display: grid;
  gap: var(--gap);
  /* Single column on mobile, two columns on wider screens */
  grid-template-columns: 1fr;
}

/* ============================================================
   Card
   ============================================================ */
.card {
  background: var(--color-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem 1.5rem;
  overflow: hidden;
}

.card h2 {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 1rem;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* ============================================================
   Form
   ============================================================ */
.field {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  margin-bottom: 0.875rem;
}

label {
  font-size: 0.875rem;
  font-weight: 500;
}

input,
select {
  width: 100%;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  font-size: 1rem;
  background: var(--color-surface);
  color: var(--color-text);
  transition: border-color 0.15s;
  /* Prevent zoom on iOS */
  font-size: max(16px, 1rem);
}

input:focus,
select:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.2);
}

button[type="submit"] {
  width: 100%;
  padding: 0.625rem 1rem;
  background: var(--color-primary);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
  margin-top: 0.25rem;
}

button[type="submit"]:hover,
button[type="submit"]:focus-visible {
  background: var(--color-primary-hover);
  outline: none;
}

/* ============================================================
   Error messages
   ============================================================ */
.error {
  display: block;
  font-size: 0.8rem;
  color: var(--color-error);
  min-height: 1em;
}

input.invalid,
select.invalid {
  border-color: var(--color-error);
}

/* ============================================================
   Chart
   ============================================================ */
#chart {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 0 auto;
}

/* ============================================================
   Transaction List
   ============================================================ */
#transaction-list {
  list-style: none;
  max-height: 400px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

#transaction-list li {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.625rem 0;
  border-bottom: 1px solid var(--color-border);
  flex-wrap: wrap;
}

#transaction-list li:last-child {
  border-bottom: none;
}

.item-name {
  flex: 1 1 auto;
  font-weight: 500;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.amount {
  font-weight: 600;
  white-space: nowrap;
}

.amount::before {
  content: '$';
}

/* Category badges */
.badge {
  display: inline-block;
  padding: 0.15em 0.55em;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  color: #fff;
  white-space: nowrap;
}

.badge-food {
  background: var(--color-food);
}

.badge-transport {
  background: var(--color-transport);
}

.badge-fun {
  background: var(--color-fun);
  color: #333;
}

/* Delete button */
.delete-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--color-text-muted);
  font-size: 0.9rem;
  padding: 0.2rem 0.4rem;
  border-radius: var(--radius);
  transition: color 0.15s, background 0.15s;
  flex-shrink: 0;
}

.delete-btn:hover,
.delete-btn:focus-visible {
  color: var(--color-error);
  background: rgba(229, 62, 62, 0.08);
  outline: none;
}

/* ============================================================
   Empty state
   ============================================================ */
.empty-state {
  color: var(--color-empty);
  font-style: italic;
  text-align: center;
  padding: 1.5rem 0 !important;
  border-bottom: none !important;
  display: block !important;
  width: 100%;
}

/* Theme toggle button */
#theme-toggle {
  position: absolute;
  top: 1rem;
  right: 1rem;
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: 999px;
  width: 2.25rem;
  height: 2.25rem;
  font-size: 1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, border-color 0.2s, transform 0.15s;
  line-height: 1;
}

#theme-toggle:hover {
  background: var(--color-border);
  transform: scale(1.1);
}

#theme-toggle:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

/* ============================================================
   Responsive — tablet / desktop (≥ 640px)
   ============================================================ */
@media (min-width: 640px) {
  main {
    grid-template-columns: 1fr 1fr;
  }

  /* Transaction list spans full width at the bottom */
  main > section:last-child {
    grid-column: 1 / -1;
  }
}

/* ============================================================
   Responsive — wide desktop (≥ 1024px)
   ============================================================ */
@media (min-width: 1024px) {
  body {
    padding: 1.5rem 2rem;
  }

  main {
    grid-template-columns: 1fr 1fr 2fr;
  }

  main > section:last-child {
    grid-column: auto;
  }
}
