/* ═══════════════════════════════════════════════
   Design System — ผู้ช่วยบัญชี / Budget Buddy
   Modern minimal
   ═══════════════════════════════════════════════ */

/* ── Google Fonts ── */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=Noto+Sans+Thai:wght@400;500;600;700&display=swap');

/* ── Design Tokens ── */
:root {
  /* Colors */
  --bg:        hsl(150, 10%, 97.5%);
  --bg-subtle: hsl(150, 8%, 95%);
  --panel:     hsl(0, 0%, 100%);
  --ink:       hsl(160, 14%, 13%);
  --ink-soft:  hsl(155, 8%, 32%);
  --muted:     hsl(155, 6%, 48%);
  --line:      hsl(150, 10%, 90%);
  --line-soft: hsl(150, 10%, 94%);

  --green:       hsl(152, 42%, 33%);
  --green-light: hsl(148, 32%, 93%);
  --green-soft:  hsl(148, 32%, 93%);
  --green-dark:  hsl(152, 45%, 22%);
  --green-hover: hsl(152, 42%, 27%);

  --red:       hsl(6, 48%, 50%);
  --red-light: hsl(6, 42%, 95%);
  --red-hover: hsl(6, 48%, 42%);

  --gold:       hsl(38, 62%, 48%);
  --gold-light: hsl(40, 50%, 94%);

  --blue:       hsl(212, 45%, 46%);
  --blue-light: hsl(212, 45%, 95%);

  /* Shadows — kept subtle; borders carry most of the separation */
  --shadow-sm:  0 1px 2px hsla(160, 20%, 8%, .04);
  --shadow-md:  0 2px 10px hsla(160, 20%, 8%, .05);
  --shadow-lg:  0 10px 30px hsla(160, 20%, 8%, .09);
  --shadow-xl:  0 20px 50px hsla(160, 20%, 8%, .16);

  /* Radius */
  --r-sm: 8px;
  --r-md: 10px;
  --r-lg: 14px;
  --r-xl: 18px;

  /* Spacing */
  --gap: 14px;

  /* Typography */
  --font: 'Inter', 'Noto Sans Thai', system-ui, -apple-system, sans-serif;
  --text-xs: 11px;
  --text-sm: 13px;
  --text-base: 15px;
  --text-lg: 16px;
  --text-xl: 20px;
  --text-2xl: 26px;
  --text-3xl: 34px;

  /* Transitions */
  --ease: cubic-bezier(.22, .68, 0, 1.0);
  --dur: 150ms;
}

/* ── Reset ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: var(--text-base);
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
button, input, select, textarea { font: inherit; color: inherit; }
img, svg { display: block; max-width: 100%; }

/* ── Utility ── */
.hidden   { display: none !important; }
.sr-only  { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); border: 0; }

/* ── Scrollbar ── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--line); border-radius: 10px; }
::-webkit-scrollbar-thumb:hover { background: var(--muted); }

/* ════════════════════════════════════════════
   LOGIN PAGE
   ════════════════════════════════════════════ */
.login-page {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
  background: var(--bg);
}

.login-card {
  width: min(400px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--r-xl);
  padding: 34px 28px 28px;
  box-shadow: var(--shadow-md);
  text-align: center;
  animation: fadeUp .35s var(--ease);
}

.login-card .mascot {
  font-size: 44px;
  line-height: 1;
  margin-bottom: 10px;
}

.login-card h1 {
  font-size: var(--text-xl);
  font-weight: 700;
  letter-spacing: -.2px;
  margin-bottom: 4px;
}

.login-card .subtitle {
  color: var(--muted);
  font-size: var(--text-sm);
  margin-bottom: 22px;
}

.pin-input {
  width: 100%;
  padding: 13px 16px;
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  text-align: center;
  font-size: var(--text-lg);
  font-weight: 600;
  letter-spacing: 6px;
  outline: none;
  background: var(--panel);
  transition: border-color var(--dur), box-shadow var(--dur);
}
.pin-input:focus {
  border-color: var(--green);
  box-shadow: 0 0 0 3px var(--green-light);
}
.pin-input::placeholder { letter-spacing: 4px; font-weight: 400; color: var(--muted); }
.login-card .pin-input + .pin-input { margin-top: 10px; }

.account-badge { padding: 6px 12px; border-radius: 999px; background: var(--green-soft); color: var(--green-dark); font-size: var(--text-xs); font-weight: 600; }
.users-modal { width: min(760px, 92vw); }
.user-create-form { display: grid; grid-template-columns: 1fr 1fr 1fr auto; gap: 10px; margin: 18px 0; }
.user-actions { display: flex; gap: 6px; flex-wrap: wrap; }
@media (max-width: 720px) { .user-create-form { grid-template-columns: 1fr; } .account-badge { display: none; } }

.error-text {
  color: var(--red);
  font-size: var(--text-sm);
  min-height: 22px;
  margin-top: 8px;
}

.login-lang {
  position: absolute;
  top: 18px;
  right: 18px;
}

/* ── Buttons ── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: none;
  border-radius: var(--r-md);
  padding: 11px 18px;
  font-weight: 600;
  font-size: var(--text-sm);
  cursor: pointer;
  transition: background var(--dur) var(--ease), border-color var(--dur) var(--ease), color var(--dur) var(--ease);
  white-space: nowrap;
  user-select: none;
}
.btn:active { opacity: .85; }

.btn-primary {
  background: var(--green);
  color: #fff;
}
.btn-primary:hover { background: var(--green-hover); }

.btn-ghost {
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--ink);
}
.btn-ghost:hover { background: var(--bg-subtle); border-color: var(--muted); }

.btn-danger {
  background: transparent;
  color: var(--red);
  border: 1px solid var(--line);
}
.btn-danger:hover { background: var(--red-light); border-color: var(--red); }

.btn-icon {
  padding: 6px 10px;
  border-radius: var(--r-sm);
  font-size: var(--text-xs);
  font-weight: 600;
}

.btn-lg { width: 100%; padding: 13px; margin-top: 12px; font-size: var(--text-base); }

.btn-fab {
  position: fixed;
  right: 22px;
  bottom: 22px;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: var(--green);
  color: #fff;
  font-size: 26px;
  font-weight: 300;
  border: none;
  box-shadow: var(--shadow-lg);
  cursor: pointer;
  transition: background var(--dur) var(--ease), transform var(--dur) var(--ease);
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
}
.btn-fab:hover { background: var(--green-hover); transform: scale(1.05); }
.btn-fab:active { transform: scale(.95); }

/* ════════════════════════════════════════════
   DASHBOARD LAYOUT
   ════════════════════════════════════════════ */
.app {
  max-width: 1320px;
  margin: 0 auto;
  padding: 20px 24px 80px;
  animation: fadeUp .3s var(--ease);
}

/* ── Top Bar ── */
.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 20px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-icon {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  background: var(--green-light);
  border-radius: var(--r-md);
  font-size: 24px;
}

.brand h1 {
  font-size: var(--text-xl);
  font-weight: 700;
  letter-spacing: -.2px;
}
.brand small {
  display: block;
  font-size: var(--text-xs);
  font-weight: 400;
  color: var(--muted);
  margin-top: 1px;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.lang-toggle {
  padding: 7px 12px;
  border-radius: var(--r-sm);
  border: 1px solid var(--line);
  background: var(--panel);
  font-size: var(--text-sm);
  font-weight: 600;
  cursor: pointer;
  transition: background var(--dur), border-color var(--dur);
}
.lang-toggle:hover { background: var(--bg-subtle); border-color: var(--muted); }

/* ── Filters ── */
.filters-bar {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  padding: 14px;
  display: grid;
  grid-template-columns: repeat(5, 1fr) auto;
  gap: 10px;
  margin-bottom: 16px;
}

.field { display: grid; gap: 5px; }
.field-label {
  font-size: var(--text-xs);
  font-weight: 500;
  color: var(--muted);
  letter-spacing: .2px;
}

.input, .select {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  padding: 9px 11px;
  background: var(--panel);
  color: var(--ink);
  outline: none;
  font-size: var(--text-sm);
  transition: border-color var(--dur), box-shadow var(--dur);
}
.input:focus, .select:focus {
  border-color: var(--green);
  box-shadow: 0 0 0 3px var(--green-light);
}

.filters-bar .btn { align-self: end; }

/* ── Stats Cards ──
   Minimal treatment: flat white cards, a thin colored top rule for quick
   scanning instead of large colored numbers, numbers stay near-ink and
   use tabular figures so amounts align at a glance. */
.stats-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  margin-bottom: 16px;
}

.stat-card {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  padding: 18px 20px;
  overflow: hidden;
}
.stat-card::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 3px;
  background: var(--line);
}
.stat-card:nth-child(1)::before { background: var(--green); }
.stat-card:nth-child(2)::before { background: var(--red); }
.stat-card:nth-child(3)::before { background: var(--gold); }

.stat-card .stat-label {
  font-size: var(--text-xs);
  font-weight: 500;
  color: var(--muted);
  letter-spacing: .3px;
  text-transform: uppercase;
}
.stat-card .stat-value {
  display: block;
  font-size: var(--text-2xl);
  font-weight: 700;
  margin-top: 8px;
  letter-spacing: -.3px;
  line-height: 1.15;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
}
.stat-card .stat-value.income  { color: var(--green); }
.stat-card .stat-value.expense { color: var(--red); }

/* ── Charts Grid ── */
.charts-grid {
  display: grid;
  grid-template-columns: 1.6fr 1fr;
  gap: var(--gap);
  margin-bottom: var(--gap);
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  padding: 20px;
}

.panel-title {
  font-size: var(--text-base);
  font-weight: 600;
  margin-bottom: 18px;
}

/* ── Bar Chart ── */
.chart-area {
  height: 230px;
  display: flex;
  align-items: flex-end;
  gap: 5px;
  border-bottom: 1px solid var(--line-soft);
  padding-top: 24px;
}

.bar-group {
  flex: 1;
  min-width: 4px;
  height: 100%;
  display: flex;
  align-items: flex-end;
  gap: 2px;
  position: relative;
  cursor: default;
}

.bar {
  flex: 1;
  border-radius: 3px 3px 0 0;
  min-height: 1px;
  transition: opacity var(--dur);
}
.bar.in  { background: var(--green); }
.bar.out { background: var(--red); }

.bar-group:hover .bar { opacity: .75; }
.bar-group:hover::after {
  content: attr(data-tip);
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  white-space: nowrap;
  background: var(--ink);
  color: #fff;
  padding: 6px 10px;
  border-radius: var(--r-sm);
  font-size: var(--text-xs);
  z-index: 5;
  pointer-events: none;
}

.chart-legend {
  display: flex;
  gap: 18px;
  margin-top: 14px;
  font-size: var(--text-xs);
  color: var(--muted);
}
.chart-legend .dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 5px;
  vertical-align: middle;
}

/* ── Category Breakdown ── */
.cat-list { display: grid; gap: 14px; }
.cat-item .cat-header {
  display: flex;
  justify-content: space-between;
  font-size: var(--text-sm);
  margin-bottom: 6px;
}
.cat-item .cat-header b { font-weight: 600; font-variant-numeric: tabular-nums; }
.cat-track {
  height: 6px;
  background: var(--line-soft);
  border-radius: 10px;
  overflow: hidden;
}
.cat-fill {
  height: 100%;
  background: var(--gold);
  border-radius: 10px;
  transition: width .5s var(--ease);
}

/* ── Transaction Table ── */
.table-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  overflow: hidden;
}

.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 18px;
  border-bottom: 1px solid var(--line-soft);
}
.table-header h2 { font-size: var(--text-base); font-weight: 600; margin: 0; }
.table-meta {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: var(--text-xs);
  color: var(--muted);
}

.table-scroll {
  overflow-x: auto;
  max-height: 540px;
  overflow-y: auto;
}

table { width: 100%; border-collapse: collapse; min-width: 900px; }
th, td {
  text-align: left;
  padding: 11px 16px;
  border-bottom: 1px solid var(--line-soft);
  font-size: var(--text-sm);
}
th {
  position: sticky;
  top: 0;
  background: var(--panel);
  color: var(--muted);
  font-size: var(--text-xs);
  font-weight: 600;
  letter-spacing: .3px;
  text-transform: uppercase;
  z-index: 2;
}
tr { transition: background var(--dur); }
tr:hover { background: var(--bg-subtle); }
td.income, td.expense { font-variant-numeric: tabular-nums; font-weight: 600; }
td.income  { color: var(--green); }
td.expense { color: var(--red); }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 9px 3px 7px;
  border-radius: 20px;
  font-size: var(--text-xs);
  font-weight: 600;
  letter-spacing: .1px;
}
.badge::before {
  content: '';
  width: 6px;
  height: 6px;
  border-radius: 50%;
}
.badge.income  { background: var(--green-light); color: var(--green-dark); }
.badge.income::before  { background: var(--green); }
.badge.expense { background: var(--red-light);   color: var(--red-hover);   }
.badge.expense::before { background: var(--red); }

.row-actions { display: flex; gap: 5px; }

.empty-row {
  text-align: center;
  color: var(--muted);
  padding: 40px;
  font-size: var(--text-sm);
}

/* ════════════════════════════════════════════
   DIALOGS
   ════════════════════════════════════════════ */
dialog {
  border: none;
  border-radius: var(--r-xl);
  padding: 0;
  width: min(520px, calc(100% - 28px));
  box-shadow: var(--shadow-xl);
  animation: fadeUp .25s var(--ease);
}
dialog::backdrop {
  background: hsla(160, 25%, 8%, .35);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
}

.modal-body { padding: 24px; }
.modal-body h2 {
  font-size: var(--text-lg);
  font-weight: 600;
  margin-bottom: 18px;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}
.form-grid .wide { grid-column: 1 / -1; }

/* ── Chip picker (project select in Add/Edit dialogs) ──
   Existing projects render as chips so picking one is a single tap;
   an "other" chip reveals a text field for a brand-new project name. */
.chip-group {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.chip {
  padding: 7px 14px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink-soft);
  font-size: var(--text-sm);
  font-weight: 500;
  cursor: pointer;
  transition: background var(--dur), border-color var(--dur), color var(--dur);
}
.chip:hover { background: var(--bg-subtle); border-color: var(--muted); }
.chip.active {
  background: var(--green);
  border-color: var(--green);
  color: #fff;
  font-weight: 600;
}
.chip-other.active { background: var(--ink); border-color: var(--ink); }
.chip-other-input { margin-top: 10px; }

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 20px;
}

/* ── Toast ── */
.toast {
  position: fixed;
  right: 20px;
  bottom: 20px;
  background: var(--ink);
  color: #fff;
  padding: 12px 18px;
  border-radius: var(--r-md);
  font-size: var(--text-sm);
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  z-index: 100;
  animation: slideIn .25s var(--ease);
}
.toast.error { background: hsl(6, 50%, 40%); }

/* ── Loading Overlay ── */
.loading-overlay {
  position: fixed;
  inset: 0;
  background: hsla(150, 10%, 97%, .7);
  backdrop-filter: blur(2px);
  display: grid;
  place-items: center;
  z-index: 50;
}
.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--line);
  border-top-color: var(--green);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}

/* ════════════════════════════════════════════
   ANIMATIONS
   ════════════════════════════════════════════ */
@keyframes fadeUp {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}
@keyframes slideIn {
  from { opacity: 0; transform: translateX(20px); }
  to   { opacity: 1; transform: translateX(0); }
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ════════════════════════════════════════════
   RESPONSIVE
   ════════════════════════════════════════════ */
@media (max-width: 960px) {
  .filters-bar { grid-template-columns: repeat(2, 1fr); }
  .filters-bar .btn { grid-column: 1 / -1; }
  .stats-row { grid-template-columns: repeat(2, 1fr); }
  .charts-grid { grid-template-columns: 1fr; }
}

@media (max-width: 600px) {
  .app { padding: 14px 14px 80px; }
  .top-bar { align-items: flex-start; }
  .brand small { display: none; }
  .filters-bar {
    grid-template-columns: 1fr;
    padding: 14px;
  }
  .filters-bar .btn { grid-column: auto; }
  .stats-row { grid-template-columns: 1fr 1fr; }
  .stat-card { padding: 14px 16px; }
  .stat-card .stat-value { font-size: var(--text-xl); }
  .form-grid { grid-template-columns: 1fr; }
  .form-grid .wide { grid-column: auto; }
  .btn-fab { right: 16px; bottom: 16px; }
}
