:root {
  --green: #15803d; --green-2: #166534; --green-soft: #dcfce7; --green-line: #bbf7d0;
  --red: #b91c1c; --red-soft: #fee2e2; --blue: #1d4ed8; --blue-soft: #dbeafe;
  --amber: #b45309; --amber-soft: #fef3c7;
  --bg: #f3f5f1; --card: #ffffff; --text: #1c2b22; --muted: #66756b; --line: #dde4dc; --input: #f8faf7;
  --shadow: 0 1px 2px rgba(16, 40, 24, .06), 0 4px 14px rgba(16, 40, 24, .06);
  --r: 14px;
  --nav-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --green: #22c55e; --green-2: #16a34a; --green-soft: #12301f; --green-line: #1f4d31;
    --red: #f87171; --red-soft: #3a1717; --blue: #60a5fa; --blue-soft: #172440;
    --amber: #fbbf24; --amber-soft: #3a2a0c;
    --bg: #0f1511; --card: #18201b; --text: #e6efe8; --muted: #93a398; --line: #2a362e; --input: #121a15;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg); color: var(--text); font-size: 15px; line-height: 1.5;
  overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; }
.ic { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.ic.sm { width: 16px; height: 16px; }
.ic.lg { width: 26px; height: 26px; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.italic { font-style: italic; }

/* ---------- khung */
.topbar {
  position: sticky; top: 0; z-index: 20; background: var(--green-2); color: #fff;
  padding: calc(10px + var(--safe-t)) 16px 10px; display: flex; align-items: center; gap: 10px;
}
@media (prefers-color-scheme: dark) { .topbar { background: #0b2a17; } }
.topbar .brand { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; }
.topbar .logo { width: 34px; height: 34px; border-radius: 10px; background: rgba(255,255,255,.15); display: grid; place-items: center; }
.topbar h1 { font-size: 17px; margin: 0; line-height: 1.15; }
.topbar .sub { font-size: 11px; opacity: .8; }
.net-pill { font-size: 11px; padding: 3px 8px; border-radius: 99px; background: rgba(255,255,255,.18); white-space: nowrap; }
.net-pill.off { background: #b91c1c; }

main { max-width: 860px; margin: 0 auto; padding: 14px 16px calc(var(--nav-h) + var(--safe-b) + 24px); }
.view-title { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 2px 0 12px; }
.view-title h2 { font-size: 19px; margin: 0; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--r); box-shadow: var(--shadow); padding: 14px; }
.card + .card { margin-top: 12px; }
.card h3 { margin: 0 0 10px; font-size: 15px; display: flex; align-items: center; gap: 8px; }
.step { width: 22px; height: 22px; border-radius: 50%; background: var(--green); color: #fff; font-size: 12px; display: grid; place-items: center; flex: none; }

.badge { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; font-weight: 600; padding: 3px 9px; border-radius: 99px; background: var(--green-soft); color: var(--green); }
.badge.red { background: var(--red-soft); color: var(--red); }
.badge.blue { background: var(--blue-soft); color: var(--blue); }
.badge.amber { background: var(--amber-soft); color: var(--amber); }
.badge.gray { background: var(--input); color: var(--muted); border: 1px solid var(--line); }

/* ---------- nút */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 44px; padding: 0 16px; border-radius: 12px; font-weight: 600; background: var(--input); border: 1px solid var(--line); }
.btn:active { transform: scale(.98); }
.btn[disabled] { opacity: .6; pointer-events: none; }
.btn.primary { background: var(--green); color: #fff; border-color: transparent; }
.btn.blue { background: var(--blue); color: #fff; border-color: transparent; }
@media (prefers-color-scheme: dark) { .btn.primary, .btn.blue { color: #04120a; } }
.btn.danger { color: var(--red); background: transparent; border-color: transparent; }
.btn.block { width: 100%; }
.btn.lg { min-height: 52px; font-size: 16px; }
.icon-btn { width: 40px; height: 40px; border-radius: 10px; display: grid; place-items: center; }
.icon-btn:active { background: rgba(127,127,127,.15); }

.photo-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.photo-btn { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 16px 8px; border-radius: 12px; border: 2px dashed var(--green-line); background: var(--green-soft); color: var(--green); font-weight: 600; font-size: 14px; }
.photo-btn:active { transform: scale(.98); }

/* ---------- form */
.grid2 { display: grid; grid-template-columns: 1fr; gap: 12px; }
@media (min-width: 560px) { .grid2 { grid-template-columns: 1fr 1fr; } }
.field { display: flex; flex-direction: column; gap: 5px; }
.field + .field, .grid2 + .field, .field + .grid2, .grid2 + .grid2 { margin-top: 12px; }
label.lb { font-size: 13px; font-weight: 600; }
label.lb .req { color: var(--red); }
.inp { width: 100%; min-height: 44px; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--input); font-size: 16px; }
textarea.inp { resize: vertical; min-height: 70px; }
.inp.masked { -webkit-text-security: disc; text-security: disc; }
.inp:focus { outline: 2px solid var(--green); outline-offset: 0; border-color: transparent; }
.inp.red { background: var(--red-soft); border-color: transparent; font-weight: 600; }
.inp.green { background: var(--green-soft); border-color: transparent; }
.inp.blue { background: var(--blue-soft); border-color: transparent; }
.row { display: flex; gap: 8px; align-items: center; }
.row > .inp { flex: 1; }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg label { text-align: center; padding: 10px 4px; border-radius: 10px; border: 1px solid var(--line); background: var(--input); font-size: 14px; font-weight: 600; }
.seg input:checked + label { background: var(--green); color: #fff; border-color: transparent; }
.seg input[value="nặng"]:checked + label { background: var(--red); }
.seg input[value="trung bình"]:checked + label { background: var(--amber); }

/* ---------- ảnh */
.thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 8px; margin-top: 12px; }
.thumbs:empty { display: none; }
.thumb { position: relative; aspect-ratio: 1; border-radius: 10px; overflow: hidden; border: 1px solid var(--line); background: #000; }
.thumb img, .thumb video { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb .rm { position: absolute; top: 4px; right: 4px; width: 26px; height: 26px; border-radius: 50%; background: rgba(0,0,0,.6); color: #fff; display: grid; place-items: center; }
.thumb .tag { position: absolute; left: 4px; bottom: 4px; font-size: 10px; background: rgba(0,0,0,.6); color: #fff; padding: 1px 6px; border-radius: 6px; }

/* ---------- thư viện */
.search { position: relative; }
.search .ic { position: absolute; left: 12px; top: 12px; color: var(--muted); }
.search .inp { padding-left: 40px; background: var(--card); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; padding: 10px 0 4px; }
.chip { flex: none; padding: 7px 12px; border-radius: 99px; border: 1px solid var(--line); background: var(--card); font-size: 13px; font-weight: 600; white-space: nowrap; }
.chip:hover { border-color: var(--green-line); }
.chip.on { background: var(--green); color: #fff; border-color: transparent; }
.chip.more { background: transparent; border-style: dashed; color: var(--green); }
.lib { display: grid; grid-template-columns: 1fr; gap: 12px; margin-top: 10px; }
@media (min-width: 560px) { .lib { grid-template-columns: 1fr 1fr; } }
.rec { display: flex; gap: 12px; background: var(--card); border: 1px solid var(--line); border-radius: var(--r); padding: 10px; box-shadow: var(--shadow); text-align: left; width: 100%; }
.rec:active { transform: scale(.99); }
.rec .pic { width: 88px; height: 88px; border-radius: 10px; object-fit: cover; flex: none; background: var(--input); display: grid; place-items: center; color: var(--muted); }
.rec .body { min-width: 0; flex: 1; display: flex; flex-direction: column; gap: 3px; }
.rec .dz { font-weight: 700; color: var(--red); font-size: 15.5px; line-height: 1.3; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.rec .meta { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.empty { text-align: center; padding: 48px 16px; color: var(--muted); }
.empty .ic { width: 48px; height: 48px; stroke-width: 1.4; opacity: .5; }

/* ---------- kết quả AI */
.ai-card { border: 1px solid var(--blue-soft); background: linear-gradient(180deg, var(--blue-soft), var(--card) 70px); }
.ai-head { display: flex; justify-content: space-between; gap: 8px; align-items: flex-start; }
.ai-head h4 { margin: 0; font-size: 17px; color: var(--red); }
.conf { height: 8px; border-radius: 99px; background: var(--line); overflow: hidden; margin: 8px 0 4px; }
.conf > i { display: block; height: 100%; background: var(--green); }
.conf.mid > i { background: var(--amber); }
.conf.low > i { background: var(--red); }
.kv { font-size: 14px; margin-top: 8px; }
.kv b { display: block; font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .02em; }
.alts { margin: 4px 0 0; padding-left: 18px; font-size: 14px; }
.note { border-radius: 10px; padding: 10px 12px; font-size: 13.5px; margin-top: 10px; }
.note.amber { background: var(--amber-soft); color: var(--amber); }
.note.blue { background: var(--blue-soft); color: var(--blue); }

/* ---------- bottom nav */
.nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: var(--safe-b); display: grid; grid-template-columns: 1fr 88px 1fr;
}
.nav button { height: var(--nav-h); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: 11.5px; font-weight: 600; color: var(--muted); }
.nav button.on { color: var(--green); }
.nav .fab-wrap { position: relative; }
.nav .fab { position: absolute; left: 50%; top: -22px; transform: translateX(-50%); width: 64px; height: 64px; border-radius: 50%; background: var(--green); color: #fff; box-shadow: 0 6px 18px rgba(21,128,61,.45); border: 4px solid var(--bg); }
.nav .fab .ic { width: 28px; height: 28px; }
.nav .fab-label { position: absolute; bottom: 8px; left: 0; right: 0; text-align: center; font-size: 11.5px; font-weight: 600; color: var(--green); }

/* ---------- sheet / modal */
.overlay { position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.55); display: flex; align-items: flex-end; justify-content: center; }
@media (min-width: 640px) { .overlay { align-items: center; padding: 16px; } }
.sheet { background: var(--card); width: 100%; max-width: 680px; max-height: 92vh; border-radius: 18px 18px 0 0; display: flex; flex-direction: column; overflow: hidden; animation: up .2s ease-out; }
@media (min-width: 640px) { .sheet { border-radius: 18px; } }
@keyframes up { from { transform: translateY(30px); opacity: .5; } to { transform: none; opacity: 1; } }
.sheet-head { display: flex; align-items: center; gap: 8px; padding: 12px 12px 12px 16px; border-bottom: 1px solid var(--line); }
.sheet-head h3 { margin: 0; font-size: 17px; flex: 1; min-width: 0; color: var(--red); }
.sheet-body { overflow-y: auto; padding: 14px 16px; -webkit-overflow-scrolling: touch; }
.sheet-foot { display: flex; gap: 8px; padding: 10px 16px calc(10px + var(--safe-b)); border-top: 1px solid var(--line); }
.sheet-foot .btn { flex: 1; }
.dialog { background: var(--card); width: calc(100% - 32px); max-width: 400px; border-radius: 18px; padding: 20px; margin: auto; animation: up .2s ease-out; }
.overlay.center { align-items: center; }
.dialog h3 { margin: 0 0 8px; font-size: 17px; }
.dialog p { margin: 0 0 16px; color: var(--muted); white-space: pre-line; font-size: 14.5px; }
.dialog .acts { display: flex; gap: 8px; justify-content: flex-end; }

.gallery { display: flex; gap: 8px; overflow-x: auto; scroll-snap-type: x mandatory; margin: 0 -16px 12px; padding: 0 16px; }
.gallery img, .gallery video { height: 200px; max-width: 85%; border-radius: 12px; object-fit: cover; scroll-snap-align: center; flex: none; background: #000; }
.facts { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; background: var(--input); border: 1px solid var(--line); border-radius: 12px; padding: 12px; font-size: 14px; }
.facts .full { grid-column: 1 / -1; }
.facts span { display: block; font-size: 11.5px; color: var(--muted); }
.block { margin-top: 12px; border-radius: 12px; padding: 12px; font-size: 14.5px; white-space: pre-line; }
.block h4 { margin: 0 0 4px; font-size: 13px; display: flex; gap: 6px; align-items: center; }
.block.plain { padding: 0; }
.block.green { background: var(--green-soft); }
.block.green h4 { color: var(--green); }
.block.blue { background: var(--blue-soft); }
.block.blue h4 { color: var(--blue); }
.block.amber { background: var(--amber-soft); }
.block.amber h4 { color: var(--amber); }

.qa { margin-top: 16px; border-top: 1px dashed var(--line); padding-top: 12px; }
.qa-item { margin-top: 10px; }
.qa-q { font-weight: 600; font-size: 14px; }
.qa-a { margin-top: 4px; background: var(--input); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; font-size: 14px; white-space: pre-line; }
.qa-meta { font-size: 11px; color: var(--muted); margin-top: 2px; }

.lightbox { position: fixed; inset: 0; z-index: 70; background: #000; display: flex; align-items: center; justify-content: center; }
.lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; }
.lightbox .icon-btn { position: absolute; top: calc(10px + var(--safe-t)); right: 10px; color: #fff; background: rgba(255,255,255,.15); }

.toast { position: fixed; left: 50%; bottom: calc(var(--nav-h) + var(--safe-b) + 18px); transform: translate(-50%, 10px); z-index: 80; background: #1f2937; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px; max-width: calc(100% - 32px); text-align: center; opacity: 0; transition: .25s; pointer-events: none; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.spin { width: 18px; height: 18px; border-radius: 50%; border: 2.5px solid rgba(255,255,255,.4); border-top-color: #fff; animation: rot .8s linear infinite; }
@keyframes rot { to { transform: rotate(360deg); } }

.setting-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); font-size: 14.5px; }
.setting-row:last-child { border-bottom: 0; }
.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; background: var(--muted); margin-right: 6px; }
.dot.ok { background: #22c55e; }
.dot.bad { background: #ef4444; }
