:root {
  --bg: #05060d; --panel: rgba(14, 17, 32, .82); --panel-solid: #0e1120; --line: rgba(130, 150, 255, .16);
  --text: #e6e9ff; --muted: #8a91b4; --accent: #7c8cff; --accent2: #3ee6ff; --good: #4dff9a; --warn: #ffc94d; --bad: #ff5c7a;
  --r: 12px; --left-w: 290px; --right-w: 360px; --head-h: 56px;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overflow: hidden; }
body { display: flex; flex-direction: column; }
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }

header#top { height: var(--head-h); display: flex; align-items: center; gap: 16px; padding: 0 16px; border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, #0b0e1d, #070914); position: relative; z-index: 5; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; letter-spacing: .3px; font-size: 18px; white-space: nowrap; }
.logo { position: relative; width: 26px; height: 24px; }
.logo i { position: absolute; width: 11px; height: 11px; border-radius: 50%; box-shadow: 0 0 10px currentColor; }
.logo i:nth-child(1) { left: 0; bottom: 0; background: #ff7a59; color: #ff7a59; }
.logo i:nth-child(2) { right: 0; bottom: 0; background: #ff7a59; color: #ff7a59; }
.logo i:nth-child(3) { left: 7.5px; top: 0; background: #4fc3f7; color: #4fc3f7; }
.tabs { display: flex; gap: 6px; margin-left: 8px; overflow-x: auto; scrollbar-width: none; }
.tab { background: transparent; border: 1px solid transparent; padding: 8px 14px; border-radius: 999px; color: var(--muted); cursor: pointer; white-space: nowrap; transition: .2s; }
.tab:hover { color: var(--text); background: rgba(124, 140, 255, .08); }
.tab.active { color: #fff; background: linear-gradient(135deg, rgba(124,140,255,.35), rgba(62,230,255,.18)); border-color: rgba(124,140,255,.5); box-shadow: 0 0 18px rgba(124,140,255,.25); }
.icon-btn { margin-left: auto; width: 34px; height: 34px; border-radius: 50%; border: 1px solid var(--line); background: rgba(255,255,255,.04); cursor: pointer; font-weight: 700; flex: none; }
.icon-btn:hover { border-color: var(--accent); }

main#app { flex: 1; position: relative; min-height: 0; }
#stage { position: absolute; inset: 0; }
#c { width: 100%; height: 100%; display: block; touch-action: none; outline: none; }
.panel { position: absolute; top: 12px; bottom: 12px; z-index: 3; background: var(--panel); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border: 1px solid var(--line); border-radius: var(--r); overflow-y: auto; padding: 14px 16px; scrollbar-width: thin; scrollbar-color: #2a3160 transparent; }
#left { left: 12px; width: var(--left-w); }
#right { right: 12px; width: var(--right-w); }
.panel h2 { font-size: 11.5px; text-transform: uppercase; letter-spacing: 1.4px; color: var(--muted); margin: 14px 0 8px; font-weight: 600; }
.panel h2:first-child { margin-top: 0; }
.hint { font-size: 12.5px; color: var(--muted); margin: 0 0 10px; line-height: 1.45; }
.touch-only { display: none; }
.mobile-only, .tab .short, footer .less { display: none; }
.row .btn { white-space: nowrap; font-size: 12.5px; padding: 8px 6px; }
@media (hover: none) { .touch-only { display: block; } .desktop-only { display: none; } }

.palette { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; }
.qbtn { aspect-ratio: 1; border-radius: 50%; border: 1px solid rgba(255,255,255,.12); cursor: grab; position: relative; display: grid; place-items: center;
  font-weight: 700; font-size: 16px; background: radial-gradient(circle at 35% 30%, color-mix(in srgb, var(--c) 90%, white), var(--c) 45%, color-mix(in srgb, var(--c) 40%, black));
  color: #0b0d18; box-shadow: 0 0 12px color-mix(in srgb, var(--c) 55%, transparent); transition: transform .15s, box-shadow .15s; user-select: none; -webkit-user-select: none; }
.qbtn.anti { background: radial-gradient(circle at 35% 30%, color-mix(in srgb, var(--c) 30%, #1a1d33), #121428 70%); color: var(--c); border: 2px solid var(--c); }
.qbtn:hover { transform: scale(1.08); }
.qbtn.sel { box-shadow: 0 0 0 3px #fff, 0 0 22px var(--c); }
.qbtn .bar { text-decoration: overline; }
.qbtn small { position: absolute; bottom: -2px; right: -4px; font-size: 9px; background: #0b0d18; color: var(--text); border-radius: 6px; padding: 1px 3px; border: 1px solid var(--line); font-weight: 600; }
.pal-label { grid-column: 1 / -1; font-size: 11px; color: var(--muted); margin-top: 4px; }

.examples { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { border: 1px solid var(--line); background: rgba(255,255,255,.03); border-radius: 999px; padding: 5px 10px; font-size: 12.5px; cursor: pointer; transition: .15s; }
.chip:hover { border-color: var(--accent); background: rgba(124,140,255,.12); }
.chip.exotic { border-color: rgba(255, 64, 129, .35); }
.row { display: flex; gap: 8px; margin: 8px 0; }
.btn { flex: 1; border: 1px solid var(--line); background: rgba(255,255,255,.05); border-radius: 9px; padding: 8px 10px; cursor: pointer; font-size: 13px; transition: .15s; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: linear-gradient(135deg, #5664ff, #2bb8d9); border: none; color: #fff; font-weight: 600; }
.legend { margin-top: 14px; font-size: 12px; display: grid; gap: 6px; color: var(--text); }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; background: var(--c); box-shadow: 0 0 8px var(--c); margin-right: 4px; vertical-align: -1px; }
.wave { display: inline-block; width: 30px; height: 10px; margin-right: 6px; vertical-align: -1px;
  background: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 10'><path d='M0 5 Q3.75 0 7.5 5 T15 5 T22.5 5 T30 5' fill='none' stroke='%23ffd36e' stroke-width='1.6'/></svg>"); }

.counter { display: grid; grid-template-columns: 1fr 34px 46px 34px; align-items: center; gap: 6px; margin: 8px 0; }
.counter .lbl { font-size: 14px; }
.counter output { text-align: center; font-variant-numeric: tabular-nums; font-size: 18px; font-weight: 700; }
.pm { height: 34px; border-radius: 9px; border: 1px solid var(--line); background: rgba(255,255,255,.05); cursor: pointer; font-size: 18px; }
.pm:hover { border-color: var(--accent); }
.totals { font-size: 12.5px; color: var(--muted); line-height: 1.6; border-top: 1px solid var(--line); padding-top: 10px; }
.totals b { color: var(--text); }

/* Info cards */
#info { font-size: 13.5px; line-height: 1.5; }
.card-head { display: flex; align-items: center; gap: 12px; margin-bottom: 10px; }
.big-sym { width: 58px; height: 58px; border-radius: 14px; display: grid; place-items: center; font-size: 26px; font-weight: 800; flex: none;
  background: radial-gradient(circle at 35% 30%, color-mix(in srgb, var(--c) 80%, white), var(--c) 50%, color-mix(in srgb, var(--c) 35%, black)); color: #0b0d18;
  box-shadow: 0 0 22px color-mix(in srgb, var(--c) 50%, transparent); }
.big-sym.anti { background: #121428; color: var(--c); border: 2px solid var(--c); }
.card-head h3 { margin: 0; font-size: 20px; }
.card-head .sub { color: var(--muted); font-size: 12.5px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 10px 0; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.sec { margin-top: 12px; }
.sec h4 { margin: 0 0 4px; font-size: 12px; text-transform: uppercase; letter-spacing: 1.2px; color: var(--accent2); }
.sec p, .sec ul { margin: 0; }
.sec ul { padding-left: 18px; }
.sec li { margin: 3px 0; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; border: 1px solid currentColor; }
.badge.stable { color: var(--good); } .badge.long { color: #9be15d; } .badge.weak { color: var(--warn); } .badge.unstable { color: var(--warn); }
.badge.strong, .badge.very-unstable { color: var(--bad); } .badge.none { color: var(--muted); }
.callout { background: rgba(124,140,255,.08); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; margin-top: 10px; font-size: 12.5px; }
.callout.warn { background: rgba(255, 92, 122, .08); border-color: rgba(255,92,122,.3); }
.elem-tile { width: 92px; height: 92px; border-radius: 14px; border: 1px solid var(--line); background: linear-gradient(145deg, rgba(124,140,255,.25), rgba(62,230,255,.08));
  display: grid; place-items: center; position: relative; flex: none; }
.elem-tile .s { font-size: 34px; font-weight: 800; }
.elem-tile .z { position: absolute; left: 8px; top: 5px; font-size: 12px; color: var(--muted); }
.elem-tile .a { position: absolute; right: 8px; top: 5px; font-size: 12px; color: var(--muted); }
.shellbar { display: flex; gap: 4px; flex-wrap: wrap; }
.shellbar span { background: rgba(125, 249, 255, .1); border: 1px solid rgba(125,249,255,.35); color: #bffcff; border-radius: 6px; padding: 1px 7px; font-size: 12px; font-variant-numeric: tabular-nums; }
.btn-inline { display: inline-block; margin-top: 10px; }

/* Labels over 3D */
#labels { position: absolute; inset: 0; pointer-events: none; overflow: hidden; z-index: 2; }
.hlabel { position: absolute; transform: translate(-50%, -100%); padding: 4px 10px; border-radius: 10px; font-size: 13px; white-space: nowrap; pointer-events: auto; cursor: pointer;
  background: rgba(10, 12, 26, .82); border: 1px solid rgba(124,140,255,.5); box-shadow: 0 0 16px rgba(124,140,255,.25); transition: opacity .2s; }
.hlabel b { font-size: 15px; }
.hlabel .q { color: var(--muted); margin-left: 6px; font-size: 12px; }
.hlabel.bad { border-color: rgba(255,92,122,.6); color: #ffc2cf; box-shadow: 0 0 14px rgba(255,92,122,.25); }
.hlabel.top { border-color: rgba(255,64,129,.7); }
#hud { position: absolute; left: 50%; top: 16px; transform: translateX(-50%); z-index: 2; pointer-events: none; text-align: center; text-shadow: 0 0 18px rgba(124,140,255,.6); }
#hud .el { font-size: 40px; font-weight: 800; letter-spacing: 1px; }
#hud .el sup, #hud .el sub { font-size: 16px; font-weight: 600; }
#hud .nm { color: var(--muted); font-size: 14px; }
#toast { position: absolute; left: 50%; bottom: 22px; transform: translate(-50%, 20px); opacity: 0; z-index: 4; background: rgba(14,17,32,.95); border: 1px solid var(--line);
  border-radius: 12px; padding: 10px 16px; font-size: 13.5px; max-width: min(560px, 90vw); text-align: center; transition: .3s; pointer-events: none; }
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#infoToggle { position: absolute; right: 12px; bottom: 12px; z-index: 4; flex: none; }

footer#note { font-size: 11.5px; color: var(--muted); padding: 7px 16px; border-top: 1px solid var(--line); background: #070914; line-height: 1.4; }
footer#note strong { color: var(--warn); }

/* Modals */
.modal { position: fixed; inset: 0; z-index: 20; background: rgba(3, 4, 10, .72); backdrop-filter: blur(6px); display: grid; place-items: center; padding: 16px; }
.modal-card { background: var(--panel-solid); border: 1px solid var(--line); border-radius: 16px; padding: 18px 20px; max-width: 560px; width: 100%; max-height: 92vh; overflow: auto; box-shadow: 0 20px 80px rgba(0,0,0,.6); }
.modal-card.wide { max-width: 1180px; }
.modal-head { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.modal-head h2 { margin: 0; font-size: 20px; }
.modal-head .muted { font-size: 13px; }
.help { padding-left: 20px; line-height: 1.6; font-size: 14px; }
.help li { margin-bottom: 8px; }
.ptable { display: grid; grid-template-columns: repeat(18, minmax(34px, 1fr)); gap: 3px; min-width: 700px; }
.pt-wrap { overflow-x: auto; }
.pel { border-radius: 6px; border: 1px solid rgba(255,255,255,.08); padding: 3px 4px 4px; cursor: pointer; position: relative; background: color-mix(in srgb, var(--c) 18%, #0d1022);
  transition: transform .12s, box-shadow .12s; min-height: 50px; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.pel:hover, .pel:focus-visible { transform: scale(1.12); z-index: 2; box-shadow: 0 0 16px var(--c); border-color: var(--c); outline: none; }
.pel .zz { position: absolute; top: 2px; left: 4px; font-size: 9px; color: var(--muted); }
.pel .ss { font-weight: 700; font-size: 16px; color: color-mix(in srgb, var(--c) 60%, white); }
.pel .nn { font-size: 8.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.pel.cur { box-shadow: 0 0 0 2px #fff; }
.pt-gap { grid-column: 3; display: grid; place-items: center; font-size: 9px; color: var(--muted); }
.pt-spacer { grid-column: 1 / -1; height: 10px; }
.pt-legend { display: flex; flex-wrap: wrap; gap: 10px 16px; margin-top: 12px; font-size: 12px; color: var(--muted); }

#tip { position: fixed; z-index: 30; pointer-events: none; background: rgba(8, 10, 22, .96); border: 1px solid var(--line); border-radius: 8px; padding: 6px 9px;
  font-size: 12.5px; max-width: 280px; opacity: 0; transition: opacity .12s; line-height: 1.4; }
#tip.show { opacity: 1; }

/* Collapsible bottom panel (phones and portrait tablets) */
.panel-toggle { display: none; width: 100%; margin: -2px 0 6px; padding: 4px 8px; border: 1px solid var(--line); border-radius: 8px; background: rgba(255,255,255,.04); color: var(--muted); font-size: 12px; cursor: pointer; }
.panel-toggle:hover { color: var(--text); border-color: var(--accent); }
#left.collapsed > :not(#panelToggle) { display: none !important; }
#left.collapsed { padding-bottom: 6px; }

/* Responsive: tablets/phones */
@media (max-width: 1100px) { :root { --left-w: 250px; --right-w: 300px; } }
@media (max-width: 820px) {
  :root { --head-h: 52px; }
  header#top { gap: 8px; padding: 0 10px; }
  .brand span:last-child { display: none; }
  .tab { padding: 7px 10px; font-size: 13px; }
  .tab .long, footer .more { display: none; } .tab .short, footer .less { display: inline; }
  .mobile-only { display: block; }
  #infoClose { position: sticky; top: -10px; margin: -4px 0 8px auto; z-index: 2; background: var(--panel-solid); flex: none; width: auto; padding: 5px 12px; }
  #left { top: auto; bottom: 8px; left: 8px; right: 8px; width: auto; max-height: 40%; padding: 10px 12px; }
  #left .hint { display: none; }
  .palette { grid-template-columns: repeat(12, minmax(34px, 1fr)); overflow-x: auto; }
  .pal-label { display: none; }
  .legend { display: none; }
  #right { top: 8px; bottom: auto; left: 8px; right: 8px; width: auto; max-height: 55%; transform: translateY(-120%); transition: transform .3s; z-index: 6; }
  #right.open { transform: none; }
  #infoToggle { display: block; bottom: auto; top: 10px; right: 10px; flex: none; width: auto; }
  #hud { top: 8px; }
  #hud .el { font-size: 28px; }
  footer#note { font-size: 10.5px; padding: 5px 10px; }
  #toast { bottom: calc(40% + 20px); }
}

.qlink { color: var(--accent2); text-decoration: none; font-weight: 700; font-size: 15px; margin-right: 2px; }
.qlink:hover { text-decoration: underline; }
.kv dt .qlink { font-size: 13px; font-weight: 600; }

/* Phones + portrait tablets: controls dock at the bottom; the 3D view is framed in the space above */
@media (max-width: 820px), (max-width: 1100px) and (orientation: portrait) {
  .panel-toggle { display: block; }
  #left { top: auto; bottom: 8px; left: 8px; right: 8px; width: auto; max-height: 40%; padding: 10px 12px; }
  #left .hint, .pal-label, .legend { display: none; }
  .palette { grid-template-columns: repeat(12, minmax(34px, 52px)); overflow-x: auto; }
}
/* Portrait tablets wider than phones: keep the info panel as a side panel above the docked controls */
@media (min-width: 821px) and (max-width: 1100px) and (orientation: portrait) {
  #left { max-height: 32%; }
  #right { top: 12px; bottom: calc(32% + 20px); }
  .palette { grid-template-columns: repeat(12, minmax(34px, 52px)); }
}
/* Landscape tablets: slimmer side panels so the scene keeps a usable centre */
@media (min-width: 821px) and (max-width: 1100px) and (orientation: landscape) {
  :root { --left-w: 236px; --right-w: 280px; }
  .panel { padding: 12px 12px; }
}

/* Element background info (atom card): collapsible sections */
.einfo { margin-top: 14px; }
.einfo > h4 { margin-bottom: 6px; }
.isec { border: 1px solid var(--line); border-radius: 10px; background: rgba(255,255,255,.025); margin: 6px 0; }
.isec > summary { cursor: pointer; list-style: none; padding: 8px 10px; font-weight: 600; font-size: 13px; display: flex; align-items: center; gap: 8px; border-radius: 10px; user-select: none; }
.isec > summary::-webkit-details-marker { display: none; }
.isec > summary::before { content: '▸'; color: var(--accent2); transition: transform .2s; display: inline-block; width: 10px; }
.isec[open] > summary::before { transform: rotate(90deg); }
.isec > summary:hover, .isec > summary:focus-visible { background: rgba(124,140,255,.10); outline: none; }
.isec-body { padding: 0 10px 10px; font-size: 13px; overflow-wrap: anywhere; }
.isec-body p { margin: 6px 0 0; }
.isec-body .kv { margin: 4px 0 0; grid-template-columns: minmax(96px, auto) 1fr; font-size: 12.5px; }
.isec.sub { margin-top: 8px; background: transparent; }
.isec.sub > summary { font-size: 12px; font-weight: 500; color: var(--muted); padding: 6px 8px; }
.cfg sup { font-size: 9px; }
.einfo .src { font-size: 11px; color: var(--muted); margin-top: 8px; }
.einfo .src a { color: var(--accent2); }
@media (max-width: 820px) {
  .isec > summary { padding: 10px 10px; }  /* bigger touch targets */
  .isec-body .kv { grid-template-columns: minmax(84px, 38%) 1fr; }
}
/* Atom view on phones / portrait tablets: shorter info sheet so the atom stays visible in the gap below it */
@media (max-width: 820px) {
  body[data-mode="atom"] #right { max-height: 46%; }
}
/* Narrow side panel (e.g. landscape tablets): stack label above value */
#info { container-type: inline-size; }
@container (max-width: 300px) {
  .isec-body .kv { grid-template-columns: 1fr; gap: 0; }
  .isec-body .kv dt { font-size: 11.5px; }
  .isec-body .kv dd { margin-bottom: 5px; }
}
