:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --text: #1d1b16;
  --muted: #6b665c;
  --border: #e3ded3;
  --primary: #c8102e;
  --primary-text: #ffffff;
  --win: #1a7f37;
  --error: #b42318;
  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15140f;
    --surface: #1f1d18;
    --text: #f1eee6;
    --muted: #a39d90;
    --border: #34312a;
    --primary: #e5384f;
    --win: #3fb95f;
    --error: #f97066;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
}

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0;
}
.topbar h1 { font-size: 1.1rem; margin: 0; }

main { max-width: 640px; margin: 0 auto; padding: 16px; }

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

.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-wrap .card { width: 100%; max-width: 380px; }
.login-wrap h1 { margin: 0 0 4px; font-size: 1.4rem; }
.login-wrap p.sub { margin: 0 0 20px; color: var(--muted); }

label { display: block; font-weight: 600; margin: 12px 0 6px; }
input[type=text], input[type=password], input[type=date], select {
  width: 100%;
  padding: 12px;
  font-size: 1rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
}
input:focus, select:focus, button:focus-visible { outline: 2px solid var(--primary); outline-offset: 1px; }

button, .btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 48px;
  padding: 0 18px;
  font-size: 1rem; font-weight: 600;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
}
button.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
button.block { width: 100%; margin-top: 20px; }
button.small { min-height: 36px; padding: 0 12px; font-size: 0.9rem; }
button:disabled { opacity: 0.6; cursor: default; }

.msg { margin-top: 12px; min-height: 1.5em; color: var(--error); }
.muted { color: var(--muted); }

h2 { font-size: 1.1rem; margin: 0 0 4px; }
.msg.ok { color: var(--win); }
.small-note { font-size: 0.9rem; margin: 8px 0 0; }
.topbar-actions { display: flex; gap: 8px; }

/* ---- Giai đoạn 4: danh sách vé, form, điều hướng ---- */
body { padding-bottom: calc(64px + env(safe-area-inset-bottom)); }
.stack > * + * { margin-top: 16px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row > * { flex: 1 1 auto; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
@media (max-width: 380px) { .grid2 { grid-template-columns: 1fr; } }

.bottom-nav {
  position: fixed; left: 0; right: 0; bottom: 0;
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: var(--surface); border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 10;
}
.bottom-nav a {
  text-align: center; padding: 14px 2px; color: var(--muted);
  text-decoration: none; font-size: 0.8rem; font-weight: 600; white-space: nowrap;
}
.bottom-nav a.active { color: var(--primary); }

.big-actions { display: grid; gap: 10px; }
.big-actions .btn { min-height: 60px; font-size: 1.1rem; }
a.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }

.ticket-list { display: flex; flex-direction: column; gap: 8px; }
.ticket-item {
  display: grid; grid-template-columns: auto 1fr; grid-template-rows: auto auto; column-gap: 12px;
  padding: 12px 14px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); color: var(--text); text-decoration: none;
}
.ticket-item .num { grid-row: span 2; font: 700 1.35rem/1.6 ui-monospace, "SF Mono", Menlo, monospace; letter-spacing: 0.06em; align-self: center; }
.ticket-item .meta { font-size: 0.95rem; }
.ticket-item .badge { justify-self: start; }

.badge { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 0.85rem; font-weight: 600; border: 1px solid currentColor; }
.badge.win { color: var(--win); }
.badge.lost { color: var(--muted); }
.badge.pending { color: #b26a00; }
.badge.error { color: var(--error); }
@media (prefers-color-scheme: dark) { .badge.pending { color: #f0a63a; } }

.big-number { font: 700 2.4rem/1.2 ui-monospace, "SF Mono", Menlo, monospace; letter-spacing: 0.12em; text-align: center; margin: 4px 0; }
.center { text-align: center; }
.total { font-size: 1.4rem; font-weight: 700; color: var(--win); }

table.prizes { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
table.prizes th, table.prizes td { padding: 8px 6px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
table.prizes th { width: 3.5em; color: var(--muted); font-weight: 600; }
table.prizes td span { display: inline-block; margin-right: 10px; font-family: ui-monospace, Menlo, monospace; }
table.prizes td span.hit { color: var(--primary); font-weight: 700; text-decoration: underline; }
table.prizes td.amount { text-align: right; white-space: nowrap; }

.notice { padding: 12px 14px; border-radius: 10px; border: 1px solid var(--border); background: var(--bg); }
.notice.warn { border-color: #e0a100; }
.notice.error { border-color: var(--error); color: var(--error); }
input.number-input { font: 700 1.6rem ui-monospace, Menlo, monospace; letter-spacing: 0.2em; text-align: center; }
.check { display: flex; align-items: center; gap: 8px; font-weight: 400; margin-top: 8px; }
.check input { width: 20px; height: 20px; }
.summary { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; text-align: center; }
.summary strong { display: block; font-size: 1.1rem; }
button.danger { color: var(--error); border-color: var(--error); }
.manual-grid label { margin-top: 10px; }
.shrink { flex: 0 0 auto !important; }
.msg { white-space: pre-line; }

/* Ô ngày dd/mm/yyyy + nút lịch */
.date-field { position: relative; display: flex; gap: 6px; }
.date-field .date-text { flex: 1 1 auto; min-width: 0; }
.date-field .date-text.invalid { border-color: var(--error); color: var(--error); }
.date-field .date-btn { flex: 0 0 48px; padding: 0; min-height: 0; font-size: 1.2rem; }
.date-field .date-native { position: absolute; right: 0; bottom: 0; width: 48px; height: 100%; opacity: 0; pointer-events: none; }
[hidden] { display: none !important; }
a.btn.block { display: flex; width: 100%; margin-top: 12px; }

/* Kết quả dò nổi bật + bảng đối chiếu */
.result-banner { margin: 12px auto 4px; padding: 14px 12px; border-radius: 12px; }
.result-banner .label { font-size: 1.6rem; font-weight: 800; letter-spacing: 0.08em; line-height: 1.2; }
.result-banner .amount { font-size: 1.5rem; font-weight: 800; margin-top: 2px; }
.result-banner .names { font-size: 0.95rem; margin-top: 4px; }
.result-banner.win { background: var(--primary); color: #fff; }
.result-banner.lost { border: 2px solid var(--primary); color: var(--primary); }
.result-banner.other { padding: 4px; }

.draw-card { position: relative; }
.your-number {
  position: sticky; top: 61px; z-index: 5;
  display: flex; align-items: baseline; justify-content: space-between; gap: 8px;
  margin: -20px -16px 12px; padding: 10px 16px;
  background: var(--surface); border-bottom: 2px solid var(--primary);
  border-radius: var(--radius) var(--radius) 0 0;
}
.your-number strong { font: 800 1.6rem ui-monospace, Menlo, monospace; letter-spacing: 0.15em; color: var(--primary); }
table.prizes.draw thead th { font-size: 0.8rem; font-weight: 600; color: var(--muted); width: auto; }
table.prizes.draw th:first-child { width: 2.6em; }
table.prizes.draw td.mine, table.prizes.draw th.mine { text-align: right; white-space: nowrap; padding-left: 10px; border-left: 1px dashed var(--border); }
table.prizes.draw td.mine span { margin-right: 0; color: var(--muted); letter-spacing: 0.05em; }
table.prizes.draw tr.row-hit { background: color-mix(in srgb, var(--primary) 10%, transparent); }
table.prizes.draw tr.row-hit th { color: var(--primary); }
table.prizes td span.hit, table.prizes.draw td.mine span.hit { color: var(--primary); font-weight: 800; text-decoration: none; }
.topbar { z-index: 20; }

/* ---- Thống kê ---- */
.viz-root { --series-1: #2a78d6; --grid: #e7e3da; --tick: #6b665c; }
@media (prefers-color-scheme: dark) { .viz-root { --series-1: #3987e5; --grid: #2c2a24; --tick: #a39d90; } }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { min-height: 36px; padding: 0 14px; border-radius: 999px; font-size: 0.9rem; flex: 0 0 auto; }
.chip.active { background: var(--text); color: var(--surface); border-color: var(--text); }
.hero { text-align: center; }
.hero-figure { font-size: clamp(1.6rem, 9vw, 3rem); overflow-wrap: anywhere; font-weight: 800; line-height: 1.15; font-variant-numeric: tabular-nums; }
.hero-figure.pos { color: var(--win); }
.hero-figure.neg { color: var(--primary); }
.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; }
.tile span { display: block; font-size: 0.85rem; }
.tile strong { display: block; font-size: 1.25rem; font-variant-numeric: tabular-nums; margin-top: 2px; }
.chart { position: relative; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .grid { stroke: var(--grid); stroke-width: 1; }
.chart .axis { stroke: var(--tick); stroke-width: 1; }
.chart .tick { fill: var(--tick); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .bar { fill: var(--series-1); }
.chart .bar.active { fill: var(--text); }
.chart .hit { fill: transparent; cursor: pointer; outline: none; }
.chart-tip {
  position: absolute; width: 160px; padding: 8px 10px; font-size: 0.85rem; line-height: 1.4;
  background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 4px 14px rgb(0 0 0 / 0.12); pointer-events: none;
}
.stats-table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; font-size: 0.95rem; }
.stats-table th { text-align: left; font-size: 0.8rem; color: var(--muted); font-weight: 600; padding: 6px 4px; border-bottom: 1px solid var(--border); }
.stats-table td { padding: 10px 4px; border-bottom: 1px solid var(--border); }
.stats-table th:not(:first-child), .stats-table td.num { text-align: right; }
.stats-table td.win { color: var(--primary); font-weight: 700; }
.stats-table tbody tr { cursor: pointer; }
.stats-table tbody tr:hover, .stats-table tbody tr:focus { background: var(--bg); outline: none; }
.link-btn { min-height: 0; padding: 0 4px; border: 0; background: none; color: var(--primary); text-decoration: underline; font-weight: 600; font-size: inherit; }

/* Trúng / Không trúng luôn màu đỏ cho dễ nhìn */
.badge.win { background: var(--primary); border-color: var(--primary); color: #fff; }
.badge.lost { color: var(--primary); border-color: var(--primary); }
.total, #won { color: var(--primary); }

/* ---- Chụp vé ---- */
.file-btn { position: relative; overflow: hidden; }
.file-btn input[type=file] { position: absolute; inset: 0; opacity: 0; width: 100%; height: 100%; cursor: pointer; font-size: 0; }
.big-actions .grid2 .btn { min-height: 52px; font-size: 1rem; }
.scan-preview { display: block; width: 100%; max-height: 50vh; object-fit: contain; border-radius: 10px; background: var(--bg); margin: 8px 0; }
.uncertain, .date-field .date-text.uncertain { border-color: #e0a100 !important; box-shadow: 0 0 0 2px rgb(224 161 0 / 0.25); }
.spinner { width: 36px; height: 36px; margin: 8px auto; border-radius: 50%; border: 4px solid var(--border); border-top-color: var(--primary); animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 3s; } }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.big-actions .grid2 .btn { height: 56px; min-height: 56px; }
.big-actions > .btn { min-height: 64px; font-size: 1.2rem; }
.check-hint { color: #8a5a00; margin: 6px 0 0; } @media (prefers-color-scheme: dark) { .check-hint { color: #f0a63a; } }
label.file-btn { margin: 0; font-weight: 600; }
