:root {
  --bg: #0b0e14;
  --surface: #141922;
  --surface-2: #1a202b;
  --surface-3: #232b3a;
  --key: #1b212c;
  --key-hover: #232b39;
  --fn-key: #161c26;
  --line: #273043;
  --text: #edf1f8;
  --muted: #8b95a8;
  --accent: #f5a524;
  --accent-ink: #17120a;
  --danger: #ff7b72;
  --shadow: 0 24px 60px -24px rgba(0, 0, 0, .8);
  --key-h: 54px;
  --sci-h: 46px;
  --gap: 9px;
}
[data-theme="light"] {
  --bg: #eef1f6;
  --surface: #ffffff;
  --surface-2: #f5f7fb;
  --surface-3: #e9edf5;
  --key: #fbfcfe;
  --key-hover: #f1f4f9;
  --fn-key: #f4f6fa;
  --line: #e1e6ef;
  --text: #121722;
  --muted: #5f6b84;
  --accent: #b45309;
  --accent-ink: #ffffff;
  --danger: #c0281c;
  --shadow: 0 24px 60px -30px rgba(16, 24, 40, .35);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { min-height: 100%; }
body {
  margin: 0;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: max(10px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
  background: var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

.shell {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  width: 100%;
  max-width: 760px;
}

.calc {
  width: min(410px, calc(100vw - 20px));
  max-height: calc(100dvh - 20px);
  display: flex;
  flex-direction: column;
  gap: 9px;
  padding: 13px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 26px;
  box-shadow: var(--shadow);
}

/* status bar */
.statusbar { display: flex; align-items: center; gap: 6px; height: 30px; flex: none; }
.spacer { flex: 1; }
.chip {
  height: 27px;
  padding: 0 11px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  font: inherit;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .07em;
  cursor: pointer;
}
.chip:hover { color: var(--text); border-color: var(--muted); }
.flag {
  padding: 2px 7px;
  border: 1px solid var(--accent);
  border-radius: 999px;
  color: var(--accent);
  font-size: 9.5px;
  font-weight: 700;
  letter-spacing: .04em;
  line-height: 1.4;
}
.icon {
  width: 30px;
  height: 30px;
  display: grid;
  place-items: center;
  padding: 0;
  background: transparent;
  border: 0;
  border-radius: 9px;
  color: var(--muted);
  cursor: pointer;
}
.icon:hover { background: var(--surface-3); color: var(--text); }
.icon svg {
  width: 17px;
  height: 17px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}
[data-theme="dark"] .moon, [data-theme="light"] .sun { display: none; }

/* display */
.display {
  flex: none;
  min-height: 96px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: 2px;
  padding: 6px 8px 8px;
}
.expr {
  min-height: 24px;
  text-align: right;
  font-size: 17px;
  line-height: 1.45;
  color: var(--muted);
  white-space: nowrap;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  user-select: none;
  cursor: text;
}
.expr::-webkit-scrollbar { display: none; }
.expr .tok { display: inline-block; padding: 0 1px; }
.expr .tok.op { color: var(--accent); padding: 0 3px; }
.expr .tok.num { color: var(--text); }
.expr .tok.fn { color: var(--muted); }
.expr .tok.const { color: var(--accent); }
.expr .tok.ans { color: #7fa9ff; }
[data-theme="light"] .expr .tok.ans { color: #1d5fd6; }
.caret {
  display: inline-block;
  width: 2px;
  height: 1.05em;
  margin: 0 1px;
  vertical-align: -.16em;
  background: var(--accent);
  animation: blink 1.1s steps(1) infinite;
}
@keyframes blink { 50% { opacity: 0; } }
.result {
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  letter-spacing: -.02em;
  line-height: 1.12;
  white-space: nowrap;
  overflow-x: auto;
  scrollbar-width: none;
  transition: color .12s;
}
.result::-webkit-scrollbar { display: none; }
.result[data-size="lg"] { font-size: 50px; }
.result[data-size="md"] { font-size: 40px; }
.result[data-size="sm"] { font-size: 32px; }
.result[data-size="xs"] { font-size: 25px; }
.result.preview { color: var(--muted); }
.result.error { color: var(--danger); font-size: 26px; font-weight: 500; }
.result sup { font-size: .55em; vertical-align: super; }
.error {
  min-height: 0;
  margin-top: 1px;
  text-align: right;
  font-size: 12px;
  color: var(--danger);
}

/* keypads */
.pad { display: grid; gap: var(--gap); flex: none; }
.sci { grid-template-columns: repeat(4, 1fr); }
.basic { grid-template-columns: repeat(5, 1fr); }

.key {
  display: flex;
  align-items: center;
  justify-content: center;
  height: var(--key-h);
  padding: 0 4px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--key);
  color: var(--text);
  font: inherit;
  font-size: 19px;
  font-weight: 500;
  line-height: 1;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  transition: transform .09s ease, background .12s ease, border-color .12s ease, color .12s ease;
}
.key:hover { background: var(--key-hover); }
.key:active, .key.is-pressed { transform: scale(.96); }
.sci .key {
  height: var(--sci-h);
  border-radius: 13px;
  background: var(--fn-key);
  color: var(--muted);
  font-size: 15px;
}
.sci .key:hover { color: var(--text); }
.key.num { font-size: 21px; }
.key.op { color: var(--accent); font-size: 22px; }
.key.util { color: var(--muted); font-size: 15px; }
.key svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.key.eq {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-size: 24px;
  font-weight: 600;
}
.key.eq:hover { background: var(--accent); filter: brightness(1.07); }
.key.shift.is-on {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 700;
}

:where(.key, .icon, .chip, .ghost):focus-visible,
.hist-main:focus-visible, .hist-del:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.hint {
  margin: 0;
  text-align: center;
  font-size: 10.5px;
  color: var(--muted);
  letter-spacing: .015em;
}

/* history */
.history {
  width: 300px;
  height: 580px;
  max-height: calc(100dvh - 20px);
  display: none;
  flex-direction: column;
  gap: 10px;
  padding: 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 22px;
  box-shadow: var(--shadow);
}
.history-head { display: flex; align-items: center; gap: 8px; flex: none; }
.history-head h2 {
  margin: 0;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .09em;
  text-transform: uppercase;
  color: var(--muted);
}
.ghost {
  padding: 4px 7px;
  border: 0;
  border-radius: 7px;
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 11px;
  cursor: pointer;
}
.ghost:hover { color: var(--text); background: var(--surface-3); }
.history-list {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 5px;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--surface-3) transparent;
}
.hist-item { display: flex; align-items: stretch; gap: 2px; }
.hist-main {
  flex: 1;
  min-width: 0;
  padding: 7px 8px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--text);
  font: inherit;
  text-align: right;
  cursor: pointer;
}
.hist-main:hover { background: var(--surface-2); }
.hist-expr {
  display: block;
  font-size: 11.5px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.hist-res {
  display: block;
  margin-top: 2px;
  font-size: 17px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.hist-del {
  flex: none;
  width: 26px;
  border: 0;
  border-radius: 8px;
  background: none;
  color: var(--muted);
  font-size: 15px;
  cursor: pointer;
  opacity: .55;
}
.hist-del:hover { color: var(--danger); background: var(--surface-2); opacity: 1; }
.empty {
  margin: auto;
  padding: 18px;
  text-align: center;
  font-size: 12px;
  line-height: 1.65;
  color: var(--muted);
}

/* toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: max(18px, env(safe-area-inset-bottom));
  z-index: 60;
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface-3);
  color: var(--text);
  font-size: 12px;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 8px);
  transition: opacity .18s ease, transform .18s ease;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* responsive */
@media (max-width: 899px) {
  .history {
    position: fixed;
    inset: 10px 10px 10px auto;
    z-index: 51;
    width: min(340px, calc(100vw - 20px));
    height: auto;
    max-height: none;
  }
  .history.open { display: flex; }
  .backdrop { position: fixed; inset: 0; z-index: 50; background: rgba(0, 0, 0, .52); }
  .close-history { font-size: 20px; line-height: 1; }
}
@media (min-width: 900px) {
  .history { display: flex; }
  .history-toggle, .close-history { display: none; }
  .backdrop { display: none; }
}

@media (max-height: 800px) {
  :root { --key-h: 48px; --sci-h: 42px; --gap: 8px; }
  .display { min-height: 86px; }
  .result[data-size="lg"] { font-size: 44px; }
}
@media (max-height: 700px) {
  :root { --key-h: 44px; --sci-h: 38px; --gap: 6px; }
  .display { min-height: 74px; padding: 2px 6px 4px; }
  .result[data-size="lg"] { font-size: 38px; }
  .result[data-size="md"] { font-size: 32px; }
  .calc { gap: 7px; }
}
@media (max-height: 620px) {
  :root { --key-h: 39px; --sci-h: 33px; --gap: 5px; }
  .display { min-height: 62px; }
  .result[data-size="lg"] { font-size: 32px; }
  .result[data-size="md"] { font-size: 27px; }
  .statusbar { height: 26px; }
  .hint { display: none; }
  .calc { padding: 10px; gap: 6px; border-radius: 20px; }
  .key { border-radius: 12px; }
}
@media (max-width: 380px) {
  .calc { padding: 10px; border-radius: 22px; }
  .key { border-radius: 13px; }
  .sci .key { font-size: 13px; }
  .result[data-size="lg"] { font-size: 40px; }
  .result[data-size="md"] { font-size: 34px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .001ms !important;
  }
}
