:root {
  color-scheme: dark;
  --bg: #11151a;
  --surface: #1b2027;
  --surface-2: #232a33;
  --surface-3: #2d3540;
  --border: #343d49;
  --text: #f7f9fc;
  --muted: #9ca8b7;
  --accent: #51d49b;
  --accent-dark: #24845d;
  --blue: #315f7c;
  --blue-hover: #3d7394;
  --orange: #df7a1c;
  --orange-hover: #f28b29;
  --red: #a74040;
  --red-hover: #bd4f4f;
  --number: #414851;
  --number-hover: #515a65;
  --shadow: 0 22px 70px rgb(0 0 0 / 32%);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--bg);
}

body {
  min-width: 320px;
  min-height: 100vh;
  margin: 0;
  color: var(--text);
  background:
    radial-gradient(circle at 10% 0%, rgb(41 111 89 / 20%), transparent 30rem),
    radial-gradient(circle at 100% 20%, rgb(49 95 124 / 18%), transparent 34rem),
    var(--bg);
}

button,
output {
  font: inherit;
}

button {
  color: inherit;
}

button:focus-visible {
  outline: 3px solid rgb(81 212 155 / 70%);
  outline-offset: 2px;
}

.page-shell {
  width: min(1160px, calc(100% - 32px));
  margin: 0 auto;
  padding: 30px 0 20px;
}

.site-header {
  display: grid;
  grid-template-columns: auto auto 1fr;
  align-items: center;
  gap: 14px;
  margin-bottom: 18px;
}

.brand-mark {
  display: grid;
  width: 48px;
  height: 48px;
  place-items: center;
  border: 1px solid rgb(81 212 155 / 35%);
  border-radius: 14px;
  color: var(--accent);
  background: rgb(81 212 155 / 10%);
  font-family: Georgia, serif;
  font-size: 1.65rem;
  font-weight: 700;
  box-shadow: inset 0 0 20px rgb(81 212 155 / 7%);
}

.eyebrow {
  margin: 0 0 3px;
  color: var(--muted);
  font-size: .71rem;
  font-weight: 750;
  letter-spacing: .12em;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: clamp(1.45rem, 3vw, 2rem);
  line-height: 1;
}

h2 {
  margin-bottom: 0;
  font-size: 1.25rem;
}

.calculator {
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.display-panel {
  min-height: 125px;
  padding: 20px 24px 18px;
  border-bottom: 1px solid var(--border);
  background:
    linear-gradient(135deg, rgb(255 255 255 / 2%), transparent),
    #171c22;
}

.display-meta {
  display: flex;
  min-height: 25px;
  align-items: center;
  justify-content: flex-end;
  gap: 9px;
  color: var(--muted);
  font-size: .85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.memory-indicator {
  display: inline-grid;
  width: 22px;
  height: 22px;
  place-items: center;
  border-radius: 6px;
  color: var(--accent);
  background: rgb(81 212 155 / 12%);
  font-size: .72rem;
  font-weight: 800;
}

.display-value {
  display: block;
  width: 100%;
  margin-top: 4px;
  overflow: hidden;
  color: var(--text);
  font-size: clamp(2.15rem, 6vw, 3.35rem);
  font-variant-numeric: tabular-nums;
  line-height: 1.15;
  text-align: right;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.calculator-body {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 275px;
  min-height: 610px;
}

.controls {
  display: grid;
  grid-template-rows: auto auto 1fr;
  gap: 10px;
  padding: 16px;
  border-right: 1px solid var(--border);
}

.button-grid {
  display: grid;
  gap: 7px;
}

.scientific-grid {
  grid-template-columns: repeat(6, 1fr);
}

.memory-grid {
  grid-template-columns: repeat(4, 1fr);
}

.keypad-grid {
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(5, minmax(58px, 1fr));
}

.key {
  min-height: 50px;
  border: 0;
  border-radius: 9px;
  color: #fff;
  font-size: 1rem;
  font-weight: 750;
  cursor: pointer;
  box-shadow: inset 0 1px rgb(255 255 255 / 7%);
  transition: background-color 120ms ease, transform 80ms ease, box-shadow 120ms ease;
}

.key:hover {
  box-shadow: inset 0 1px rgb(255 255 255 / 11%), 0 4px 12px rgb(0 0 0 / 18%);
}

.key:active {
  transform: translateY(1px) scale(.992);
}

.key-number {
  background: var(--number);
}

.key-number:hover {
  background: var(--number-hover);
}

.key-scientific {
  background: var(--blue);
}

.key-scientific:hover {
  background: var(--blue-hover);
}

.key-memory {
  background: #4b535e;
}

.key-memory:hover {
  background: #5a6471;
}

.key-operator {
  background: var(--orange);
  font-size: 1.2rem;
}

.key-operator:hover {
  background: var(--orange-hover);
}

.key-clear {
  background: var(--red);
}

.key-clear:hover {
  background: var(--red-hover);
}

.key-equals {
  background: var(--accent-dark);
  font-size: 1.2rem;
}

.key-equals:hover {
  background: #2f9c70;
}

.history-panel {
  display: grid;
  min-height: 0;
  grid-template-rows: auto minmax(0, 1fr) auto auto auto;
  gap: 10px;
  padding: 18px;
  background: #191e24;
}

.panel-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.count-badge {
  display: grid;
  min-width: 30px;
  height: 30px;
  padding: 0 8px;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted);
  background: var(--surface-2);
  font-size: .78rem;
  font-weight: 700;
}

.history-list {
  min-height: 0;
  overflow-y: auto;
  border: 1px solid #292f38;
  border-radius: 11px;
  background: #15191e;
  scrollbar-color: #4a5462 transparent;
}

.empty-state {
  display: grid;
  min-height: 0;
  height: 100%;
  margin: 0;
  padding: 24px;
  place-items: center;
  color: #75808d;
  font-size: .9rem;
  line-height: 1.55;
  text-align: center;
}

.empty-state[hidden] {
  display: none;
}

.history-item {
  display: block;
  width: 100%;
  padding: 12px 13px;
  border: 0;
  border-bottom: 1px solid #292f38;
  color: inherit;
  background: transparent;
  cursor: pointer;
  text-align: right;
  transition: background 120ms ease;
}

.history-item:hover {
  background: var(--surface-2);
}

.history-item:last-child {
  border-bottom: 0;
}

.history-expression {
  display: block;
  overflow: hidden;
  color: var(--muted);
  font-size: .74rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-result {
  display: block;
  margin-top: 3px;
  font-size: 1.05rem;
  font-variant-numeric: tabular-nums;
  font-weight: 750;
}

.panel-action,
.primary-action,
.secondary-action,
.danger-action {
  min-height: 46px;
  border: 0;
  border-radius: 9px;
  color: #fff;
  background: #4b535e;
  font-weight: 750;
  cursor: pointer;
  transition: background 120ms ease, transform 80ms ease;
}

.panel-action:hover,
.secondary-action:hover {
  background: #5a6471;
}

.panel-action:active,
.primary-action:active,
.secondary-action:active,
.danger-action:active {
  transform: translateY(1px);
}

.panel-action-primary,
.primary-action {
  background: var(--accent-dark);
}

.panel-action-primary:hover,
.primary-action:hover {
  background: #2f9c70;
}

.danger-action {
  background: var(--red);
}

.danger-action:hover {
  background: var(--red-hover);
}

.action-icon {
  margin-right: 5px;
  color: #c8ffe8;
}

.constants-dialog,
.pse-dialog {
  max-width: none;
  max-height: none;
  padding: 0;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 18px;
  color: var(--text);
  background: var(--surface);
  box-shadow: 0 30px 100px rgb(0 0 0 / 60%);
}

.constants-dialog {
  width: min(760px, calc(100vw - 32px));
  height: min(610px, calc(100dvh - 32px));
}

.pse-dialog {
  width: min(1460px, 97vw);
  height: min(940px, 96vh);
}

.constants-dialog::backdrop,
.pse-dialog::backdrop {
  background: rgb(5 8 12 / 78%);
  backdrop-filter: blur(5px);
}

.constants-shell {
  display: grid;
  height: 100%;
  min-height: 0;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 14px;
  padding: 18px;
}

.constants-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.constants-grid {
  display: grid;
  min-height: 0;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 9px;
  overflow-y: auto;
  scrollbar-color: #4a5462 transparent;
}

.constant-button {
  display: grid;
  min-width: 0;
  min-height: 108px;
  grid-template-columns: auto 1fr;
  grid-template-rows: auto auto auto;
  gap: 3px 12px;
  align-items: center;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 11px;
  color: var(--text);
  background: var(--surface-2);
  cursor: pointer;
  text-align: left;
  transition: background 120ms ease, border-color 120ms ease, transform 80ms ease;
}

.constant-button:hover {
  border-color: rgb(81 212 155 / 48%);
  background: var(--surface-3);
}

.constant-button:active {
  transform: translateY(1px);
}

.constant-symbol {
  grid-row: 1 / span 3;
  min-width: 52px;
  color: var(--accent);
  font-family: Georgia, serif;
  font-size: 1.45rem;
  font-weight: 800;
  text-align: center;
}

.constant-name {
  overflow: hidden;
  color: var(--muted);
  font-size: .74rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.constant-button strong {
  overflow: hidden;
  font-size: .98rem;
  font-variant-numeric: tabular-nums;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.constant-unit {
  color: #bac3ce;
  font-size: .72rem;
}

.dialog-shell {
  display: grid;
  height: 100%;
  grid-template-rows: auto auto auto minmax(260px, 1fr) auto auto auto;
  gap: 10px;
  padding: 16px;
}

.pse-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.pse-header h2 {
  font-size: 1.45rem;
}

.icon-button {
  display: grid;
  width: 42px;
  height: 42px;
  flex: 0 0 auto;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--muted);
  background: var(--surface-2);
  font-size: 1.65rem;
  line-height: 1;
  cursor: pointer;
}

.icon-button:hover {
  color: #fff;
  background: var(--surface-3);
}

.formula-summary {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  padding: 14px 17px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #171c22;
}

.summary-label {
  display: block;
  margin-bottom: 3px;
  color: var(--muted);
  font-size: .7rem;
  font-weight: 750;
  letter-spacing: .1em;
  text-transform: uppercase;
}

.formula-value,
.mass-value {
  display: block;
  overflow: hidden;
  color: var(--accent);
  font-size: clamp(1.35rem, 3vw, 2rem);
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mass-summary {
  text-align: right;
}

.mass-value {
  color: #fff;
}

.mass-value.error {
  color: #ff8d8d;
  font-size: 1.2rem;
}

.pse-instructions {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 8px 20px;
  color: var(--muted);
  font-size: .76rem;
}

.periodic-scroll {
  min-height: 0;
  overflow: auto;
  border: 1px solid #2b323b;
  border-radius: 12px;
  background: #14191e;
  scrollbar-color: #4a5462 #171c22;
}

.periodic-table {
  display: grid;
  width: 100%;
  min-width: 1120px;
  min-height: 100%;
  grid-template-columns: 38px repeat(18, minmax(55px, 1fr));
  grid-template-rows: 24px repeat(6, minmax(65px, 1fr)) 13px minmax(65px, 1fr);
  gap: 4px;
  padding: 8px;
}

.group-label,
.period-label {
  display: grid;
  place-items: center;
  color: #7f8b98;
  font-size: .66rem;
  font-weight: 750;
}

.element-button,
.lanthanide-marker {
  display: grid;
  min-width: 0;
  padding: 4px 2px;
  place-items: center;
  border: 0;
  border-radius: 5px;
  color: #fff;
  cursor: pointer;
  box-shadow: inset 0 1px rgb(255 255 255 / 8%);
  transition: filter 110ms ease, transform 80ms ease;
}

.element-button:hover {
  z-index: 2;
  filter: brightness(1.2);
  transform: scale(1.055);
}

.element-button:active {
  transform: scale(.98);
}

.element-number,
.element-mass {
  font-size: .58rem;
  font-variant-numeric: tabular-nums;
  line-height: 1.05;
}

.element-symbol {
  font-size: .88rem;
  font-weight: 850;
  line-height: 1.05;
}

.lanthanide-marker {
  color: #dbc99f;
  background: #5b4b2f;
  cursor: default;
  font-size: .62rem;
  font-weight: 750;
  line-height: 1.2;
  text-align: center;
}

.category-alkali,
.legend-swatch.alkali { background: #8f4141; }
.category-alkaline,
.legend-swatch.alkaline { background: #995d2c; }
.category-transition,
.legend-swatch.transition { background: #335b78; }
.category-post { background: #515962; }
.category-metalloid,
.legend-swatch.metalloid { background: #347069; }
.category-nonmetal,
.legend-swatch.nonmetal { background: #377049; }
.category-halogen,
.legend-swatch.halogen { background: #6b4989; }
.category-noble,
.legend-swatch.noble { background: #4a468b; }
.category-lanthanide,
.legend-swatch.lanthanide { background: #89652d; }

.formula-tools {
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  gap: 7px;
}

.formula-tools button {
  min-height: 40px;
  border: 0;
  border-radius: 8px;
  color: #fff;
  background: #665333;
  font-size: 1.05rem;
  font-weight: 800;
  cursor: pointer;
}

.formula-tools button:nth-child(-n + 2) {
  background: var(--blue);
}

.formula-tools button:hover {
  filter: brightness(1.18);
}

.legend {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px 15px;
  color: var(--muted);
  font-size: .65rem;
}

.legend span {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.legend-swatch {
  display: inline-block;
  width: 9px;
  height: 9px;
  border-radius: 3px;
}

.pse-actions {
  display: grid;
  grid-template-columns: 1fr 1fr 1.55fr 1fr;
  gap: 8px;
}

.toast {
  position: fixed;
  z-index: 100;
  right: 20px;
  bottom: 20px;
  max-width: min(380px, calc(100vw - 40px));
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  color: #fff;
  background: #242b34;
  box-shadow: 0 15px 45px rgb(0 0 0 / 40%);
  font-size: .84rem;
  opacity: 0;
  pointer-events: none;
  transform: translateY(12px);
  transition: opacity 180ms ease, transform 180ms ease;
}

.toast.visible {
  opacity: 1;
  transform: translateY(0);
}

@media (max-width: 900px) {
  .calculator-body {
    grid-template-columns: 1fr;
  }

  .controls {
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }

  .history-panel {
    grid-template-rows: auto minmax(170px, 280px) auto auto auto;
  }

  .site-header {
    grid-template-columns: auto 1fr;
  }
}

@media (min-width: 901px) {
  html,
  body {
    height: 100%;
    overflow: hidden;
  }

  .page-shell {
    display: grid;
    height: 100dvh;
    grid-template-rows: auto minmax(0, 1fr);
    gap: clamp(6px, 1.2vh, 12px);
    padding: clamp(6px, 1.5vh, 14px) 0;
  }

  .site-header {
    min-height: clamp(38px, 6vh, 48px);
    grid-template-columns: auto 1fr;
    gap: 10px;
    margin-bottom: 0;
  }

  .brand-mark {
    width: clamp(38px, 5.5vh, 46px);
    height: clamp(38px, 5.5vh, 46px);
    border-radius: 12px;
  }

  .calculator {
    display: grid;
    min-height: 0;
    grid-template-rows: auto minmax(0, 1fr);
  }

  .display-panel {
    min-height: 0;
    height: clamp(78px, 13vh, 104px);
    padding: clamp(10px, 1.8vh, 16px) 20px 10px;
  }

  .display-meta {
    min-height: 20px;
  }

  .display-value {
    font-size: clamp(2rem, 5.5vh, 3rem);
  }

  .calculator-body {
    min-height: 0;
  }

  .controls {
    min-height: 0;
    grid-template-rows: auto auto minmax(0, 1fr);
    gap: clamp(5px, 1vh, 8px);
    padding: clamp(8px, 1.6vh, 13px);
  }

  .button-grid {
    gap: clamp(4px, .8vh, 7px);
  }

  .key {
    min-height: clamp(36px, 6vh, 48px);
  }

  .keypad-grid {
    min-height: 0;
    grid-template-rows: repeat(5, minmax(34px, 1fr));
  }

  .history-panel {
    overflow: hidden;
    padding: clamp(10px, 1.8vh, 16px);
  }

  .pse-dialog {
    width: 100vw;
    height: 100dvh;
    border: 0;
    border-radius: 0;
  }

  .dialog-shell {
    grid-template-rows: auto auto auto minmax(0, 1fr) auto auto auto;
    gap: clamp(4px, .8vh, 7px);
    padding: clamp(6px, 1vh, 10px);
  }

  .pse-header .eyebrow {
    display: none;
  }

  .pse-header h2 {
    font-size: clamp(1.05rem, 2.5vh, 1.3rem);
  }

  .pse-header .icon-button {
    width: clamp(34px, 5vh, 40px);
    height: clamp(34px, 5vh, 40px);
  }

  .formula-summary {
    gap: 10px;
    padding: clamp(5px, 1vh, 9px) 12px;
  }

  .summary-label {
    margin-bottom: 1px;
    font-size: .58rem;
  }

  .formula-value,
  .mass-value {
    font-size: clamp(1.05rem, 2.7vh, 1.45rem);
  }

  .pse-instructions {
    gap: 5px 14px;
    font-size: clamp(.6rem, 1.35vh, .72rem);
  }

  .periodic-scroll {
    overflow: hidden;
  }

  .periodic-table {
    min-width: 0;
    grid-template-columns: 28px repeat(18, minmax(0, 1fr));
    grid-template-rows: 18px repeat(6, minmax(45px, 1fr)) 8px minmax(45px, 1fr);
    gap: clamp(2px, .3vw, 4px);
    padding: clamp(3px, .5vh, 6px);
  }

  .element-button,
  .lanthanide-marker {
    padding: 2px 1px;
  }

  .element-number,
  .element-mass {
    font-size: clamp(.48rem, .8vw, .58rem);
  }

  .element-symbol {
    font-size: clamp(.75rem, 1.15vw, .92rem);
  }

  .formula-tools {
    gap: 5px;
  }

  .formula-tools button {
    min-height: clamp(30px, 4.8vh, 38px);
  }

  .legend {
    gap: 3px 11px;
    font-size: clamp(.55rem, 1.2vh, .64rem);
  }

  .pse-actions {
    gap: 6px;
  }

  .pse-actions button {
    min-height: clamp(34px, 5.5vh, 42px);
  }
}

@media (max-width: 620px) {
  .page-shell {
    width: min(100% - 16px, 1160px);
    padding-top: 14px;
  }

  .calculator {
    border-radius: 15px;
  }

  .display-panel {
    min-height: 105px;
    padding: 15px 16px;
  }

  .controls {
    gap: 7px;
    padding: 9px;
  }

  .scientific-grid {
    grid-template-columns: repeat(3, 1fr);
  }

  .button-grid {
    gap: 5px;
  }

  .key {
    min-height: 48px;
    border-radius: 7px;
  }

  .constants-dialog {
    width: 100vw;
    height: 100dvh;
    border: 0;
    border-radius: 0;
  }

  .constants-shell {
    padding: 12px;
  }

  .constants-grid {
    grid-template-columns: 1fr;
  }

  .constant-button {
    min-height: 96px;
  }

  .pse-dialog {
    width: 100vw;
    height: 100dvh;
    border: 0;
    border-radius: 0;
  }

  .dialog-shell {
    padding: 10px;
  }

  .formula-summary {
    grid-template-columns: 1fr;
    padding: 10px 12px;
  }

  .mass-summary {
    text-align: left;
  }

  .pse-instructions span:nth-child(2) {
    display: none;
  }

  .formula-tools {
    gap: 4px;
  }

  .formula-tools button {
    min-height: 38px;
    padding: 0;
  }

  .legend {
    display: none;
  }

  .pse-actions {
    grid-template-columns: 1fr 1fr;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: .01ms !important;
  }
}
