:root {
  --bg: #f7f7fb;
  --surface: #ffffff;
  --surface-2: #f0f0f7;
  --text: #16162a;
  --muted: #5d5d78;
  --border: #e2e2ee;
  --accent: #4338ca;
  --accent-soft: #eef0ff;
  --ok: #117a4a;   --ok-bg: #e5f6ee;
  --low: #0e7490;  --low-bg: #e3f4f8;
  --mod: #a15c00;  --mod-bg: #fff3e0;
  --high: #b42318; --high-bg: #fdecea;
  --vhigh: #7a1010; --vhigh-bg: #f9dcdc;
  --pos: #4338ca;  --pos-bg: #eef0ff;
  --neg: #4b5563;  --neg-bg: #f1f2f4;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(20, 20, 50, .06), 0 4px 16px rgba(20, 20, 50, .05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f0f1a; --surface: #181828; --surface-2: #20203a; --text: #ececf6; --muted: #a3a3c2; --border: #2c2c46;
    --accent: #a5b4fc; --accent-soft: #25254a;
    --ok: #6ee7b7; --ok-bg: #0f2d22; --low: #67e8f9; --low-bg: #0d2a33; --mod: #fcd34d; --mod-bg: #33260b;
    --high: #fca5a5; --high-bg: #3a1414; --vhigh: #fecaca; --vhigh-bg: #4a0f0f;
    --pos: #c7d2fe; --pos-bg: #25254a; --neg: #cbd5e1; --neg-bg: #262636;
    --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f0f1a; --surface: #181828; --surface-2: #20203a; --text: #ececf6; --muted: #a3a3c2; --border: #2c2c46;
  --accent: #a5b4fc; --accent-soft: #25254a;
  --ok: #6ee7b7; --ok-bg: #0f2d22; --low: #67e8f9; --low-bg: #0d2a33; --mod: #fcd34d; --mod-bg: #33260b;
  --high: #fca5a5; --high-bg: #3a1414; --vhigh: #fecaca; --vhigh-bg: #4a0f0f;
  --pos: #c7d2fe; --pos-bg: #25254a; --neg: #cbd5e1; --neg-bg: #262636;
  --shadow: none; color-scheme: dark;
}

* { 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, "Helvetica Neue", Arial, sans-serif;
}
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.25; }
.muted { color: var(--muted); }

/* ---------- Cabecera ---------- */
.top {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 1.15rem; color: var(--text); text-decoration: none; }
.logo { display: grid; place-items: center; width: 32px; height: 32px; border-radius: 9px; background: var(--accent); color: var(--surface); font-weight: 800; }
.top-actions { display: flex; gap: 8px; align-items: center; }
.lang, .unit {
  font: inherit; font-size: .9rem; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 6px 8px;
}
.icon-btn {
  font: inherit; font-size: 1.1rem; width: 38px; height: 38px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); cursor: pointer;
}
.icon-btn.fav.on { color: #d49b00; }

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

/* ---------- Inicio ---------- */
.hero { padding: 16px 0 8px; }
.hero h1 { font-size: clamp(1.4rem, 4vw, 2rem); margin: 0 0 4px; }
.hero p { margin: 0 0 16px; }
.search {
  width: 100%; font: inherit; font-size: 1.05rem; padding: 14px 16px; border-radius: var(--radius);
  border: 1px solid var(--border); background: var(--surface); color: var(--text); box-shadow: var(--shadow);
}
.search:focus-visible, input:focus-visible, select:focus-visible, button:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.cat h2 { font-size: .85rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 28px 0 10px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 10px; }
.card {
  display: block; padding: 14px 16px; border-radius: var(--radius); background: var(--surface); border: 1px solid var(--border);
  text-decoration: none; color: var(--text); box-shadow: var(--shadow); transition: border-color .15s, transform .15s;
}
.card:hover { border-color: var(--accent); transform: translateY(-1px); }
.card-name { font-weight: 700; display: flex; justify-content: space-between; gap: 8px; }
.card-title { color: var(--muted); font-size: .9rem; margin-top: 2px; }
.star { color: #d49b00; }
.empty { color: var(--muted); padding: 24px 0; }

/* ---------- Calculadora ---------- */
.back { display: inline-block; margin: 4px 0 8px; text-decoration: none; font-weight: 600; }
.calc-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.calc-head h1 { margin: 2px 0 6px; font-size: clamp(1.3rem, 3.5vw, 1.75rem); }
.calc-head p { margin: 0; }
.chip { display: inline-block; font-size: .78rem; font-weight: 600; color: var(--accent); background: var(--accent-soft); padding: 2px 10px; border-radius: 999px; }
.layout { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 20px; margin-top: 18px; align-items: start; }
.side { position: sticky; top: 72px; display: grid; gap: 10px; }
@media (max-width: 860px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  /* En móvil solo el resultado queda fijo arriba; los botones van tras el formulario. */
  .side { display: contents; }
  .result { order: -1; position: sticky; top: 58px; z-index: 5; }
}

.form { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 8px 16px 16px; box-shadow: var(--shadow); }
.fhead { font-size: .8rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 18px 0 6px; }
.fnote { font-size: .9rem; color: var(--muted); margin: 6px 0; }
.field { margin: 14px 0; }
.field > label { display: block; font-weight: 600; font-size: .95rem; margin-bottom: 6px; }
.opt-tag { font-weight: 400; color: var(--muted); }
.input-row { display: flex; gap: 8px; align-items: center; }
.input-row input[type="text"] {
  font: inherit; width: 120px; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text);
}
.input-row .scale-num { width: 80px; }
.unit-text { color: var(--muted); font-size: .9rem; }
.range { flex: 1; min-width: 0; accent-color: var(--accent); height: 32px; }
.range.unset { opacity: .35; }

.check {
  display: flex; align-items: flex-start; gap: 10px; padding: 10px 12px; margin: 6px 0; border-radius: 10px;
  border: 1px solid var(--border); cursor: pointer; background: var(--surface);
}
.check:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.check input { margin-top: 3px; width: 18px; height: 18px; accent-color: var(--accent); flex: none; }
.ctext { flex: 1; }
.pts { flex: none; font-weight: 700; font-size: .85rem; color: var(--accent); background: var(--accent-soft); border-radius: 8px; padding: 1px 8px; align-self: center; }
.pts.neg { color: var(--high); background: var(--high-bg); }

.choice { border: 0; margin: 14px 0; padding: 0; }
.choice legend { font-weight: 600; font-size: .95rem; margin-bottom: 6px; padding: 0; }
.opt { display: flex; gap: 10px; align-items: flex-start; padding: 9px 12px; margin: 4px 0; border: 1px solid var(--border); border-radius: 10px; cursor: pointer; }
.opt:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.opt input { margin-top: 4px; accent-color: var(--accent); flex: none; }

.btn {
  font: inherit; font-weight: 600; padding: 10px 14px; border-radius: 10px; border: 1px solid var(--accent);
  background: var(--accent); color: var(--surface); cursor: pointer;
}
.btn.ghost { background: transparent; color: var(--accent); }
.joint-btn { width: 100%; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.actions .btn { flex: 1; }

/* ---------- Resultado ---------- */
.result { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.rlabel { margin: 0; font-size: .78rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 600; }
.rscore { margin: 2px 0; font-size: 2.2rem; font-weight: 800; font-variant-numeric: tabular-nums; line-height: 1.15; }
.rmax { font-size: 1rem; font-weight: 500; color: var(--muted); }
.rband { margin: 4px 0 0; font-weight: 700; }
.rextra { margin: 8px 0 0; font-weight: 600; padding: 6px 10px; border-radius: 8px; }
.rlines { margin: 10px 0 0; padding-left: 18px; font-size: .9rem; color: var(--muted); }
.rlines li { margin: 3px 0; }
.rmissing { margin: 6px 0 0; font-weight: 600; }
.rmissing-list { margin: 4px 0 0; color: var(--muted); font-size: .9rem; }
@media (max-width: 860px) {
  .rscore { font-size: 1.7rem; }
  .rlines { display: none; }
  .result { padding: 10px 14px; }
}

.tone-ok { border-color: var(--ok); } .tone-ok .rband, .rextra.tone-ok { color: var(--ok); } .rextra.tone-ok, .result.tone-ok { background: var(--ok-bg); }
.tone-low { border-color: var(--low); } .tone-low .rband, .rextra.tone-low { color: var(--low); } .rextra.tone-low, .result.tone-low { background: var(--low-bg); }
.tone-mod { border-color: var(--mod); } .tone-mod .rband, .rextra.tone-mod { color: var(--mod); } .rextra.tone-mod, .result.tone-mod { background: var(--mod-bg); }
.tone-high { border-color: var(--high); } .tone-high .rband, .rextra.tone-high { color: var(--high); } .rextra.tone-high, .result.tone-high { background: var(--high-bg); }
.tone-vhigh { border-color: var(--vhigh); } .tone-vhigh .rband, .rextra.tone-vhigh { color: var(--vhigh); } .rextra.tone-vhigh, .result.tone-vhigh { background: var(--vhigh-bg); }
.tone-pos { border-color: var(--pos); } .tone-pos .rband, .rextra.tone-pos { color: var(--pos); } .rextra.tone-pos, .result.tone-pos { background: var(--pos-bg); }
.tone-neg { border-color: var(--border); } .tone-neg .rband { color: var(--neg); } .result.tone-neg { background: var(--neg-bg); }
.rextra.tone-info { background: var(--surface-2); color: var(--text); }

/* ---------- Información ---------- */
.info { margin-top: 24px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 4px 16px 12px; }
.info h3 { font-size: .85rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 16px 0 6px; }
.info p { margin: 0 0 8px; }
.formula { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .9rem; background: var(--surface-2); padding: 10px 12px; border-radius: 10px; overflow-wrap: anywhere; }
.refs { padding-left: 20px; font-size: .9rem; }
.refs li { margin: 6px 0; overflow-wrap: anywhere; }

/* ---------- Selector de articulaciones ---------- */
.jdialog { width: min(560px, calc(100vw - 32px)); max-height: calc(100vh - 32px); border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text); padding: 16px; }
.jdialog::backdrop { background: rgba(10, 10, 30, .5); }
.jdialog h2 { margin: 0 0 4px; font-size: 1.15rem; }
.jscroll { max-height: 55vh; overflow: auto; margin: 10px 0; border: 1px solid var(--border); border-radius: 10px; }
.jtable { width: 100%; border-collapse: collapse; font-size: .92rem; }
.jtable th, .jtable td { padding: 6px 8px; border-bottom: 1px solid var(--border); text-align: center; }
.jtable tbody th { text-align: left; font-weight: 500; }
.jtable thead th { position: sticky; top: 0; background: var(--surface-2); }
.jtable thead tr:nth-child(2) th { top: 33px; }
.jbox { width: 22px; height: 22px; accent-color: var(--high); cursor: pointer; }
.jbox.s { accent-color: var(--accent); }
.na { color: var(--muted); }
.jcounts { font-weight: 700; }

.foot { max-width: 1100px; margin: 16px auto 32px; padding: 0 16px; color: var(--muted); font-size: .85rem; }
