:root {
  color-scheme: light;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --canvas: #eeeee9;
  --paper: #fcfcf9;
  --surface: #f4f4ef;
  --line: #ddded7;
  --ink: #172139;
  --muted: #546073;
  --navy: #111e39;
  --accent: #234ed9;
  --accent-hover: #1c3fb2;
  --accent-soft: #e8edff;
  --shadow: 0 22px 60px #17213917, 0 3px 10px #1721390a;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
html { min-height: 100%; background: var(--canvas); }
body {
  min-height: 100dvh;
  margin: 0;
  padding: max(.75rem, env(safe-area-inset-top)) max(.75rem, env(safe-area-inset-right))
    max(.75rem, env(safe-area-inset-bottom)) max(.75rem, env(safe-area-inset-left));
  color: var(--ink);
  background: radial-gradient(ellipse at 52% 0%, #fafaf6 0%, var(--canvas) 73%);
}
body:has(.calculator:not([hidden])) { display: grid; place-items: center; }

h1, h2, p { margin: 0; }
h1, h2 { letter-spacing: -.045em; line-height: 1.08; }
h1 { font-size: clamp(1.65rem, 6vw, 2.4rem); }
h2 { font-size: clamp(1.27rem, 4vw, 1.75rem); }
button, input, textarea, select { font: inherit; }

button, .button {
  min-height: 2.75rem;
  border: 1px solid var(--line);
  border-radius: .68rem;
  padding: .55rem .8rem;
  color: var(--ink);
  background: var(--paper);
  font-weight: 650;
  cursor: pointer;
  touch-action: manipulation;
}

button:disabled { cursor: default; opacity: .5; }
button:not(:disabled):active, .button:active { filter: brightness(.95); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
.primary {
  border-color: transparent;
  color: #fff;
  background: var(--accent);
}
.outline { background: transparent; }
.button { display: inline-grid; place-items: center; text-align: center; text-decoration: none; }
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.calculator {
  display: grid;
  width: 100%;
  max-width: 31rem;
  gap: .8rem;
  padding: .7rem;
  border: 1px solid var(--line);
  border-radius: 1.55rem;
  background: var(--paper);
  box-shadow: var(--shadow);
}

.calc-panel {
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: clamp(.85rem, 3dvh, 2rem);
  min-height: 9.4rem;
  padding: .8rem;
  border: 1px solid #283754;
  border-radius: 1.03rem;
  color: #f7f9ff;
  background:
    radial-gradient(ellipse at 108% 0%, #264b9b80 0%, transparent 66%),
    linear-gradient(150deg, #162745, #101b31 70%);
}

.calc-header, .vault-header, .viewer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .65rem;
}
.calc-header h1 {
  min-width: 0;
  display: flex;
  align-items: center;
  font-size: clamp(1.18rem, 5.3vw, 1.88rem);
  letter-spacing: -.04em;
  white-space: nowrap;
}
.calc-mark {
  flex: none;
  display: grid;
  place-items: center;
  width: 1.55rem;
  height: 1.55rem;
  margin-right: .52rem;
  border-radius: .45rem;
  color: #172a53;
  background: #aabfff;
  font-size: 1.2rem;
  line-height: 1;
}
.brand-plus { margin-left: .05em; color: #aabfff; }
.angle {
  min-width: 3.25rem;
  flex: none;
  padding: .4rem .48rem;
  border-color: #607499;
  color: #e3eaff;
  background: #ffffff0d;
  font: 700 .8rem/1 ui-monospace, Consolas, monospace;
  letter-spacing: .035em;
}
.calc-panel :focus-visible { outline-color: #b6c9ff; }

.display { display: grid; gap: .35rem; min-width: 0; }
.expression, .result {
  width: 100%;
  min-width: 0;
  text-align: right;
  font-variant-numeric: lining-nums tabular-nums;
}
.expression {
  min-height: 1.95rem;
  margin: 0;
  padding: .22rem .26rem;
  border: 1px solid transparent;
  border-bottom-color: #ffffff32;
  border-radius: 0;
  color: #ccd9ff;
  background: transparent;
  font: 500 1rem/1.35 ui-monospace, Consolas, monospace;
  caret-color: #ccd9ff;
}
.expression::placeholder { color: #9eb0d0; }
.expression:focus { border-bottom-color: #a9bfff; }
.result {
  display: block;
  padding: .18rem .1rem;
  overflow-wrap: anywhere;
  font-size: clamp(2.1rem, 10vw, 4.15rem);
  line-height: 1;
  letter-spacing: -.067em;
  font-weight: 560;
}
.result[data-error="true"] { font-size: clamp(1rem, 4vw, 1.35rem); letter-spacing: -.015em; }

.keypad, .science-keys, .number-keys { display: grid; gap: clamp(.32rem, 1.5vw, .58rem); }
.science-keys { grid-template-columns: repeat(5, minmax(0, 1fr)); }
.number-keys { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.keypad button {
  min-width: 0;
  min-height: clamp(2.75rem, 6.9dvh, 3.95rem);
  padding: .15rem;
  border-radius: .65rem;
  background: #fbfbf8;
  box-shadow: 0 1px 1px #1721390d, inset 0 -1px 0 #1721390c;
  font-size: clamp(1.2rem, 5vw, 1.65rem);
  font-weight: 640;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}
.science-keys button {
  min-height: clamp(2.75rem, 5.8dvh, 3.15rem);
  color: #344662;
  background: #f2f3ef;
  font-size: clamp(.87rem, 3.6vw, 1.06rem);
  font-weight: 700;
}
.science-keys .utility {
  border-color: #e2dacf;
  color: #78452c;
  background: #f3ede5;
}
.number-keys .operator {
  color: #213f9b;
  border-color: #dae2ff;
  background: var(--accent-soft);
}
.number-keys .equals {
  color: #fff;
  border-color: #2346c4;
  background: var(--accent);
  font-size: 1.95rem;
}

.vault {
  max-width: 66rem;
  margin: clamp(.4rem, 4dvh, 2rem) auto;
}
.vault-header {
  min-height: 5.4rem;
  margin-bottom: .95rem;
  padding: .85rem clamp(.95rem, 3vw, 1.6rem);
  border-radius: 1.2rem;
  color: #f8f9ff;
  background: linear-gradient(117deg, var(--navy), #243e70);
}
.vault-header button {
  border-color: #7386ad;
  color: #f8f9ff;
  background: #ffffff12;
}
.vault-header :focus-visible { outline-color: #b6c9ff; }
.library-layout { display: grid; gap: .95rem; align-items: start; }
.library, .settings {
  min-width: 0;
  padding: clamp(.78rem, 3vw, 1.25rem);
  border: 1px solid var(--line);
  border-radius: 1.2rem;
  background: var(--paper);
  box-shadow: 0 3px 10px #17213908;
}
.primary-actions, .dialog-actions, .viewer-actions, .media-controls, .file-controls {
  display: flex;
  flex-wrap: wrap;
  gap: .55rem;
}
.primary-actions > button { flex: 1 0 auto; }
.filters {
  display: grid;
  grid-template-columns: minmax(0, 3fr) minmax(0, 2fr);
  gap: .65rem;
  margin-top: 1.2rem;
  padding-top: 1rem;
  border-top: 1px solid var(--line);
}
label {
  display: grid;
  gap: .35rem;
  color: var(--ink);
  font-size: .91rem;
  font-weight: 690;
}
label input, label textarea, label select, #generated {
  width: 100%;
  min-width: 0;
  min-height: 2.75rem;
  padding: .55rem .65rem;
  border: 1px solid #cdd1d2;
  border-radius: .57rem;
  color: var(--ink);
  background: var(--paper);
  font-size: 1rem;
  font-weight: 400;
}
label textarea { resize: vertical; line-height: 1.53; }
label input::placeholder { color: #667182; }
.status { color: var(--muted); font-size: .92rem; line-height: 1.48; overflow-wrap: anywhere; }
.status:empty { display: none; }
.library > .status:not(:empty) {
  margin-top: .85rem;
  padding: .7rem .8rem;
  border-radius: .55rem;
  background: var(--accent-soft);
}
.status a { color: #1c3ea6; }
.items {
  list-style: none;
  margin: 1rem 0 0;
  padding: 0;
  display: grid;
  gap: .45rem;
}
.item-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: .4rem;
  padding: .35rem;
  border: 1px solid var(--line);
  border-radius: .75rem;
  background: var(--paper);
}
.item-open {
  display: flex;
  flex: 1 1 10.5rem;
  align-items: center;
  gap: .65rem;
  min-width: 0;
  padding: .32rem;
  border-color: transparent;
  text-align: left;
}
.item-symbol {
  flex: none;
  display: grid;
  place-items: center;
  width: 2.55rem;
  min-height: 2.55rem;
  border-radius: .48rem;
  background: var(--accent-soft);
  color: #2946a5;
  font-size: .74rem;
  font-weight: 800;
}
img.item-symbol { height: 2.55rem; object-fit: cover; }
.item-label { min-width: 0; overflow-wrap: anywhere; }
.item-label small { display: block; margin-top: .15rem; color: var(--muted); font-size: .85rem; font-weight: 450; }
.item-row .secondary { padding-inline: .65rem; font-size: .87rem; }
.item-row .secondary:last-child { color: #80372d; }
.empty-state {
  padding: clamp(1.2rem, 4vw, 2.2rem) .85rem;
  border: 1px dashed #cdd1d2;
  border-radius: .65rem;
  color: var(--muted);
  line-height: 1.45;
  text-align: center;
}

.settings summary {
  padding: .5rem .15rem;
  color: var(--ink);
  font-weight: 730;
  cursor: pointer;
}
.settings-actions { display: grid; gap: .65rem; padding-top: .65rem; }
.settings-actions label { margin-top: .2rem; }

dialog {
  width: min(33rem, calc(100% - 1.5rem));
  max-height: calc(100dvh - max(1.5rem, env(safe-area-inset-top)) - max(1.5rem, env(safe-area-inset-bottom)));
  padding: clamp(.9rem, 4vw, 1.45rem);
  border: 1px solid var(--line);
  border-radius: 1.2rem;
  color: var(--ink);
  background: var(--paper);
  box-shadow: 0 1rem 4rem #121a33a0;
}
dialog::backdrop { background: #121a33b5; }
dialog form { display: grid; gap: .83rem; }
#auth-copy { color: var(--muted); line-height: 1.48; }
.dialog-actions { justify-content: flex-end; }
#editor { width: min(41rem, calc(100% - 1.5rem)); }
#edit-text { min-height: 10rem; }

.viewer { width: min(69rem, calc(100% - 1.5rem)); }
.viewer[open] { display: grid; grid-template-rows: auto minmax(2rem, 1fr) auto auto; gap: .68rem; }
.viewer-header > div { min-width: 0; }
.viewer-header h2, .viewer-meta { overflow-wrap: anywhere; }
.viewer-meta { margin-top: .2rem; color: var(--muted); font-size: .9rem; }
.viewer-meta:empty { display: none; }
.view-content {
  min-height: 0;
  max-height: 65dvh;
  overflow: auto;
  border-radius: .65rem;
  background: var(--surface);
  overscroll-behavior: contain;
}
.view-content:empty { display: none; }
.view-content[data-kind="img"], .view-content[data-kind="video"] { background: #0e1728; }
.view-content img, .view-content video {
  display: block;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 65dvh;
  margin-inline: auto;
}
.view-content video { width: 100%; }
.view-content:is([data-zoom="actual"], [data-zoom="large"], [data-zoom="extra"]) > img {
  max-width: none;
  max-height: none;
}
.view-content[data-zoom="large"] > img { zoom: 1.5; }
.view-content[data-zoom="extra"] > img { zoom: 2; }
.view-content[data-kind="audio"] { padding: 1rem; }
.view-content audio { display: block; width: min(100%, 36rem); margin: 0 auto; }
.view-content pre {
  margin: 0;
  padding: clamp(.8rem, 3vw, 1.35rem);
  color: var(--ink);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font: 1rem/1.58 ui-monospace, Consolas, monospace;
}
.view-content > p { padding: 1rem; line-height: 1.45; }
.viewer-actions { align-items: flex-start; justify-content: space-between; }
.media-controls, .file-controls { min-width: 0; }
.viewer-actions button, .viewer-actions .button { font-size: .88rem; }
.share-fields { display: grid; gap: .7rem; }
.chat-dialog {
  --chat-height: calc(100dvh - max(1.5rem, env(safe-area-inset-top)) - max(1.5rem, env(safe-area-inset-bottom)));
  width: min(43rem, calc(100% - 1.5rem));
  max-height: var(--chat-height);
}
.chat-dialog[open] { display: flex; flex-direction: column; gap: .72rem; overflow: hidden; }
.chat-dialog[open]:has(.chat-thread:not([hidden])) { height: min(44rem, var(--chat-height)); }
.chat-heading, .chat-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: .55rem;
}
.chat-header {
  flex: none;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: .55rem;
}
.chat-heading { min-width: 0; flex-wrap: nowrap; }
.chat-heading h2 { min-width: 0; overflow-wrap: anywhere; }
#chat-status:not(:empty) {
  flex: none;
  padding: .5rem .68rem;
  border-radius: .6rem;
  background: var(--accent-soft);
}
.chat-content { flex: 1; min-height: 0; overflow: auto; overscroll-behavior: contain; padding: 5px; }
.chat-lobby, .chat-thread, .chat-panel { display: grid; gap: .75rem; }
.chat-content > :not([hidden]) ~ :not([hidden]) { margin-top: .75rem; }
.chat-content:has(.chat-thread:not([hidden])):not(:has(.chat-panel:not([hidden]))) {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.chat-thread {
  flex: 1;
  min-height: 0;
  grid-template-rows: minmax(0, 1fr) auto;
}
.chat-panel, dialog .chat-join-form {
  padding: .75rem;
  border: 1px solid var(--line);
  border-radius: .8rem;
  background: var(--surface);
}
dialog .chat-join-form, dialog .chat-form {
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: end;
  gap: .5rem;
}
#chat-link, #chat-join-link {
  font: 1rem/1.3 ui-monospace, Consolas, monospace;
}
.chat-verification {
  display: block;
  padding: .6rem .75rem;
  border-radius: .6rem;
  background: var(--accent-soft);
  color: var(--ink);
  font: 700 clamp(1rem, 4.5vw, 1.22rem)/1.4 ui-monospace, Consolas, monospace;
  letter-spacing: .045em;
  overflow-wrap: anywhere;
}
.chat-list, .chat-messages {
  min-width: 0;
  display: grid;
  gap: .5rem;
  margin: 0;
  padding: 0;
  list-style: none;
}
.chat-list:empty { display: none; }
.chat-list li, .chat-messages li { min-width: 0; overflow-wrap: anywhere; }
.chat-list li > button { width: 100%; text-align: left; }
.chat-messages {
  align-content: start;
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
  padding: .7rem;
  border: 1px solid var(--line);
  border-radius: .8rem;
  background: var(--surface);
}
.chat-messages li {
  justify-self: start;
  width: fit-content;
  max-width: 88%;
  border: 1px solid var(--line);
  border-radius: .8rem .8rem .8rem .24rem;
  padding: .5rem .7rem;
  background: var(--paper);
  font-size: .95rem;
  line-height: 1.48;
  white-space: pre-wrap;
}
.chat-messages li[data-side="mine"] {
  justify-self: end;
  border-color: #dae2ff;
  border-radius: .8rem .8rem .24rem .8rem;
  background: var(--accent-soft);
}
.chat-messages li small { display: block; margin-top: .2rem; color: var(--muted); font-size: .82rem; }
.chat-list li:not(:has(button)), .chat-messages .chat-empty {
  justify-self: stretch;
  width: auto;
  max-width: none;
  padding: .6rem .7rem;
  border: 1px dashed var(--line);
  border-radius: .6rem;
  background: transparent;
  color: var(--muted);
  font-size: .9rem;
  text-align: center;
}
#chat-message { min-height: 3.2rem; max-height: min(28dvh, 11rem); }
@media (max-width: 27rem) {
  dialog .chat-join-form { grid-template-columns: minmax(0, 1fr); }
  .chat-header, .chat-heading { gap: .4rem; }
  .chat-header button { padding-inline: .57rem; font-size: .9rem; }
  .chat-heading h2 { font-size: 1.18rem; }
  .chat-actions button { flex: 1 1 6.5rem; }
}
@media (max-height: 32rem) {
  .chat-dialog {
    --chat-height: calc(100dvh - max(.6rem, env(safe-area-inset-top)) - max(.6rem, env(safe-area-inset-bottom)));
    padding: .65rem;
  }
  .chat-dialog[open] { gap: .4rem; }
  .chat-thread { gap: .45rem; }
  #chat-message { min-height: 2.75rem; }
}
#backup-output { margin-top: .65rem; justify-content: flex-start; }
#share-generated { font-family: ui-monospace, Consolas, monospace; }
#view-download, #share-download { overflow-wrap: anywhere; }

@media (hover: hover) {
  button:not(:disabled):hover, .button:hover { filter: brightness(.97); }
  .primary:not(:disabled):hover, .number-keys .equals:hover { background: var(--accent-hover); }
}
@media (min-width: 49rem) {
  .calculator {
    grid-template-columns: minmax(0, .92fr) minmax(0, 1.08fr);
    width: min(100%, 63rem);
    max-width: none;
    gap: 1.1rem;
    padding: 1rem;
    border-radius: 1.95rem;
  }

  .calc-panel {
    position: relative;
    isolation: isolate;
    padding: clamp(1.15rem, 3vw, 2rem);
    border-radius: 1.33rem;
    overflow: hidden;
  }
  .calc-panel::before {
    content: "";
    position: absolute;
    z-index: -1;
    inset: 28% 0 12%;
    background-image: linear-gradient(#94b0ff12 1px, transparent 1px),
      linear-gradient(90deg, #94b0ff12 1px, transparent 1px);
    background-size: 1.45rem 1.45rem;
    mask-image: radial-gradient(ellipse at 90% 55%, #000 5%, transparent 70%);
    pointer-events: none;
  }

  .display { gap: .7rem; }
  .result { font-size: clamp(2.7rem, 6.4vw, 5.2rem); }
  .keypad { gap: .7rem; }
  .primary-actions > button { flex: 0 1 auto; }
}
@media (min-width: 58rem) {
  .library-layout { grid-template-columns: minmax(0, 1fr) 15.3rem; }
}
@media (max-width: 36rem) {
  .viewer-actions { display: grid; grid-template-columns: minmax(0, 1fr); }
  .media-controls {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(4.25rem, 1fr));
  }
  .media-controls button { padding-inline: .2rem; line-height: 1.15; }
  .file-controls > :not([hidden]) { flex: 1 1 auto; }
}
@media (max-width: 24rem) {
  .calc-panel { gap: .55rem; padding: .65rem; }
  .filters { grid-template-columns: minmax(0, 1fr); }
  .item-row > .secondary { flex: 1 0 auto; }
  .primary-actions > button { padding-inline: .52rem; font-size: .9rem; }
  .media-controls { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
