:root {
  --navy: #072A36;
  --navy-2: #0E3A4A;
  --teal: #2095A8;
  --action: #087F8C;
  --action-hover: #066D78;
  --selected: #E8F5F7;
  --ground: #F4F8F9;
  --surface: #FFFFFF;
  --muted: #52656D;
  --border: #DCE5E8;
  --border-strong: #9FB3BA;
  --ok: #1B7F3B; --ok-bg: #E7F5EC;
  --warn: #8A5300; --warn-bg: #FFF4E0;
  --danger: #B42318; --danger-bg: #FDECEA;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(7, 42, 54, .06);
  --font: "Be Vietnam Pro", system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; font: 16px/1.5 var(--font); color: var(--navy); background: var(--ground); min-height: 100vh; }
a { color: var(--action); }
img { max-width: 100%; }
h1, h2, h3 { line-height: 1.25; margin: 0; }
h1 { font-size: 22px; }
h2 { font-size: 18px; }
h3 { font-size: 16px; }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid rgba(32, 149, 168, .55); outline-offset: 2px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: 14px; }
.hidden { display: none !important; }

/* ---------- top bar ---------- */
.topbar { position: sticky; top: 0; z-index: 30; background: var(--surface); border-bottom: 1px solid var(--border); }
.topbar-inner { max-width: 1200px; margin: 0 auto; padding: 0 16px; height: 60px; display: flex; align-items: center; gap: 16px; }
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--navy); min-width: 0; }
.brand img { height: 40px; width: auto; display: block; }
.brand-mark { width: 38px; height: 38px; border-radius: 10px; background: var(--navy); color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 15px; letter-spacing: .5px; flex: none; }
.brand-text { display: flex; flex-direction: column; line-height: 1.2; min-width: 0; }
.brand-text b { font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-text span { font-size: 12px; color: var(--muted); }
.mainnav { display: flex; gap: 4px; margin-left: 8px; }
.mainnav a { text-decoration: none; color: var(--navy); padding: 8px 12px; border-radius: 8px; font-weight: 500; }
.mainnav a:hover { background: var(--ground); }
.mainnav a.active { background: var(--selected); color: var(--navy); font-weight: 600; }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.usermenu { position: relative; }
.usermenu-btn { display: flex; align-items: center; gap: 8px; border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 4px 12px 4px 4px; min-height: 40px; cursor: pointer; }
.avatar { width: 30px; height: 30px; border-radius: 50%; background: var(--selected); color: var(--navy); display: grid; place-items: center; font-weight: 700; font-size: 13px; }
.usermenu-btn .name { max-width: 160px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; font-weight: 500; }
.dropdown { position: absolute; right: 0; top: calc(100% + 6px); background: var(--surface); border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 8px 24px rgba(7, 42, 54, .12); min-width: 200px; padding: 6px; z-index: 40; }
.dropdown a, .dropdown button { display: block; width: 100%; text-align: left; padding: 10px 12px; border: 0; background: none; border-radius: 6px; text-decoration: none; color: var(--navy); cursor: pointer; }
.dropdown a:hover, .dropdown button:hover { background: var(--ground); }
.dropdown .who { padding: 8px 12px 10px; border-bottom: 1px solid var(--border); margin-bottom: 4px; font-size: 14px; }

/* bottom tab bar (phone) */
.tabbar { display: none; }
@media (max-width: 760px) {
  .mainnav { display: none; }
  .usermenu-btn .name { display: none; }
  .usermenu-btn { padding: 4px; }
  .tabbar { display: flex; position: fixed; bottom: 0; left: 0; right: 0; z-index: 30; background: var(--surface); border-top: 1px solid var(--border); padding-bottom: env(safe-area-inset-bottom); }
  .tabbar a { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 4px; min-height: 56px; text-decoration: none; color: var(--muted); font-size: 12px; font-weight: 500; }
  .tabbar a.active { color: var(--action); font-weight: 700; }
  .tabbar svg { width: 22px; height: 22px; }
  body.has-tabbar { padding-bottom: 72px; }
}

/* ---------- layout ---------- */
.page { max-width: 1200px; margin: 0 auto; padding: 20px 16px 40px; }
.page-narrow { max-width: 760px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head p { margin: 4px 0 0; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-pad { padding: 16px; }
.stack > * + * { margin-top: 12px; }

/* ---------- buttons ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 44px; padding: 0 16px; border-radius: 10px; border: 1px solid var(--border-strong); background: var(--surface); color: var(--navy); font-weight: 600; text-decoration: none; cursor: pointer; white-space: nowrap; font-size: 15px; }
.btn:hover { background: var(--ground); }
.btn-primary { background: var(--action); border-color: var(--action); color: #fff; }
.btn-primary:hover { background: var(--action-hover); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-danger { color: var(--danger); border-color: #E7B4AE; }
.btn-danger:hover { background: var(--danger-bg); }
.btn-sm { min-height: 36px; padding: 0 12px; font-size: 14px; border-radius: 8px; }
.btn[disabled], .btn[aria-busy="true"] { opacity: .6; cursor: progress; }
.btn svg { width: 18px; height: 18px; flex: none; }
.icon-btn { min-width: 44px; padding: 0 10px; }

/* ---------- forms ---------- */
.field { display: flex; flex-direction: column; gap: 6px; }
.field > label, .field > .label { font-weight: 600; font-size: 14px; }
.field .hint { font-size: 13px; color: var(--muted); }
.input, .select, .textarea { width: 100%; min-height: 44px; padding: 8px 12px; border: 1px solid var(--border-strong); border-radius: 10px; background: var(--surface); }
.textarea { min-height: 88px; resize: vertical; }
.input:focus, .select:focus, .textarea:focus { border-color: var(--action); outline: 3px solid rgba(32, 149, 168, .25); outline-offset: 0; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
@media (max-width: 640px) { .grid-2, .grid-3 { grid-template-columns: 1fr; } }
.check { display: flex; align-items: flex-start; gap: 10px; padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; cursor: pointer; background: var(--surface); }
.check input { width: 20px; height: 20px; margin: 2px 0 0; accent-color: var(--action); flex: none; }
.check:has(input:checked) { border-color: var(--action); background: var(--selected); }
.check .t { font-weight: 600; }
.check .d { font-size: 14px; color: var(--muted); }
.check-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
.segmented { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.form-error { background: var(--danger-bg); color: var(--danger); border-radius: 10px; padding: 10px 12px; font-size: 14px; }

/* ---------- search & filters ---------- */
.searchbar { display: flex; gap: 8px; }
.search-wrap { position: relative; flex: 1; }
.search-wrap svg { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); width: 20px; height: 20px; color: var(--muted); pointer-events: none; }
.search-wrap .input { padding-left: 40px; padding-right: 44px; min-height: 48px; font-size: 16px; }
.search-clear { position: absolute; right: 4px; top: 50%; transform: translateY(-50%); width: 40px; height: 40px; border: 0; background: none; border-radius: 8px; cursor: pointer; color: var(--muted); font-size: 22px; line-height: 1; }
.filters { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; margin-top: 10px; }
@media (max-width: 640px) { .filters { grid-template-columns: 1fr 1fr; } .filters .select:last-child { grid-column: 1 / -1; } }
.result-meta { display: flex; justify-content: space-between; align-items: center; margin: 16px 0 8px; font-size: 14px; color: var(--muted); gap: 8px; flex-wrap: wrap; }

/* ---------- exercise cards ---------- */
.ex-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 12px; }
@media (max-width: 400px) { .ex-list { grid-template-columns: 1fr; } }
.ex-card { display: flex; flex-direction: column; gap: 8px; padding: 16px; }
.crumb { font-size: 13px; color: var(--muted); display: flex; flex-wrap: wrap; gap: 2px 6px; align-items: center; }
.crumb .sep { color: var(--border-strong); }
.ex-card h3 { font-size: 17px; }
.ex-card .desc { margin: 0; color: var(--muted); font-size: 14px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.ex-card .actions { display: flex; gap: 8px; margin-top: auto; padding-top: 4px; }
.ex-card .actions .btn-primary { flex: 1; }
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 2px 10px; border-radius: 999px; font-size: 13px; font-weight: 600; border: 1px solid var(--border); background: var(--surface); color: var(--navy); white-space: nowrap; }
.chip-teal { background: var(--selected); border-color: #BFE3E8; }
.chip-ok { background: var(--ok-bg); border-color: #B7DEC4; color: var(--ok); }
.chip-warn { background: var(--warn-bg); border-color: #F1D29B; color: var(--warn); }
.chip-danger { background: var(--danger-bg); border-color: #F2C1BB; color: var(--danger); }
.chip-muted { background: var(--ground); color: var(--muted); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip-btn { min-height: 40px; padding: 0 14px; font-size: 14px; cursor: pointer; }
.chip-btn[aria-pressed="true"] { background: var(--navy); border-color: var(--navy); color: #fff; }

/* ---------- states ---------- */
.empty { text-align: center; padding: 40px 16px; color: var(--muted); }
.empty h3 { color: var(--navy); margin-bottom: 6px; }
.empty .btn { margin-top: 12px; }
.skel { background: linear-gradient(90deg, #E9EFF1 25%, #F3F7F8 37%, #E9EFF1 63%); background-size: 400% 100%; animation: shimmer 1.3s ease infinite; border-radius: 6px; }
@keyframes shimmer { 0% { background-position: 100% 50%; } 100% { background-position: 0 50%; } }
@media (prefers-reduced-motion: reduce) { .skel { animation: none; } }
.skel-line { height: 12px; margin: 8px 0; }
.spinner { width: 18px; height: 18px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .7s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }
.banner { padding: 10px 14px; border-radius: 10px; font-size: 14px; display: flex; gap: 8px; align-items: flex-start; }
.banner-warn { background: var(--warn-bg); color: var(--warn); }
.banner-info { background: var(--selected); color: var(--navy); }

/* ---------- player ---------- */
.player-head { background: var(--surface); border-bottom: 1px solid var(--border); }
.player-head-inner { max-width: 1200px; margin: 0 auto; padding: 12px 16px; display: flex; flex-direction: column; gap: 8px; }
.player-top { display: flex; gap: 12px; align-items: flex-start; justify-content: space-between; flex-wrap: wrap; }
.player-top h1 { font-size: 20px; }
.player-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.player-instr { margin: 0; color: var(--navy); font-size: 15px; white-space: pre-line; }
.player-frame-wrap { max-width: 1200px; margin: 0 auto; padding: 12px 16px 16px; }
.player-frame { width: 100%; height: calc(100vh - 210px); min-height: 520px; border: 1px solid var(--border); border-radius: var(--radius); background: #fff; display: block; }
@media (max-width: 760px) {
  .player-frame-wrap { padding: 8px 0 0; }
  .player-frame { border-radius: 0; border-left: 0; border-right: 0; height: calc(100vh - 190px); min-height: 480px; }
  .player-top h1 { font-size: 18px; }
}
.newtab-card { max-width: 560px; margin: 24px auto; text-align: center; padding: 28px 20px; }
.newtab-card .btn { margin-top: 16px; min-height: 52px; padding: 0 28px; font-size: 16px; }
.gate { max-width: 440px; margin: 32px auto; }

/* ---------- login ---------- */
.auth-card { max-width: 420px; margin: 32px auto; padding: 24px; }
.auth-card h1 { margin-bottom: 4px; }

/* ---------- admin ---------- */
.admin { max-width: 1280px; margin: 0 auto; display: grid; grid-template-columns: 232px 1fr; gap: 20px; padding: 20px 16px 40px; }
.sidenav { position: sticky; top: 80px; align-self: start; display: flex; flex-direction: column; gap: 2px; }
.sidenav a { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 8px; text-decoration: none; color: var(--navy); font-weight: 500; }
.sidenav a:hover { background: var(--surface); }
.sidenav a.active { background: var(--selected); font-weight: 700; }
.sidenav svg { width: 20px; height: 20px; color: var(--muted); flex: none; }
.sidenav a.active svg { color: var(--action); }
.admin-main { min-width: 0; }
@media (max-width: 900px) {
  .admin { grid-template-columns: 1fr; padding-top: 0; gap: 12px; }
  .sidenav { position: sticky; top: 60px; z-index: 20; flex-direction: row; overflow-x: auto; margin: 0 -16px; padding: 8px 16px; background: var(--ground); border-bottom: 1px solid var(--border); scrollbar-width: none; }
  .sidenav a { white-space: nowrap; padding: 8px 12px; font-size: 14px; background: var(--surface); border: 1px solid var(--border); }
  .sidenav a.active { border-color: var(--action); }
  .sidenav svg { display: none; }
}
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; }
.stat { padding: 14px 16px; }
.stat .n { font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat .l { font-size: 14px; color: var(--muted); }
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.toolbar .input, .toolbar .select { width: auto; flex: 1 1 160px; }
.toolbar .search-wrap { flex: 2 1 240px; }
.toolbar .search-wrap .input { min-height: 44px; width: 100%; }

.rows { display: flex; flex-direction: column; }
.row { display: flex; gap: 12px; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.row:last-child { border-bottom: 0; }
.row-main { flex: 1; min-width: 0; }
.row-title { font-weight: 600; overflow-wrap: anywhere; }
.row-sub { font-size: 14px; color: var(--muted); margin-top: 2px; }
.row-meta { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.row-actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
@media (max-width: 640px) {
  .row { flex-direction: column; align-items: stretch; }
  .row-actions { justify-content: flex-start; }
}
.order-btns { display: flex; gap: 4px; }
.order-btns .btn { min-width: 36px; padding: 0; }

table.tbl { width: 100%; border-collapse: collapse; font-size: 14px; }
.tbl th, .tbl td { padding: 10px 12px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
.tbl th { font-size: 13px; color: var(--muted); font-weight: 600; background: var(--ground); }
.tbl-wrap { overflow-x: auto; }

.form-section { padding: 16px; }
.form-section + .form-section { border-top: 1px solid var(--border); }
.form-section h2 { font-size: 16px; display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.step-no { width: 26px; height: 26px; border-radius: 50%; background: var(--navy); color: #fff; font-size: 13px; display: grid; place-items: center; flex: none; }
.sticky-actions { position: sticky; bottom: 0; background: rgba(255, 255, 255, .96); border-top: 1px solid var(--border); padding: 12px 16px; display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; border-radius: 0 0 var(--radius) var(--radius); z-index: 5; }
@media (max-width: 760px) { body.has-tabbar .sticky-actions { bottom: 72px; } .sticky-actions .btn { flex: 1; } }
.dropzone { border: 2px dashed var(--border-strong); border-radius: var(--radius); padding: 20px; text-align: center; background: var(--ground); cursor: pointer; }
.dropzone.drag { border-color: var(--action); background: var(--selected); }
.tree-unit { border: 1px solid var(--border); border-radius: 10px; background: var(--surface); }
.tree-unit + .tree-unit { margin-top: 8px; }
.tree-children { border-top: 1px solid var(--border); padding-left: 24px; }
.tree-children .row { padding: 8px 12px; }
.logo-preview { height: 64px; display: flex; align-items: center; }
.logo-preview img { max-height: 64px; }

/* ---------- modal & toast ---------- */
.modal-back { position: fixed; inset: 0; background: rgba(7, 42, 54, .45); z-index: 100; display: flex; align-items: flex-end; justify-content: center; }
@media (min-width: 641px) { .modal-back { align-items: center; padding: 24px; } }
.modal { background: var(--surface); width: 100%; max-width: 560px; max-height: 92vh; overflow: auto; border-radius: 16px 16px 0 0; display: flex; flex-direction: column; }
@media (min-width: 641px) { .modal { border-radius: 16px; } }
.modal-head { padding: 16px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; gap: 8px; position: sticky; top: 0; background: var(--surface); z-index: 1; }
.modal-body { padding: 16px; }
.modal-foot { padding: 12px 16px; border-top: 1px solid var(--border); display: flex; gap: 8px; justify-content: flex-end; position: sticky; bottom: 0; background: var(--surface); }
@media (max-width: 640px) { .modal-foot .btn { flex: 1; } }
.toasts { position: fixed; left: 50%; transform: translateX(-50%); bottom: 24px; z-index: 200; display: flex; flex-direction: column; gap: 8px; width: min(92vw, 420px); pointer-events: none; }
@media (max-width: 760px) { .toasts { bottom: 88px; } }
.toast { background: var(--navy); color: #fff; padding: 12px 16px; border-radius: 10px; box-shadow: 0 8px 24px rgba(7, 42, 54, .25); font-size: 15px; pointer-events: auto; }
.toast.err { background: var(--danger); }
.pw-box { font: 600 20px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--ground); border: 1px dashed var(--border-strong); padding: 10px 14px; border-radius: 10px; letter-spacing: 1px; user-select: all; }
.pager { display: flex; gap: 8px; justify-content: center; align-items: center; padding: 12px; }
