/* ============================================================
   Expense & Budget Visualizer — style.css
   Mobile-first, responsive from 320px to 1920px
   ============================================================ */

/* ── Dark Theme ─────────────────────────────────────────────── */
[data-theme="dark"] {
  --color-bg:           #0f1117;
  --color-surface:      #1a1d27;
  --color-border:       #2d3148;
  --color-text:         #e8eaf0;
  --color-text-muted:   #8b92a8;
  --color-primary:      #6366f1;
  --color-primary-dark: #4f46e5;
  --color-error:        #f87171;
  --color-error-bg:     #2d1515;

  /* Category badge colours — adjusted for dark bg contrast */
  --color-food:         #4ade80;
  --color-food-bg:      #052e16;
  --color-transport:    #60a5fa;
  --color-transport-bg: #0c1a3a;
  --color-fun:          #fbbf24;
  --color-fun-bg:       #2d1f00;

  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.5);
}

/* ── Theme Toggle Button ────────────────────────────────────── */
.btn-theme {
  min-width: 44px;
  min-height: 44px;
  padding: var(--space-xs) var(--space-md);
  background-color: rgba(255, 255, 255, 0.15);
  color: #ffffff;
  border: 1.5px solid rgba(255, 255, 255, 0.3);
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease;
  white-space: nowrap;
  flex-shrink: 0;
}

.btn-theme:hover {
  background-color: rgba(255, 255, 255, 0.25);
  border-color: rgba(255, 255, 255, 0.5);
}

.btn-theme:focus-visible {
  outline: 3px solid #ffffff;
  outline-offset: 3px;
}

/* ── Reset & Box Model ─────────────────────────────────────── */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

/* ── Design Tokens ─────────────────────────────────────────── */
:root {
  /* Palette */
  --color-bg:           #f5f7fa;
  --color-surface:      #ffffff;
  --color-border:       #dde1e7;
  --color-text:         #1a1d23;
  --color-text-muted:   #6b7280;
  --color-primary:      #4f46e5;
  --color-primary-dark: #3730a3;
  --color-error:        #dc2626;
  --color-error-bg:     #fef2f2;

  /* Category badge colours */
  --color-food:         #16a34a;   /* green  */
  --color-food-bg:      #dcfce7;
  --color-transport:    #2563eb;   /* blue   */
  --color-transport-bg: #dbeafe;
  --color-fun:          #d97706;   /* amber  */
  --color-fun-bg:       #fef3c7;

  /* Spacing */
  --space-xs:  0.25rem;   /*  4px */
  --space-sm:  0.5rem;    /*  8px */
  --space-md:  1rem;      /* 16px */
  --space-lg:  1.5rem;    /* 24px */
  --space-xl:  2rem;      /* 32px */

  /* Typography */
  --font-base: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --text-sm:   0.875rem;  /* 14px */
  --text-base: 1rem;      /* 16px */
  --text-lg:   1.125rem;  /* 18px */
  --text-xl:   1.25rem;   /* 20px */
  --text-2xl:  1.5rem;    /* 24px */
  --text-3xl:  1.875rem;  /* 30px */

  /* Misc */
  --radius-sm: 0.25rem;
  --radius-md: 0.5rem;
  --radius-lg: 0.75rem;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.08);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.10);

  /* Minimum tap-target size (WCAG 2.5.5 / Req 8.3) */
  --tap-min: 44px;
}

/* ── Base ───────────────────────────────────────────────────── */
html {
  font-size: 100%;          /* respect user browser settings */
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: var(--font-base);
  font-size: var(--text-base);
  line-height: 1.6;
  color: var(--color-text);
  background-color: var(--color-bg);
  min-width: 320px;         /* Req 8.1 — never narrower than 320px */
  overflow-x: hidden;       /* prevent horizontal scroll */
}

/* ── App Wrapper ────────────────────────────────────────────── */
.app-wrapper {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;       /* full viewport height */
  max-width: 1920px;
  margin-inline: auto;
}

/* ── Header / Balance ───────────────────────────────────────── */
.app-header {
  background-color: var(--color-primary);
  color: #ffffff;
  padding: var(--space-lg) var(--space-md);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-sm);
  text-align: center;
}

.app-title {
  font-size: var(--text-xl);
  font-weight: 700;
  letter-spacing: -0.01em;
}

.balance-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-xs);
}

.balance-label {
  font-size: var(--text-sm);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  opacity: 0.85;
}

.balance-amount {
  font-size: var(--text-3xl);
  font-weight: 800;
  line-height: 1.1;
}

/* ── Main Content ───────────────────────────────────────────── */
.app-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: var(--space-lg);
  padding: var(--space-lg) var(--space-md);
  width: 100%;
}

/* ── Section Cards ──────────────────────────────────────────── */
.form-section,
.list-section,
.chart-section {
  background-color: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-lg);
  box-shadow: var(--shadow-sm);
}

.section-heading {
  font-size: var(--text-lg);
  font-weight: 600;
  margin-bottom: var(--space-md);
  color: var(--color-text);
}

/* ── Form ───────────────────────────────────────────────────── */
.form-group {
  display: flex;
  flex-direction: column;
  gap: var(--space-xs);
  margin-bottom: var(--space-md);
}

.form-group label {
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--color-text);
}

/* Inputs & Select — minimum 44px tap target (Req 8.3) */
.form-group input,
.form-group select {
  width: 100%;
  min-height: var(--tap-min);
  padding: var(--space-sm) var(--space-md);
  font-size: var(--text-base);
  font-family: inherit;
  color: var(--color-text);
  background-color: var(--color-surface);
  border: 1.5px solid var(--color-border);
  border-radius: var(--radius-md);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
  appearance: none;
  -webkit-appearance: none;
}

/* Custom select arrow */
.form-group select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%236b7280' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right var(--space-md) center;
  padding-right: calc(var(--space-md) * 2.5);
}

.form-group input:focus,
.form-group select:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.18);
}

.form-group input::placeholder {
  color: var(--color-text-muted);
}

/* Inline field errors */
.field-error {
  font-size: var(--text-sm);
  color: var(--color-error);
  min-height: 1.25em;       /* reserve space to avoid layout shift */
}

.form-group input[aria-invalid="true"],
.form-group select[aria-invalid="true"] {
  border-color: var(--color-error);
  background-color: var(--color-error-bg);
}

/* ── Primary Button — minimum 44px tap target (Req 8.3) ─────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: var(--tap-min);
  min-width: var(--tap-min);
  padding: var(--space-sm) var(--space-lg);
  font-size: var(--text-base);
  font-family: inherit;
  font-weight: 600;
  border: none;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: background-color 0.15s ease, transform 0.1s ease, box-shadow 0.15s ease;
  text-decoration: none;
  white-space: nowrap;
}

.btn-primary {
  width: 100%;
  background-color: var(--color-primary);
  color: #ffffff;
  box-shadow: var(--shadow-sm);
}

.btn-primary:hover {
  background-color: var(--color-primary-dark);
  box-shadow: var(--shadow-md);
}

.btn-primary:active {
  transform: translateY(1px);
  box-shadow: none;
}

.btn-primary:focus-visible {
  outline: 3px solid var(--color-primary);
  outline-offset: 3px;
}

/* ── Transaction List — scrollable (Req 3.2) ────────────────── */
.list-section {
  display: flex;
  flex-direction: column;
}

.transaction-list {
  list-style: none;
  overflow-y: auto;
  max-height: 320px;        /* scrollable when entries overflow */
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  /* Smooth scrolling on iOS */
  -webkit-overflow-scrolling: touch;
}

/* Scrollbar styling (Webkit) */
.transaction-list::-webkit-scrollbar {
  width: 6px;
}
.transaction-list::-webkit-scrollbar-track {
  background: var(--color-bg);
  border-radius: 3px;
}
.transaction-list::-webkit-scrollbar-thumb {
  background: var(--color-border);
  border-radius: 3px;
}
.transaction-list::-webkit-scrollbar-thumb:hover {
  background: var(--color-text-muted);
}

/* Individual transaction item */
.transaction-list li {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: var(--space-sm) var(--space-md);
  background-color: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  flex-wrap: wrap;
}

.transaction-name {
  flex: 1 1 auto;
  font-weight: 500;
  word-break: break-word;
  min-width: 0;
}

.transaction-amount {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  color: var(--color-text);
}

/* ── Category Badges (Req — distinct colors) ────────────────── */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px var(--space-sm);
  font-size: var(--text-sm);
  font-weight: 600;
  border-radius: 9999px;    /* pill shape */
  white-space: nowrap;
  line-height: 1.4;
}

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

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

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

/* ── Delete Button — minimum 44px tap target (Req 8.3) ─────── */
.btn-delete {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: var(--tap-min);
  min-height: var(--tap-min);
  padding: var(--space-xs);
  background: transparent;
  border: none;
  border-radius: var(--radius-sm);
  color: var(--color-text-muted);
  cursor: pointer;
  font-size: var(--text-lg);
  line-height: 1;
  transition: color 0.15s ease, background-color 0.15s ease;
  flex-shrink: 0;
}

.btn-delete:hover {
  color: var(--color-error);
  background-color: var(--color-error-bg);
}

.btn-delete:focus-visible {
  outline: 3px solid var(--color-primary);
  outline-offset: 2px;
}

/* Empty state within list */
.transaction-list:empty::after {
  content: "No transactions yet.";
  display: block;
  text-align: center;
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  padding: var(--space-lg) 0;
}

/* ── Chart Section ──────────────────────────────────────────── */
.chart-container {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 240px;
}

#spending-chart {
  max-width: 100%;
  max-height: 320px;
}

.chart-placeholder {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  padding: var(--space-md);
  pointer-events: none;
}

/* ── Category Section ───────────────────────────────────────── */
.category-section {
  background-color: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-lg);
  box-shadow: var(--shadow-sm);
}

.category-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  margin-top: var(--space-md);
}

.category-item {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: var(--space-sm) var(--space-md);
  background-color: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
}

.category-name {
  flex: 1 1 auto;
  font-weight: 500;
  word-break: break-word;
}

.badge-builtin {
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--color-text-muted);
  background-color: var(--color-border);
  border-radius: 9999px;
  padding: 1px 6px;
  vertical-align: middle;
}

/* ── Spending Limits Section ────────────────────────────────── */
.limits-section {
  background-color: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-lg);
  box-shadow: var(--shadow-sm);
}

.limits-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  margin-top: var(--space-md);
}

.limit-item {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: var(--space-sm) var(--space-md);
  background-color: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
}

.limit-category {
  flex: 1 1 auto;
  font-weight: 500;
}

.limit-amount {
  font-variant-numeric: tabular-nums;
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  white-space: nowrap;
}

.limits-empty {
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  padding: var(--space-sm) 0;
}

/* ── Over-limit Highlighting (Req 4.4, 4.9) ─────────────────── */
/* Uses both color AND text label to meet WCAG — not color alone */
.transaction-item.over-limit {
  background-color: #fef2f2;
  border-color: #fca5a5;
}

.over-limit-row td {
  background-color: #fef2f2;
}

.over-limit-indicator {
  color: #dc2626;
  font-size: var(--text-sm);
  margin-left: var(--space-xs);
}

/* ── Sort Controls ──────────────────────────────────────────── */
.sort-controls {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  margin-bottom: var(--space-md);
  flex-wrap: wrap;
}

.sort-label {
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--color-text-muted);
  white-space: nowrap;
}

.sort-controls select {
  min-height: var(--tap-min);
  padding: var(--space-xs) var(--space-md);
  padding-right: calc(var(--space-md) * 2.5);
  font-size: var(--text-sm);
  font-family: inherit;
  color: var(--color-text);
  background-color: var(--color-surface);
  border: 1.5px solid var(--color-border);
  border-radius: var(--radius-md);
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%236b7280' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right var(--space-sm) center;
  cursor: pointer;
}

.sort-controls select:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.18);
}

/* ── Summary Section ────────────────────────────────────────── */
.summary-section {
  background-color: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-lg);
  box-shadow: var(--shadow-sm);
}

.summary-empty {
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  text-align: center;
  padding: var(--space-lg) 0;
}

.summary-month {
  margin-bottom: var(--space-lg);
}

.summary-month:last-child {
  margin-bottom: 0;
}

.summary-month-heading {
  font-size: var(--text-base);
  font-weight: 600;
  color: var(--color-primary);
  margin-bottom: var(--space-sm);
  padding-bottom: var(--space-xs);
  border-bottom: 1px solid var(--color-border);
}

.summary-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-sm);
}

.summary-table th,
.summary-table td {
  padding: var(--space-xs) var(--space-sm);
  text-align: left;
}

.summary-table th {
  font-weight: 600;
  color: var(--color-text-muted);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.summary-amount {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.summary-grand-total td {
  border-top: 1px solid var(--color-border);
  padding-top: var(--space-sm);
}

/* ── Footer ─────────────────────────────────────────────────── */
.app-footer {
  padding: var(--space-md);
  text-align: center;
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  border-top: 1px solid var(--color-border);
}

/* ============================================================
   Responsive Breakpoints (mobile-first)
   ============================================================ */

/* ── Small phones (375px+) ──────────────────────────────────── */
@media (min-width: 375px) {
  .app-title {
    font-size: var(--text-2xl);
  }

  .balance-amount {
    font-size: var(--text-3xl);
  }
}

/* ── Tablets (640px+) ───────────────────────────────────────── */
@media (min-width: 640px) {
  .app-header {
    flex-direction: row;
    justify-content: space-between;
    text-align: left;
    padding: var(--space-lg) var(--space-xl);
  }

  .balance-section {
    align-items: flex-end;
  }

  .app-main {
    padding: var(--space-xl);
  }

  .transaction-list {
    max-height: 400px;
  }

  .chart-container {
    min-height: 300px;
  }

  #spending-chart {
    max-height: 380px;
  }
}

/* ── Desktop (1024px+) ──────────────────────────────────────── */
@media (min-width: 1024px) {
  .app-main {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: auto auto;
    gap: var(--space-xl);
    align-items: start;
    max-width: 1200px;
    margin-inline: auto;
    width: 100%;
  }

  .form-section {
    grid-column: 1;
    grid-row: 1;
  }

  .list-section {
    grid-column: 1;
    grid-row: 2;
  }

  .chart-section {
    grid-column: 2;
    grid-row: 1 / 3;   /* span both rows */
    align-self: stretch;
  }

  .transaction-list {
    max-height: 360px;
  }

  .chart-container {
    min-height: 400px;
  }

  #spending-chart {
    max-height: 460px;
  }
}

/* ── Wide Desktop (1440px+) ─────────────────────────────────── */
@media (min-width: 1440px) {
  .app-main {
    max-width: 1400px;
    gap: var(--space-xl);
  }

  .app-header {
    padding-inline: calc((100% - 1400px) / 2 + var(--space-xl));
  }
}

/* ── Accessibility: reduced motion ─────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
  }
}
