:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --text: #1d2330;
  --muted: #6b7280;
  --border: #e3e5ea;
  --accent: #2f6fed;
  --accent-soft: #e8efff;
  --paid: #1f9d55;
  --paid-soft: #e3f6ea;
  --pending: #b7791f;
  --pending-soft: #fdf3e1;
  --danger: #d64545;
  --radius: 14px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151b;
    --card: #1b1f27;
    --text: #e8eaf0;
    --muted: #9aa1ad;
    --border: #2c323d;
    --accent: #6b9bff;
    --accent-soft: #1f2a44;
    --paid: #4cc38a;
    --paid-soft: #17301f;
    --pending: #e0a948;
    --pending-soft: #33291a;
    --danger: #f07070;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", "Noto Sans Thai", sans-serif;
}

.wrap { max-width: 640px; margin: 0 auto; padding: 24px 16px 120px; }
.wrap.wide { max-width: 1100px; }

header h1 { margin: 0 0 4px; font-size: 1.6rem; }
header p { margin: 0 0 20px; color: var(--muted); }

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

.card h2 {
  margin: 0 0 12px;
  font-size: 1.05rem;
  display: flex;
  align-items: center;
  gap: 10px;
}

.step {
  display: inline-grid;
  place-items: center;
  width: 26px; height: 26px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 0.85rem;
  flex-shrink: 0;
}

select, input[type="text"], input[type="password"], textarea {
  width: 100%;
  padding: 12px;
  font: inherit;
  color: inherit;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
}
textarea { resize: vertical; min-height: 60px; }

.hint { color: var(--muted); font-size: 0.9rem; margin: 8px 0 0; }

/* Month picker */
.year { margin: 14px 0 6px; font-weight: 600; color: var(--muted); font-size: 0.9rem; }
.year:first-child { margin-top: 0; }
.months { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
@media (max-width: 420px) { .months { grid-template-columns: repeat(3, 1fr); } }

.month {
  position: relative;
  padding: 10px 4px;
  text-align: center;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  cursor: pointer;
  user-select: none;
  font-weight: 500;
  font-size: 0.95rem;
}
.month small { display: block; font-size: 0.7rem; font-weight: 400; color: var(--muted); }
.month input { position: absolute; opacity: 0; pointer-events: none; }
.month:hover { border-color: var(--accent); }
.month.selected { background: var(--accent); border-color: var(--accent); color: #fff; }
.month.selected small { color: #fff; }
.month.paid { background: var(--paid-soft); border-color: transparent; color: var(--paid); cursor: not-allowed; }
.month.pending { background: var(--pending-soft); border-color: transparent; color: var(--pending); cursor: not-allowed; }
.month.paid small, .month.pending small { color: inherit; }
.month.current:not(.selected):not(.paid):not(.pending) { border-color: var(--accent); }

.legend { display: flex; flex-wrap: wrap; gap: 14px; margin-top: 12px; font-size: 0.82rem; color: var(--muted); }
.legend span::before {
  content: ""; display: inline-block; width: 10px; height: 10px; border-radius: 3px;
  margin-right: 6px; vertical-align: -1px; background: var(--c);
}

.quick { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }

button {
  font: inherit;
  cursor: pointer;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  padding: 8px 14px;
}
button:hover { border-color: var(--accent); }
button.primary {
  width: 100%;
  padding: 14px;
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
  font-size: 1.05rem;
}
button.primary:disabled { opacity: 0.45; cursor: not-allowed; }
button.small { padding: 6px 10px; font-size: 0.85rem; }

/* QR */
.qr-box { text-align: center; }
.qr-box img {
  width: 100%; max-width: 280px;
  border-radius: 12px; background: #fff; padding: 10px;
  border: 1px solid var(--border);
}
.qr-missing {
  max-width: 280px; margin: 0 auto; padding: 40px 16px;
  border: 2px dashed var(--border); border-radius: 12px; color: var(--muted);
}
.qr-amount { font-size: 2rem; font-weight: 700; margin: 12px 0 0; }
.pay-to { color: var(--muted); margin: 2px 0 0; }

/* Upload */
.drop {
  display: block;
  border: 2px dashed var(--border);
  border-radius: 12px;
  padding: 24px;
  text-align: center;
  cursor: pointer;
  color: var(--muted);
}
.drop:hover, .drop.over { border-color: var(--accent); color: var(--accent); }
.drop input { display: none; }
.preview { margin-top: 12px; text-align: center; }
.preview img { max-width: 100%; max-height: 360px; border-radius: 10px; border: 1px solid var(--border); }

/* Sticky total */
.total-bar {
  position: fixed; left: 0; right: 0; bottom: 0;
  background: var(--card);
  border-top: 1px solid var(--border);
  padding: 12px 16px;
}
.total-bar .inner {
  max-width: 640px; margin: 0 auto;
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
}
.total-bar .label { color: var(--muted); font-size: 0.85rem; }
.total-bar .amount { font-size: 1.4rem; font-weight: 700; }

.error { color: var(--danger); margin: 10px 0 0; min-height: 1.2em; }

.success { text-align: center; padding: 32px 20px; }
.success .big { font-size: 3rem; }

/* Admin */
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 16px; }
.toolbar select { width: auto; }

.table-scroll { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 0.9rem; }
th, td { border-bottom: 1px solid var(--border); padding: 8px; text-align: left; white-space: nowrap; }
th { color: var(--muted); font-weight: 600; }
.grid td.cell { text-align: center; font-size: 0.8rem; }
.grid td.paid { background: var(--paid-soft); color: var(--paid); }
.grid td.pending { background: var(--pending-soft); color: var(--pending); }
.grid th:first-child, .grid td:first-child { position: sticky; left: 0; background: var(--card); }

.payments { display: grid; gap: 12px; }
.payment {
  display: grid; grid-template-columns: 120px 1fr; gap: 16px;
  border: 1px solid var(--border); border-radius: 12px; padding: 12px;
}
@media (max-width: 520px) { .payment { grid-template-columns: 1fr; } }
.payment .thumb {
  width: 100%; height: 160px; object-fit: cover; border-radius: 8px;
  background: var(--bg); cursor: zoom-in; border: 1px solid var(--border);
}
.payment .meta { display: flex; flex-direction: column; gap: 4px; }
.payment .who { font-weight: 600; font-size: 1.05rem; }
.payment .actions { display: flex; gap: 8px; margin-top: auto; flex-wrap: wrap; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 0.78rem; font-weight: 600; }
.badge.pending { background: var(--pending-soft); color: var(--pending); }
.badge.approved { background: var(--paid-soft); color: var(--paid); }
.badge.rejected { background: transparent; color: var(--danger); border: 1px solid var(--danger); }

.lightbox {
  position: fixed; inset: 0; background: rgba(0,0,0,.8);
  display: none; place-items: center; padding: 16px; cursor: zoom-out; z-index: 10;
}
.lightbox.open { display: grid; }
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 8px; }
