/* Chemistry reference workspace ------------------------------------------------
   A compact laboratory-atlas surface. Periodic tiles use their own tested ink
   and trend palette so tiny scientific labels remain readable in every theme.
   These rules intentionally do not touch theme art. */

.chemistry-workspace,
.chemistry-picker-dialog {
  --ptable-ink: #162734;
  --ptable-detail-ink: #243138;
  --ptable-unavailable-fill-a: #f3f0e8;
  --ptable-unavailable-fill-b: #d8ddd7;
}

.chemical-input { display: grid; min-width: 0; gap: 4px; }
.chemical-input math-field { width: 100%; min-width: 0; box-sizing: border-box; }
.science-field math-field.chem-field { color: var(--ink); background: var(--paper); border: 1px solid var(--line); padding: 9px; min-height: 44px; border-radius: var(--r-tight,6px); }
.chemical-input-actions { display: inline-flex; gap: 4px; }
.chemical-input-actions button { min-width: 44px; min-height: 36px; padding: 4px 8px; color: var(--ink); background: var(--sheet); border: 1px solid var(--line); border-radius: var(--r-tight,6px); }
@media (pointer: coarse) { .chemical-input-actions button { min-height: 44px; } }

.chemistry-workspace {
  --chem-gap-1: 4px;
  --chem-gap-2: 8px;
  --chem-gap-3: 12px;
  --chem-radius-1: 5px;
  --chem-radius-2: 9px;
  display: grid;
  gap: var(--chem-gap-3);
  min-width: 0;
  color: var(--ink);
}

.chem-workspace-header {
  position: sticky;
  z-index: 4;
  top: -15px;
  display: grid;
  grid-template-columns: auto minmax(150px, 1fr) minmax(170px, auto);
  gap: var(--chem-gap-2);
  align-items: center;
  margin: -15px -15px 0;
  padding: 12px 15px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in oklab, var(--panel) 94%, transparent);
  backdrop-filter: blur(8px);
}

.chem-header-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: var(--chem-gap-2);
  min-width: 0;
}

.chem-header-actions .chem-trend-select { flex: 1 1 170px; }
.chem-expand-button { flex: 0 0 auto; white-space: nowrap; }

.chem-mode-switch {
  display: inline-flex;
  align-items: center;
  padding: 2px;
  border: 1px solid var(--n84);
  border-radius: 8px;
  background: var(--n96);
}

.chem-mode-button,
.chem-button {
  min-height: 34px;
  padding: 7px 10px;
  color: var(--muted);
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  font: 600 11px/1.2 'DM Sans', system-ui, sans-serif;
  cursor: pointer;
}

.chem-mode-button[aria-pressed="true"] {
  color: var(--c33);
  border-color: var(--c87);
  background: var(--paper);
  box-shadow: var(--shadow-soft);
}

.chem-button {
  min-height: 38px;
  color: var(--c39);
  border-color: var(--c84);
  background: var(--c96);
}

.chem-button:hover,
.chem-mode-button:hover { background: var(--c93); }

.chem-button.chem-primary {
  color: var(--on-pen);
  border-color: var(--pen);
  background: var(--pen);
}

.chem-search,
.chem-select {
  min-width: 0;
  min-height: 36px;
  padding: 7px 10px;
  color: var(--ink);
  border: 1px solid var(--n84);
  border-radius: 7px;
  background: var(--paper);
  font: 12px/1.3 'DM Sans', system-ui, sans-serif;
}

:is(.chem-search, .chem-select, .chem-mode-button, .chem-button, .ptable-cell,
  .chem-display-choice input, .chemistry-dialog button, .chemistry-picker-dialog button):focus-visible {
  outline: 3px solid color-mix(in oklab, var(--focus) 40%, transparent);
  outline-offset: 2px;
}

.chem-workspace-content { min-width: 0; }
.ptable-scroll { max-width: 100%; overflow: auto; padding: 3px; scrollbar-gutter: stable; }

.chemistry-ptable-grid {
  grid-template-rows: repeat(7, minmax(46px, auto)) 12px repeat(2, minmax(46px, auto));
  gap: 3px;
  min-width: 648px;
}

.chemistry-workspace .ptable-cell,
.chemistry-picker-dialog .ptable-cell {
  position: relative;
  min-width: 32px;
  min-height: 46px;
  padding: 3px 2px;
  border-color: color-mix(in oklab, var(--depth) 13%, transparent);
  overflow: hidden;
  color: var(--ptable-ink);
  box-shadow: inset 0 1px rgb(255 255 255 / .42);
}

.chemistry-workspace .ptable-cell:hover,
.chemistry-picker-dialog .ptable-cell:hover {
  z-index: 2;
  outline: 2px solid var(--pen);
  outline-offset: 1px;
  transform: translateY(-1px);
}

.chemistry-workspace .ptable-cell[aria-pressed="true"] {
  z-index: 3;
  outline: 3px solid var(--pen);
  outline-offset: 1px;
}

.chemistry-workspace .ptable-number,
.chemistry-picker-dialog .ptable-number {
  color: var(--ptable-detail-ink);
  font: 7.5px/1 'DM Mono', monospace;
}
.chemistry-workspace .ptable-symbol,
.chemistry-picker-dialog .ptable-symbol { color: var(--ptable-ink); font-size: 12.5px; }
.chemistry-workspace .ptable-mass,
.chemistry-picker-dialog .ptable-mass {
  max-width: 100%;
  overflow: hidden;
  color: var(--ptable-detail-ink);
  font-size: 7px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.element-action-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border: 1px solid var(--c87);
  border-radius: var(--chem-radius-2);
  background: var(--paper);
  box-shadow: inset 0 2px color-mix(in oklab, var(--pen) 46%, transparent), var(--shadow-soft);
}

.element-action-card[hidden] { display: none; }
.element-action-identity { min-width: 0; }
.element-action-identity strong,
.element-action-identity span { display: block; }
.element-action-identity strong { color: var(--c30); font: 600 15px/1.2 Newsreader, Georgia, serif; }
.element-action-identity span { margin-top: 2px; color: var(--muted); font-size: var(--t-control); }
.element-action-buttons { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 6px; }

.ptable-trend-cell {
  --trend-fill: #faf8ef;
  background: var(--trend-fill) !important;
}
.ptable-trend-cell.trend-bin-1, .trend-legend-step.trend-bin-1 { --trend-fill: #faf8ef; }
.ptable-trend-cell.trend-bin-2, .trend-legend-step.trend-bin-2 { --trend-fill: #eff3e8; }
.ptable-trend-cell.trend-bin-3, .trend-legend-step.trend-bin-3 { --trend-fill: #e4eddc; }
.ptable-trend-cell.trend-bin-4, .trend-legend-step.trend-bin-4 { --trend-fill: #d8e7cf; }
.ptable-trend-cell.trend-bin-5, .trend-legend-step.trend-bin-5 { --trend-fill: #cbdfc0; }
.ptable-trend-cell.trend-bin-6, .trend-legend-step.trend-bin-6 { --trend-fill: #bed7b0; }
.ptable-trend-cell.trend-bin-7, .trend-legend-step.trend-bin-7 { --trend-fill: #b0cfa0; }
.ptable-trend-cell[data-trend-unavailable="true"] {
  background: repeating-linear-gradient(135deg, var(--ptable-unavailable-fill-a) 0 5px, var(--ptable-unavailable-fill-b) 5px 7px) !important;
}
.chemistry-workspace .ptable-cell .ptable-trend-value,
.chemistry-picker-dialog .ptable-cell .ptable-trend-value {
  display: block;
  max-width: 100%;
  overflow: hidden;
  color: var(--ptable-detail-ink);
  font: 600 7px/1.1 'DM Mono', monospace;
  text-overflow: ellipsis;
  white-space: nowrap;
}

@media (min-width: 821px) {
  body[data-reference-expanded='periodic'] .app-shell {
    grid-template-columns: minmax(0, 1fr) !important;
    grid-template-rows: minmax(0, 1fr) !important;
  }
  body[data-reference-expanded='periodic'] .workspace-pane,
  body[data-reference-expanded='periodic'] .divider { display: none !important; }
  body[data-reference-expanded='periodic'] .graph-pane {
    display: flex !important;
    position: relative !important;
    inset: auto !important;
    grid-column: 1 / -1;
    order: 1 !important;
    width: 100% !important;
    max-width: none !important;
    height: 100% !important;
    max-height: none !important;
    transform: none !important;
  }
  body[data-reference-expanded='periodic'] #periodic-view { padding: 18px 24px 24px; }
  body[data-reference-expanded='periodic'] .chemistry-ptable-grid {
    grid-template-rows: repeat(7, minmax(58px, auto)) 16px repeat(2, minmax(58px, auto));
    gap: 5px;
    min-width: 900px;
  }
  body[data-reference-expanded='periodic'] .chemistry-workspace .ptable-cell {
    min-height: 58px;
    padding: 5px 3px;
  }
  body[data-reference-expanded='periodic'] .chemistry-workspace .ptable-number { font-size: 9px; }
  body[data-reference-expanded='periodic'] .chemistry-workspace .ptable-symbol { font-size: 17px; }
  body[data-reference-expanded='periodic'] .chemistry-workspace .ptable-mass,
  body[data-reference-expanded='periodic'] .chemistry-workspace .ptable-trend-value { font-size: 8.5px; }
}
.ptable-status {
  position: absolute;
  right: 1px;
  bottom: 1px;
  max-width: calc(100% - 2px);
  padding: 1px 2px !important;
  overflow: hidden;
  border-radius: 2px !important;
  font-size: 5.5px !important;
  text-overflow: ellipsis;
}

.trend-legend {
  margin-top: 12px;
  padding: 10px;
  border: 1px solid var(--n87);
  border-radius: var(--chem-radius-2);
  background: var(--n99);
}
.trend-legend h3 { margin: 0 0 7px; color: var(--n39); font-size: var(--t-control); }
.trend-legend-steps { display: grid; grid-template-columns: repeat(7, minmax(58px, 1fr)); gap: 3px; overflow-x: auto; }
.trend-legend-step {
  display: grid;
  min-height: 36px;
  align-content: center;
  padding: 3px 4px;
  color: var(--ptable-detail-ink);
  border: 1px solid var(--n84);
  border-radius: 4px;
  background: var(--trend-fill);
  text-align: center;
}
.trend-legend-step i { font: normal 7px/1 'DM Mono', monospace; }
.trend-legend-step b { margin-top: 2px; font: 600 7px/1.1 'DM Mono', monospace; }
.trend-legend-note { margin: 7px 0 0; color: var(--muted); font-size: var(--t-control); line-height: 1.45; }

.chem-status {
  display: inline-flex;
  align-items: center;
  min-height: 17px;
  padding: 2px 5px;
  color: var(--n39);
  border: 1px solid var(--n84);
  border-radius: 999px;
  background: var(--n96);
  font: 600 8px/1 'DM Mono', monospace;
  vertical-align: middle;
}
.chem-status-predicted, .chem-status-calculated { color: var(--flag-warn-deep); border-color: var(--flag-warn-lift); }
.chem-status-unknown { color: var(--muted); border-style: dashed; }

.chemistry-dialog {
  width: min(940px, calc(100vw - 28px));
  max-height: min(880px, calc(100vh - 28px));
  overflow: hidden;
}
.chemistry-modal-header { align-items: center; }
.chemistry-close { color: var(--c39); border: 1px solid var(--c87); background: var(--c96); font-size: 23px; line-height: 1; }
.chemistry-dialog-body { max-height: calc(100vh - 190px); overflow: auto; padding: 18px 20px; }
.chem-detail-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.chem-detail-group {
  min-width: 0;
  padding: 13px;
  border: 1px solid var(--n87);
  border-radius: var(--chem-radius-2);
  background: var(--n99);
}
.chem-detail-group h3 { margin: 0 0 9px; color: var(--c36); font: 600 16px/1.1 Newsreader, Georgia, serif; }
.chem-detail-group dl { margin: 0; }
.chem-definition { display: grid; grid-template-columns: minmax(108px, .72fr) minmax(0, 1.28fr); gap: 8px; padding: 7px 0; border-bottom: 1px solid var(--n90); }
.chem-definition:last-child { border-bottom: 0; }
.chem-definition dt { color: var(--muted); font-size: var(--t-control); }
.chem-definition dd { min-width: 0; margin: 0; color: var(--ink); font-size: var(--t-control); overflow-wrap: anywhere; }
.chem-definition dd small,
.chem-isotopes li small { display: block; margin-top: 4px; color: var(--muted); font-size: var(--t-control); line-height: 1.4; }
.chem-isotopes { grid-column: 1 / -1; }
.chem-isotopes ul, .chem-sources ul { display: grid; gap: 6px; margin: 0; padding: 0; list-style: none; }
.chem-isotopes li { padding: 7px 8px; border: 1px solid var(--n90); border-radius: 5px; font-size: var(--t-control); }
.chem-sources { margin-top: 12px; border: 1px solid var(--n87); border-radius: var(--chem-radius-2); background: var(--paper); }
.chem-sources summary { padding: 11px 13px; color: var(--c36); font-weight: 600; font-size: var(--t-control); cursor: pointer; }
.chem-sources ul { padding: 0 13px 13px; }
.chem-sources li { display: grid; gap: 2px; padding-top: 8px; border-top: 1px solid var(--n90); }
.chem-sources strong { font-size: var(--t-control); }
.chem-sources span, .chem-sources small { color: var(--muted); font-size: var(--t-control); line-height: 1.35; }
.chem-sources code { color: var(--c45); font: 8px/1.35 'DM Mono', monospace; overflow-wrap: anywhere; }
.chem-empty { margin: 0; color: var(--muted); font-size: var(--t-control); }

.chem-prefill-controls { display: grid; grid-template-columns: minmax(180px, 1fr) auto; gap: 12px; align-items: end; }
.chem-field-label { display: grid; gap: 5px; color: var(--muted); font-size: var(--t-control); }
.chem-display-choice { display: flex; gap: 4px; margin: 0; padding: 0; border: 0; }
.chem-display-choice legend { margin-bottom: 5px; color: var(--muted); font-size: var(--t-control); }
.chem-display-choice label { position: relative; display: inline-flex; align-items: center; justify-content: center; min-height: 36px; padding: 7px 10px; color: var(--muted); border: 1px solid var(--n84); background: var(--n96); font-size: var(--t-control); cursor: pointer; }
.chem-display-choice label:first-of-type { border-radius: 7px 0 0 7px; }
.chem-display-choice label:last-of-type { border-radius: 0 7px 7px 0; }
.chem-display-choice input { position: absolute; opacity: 0; }
.chem-display-choice label:has(input:checked) { z-index: 1; color: var(--c33); border-color: var(--pen); background: var(--c93); }
.chem-prefill-preview-host { margin-top: 14px; }
.chem-orbital-preview { display: grid; gap: 11px; }
.chem-prefill-summary { display: grid; grid-template-columns: auto 1fr auto; gap: 4px 12px; align-items: center; padding: 12px; border: 1px solid var(--c84); border-radius: var(--chem-radius-2); background: var(--c99); }
.chem-prefill-summary strong { color: var(--c30); font: 600 21px/1 Newsreader, Georgia, serif; }
.chem-prefill-summary span { color: var(--muted); font-size: var(--t-control); }
.chem-prefill-summary code { grid-column: 1 / -1; color: var(--c36); font: 12px/1.4 'DM Mono', monospace; overflow-wrap: anywhere; }
.chem-core-label, .chem-source-line { margin: 0; color: var(--muted); font-size: var(--t-control); line-height: 1.4; }
.orbital-frame .orbital-core-label {
  display: block;
  margin: 4px 0 0 38px;
  padding: 5px 8px;
  color: var(--muted);
  border-top: 1px solid var(--n84);
  font: 9px/1.25 'DM Mono', monospace;
}
.chem-orbital-stack { display: grid; gap: 5px; max-height: 360px; overflow: auto; padding: 10px; border: 1px solid var(--n87); border-radius: var(--chem-radius-2); background: var(--paper); }
.chem-orbital-row { display: flex; align-items: center; gap: 10px; min-height: 28px; }
.chem-orbital-name { width: 28px; color: var(--c36); font: 600 10px/1 'DM Mono', monospace; text-align: right; }
.chem-orbital-boxes { display: inline-flex; gap: 3px; }
.chem-orbital-box { display: inline-flex; align-items: center; justify-content: center; width: 25px; height: 25px; color: var(--c33); border: 1px solid var(--n69); background: var(--n99); }
.chem-orbital-box i { font: normal 14px/1 Georgia, serif; }
.chem-orbital-box .spin-down { color: var(--c51); }
.chem-prefill-error { margin: 0; padding: 12px; color: var(--flag-bad); border: 1px solid var(--flag-bad-lift); border-radius: 7px; background: var(--flag-bad-wash); }
.chemistry-prefill-footer { gap: 7px; }
.chem-footer-spacer { flex: 1; }

.chemistry-picker-dialog { width: min(820px, calc(100vw - 28px)); max-height: min(760px, calc(100vh - 28px)); overflow: hidden; }
.chem-picker-body { display: grid; grid-template-columns: 1fr auto; gap: 8px; max-height: calc(100vh - 190px); overflow: auto; padding: 14px 18px; }
.chem-picker-body .ptable-scroll { grid-column: 1 / -1; }
.chem-search-result { align-self: center; color: var(--muted); font: 9px/1 'DM Mono', monospace; }

@media (max-width: 760px) {
  .chem-workspace-header { grid-template-columns: 1fr; }
  .chem-mode-switch { justify-self: start; }
  .chem-mode-button, .chem-button, .chem-search, .chem-select { min-height: 44px; }
  .chemistry-dialog button,
  .chemistry-picker-dialog button { min-height: 44px; }
  .element-action-card { align-items: stretch; flex-direction: column; }
  .element-action-buttons { display: grid; grid-template-columns: 1fr; }
  .chemistry-workspace .ptable-cell,
  .chemistry-picker-dialog .ptable-cell { min-height: 44px; }
  .chem-detail-grid { grid-template-columns: 1fr; }
  .chem-isotopes { grid-column: auto; }
  .chem-prefill-controls { grid-template-columns: 1fr; }
  .chem-display-choice label { min-height: 44px; }
  .chemistry-dialog .modal-footer { flex-wrap: wrap; }
  .chemistry-dialog .modal-footer > .primary-button { margin-left: auto; }
}

@media (max-width: 820px), (hover: none) and (pointer: coarse) {
  .chem-expand-button { display: none; }
  .chem-header-actions { justify-content: stretch; }
}

@media (max-width: 430px) {
  .chemistry-dialog,
  .chemistry-picker-dialog { width: calc(100vw - 12px); max-height: calc(100vh - 12px); }
  .chemistry-dialog-body { max-height: calc(100vh - 166px); padding: 12px; }
  .chemistry-modal-header { padding: 14px 13px 10px; }
  .chemistry-modal-header h2 { font-size: 20px; }
  .chem-definition { grid-template-columns: 1fr; gap: 3px; }
  .chem-prefill-summary { grid-template-columns: 1fr; }
  .chem-prefill-summary code { grid-column: auto; }
  .chem-picker-body { grid-template-columns: 1fr; padding: 10px; }
  .chem-search-result, .chem-picker-body .ptable-scroll { grid-column: auto; }
}

@media (prefers-reduced-motion: reduce) {
  .chemistry-workspace *,
  .chemistry-dialog *,
  .chemistry-picker-dialog * { scroll-behavior: auto !important; transition-duration: .001ms !important; animation-duration: .001ms !important; }
  .chemistry-workspace .ptable-cell:hover,
  .chemistry-picker-dialog .ptable-cell:hover { transform: none; }
}

@media print {
  .chemistry-dialog,
  .chemistry-picker-dialog,
  .chem-workspace-header,
  .element-action-card { display: none !important; }
}
