:root {
  color-scheme: light;
  --bg: #fbf7f0;
  --surface-1: #ffffff;
  --surface-2: #f4efe6;
  --line: #e6ddcf;
  --text-primary: #1c1a17;
  --text-secondary: #5a554c;
  --text-muted: #8a8377;
  --accent: #eda100;
  --good: #1baf7a;
  --bad: #e34948;
  --shadow: 0 1px 2px rgba(28,26,23,.06), 0 8px 24px rgba(28,26,23,.06);
  --radius: 16px;
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --bg: #14140f;
    --surface-1: #1f1e1a;
    --surface-2: #292822;
    --line: #38362e;
    --text-primary: #f7f4ec;
    --text-secondary: #c3bdae;
    --text-muted: #918b7d;
    --accent: #f0b73c;
    --good: #199e70;
    --bad: #e66767;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 10px 28px rgba(0,0,0,.35);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text-primary);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
  padding-bottom: 48px;
}
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }
button { font: inherit; color: inherit; cursor: pointer; }
input, select, textarea { font: inherit; color: inherit; }

.wrap { max-width: 960px; margin: 0 auto; padding: 0 16px; }

header.top {
  position: sticky; top: 0; z-index: 20;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.top-row { display: flex; align-items: center; gap: 12px; padding: 12px 0; }
.brand { display: flex; align-items: center; gap: 8px; font-size: 18px; font-weight: 700; }
.brand .logo { font-size: 22px; }
.spacer { flex: 1; }
.tabs { display: flex; gap: 4px; overflow-x: auto; padding-bottom: 10px; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tab {
  border: 1px solid transparent; background: transparent; color: var(--text-secondary);
  padding: 7px 14px; border-radius: 999px; white-space: nowrap; font-weight: 550;
}
.tab:hover { background: var(--surface-2); }
.tab[aria-current="page"] { background: var(--surface-1); border-color: var(--line); color: var(--text-primary); box-shadow: var(--shadow); }

.btn {
  border: 1px solid var(--line); background: var(--surface-1); border-radius: 999px;
  padding: 8px 16px; font-weight: 600; box-shadow: var(--shadow);
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--text-primary); color: var(--bg); border-color: transparent; }
.btn.primary:hover { opacity: .88; }
.btn.ghost { box-shadow: none; background: transparent; }
.btn:disabled { opacity: .5; cursor: default; }
.btn.sm { padding: 5px 12px; font-size: 14px; }

section.page { display: none; padding-top: 18px; }
section.page.active { display: block; }

.card {
  background: var(--surface-1); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; margin-bottom: 16px;
}
.card > h2 { font-size: 17px; margin-bottom: 4px; }
.card .sub { color: var(--text-muted); font-size: 14px; margin-bottom: 14px; }

.podium { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kid-card {
  background: var(--surface-1); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px; position: relative; overflow: hidden;
}
.kid-card .bar-accent { position: absolute; inset: 0 auto 0 0; width: 5px; }
.kid-card .head { display: flex; align-items: center; gap: 10px; }
.kid-card .avatar { font-size: 30px; line-height: 1; }
.kid-card .name { font-weight: 700; font-size: 17px; }
.kid-card .rank { font-size: 13px; color: var(--text-muted); }
.kid-card .score { font-size: 34px; font-weight: 700; letter-spacing: -0.02em; margin-top: 10px; }
.kid-card .score small { font-size: 14px; font-weight: 500; color: var(--text-muted); margin-left: 4px; letter-spacing: 0; }
.kid-card .meter { height: 8px; border-radius: 999px; background: var(--surface-2); margin-top: 10px; overflow: hidden; }
.kid-card .meter > i { display: block; height: 100%; border-radius: 999px; }
.kid-card .meta { font-size: 13px; color: var(--text-secondary); margin-top: 8px; }
.crown { position: absolute; top: 12px; right: 14px; font-size: 20px; }

.legend { display: flex; flex-wrap: wrap; gap: 14px; margin: 0 0 12px; padding: 0; list-style: none; font-size: 14px; color: var(--text-secondary); }
.legend li { display: flex; align-items: center; gap: 6px; }
.legend .swatch { width: 12px; height: 12px; border-radius: 4px; }

.chart-box { position: relative; width: 100%; overflow-x: auto; }
.chart-box svg { display: block; width: 100%; height: auto; }
.tip {
  position: fixed; z-index: 60; pointer-events: none; opacity: 0; transition: opacity .1s;
  background: var(--surface-1); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: var(--shadow); padding: 8px 10px; font-size: 13px; max-width: 240px;
}
.tip.on { opacity: 1; }
.tip .tip-title { font-weight: 650; margin-bottom: 4px; }
.tip .tip-row { display: flex; align-items: center; gap: 6px; }
.tip .tip-row .swatch { width: 9px; height: 9px; border-radius: 3px; }
.tip .tip-row b { margin-left: auto; font-variant-numeric: tabular-nums; }

.table-scroll { overflow-x: auto; }
table.grid { width: 100%; border-collapse: collapse; font-size: 15px; }
table.grid th, table.grid td { padding: 9px 10px; text-align: right; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.grid th:first-child, table.grid td:first-child { text-align: left; }
table.grid thead th { font-size: 13px; color: var(--text-muted); font-weight: 600; }
table.grid tbody tr:last-child td { border-bottom: none; }
table.grid td.num { font-variant-numeric: tabular-nums; }
table.grid tr.total td { font-weight: 700; border-top: 2px solid var(--line); }
.chip { display: inline-flex; align-items: center; gap: 6px; }
.chip .dot { width: 10px; height: 10px; border-radius: 3px; flex: none; }

.day-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-bottom: 16px; }
.day-bar input[type="date"], .day-bar select {
  background: var(--surface-1); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px;
}
.locked-note { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; background: var(--surface-2); border-radius: 12px; padding: 10px 14px; font-size: 14px; color: var(--text-secondary); margin-bottom: 16px; }

.score-kid { border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface-1); box-shadow: var(--shadow); margin-bottom: 14px; overflow: hidden; }
.score-kid > header { display: flex; align-items: center; gap: 10px; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.score-kid > header .avatar { font-size: 24px; }
.score-kid > header .name { font-weight: 700; }
.score-kid > header .today-total { margin-left: auto; font-variant-numeric: tabular-nums; color: var(--text-secondary); font-size: 14px; }
.cat-row { display: flex; align-items: center; gap: 12px; padding: 11px 16px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.cat-row:last-of-type { border-bottom: none; }
.cat-label { min-width: 150px; }
.cat-label .cat-name { font-weight: 600; }
.cat-label .cat-hint { display: block; font-size: 12.5px; color: var(--text-muted); line-height: 1.35; }
.stars { display: flex; gap: 2px; margin-left: auto; }
.star {
  border: none; background: transparent; font-size: 26px; line-height: 1; padding: 2px 3px;
  filter: grayscale(1) opacity(.32); transition: transform .08s, filter .12s;
}
.star.on { filter: none; }
.star:hover { transform: scale(1.12); }
.star:disabled { cursor: default; }
.clear-star { font-size: 13px; color: var(--text-muted); background: none; border: none; padding: 2px 4px; text-decoration: underline; }
.note-row { padding: 12px 16px; border-top: 1px solid var(--line); background: var(--surface-2); }
.note-row textarea {
  width: 100%; border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px;
  background: var(--surface-1); resize: vertical; min-height: 44px;
}
.save-bar { position: sticky; bottom: 0; background: color-mix(in srgb, var(--bg) 92%, transparent); backdrop-filter: blur(8px); padding: 12px 0; display: flex; gap: 10px; align-items: center; border-top: 1px solid var(--line); }
.save-bar .hint { font-size: 14px; color: var(--text-muted); }

.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 12px; }
.field label { font-size: 13px; color: var(--text-secondary); font-weight: 600; }
.field input, .field textarea, .field select {
  border: 1px solid var(--line); border-radius: 10px; padding: 9px 11px; background: var(--surface-1);
}
.row-inline { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
.row-inline .field { margin-bottom: 0; }

dialog {
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface-1);
  color: var(--text-primary); box-shadow: var(--shadow); padding: 20px; max-width: 340px; width: calc(100% - 32px);
}
dialog::backdrop { background: rgba(0,0,0,.42); }

.toast {
  position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%) translateY(16px);
  background: var(--text-primary); color: var(--bg); padding: 10px 18px; border-radius: 999px;
  font-size: 14px; font-weight: 550; opacity: 0; pointer-events: none; transition: all .18s; z-index: 80;
}
.toast.on { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.err { background: var(--bad); color: #fff; }

.empty { color: var(--text-muted); font-size: 15px; padding: 18px 0; text-align: center; }
.kid-edit { display: grid; grid-template-columns: 60px 1fr 90px 90px; gap: 10px; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--line); }
.kid-edit input[type="text"], .kid-edit input[type="color"] { border: 1px solid var(--line); border-radius: 10px; padding: 8px; background: var(--surface-1); width: 100%; }
.kid-edit input[type="color"] { padding: 2px; height: 38px; }
@media (max-width: 560px) {
  .kid-edit { grid-template-columns: 52px 1fr; }
  .kid-edit .wide { grid-column: 1 / -1; }
  .cat-label { min-width: 100%; }
  .stars { margin-left: 0; }
}
.footnote { color: var(--text-muted); font-size: 13px; margin-top: 6px; }

/* trợ lý AI */
.ai-card { border-color: color-mix(in srgb, var(--accent) 35%, var(--line)); }
.ai-card > h2 { display: flex; align-items: center; gap: 6px; }
#quickTellText { width: 100%; border: 1px solid var(--line); border-radius: 12px; padding: 11px 13px; background: var(--surface-2); resize: vertical; }
.ai-pick { border: 1px solid var(--line); border-radius: 12px; background: var(--surface-2); padding: 12px 14px; margin-top: 14px; }
.ai-pick h3 { font-size: 15px; margin-bottom: 6px; }
.ai-pick ul { margin: 0; padding-left: 18px; font-size: 14.5px; line-height: 1.6; }
.ai-pick .warn { margin-top: 10px; font-size: 14px; color: var(--text-secondary); background: color-mix(in srgb, var(--accent) 16%, transparent); border-radius: 10px; padding: 8px 11px; }
.ai-kid { border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; margin-top: 12px; background: var(--surface-2); position: relative; overflow: hidden; }
.ai-kid .bar-accent { position: absolute; inset: 0 auto 0 0; width: 4px; }
.ai-kid h3 { font-size: 16px; margin-bottom: 6px; }
.ai-kid p { margin: 0 0 8px; font-size: 15px; line-height: 1.55; }
.ai-kid dl { display: grid; grid-template-columns: 132px 1fr; gap: 6px 12px; margin: 0; font-size: 14.5px; }
.ai-kid dt { color: var(--text-muted); }
.ai-kid dd { margin: 0; }
.ai-family { margin-top: 14px; font-size: 15px; line-height: 1.6; background: color-mix(in srgb, var(--accent) 12%, transparent); border-radius: 12px; padding: 12px 14px; }
.ask-samples { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.ask-samples button { border: 1px solid var(--line); background: var(--surface-2); border-radius: 999px; padding: 6px 12px; font-size: 13.5px; color: var(--text-secondary); }
.ask-samples button:hover { color: var(--text-primary); }
.ask-item { margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--line); }
.ask-item .q { font-weight: 650; margin-bottom: 6px; }
.ask-item .a { font-size: 15px; line-height: 1.6; white-space: pre-wrap; }
.ask-item .meta { font-size: 12.5px; color: var(--text-muted); margin-top: 6px; }
.spin { display: inline-block; width: 13px; height: 13px; border: 2px solid var(--line); border-top-color: var(--text-secondary); border-radius: 50%; animation: spin .7s linear infinite; vertical-align: -2px; margin-right: 6px; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (max-width: 560px) { .ai-kid dl { grid-template-columns: 1fr; gap: 2px; } .ai-kid dt { margin-top: 6px; } }

/* tuổi, sinh nhật, sửa thông tin con */
.kid-card .age { font-size: 13px; color: var(--text-muted); font-weight: 500; }
.bday { display: inline-block; margin-top: 8px; font-size: 13px; font-weight: 600; padding: 4px 10px; border-radius: 999px;
  background: color-mix(in srgb, var(--accent) 22%, transparent); color: var(--text-primary); }
.kid-row { border-bottom: 1px solid var(--line); padding: 12px 0; }
.kid-row:last-child { border-bottom: none; }
.kid-row-top { display: flex; gap: 10px; align-items: center; }
.kid-row-bottom { display: flex; gap: 10px; align-items: center; margin-top: 10px; flex-wrap: wrap; }
.kid-row-bottom label { font-size: 13px; color: var(--text-secondary); font-weight: 600; }
.kid-row input[type="text"], .kid-row input[type="date"] { border: 1px solid var(--line); border-radius: 10px; padding: 10px; background: var(--surface-1); }
.kid-row input.ic { width: 56px; text-align: center; }
.kid-row input.nm { flex: 1; min-width: 0; }
.kid-row input[type="color"] { width: 52px; height: 42px; padding: 2px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-1); }
.kid-row .age-note { font-size: 13px; color: var(--text-muted); }
.kid-row .btn { margin-left: auto; }

/* tối ưu cho điện thoại */
@media (max-width: 640px) {
  /* iOS tự phóng to trang khi ô nhập nhỏ hơn 16px */
  input, select, textarea { font-size: 16px; }
  .wrap { padding: 0 12px; }
  .card { padding: 15px; border-radius: 14px; }
  .top-row { padding: 10px 0; }
  .brand { font-size: 16px; }
  .tab { padding: 9px 14px; }
  .btn { padding: 11px 18px; }
  .btn.sm { padding: 8px 14px; }

  /* sao to và thưa hơn để bấm bằng ngón tay */
  .star { font-size: 32px; padding: 5px 6px; }
  .stars { gap: 4px; margin-top: 6px; width: 100%; justify-content: space-between; }
  .clear-star { font-size: 14px; padding: 6px 8px; }
  .cat-row { padding: 13px 14px; gap: 6px; }
  .cat-label .cat-name { font-size: 16px; }

  .score-kid > header { padding: 13px 14px; }
  #scoreList { margin-bottom: 8px; }
  .score-kid:last-of-type { margin-bottom: 18px; }
  .save-bar { padding: 10px 0 14px; }
  .save-bar .btn { flex: 1; }

  .day-bar { gap: 8px; }
  .day-bar input[type="date"] { flex: 1; min-width: 130px; }
  .podium { gap: 10px; }
  .kid-card .score { font-size: 30px; }
  table.grid th, table.grid td { padding: 10px 8px; font-size: 14.5px; }
  .row-inline .btn { flex: 1; }
  #quickTellText { min-height: 110px; }
  .ai-card h2 { font-size: 17px; }
  .kid-row .btn { min-width: 88px; }
}

/* tài khoản */
.user-row { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; padding: 11px 0; border-bottom: 1px solid var(--line); }
.user-row:last-child { border-bottom: none; }
.user-row .who { min-width: 150px; }
.user-row .uname { color: var(--text-muted); font-size: 13px; margin-left: 6px; }
.user-row .acts { display: flex; gap: 8px; align-items: center; margin-left: auto; flex-wrap: wrap; }
.user-row input { border: 1px solid var(--line); border-radius: 10px; padding: 9px 10px; background: var(--surface-1); width: 170px; }
@media (max-width: 560px) {
  .user-row .acts { margin-left: 0; width: 100%; }
  .user-row input { flex: 1; width: auto; min-width: 120px; }
}

.kid-card.empty-week .score, .kid-card.empty-week .meter { display: none; }
.kid-card.empty-week .meta { margin-top: 12px; }

/* nút làm mới */
.icon-btn { display: inline-flex; align-items: center; gap: 6px; }
@media (max-width: 560px) { .icon-btn .lbl { display: none; } .icon-btn { padding: 10px 13px; font-size: 17px; } }

/* hướng dẫn thang điểm */
.guide { padding: 0; }
.guide > summary { cursor: pointer; padding: 15px 18px; font-weight: 650; list-style: none; display: flex; align-items: center; gap: 8px; }
.guide > summary::-webkit-details-marker { display: none; }
.guide > summary::after { content: '▾'; margin-left: auto; color: var(--text-muted); transition: transform .15s; }
.guide[open] > summary::after { transform: rotate(180deg); }
.guide-body { padding: 0 18px 16px; }
.guide .scale { list-style: none; margin: 0 0 12px; padding: 0; }
.guide .scale li { padding: 7px 0; border-bottom: 1px solid var(--line); font-size: 15px; }
.guide .scale li:last-child { border-bottom: none; }
.guide .scale b { display: inline-block; min-width: 46px; }
.guide .tip { font-size: 14px; color: var(--text-secondary); line-height: 1.55; margin: 8px 0 0; }

/* nhãn mức sao */
.star-word { font-size: 13px; color: var(--text-muted); min-width: 76px; text-align: right; }
@media (max-width: 560px) { .star-word { width: 100%; text-align: left; margin-top: 2px; } }

/* màn hình đăng nhập chặn */
#gate { position: fixed; inset: 0; z-index: 200; background: var(--bg); display: flex; align-items: center; justify-content: center; padding: 24px 16px; overflow-y: auto; }
#gate[hidden] { display: none; }
.gate-box { width: 100%; max-width: 380px; background: var(--surface-1); border: 1px solid var(--line); border-radius: 20px; box-shadow: var(--shadow); padding: 26px 22px 22px; text-align: center; }
.gate-logo { font-size: 46px; line-height: 1; }
.gate-box h1 { font-size: 21px; margin: 10px 0 6px; }
.gate-sub { color: var(--text-secondary); font-size: 14.5px; margin: 0 0 20px; line-height: 1.5; }
.gate-box .field { text-align: left; margin-bottom: 14px; }
.gate-box .field input { padding: 13px 13px; border-radius: 12px; }
.gate-btn { width: 100%; padding: 14px; font-size: 16px; margin-top: 4px; }
.gate-msg { min-height: 20px; margin: 10px 0 0; font-size: 14px; color: var(--bad); }
.gate-note { color: var(--text-muted); font-size: 13px; margin: 12px 0 0; line-height: 1.5; }
body.locked > header.top, body.locked > main { display: none; }
