@import "./tokens.css";

* { box-sizing: border-box; }
html, body { margin: 0; width: 100%; height: 100%; overflow: hidden; background: var(--bg); color: var(--text); }
body { font-family: var(--font-ui); font-size: 14px; -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility; }
button, select, input { font: inherit; }
button { border: 0; color: inherit; }
svg { display: block; }
.hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }

/* ---------- shared controls ---------- */
.segmented { display: inline-flex; min-width: 0; border: 1px solid var(--line); background: var(--surface-0); }
.segment { min-height: 30px; padding: 0 11px; background: transparent; color: var(--muted); cursor: pointer; font-size: 12px; font-weight: 600; white-space: nowrap; }
.segment:hover { color: var(--text-2); background: var(--surface-1); }
.segment.active { color: var(--text); background: var(--surface-2); }
.segment + .segment { border-left: 1px solid var(--line); }

.icon-button { position: relative; width: 34px; height: 34px; display: grid; place-items: center; background: transparent; color: var(--muted); border: 1px solid transparent; cursor: pointer; }
.icon-button:hover { color: var(--text-2); background: var(--surface-1); border-color: var(--line); }
.icon-button.active, .icon-button[aria-pressed="true"] { color: var(--text); background: var(--surface-2); border-color: var(--line-strong); }
.icon-button svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
.icon-button.compact { width: 28px; height: 28px; font-size: 18px; }

.field { display: grid; gap: 5px; margin: 0 0 10px; color: var(--text-2); font-size: 11.5px; }
.field select, .hkl-grid input, .catalog-toolbar select { width: 100%; min-height: var(--control-h); padding: 5px 8px; border: 1px solid var(--line); border-radius: 2px; outline: 0; background: var(--surface-0); color: var(--text); }
.field select:focus, .hkl-grid input:focus, .catalog-toolbar select:focus, .search-field:focus-within { border-color: var(--line-strong); }

.text-button, .preset-grid button, .catalog-jump, .layer-jump { min-height: 31px; border: 1px solid var(--line); border-radius: 2px; background: transparent; color: var(--text-2); cursor: pointer; }
.text-button:hover, .preset-grid button:hover, .catalog-jump:hover, .layer-jump:hover, .text-button.active { background: var(--surface-2); border-color: var(--line-strong); color: var(--text); }

/* ---------- application frame ---------- */
.app-shell { height: 100vh; display: grid; grid-template-rows: var(--toolbar-h) minmax(0, 1fr); background: var(--bg); }
.toolbar { min-width: 0; display: grid; grid-template-columns: auto minmax(0, 1fr); align-items: center; gap: 14px; padding: 6px 10px; border-bottom: 1px solid var(--line); background: var(--surface-0); }
.mode-switch { justify-self: start; }
.mode-switch .segment { min-width: 116px; display: flex; align-items: center; justify-content: center; gap: 7px; }
.mode-switch svg, .search-field svg, #symmetryApplyBtn svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
.toolbar-actions { min-width: 0; justify-self: end; display: flex; align-items: center; gap: 3px; }
.space-switch { margin-right: 3px; }
.repeat-global-icon { display: none; }
#repeatToggleBtn[aria-pressed="true"] .repeat-local-icon { display: none; }
#repeatToggleBtn[aria-pressed="true"] .repeat-global-icon { display: block; }

.tool-menu { position: relative; }
.tool-menu > summary { list-style: none; }
.tool-menu > summary::-webkit-details-marker { display: none; }
.menu-popover { position: absolute; top: 39px; right: 0; z-index: 80; display: grid; grid-template-columns: repeat(5, 50px); padding: 4px; border: 1px solid var(--line-strong); background: var(--surface-1); box-shadow: 0 12px 28px rgba(0,0,0,.34); }
.menu-popover button { height: 44px; display: grid; place-items: center; gap: 1px; background: transparent; color: var(--muted); cursor: pointer; font-size: 10.5px; }
.menu-popover button:hover { background: var(--surface-2); color: var(--text); }

.workspace { min-height: 0; display: grid; grid-template-columns: var(--inspector-w) minmax(0, 1fr) var(--theory-w); background: var(--surface-0); }
.theory-closed .workspace { grid-template-columns: var(--inspector-w) minmax(0, 1fr); }
.theory-closed .theory-panel { display: none; }

/* ---------- inspector ---------- */
.inspector { min-height: 0; border-right: 1px solid var(--line); background: var(--surface-1); }
.inspector-scroll { height: 100%; overflow: auto; padding: 12px 13px 18px; }
.inspector-section { padding: 16px 0 18px; border-bottom: 1px solid var(--line); }
.inspector-section:first-child { padding-top: 2px; }
.inspector-section:last-child { border-bottom: 0; }
/* 分组标题：加左侧色条 + 提高字号和对比度，让三个板块一眼可分（颜色保持中性，
   语义色只留给物理含义，见 docs/design-system.md）。 */
.inspector-section > h2 { display: flex; align-items: center; gap: 7px; margin: 0 0 12px; color: var(--text); font-size: 12.5px; line-height: 1.2; font-weight: 700; letter-spacing: .03em; }
.inspector-section > h2::before { content: ''; flex: none; width: 3px; height: 12px; border-radius: 1px; background: var(--line-strong); }
.object-identity { display: grid; gap: 2px; padding-top: 1px; line-height: 1.35; }
.object-identity span { color: var(--muted); font-size: 11px; }
.control-block { display: grid; gap: 8px; padding: 4px 0 7px; }
.control-block + .control-block { border-top: 1px solid rgba(53,61,68,.45); padding-top: 10px; }
.control-details { display: grid; gap: 8px; padding-left: 21px; }
.toggle-row { min-height: 26px; display: flex; align-items: center; gap: 8px; color: var(--text-2); font-size: 12px; cursor: pointer; }
.toggle-row.subtle { padding-top: 7px; border-top: 1px solid var(--line); color: var(--muted); }
.toggle-row input { width: 13px; height: 13px; margin: 0; accent-color: #98a4aa; }
.hkl-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 5px; }
.hkl-grid label { display: grid; grid-template-columns: 14px 1fr; align-items: center; gap: 3px; color: var(--muted); font-size: 11px; }
.hkl-grid input { min-width: 0; min-height: 31px; text-align: center; }
.preset-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; }
.preset-grid button { min-width: 0; font-size: 11px; }
.text-button { display: flex; align-items: center; justify-content: center; gap: 6px; font-size: 11.5px; }
#symmetryApplyBtn svg { width: 14px; height: 14px; fill: currentColor; stroke: none; }
.micro-readout { min-height: 14px; color: var(--muted); font-size: 10.5px; line-height: 1.45; }

/* ---------- 3D stage ---------- */
.stage-shell { min-width: 0; min-height: 0; background: #090b0d; }
.stage { position: relative; width: 100%; height: 100%; min-width: 0; min-height: 0; overflow: hidden; background: #090b0d; }
.stage-viewport { position: absolute; inset: 0; }
.stage-viewport canvas { display: block; width: 100%; height: 100%; }
.stage-meta { position: absolute; z-index: 5; top: 12px; left: 14px; display: flex; align-items: center; gap: 12px; min-height: 28px; pointer-events: none; }
.stage-title { display: flex; align-items: center; gap: 7px; font-size: 12px; }
.stage-title strong { font-weight: 650; }
.stage-caption { color: var(--muted); font: 10.5px var(--font-mono); }
.space-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--direct); }
.space-dot.is-reciprocal { background: var(--reciprocal); }
.stage-legend { position: absolute; z-index: 4; left: 14px; bottom: 82px; max-width: 70%; display: flex; flex-wrap: wrap; gap: 5px 13px; color: var(--muted); font-size: 10.5px; line-height: 1.4; pointer-events: none; }
.legend-item { display: inline-flex; align-items: center; gap: 5px; white-space: nowrap; }
.legend-mark { width: 5px; height: 5px; border-radius: 50%; background: var(--muted); }
.legend-mark.reciprocal { background: var(--reciprocal); }
.legend-mark.symmetry { background: var(--symmetry); }
.legend-mark.ws { background: var(--ws); }
.legend-mark.bz { background: var(--bz); }

.stage-readout { position: absolute; z-index: 5; right: 14px; top: 12px; min-width: 188px; max-width: 250px; padding: 8px 0 7px 11px; border-left: 1px solid var(--line-strong); background: rgba(9,11,13,.78); font-size: 10.5px; }
.stage-readout:empty { display: none; }
.readout-title { margin-bottom: 5px; color: var(--text); font-size: 11px; font-weight: 650; }
.readout-row { display: flex; justify-content: space-between; gap: 16px; padding: 2px 0; color: var(--muted); }
.readout-row b { color: var(--text-2); font-weight: 600; }
.readout-separator { height: 1px; margin: 5px 0; background: var(--line); }

.layer-dock { position: absolute; z-index: 6; left: 50%; bottom: 12px; transform: translateX(-50%); max-width: calc(100% - 24px); padding: 4px; border: 1px solid var(--line); background: rgba(13,15,17,.94); overflow-x: auto; }
.layer-grid { display: grid; grid-template-columns: 58px repeat(4, minmax(70px, 1fr)); align-items: center; min-width: 358px; }
.layer-grid + .layer-grid { border-top: 1px solid rgba(37,43,48,.7); }
.layer-grid-shared .layer-button { grid-column: 2 / 6; }
.layer-space-label { padding: 0 8px 0 4px; color: var(--dim); font-size: 9.5px; letter-spacing: .04em; }
.layer-button { height: 28px; padding: 0 8px; background: transparent; color: var(--muted); cursor: pointer; font-size: 10.8px; font-weight: 600; white-space: nowrap; }
.layer-button:hover { color: var(--text-2); background: var(--surface-2); }
.layer-button.active { color: var(--text); background: var(--surface-3); }
.layer-button.is-context-hidden { visibility: hidden; pointer-events: none; }
.layer-button i { display: inline-block; width: 4px; height: 4px; margin-right: 6px; border-radius: 50%; background: currentColor; vertical-align: 2px; }
.layer-button[data-layer="primitive"] i, .layer-button[data-layer="ws"] i, .layer-button[data-layer="conventional"] i, .layer-button[data-layer="family"] i { background: var(--direct); }
.layer-button[data-layer="recipPrim"] i, .layer-button[data-layer="recipConv"] i, .layer-button[data-layer="bz"] i { background: var(--reciprocal); }
.layer-button[data-layer="g"] i { background: var(--symmetry); }
.reciprocal-layer-row { display: none; }
html[data-space-view="reciprocal"] .direct-layer-row { display: none; }
html[data-space-view="reciprocal"] .reciprocal-layer-row { display: grid; }
html[data-space-view="overlay"] .direct-layer-row, html[data-space-view="overlay"] .reciprocal-layer-row { display: grid; }

/* ---------- theory ---------- */
/* 面板是弹性列：内容区自己撑满剩余高度，底部声明贴底。
   原来给 .theory-content 写死的是 height: calc(100% - 78px)——那 78px 是「头部 32 + 页签与间距」
   手算出来的，一加页脚就得重算一次；让 flex 自己算，以后加减顶栏元素都不用再动这个数。 */
.theory-panel { min-height: 0; border-left: 1px solid var(--line); background: var(--surface-1); padding: 10px 13px 14px; overflow: hidden; display: flex; flex-direction: column; }
/* 抽屉顶部只剩关闭按钮：页签本身就是标题，再放一个同名的 h2 是重复。
   单个 flex 子项配 space-between 会把它推到最左，所以这里要 flex-end。 */
.theory-header { height: 32px; display: flex; align-items: center; justify-content: flex-end; }
.theory-tabs { display: flex; width: 100%; margin: 6px 0 10px; }
.theory-tabs .segment { flex: 1; }
.theory-content { flex: 1; min-height: 0; overflow: auto; padding-right: 7px; color: var(--text-2); font-size: 12.5px; line-height: 1.67; overflow-wrap: normal; word-break: normal; }
.theory-footer { flex: none; margin-top: 9px; padding-top: 9px; border-top: 1px solid var(--line); font-size: 11.5px; }
.theory-footer a { color: var(--muted); text-decoration: none; }
.theory-footer a:hover { color: var(--text); text-decoration: underline; }
.theory-content section { padding: 11px 0 13px; border-bottom: 1px solid var(--line); }
.theory-content section:first-child { padding-top: 0; }
.theory-content section:last-child { border-bottom: 0; }
.theory-content h3, .catalog-theory-block h3 { margin: 0 0 6px; color: var(--text); font-size: 13px; font-weight: 650; }
.theory-content p, .catalog-theory-block p { margin: 6px 0; }
.equation-block { max-width: 100%; overflow: hidden; }
.theory-content mjx-container:not([display="true"]), .catalog-detail mjx-container:not([display="true"]) { display: inline-block !important; margin: 0 .03em !important; white-space: nowrap !important; max-width: none !important; vertical-align: -.08em; }
.theory-content mjx-container[display="true"], .catalog-detail mjx-container[display="true"] { display: block !important; margin: .42em 0 !important; max-width: 100%; overflow-x: auto; overflow-y: hidden; padding: 2px 0 3px; white-space: nowrap; }
.ref-table { width: 100%; border-collapse: collapse; margin: 8px 0; font-size: 11.5px; }
.ref-table th, .ref-table td { padding: 6px 7px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.ref-table th { color: var(--muted); font-weight: 600; }
.fact-grid, .catalog-facts { display: grid; grid-template-columns: 118px minmax(0,1fr); gap: 5px 10px; margin: 8px 0; padding: 8px 0; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); font-size: 11.5px; }
.fact-grid dt, .catalog-facts dt { color: var(--muted); }
.fact-grid dd, .catalog-facts dd { margin: 0; color: var(--text-2); }
.layer-jump { min-height: 25px; margin: 2px 3px 2px 0; padding: 3px 7px; font-size: 10.5px; }
.status { padding-left: 8px; border-left: 2px solid var(--line-strong); }
.status.ok { border-color: var(--positive); }
.status.bad { border-color: var(--negative); }

/* ---------- catalog ---------- */
.catalog-mode .visual-tool { display: none !important; }
.workspace.catalog-open, .theory-closed .workspace.catalog-open { grid-template-columns: minmax(0,1fr) !important; }
.workspace.catalog-open > .inspector, .workspace.catalog-open > .stage-shell, .workspace.catalog-open > .theory-panel { display: none !important; }
.workspace.catalog-open > .catalog-area { display: grid !important; grid-column: 1 / -1; min-width: 0; }
.catalog-area { min-height: 0; height: 100%; grid-template-rows: 48px minmax(0,1fr); background: var(--surface-1); overflow: hidden; }
.catalog-toolbar { display: grid; grid-template-columns: auto minmax(260px,1fr) 150px 70px; align-items: center; gap: 7px; padding: 6px 10px; border-bottom: 1px solid var(--line); background: var(--surface-0); }
.catalog-kind-tabs .segment { min-height: 32px; font-size: 11px; }
.search-field { height: 34px; display: flex; align-items: center; gap: 7px; padding: 0 9px; border: 1px solid var(--line); background: var(--surface-0); }
.search-field svg { width: 15px; height: 15px; color: var(--dim); }
.search-field input { min-width: 0; width: 100%; border: 0; outline: 0; background: transparent; color: var(--text); font-size: 12px; }
.catalog-count { text-align: right; color: var(--dim); font: 10.5px var(--font-mono); }
.catalog-body { min-height: 0; display: grid; grid-template-columns: minmax(320px,34%) minmax(0,1fr); }
.catalog-list { min-height: 0; overflow: auto; border-right: 1px solid var(--line); padding: 5px 0; }
.catalog-row { width: 100%; height: 42px; display: grid; grid-template-columns: 48px minmax(100px,1fr) 88px 58px; align-items: center; gap: 6px; padding: 0 12px; background: transparent; color: var(--muted); text-align: left; cursor: pointer; }
.catalog-row:hover { background: var(--surface-2); }
.catalog-row.active { background: var(--surface-3); color: var(--text); }
.catalog-row strong { color: var(--text-2); font-size: 12.5px; }
.catalog-row .row-no, .catalog-row em { color: var(--dim); font: 10px var(--font-mono); font-style: normal; }
.catalog-row span:nth-child(3) { font-size: 11px; }
.catalog-detail { min-height: 0; overflow: auto; padding: 18px 24px 30px; color: var(--text-2); }
.catalog-detail-head { position: sticky; top: -18px; z-index: 2; display: flex; align-items: flex-start; justify-content: space-between; gap: 18px; padding: 15px 0 10px; border-bottom: 1px solid var(--line); background: var(--surface-1); }
.catalog-detail-head h2 { margin: 2px 0 0; font-size: 24px; line-height: 1.1; font-weight: 680; letter-spacing: -.015em; }
.catalog-number { color: var(--muted); font: 10px var(--font-mono); }
.catalog-system { color: var(--text-2); font-size: 11px; }
.catalog-tags { display: flex; flex-wrap: wrap; gap: 10px; margin: 11px 0 4px; color: var(--muted); font-size: 10.5px; }
.catalog-tags span + span::before { content: "·"; margin-right: 10px; color: var(--dim); }
.catalog-facts { grid-template-columns: 138px 1fr; margin: 12px 0 18px; font-size: 12px; }
.catalog-facts dd { font-family: var(--font-mono); }
.catalog-theory-block { max-width: 920px; padding: 12px 0; border-top: 1px solid var(--line); }
.catalog-theory-block p { color: var(--text-2); font-size: 12.5px; line-height: 1.62; }
.catalog-jump { min-height: 32px; padding: 0 10px; font-size: 11px; }

@media (max-width: 1280px) {
  :root { --inspector-w: 232px; --theory-w: 378px; }
  .space-switch .segment { padding: 0 8px; }
}

@media (max-width: 1040px) {
  :root { --inspector-w: 218px; }
  .theory-panel { position: absolute; z-index: 70; top: var(--toolbar-h); right: 0; bottom: 0; width: 380px; box-shadow: -14px 0 30px rgba(0,0,0,.30); }
  .catalog-toolbar { grid-template-columns: minmax(0,1fr); grid-auto-rows: auto; }
  .catalog-count { text-align: left; }
}

@media (max-width: 760px) {
  html, body { height: auto; min-height: 100%; overflow: auto; }
  .app-shell { height: auto; min-height: 100vh; display: block; }
  .toolbar { position: sticky; top: 0; z-index: 90; display: flex; flex-wrap: wrap; }
  .mode-switch { flex: 1; }
  .mode-switch .segment { min-width: 0; flex: 1; }
  .toolbar-actions { width: 100%; justify-content: flex-end; }
  .workspace, .theory-closed .workspace { display: block; }
  .inspector { max-height: 360px; border-right: 0; border-bottom: 1px solid var(--line); }
  .inspector-scroll { max-height: 360px; }
  .stage-shell { height: 64vh; }
  .theory-panel { position: fixed; left: 0; right: 0; top: 86px; bottom: 0; width: auto; }
  .stage-readout { display: none; }
  .catalog-body { grid-template-columns: 1fr; }
  .catalog-list { max-height: 32vh; border-right: 0; border-bottom: 1px solid var(--line); }
  .catalog-detail { padding: 15px; }
  .layer-dock { max-width: calc(100% - 12px); }
}
