/* ============ Design tokens ============ */
:root {
  --bg: #f2f5f4;
  --bg-accent: radial-gradient(1200px 600px at 80% -10%, #d3f1e4 0%, transparent 60%),
               radial-gradient(900px 500px at -10% 110%, #d8ecf7 0%, transparent 55%);
  --surface: #ffffff;
  --surface-2: #f6f8f7;
  --text: #172420;
  --text-dim: #5d6b66;
  --border: #e2e8e5;
  --primary: #0b8f6a;
  --primary-strong: #087657;
  --primary-soft: #e0f4ec;
  --income: #0b8f6a;
  --expense: #d9414e;
  --danger-soft: #fdecee;
  --warn-soft: #fdf3d8;
  --warn-text: #7a5b0a;
  --ring: rgba(11, 143, 106, 0.25);
  --shadow: 0 10px 30px rgba(23, 36, 32, 0.08);
  --radius: 16px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1513;
    --bg-accent: radial-gradient(1200px 600px at 80% -10%, #123527 0%, transparent 60%),
                 radial-gradient(900px 500px at -10% 110%, #0f2733 0%, transparent 55%);
    --surface: #16201d;
    --surface-2: #1b2622;
    --text: #e6efec;
    --text-dim: #93a49e;
    --border: #26332e;
    --primary: #2fbd8f;
    --primary-strong: #48d3a6;
    --primary-soft: #143627;
    --income: #3ecf9a;
    --expense: #ff7a85;
    --danger-soft: #3a1d20;
    --warn-soft: #3a2f12;
    --warn-text: #ecc45c;
    --ring: rgba(47, 189, 143, 0.35);
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
}

body {
  font-family: 'Noto Sans Thai', system-ui, -apple-system, sans-serif;
  background: var(--bg-accent), var(--bg);
  background-attachment: fixed;
  color: var(--text);
  line-height: 1.55;
}

/* ============ ฟอร์มพื้นฐาน ============ */
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 14px;
}

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

.field input,
.field select {
  font: inherit;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 11px 14px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.field input:focus,
.field select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--ring);
}

.btn-primary {
  width: 100%;
  font: inherit;
  font-weight: 700;
  color: #fff;
  background: linear-gradient(135deg, var(--primary), var(--primary-strong));
  border: none;
  border-radius: 12px;
  padding: 13px 16px;
  cursor: pointer;
  transition: filter 0.15s, transform 0.05s;
}

.btn-primary:hover { filter: brightness(1.06); }
.btn-primary:active { transform: scale(0.99); }
.btn-primary:disabled { opacity: 0.6; cursor: wait; }

.btn-ghost {
  font: inherit;
  font-size: 0.85rem;
  color: var(--text-dim);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 6px 12px;
  cursor: pointer;
}

.btn-ghost:hover { color: var(--text); border-color: var(--text-dim); }

.form-error {
  background: var(--danger-soft);
  color: var(--expense);
  font-size: 0.88rem;
  border-radius: 10px;
  padding: 10px 14px;
  margin: 0 0 14px;
}

/* ============ หน้าเข้าสู่ระบบ ============ */
.login-wrap {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}

.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 32px 28px 28px;
}

.brand { text-align: center; margin-bottom: 22px; }

.brand-mark {
  width: 64px;
  height: 64px;
  margin: 0 auto 12px;
  display: grid;
  place-items: center;
  font-size: 30px;
  border-radius: 18px;
  background: linear-gradient(135deg, var(--primary), var(--primary-strong));
  box-shadow: 0 8px 20px var(--ring);
}

.brand h1 {
  font-size: 1.35rem;
  font-weight: 700;
  margin: 0 0 4px;
}

.brand-sub {
  color: var(--text-dim);
  font-size: 0.88rem;
  margin: 0;
}

.demo-banner {
  background: var(--warn-soft);
  color: var(--warn-text);
  font-size: 0.85rem;
  text-align: center;
  border-radius: 10px;
  padding: 10px 14px;
  margin-bottom: 16px;
}

/* ============ แถบบนหน้าแอป ============ */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}

.topbar-inner {
  max-width: 680px;
  margin: 0 auto;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
}

.topbar-mark { font-size: 1.2rem; }

.topbar-user {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.9rem;
  color: var(--text-dim);
}

/* ============ เนื้อหาแอป ============ */
.container {
  max-width: 680px;
  margin: 0 auto;
  padding: 20px 16px 48px;
}

.summary {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 18px;
}

.tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.tile-label { font-size: 0.78rem; color: var(--text-dim); }

.tile-value {
  font-size: clamp(1rem, 3.5vw, 1.3rem);
  font-weight: 700;
  overflow-wrap: anywhere;
}

.tile-income .tile-value { color: var(--income); }
.tile-expense .tile-value { color: var(--expense); }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  margin-bottom: 18px;
}

.card-title {
  font-size: 1rem;
  font-weight: 700;
  margin: 0 0 14px;
}

/* ปุ่มเลือก รายรับ/รายจ่าย */
.segment {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 5px;
  margin-bottom: 16px;
}

.segment input { display: none; }

.segment span {
  display: block;
  text-align: center;
  font-weight: 600;
  font-size: 0.92rem;
  color: var(--text-dim);
  border-radius: 9px;
  padding: 8px 0;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.segment input[value="expense"]:checked + span {
  background: var(--danger-soft);
  color: var(--expense);
}

.segment input[value="income"]:checked + span {
  background: var(--primary-soft);
  color: var(--income);
}

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

@media (max-width: 480px) {
  .form-grid { grid-template-columns: 1fr; }
}

/* ============ รายการ ============ */
.record-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.record {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 2px;
  border-bottom: 1px solid var(--border);
}

.record:last-child { border-bottom: none; }

.record-icon {
  width: 40px;
  height: 40px;
  flex: none;
  display: grid;
  place-items: center;
  font-size: 1.1rem;
  border-radius: 12px;
  background: var(--surface-2);
}

.record-main { flex: 1; min-width: 0; }

.record-cat { font-weight: 600; font-size: 0.95rem; }

.record-meta {
  font-size: 0.8rem;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

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

.record-amount.income { color: var(--income); }
.record-amount.expense { color: var(--expense); }

.empty {
  color: var(--text-dim);
  text-align: center;
  padding: 18px 0;
  margin: 0;
}

.loading { opacity: 0.55; pointer-events: none; }
