:root {
  color-scheme: dark;
  --page: #090b10;
  --surface: #151820;
  --surface-2: #20242e;
  --key: #292e39;
  --key-hover: #343a47;
  --ink: #f7f3ec;
  --muted: #979dab;
  --accent: #ff8a3d;
  --accent-hover: #ff9d5c;
  --utility: #d8dbe2;
  --utility-ink: #191c22;
  --ring: rgba(255, 138, 61, .48);
  --shadow: 0 28px 80px rgba(0, 0, 0, .5);
}

* { box-sizing: border-box; }

html { min-width: 320px; }

body {
  min-height: 100vh;
  min-height: 100svh;
  margin: 0;
  display: grid;
  place-items: center;
  overflow-x: hidden;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans JP", sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at 14% 12%, rgba(255, 138, 61, .14), transparent 30rem),
    radial-gradient(circle at 88% 88%, rgba(82, 94, 123, .22), transparent 34rem),
    var(--page);
  padding: 28px 18px;
}

.app {
  width: min(100%, 880px);
}

.topline {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding: 0 4px 14px;
}

h1 {
  margin: 0;
  font-size: .875rem;
  line-height: 1.4;
  letter-spacing: .18em;
  text-transform: uppercase;
  font-weight: 750;
  color: var(--muted);
}

.keyboard-hint {
  margin: 0;
  font-size: .75rem;
  color: #6f7685;
}

.calculator-options { margin: 0 4px 16px; }
.scientific-toggle { display: inline-flex; flex-wrap: wrap; align-items: center; gap: 10px; min-height: 40px; cursor: pointer; font-size: .875rem; }
.scientific-toggle input { width: 20px; height: 20px; margin: 0; accent-color: var(--accent); }
.option-tag { color: var(--muted); border: 1px solid rgba(255,255,255,.12); border-radius: 999px; padding: 3px 8px; font-size: .75rem; }
.scientific-panel { margin: 0 0 16px; padding: 14px; border: 1px solid rgba(255,138,61,.25); border-radius: 18px; background: rgba(255,138,61,.045); }
.scientific-panel[hidden] { display: none; }
.scientific-heading { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px; }
.scientific-heading h2 { margin: 0; font-size: 1rem; }
.angle-mode { display: flex; flex-wrap: wrap; gap: 12px; margin: 0; padding: 0; border: 0; font-size: .875rem; }
.angle-mode label { display: flex; align-items: center; gap: 4px; min-height: 32px; cursor: pointer; }
.angle-mode input { margin: 0; accent-color: var(--accent); }
.scientific-guide { margin: 8px 0 12px; color: var(--muted); font-size: .8125rem; line-height: 1.6; }
.scientific-keys { display: grid; grid-template-columns: repeat(4,minmax(0,1fr)); gap: 8px; }
.scientific-keys button { min-width: 0; min-height: 44px; border: 1px solid rgba(255,255,255,.1); border-radius: 12px; background: var(--surface-2); color: #ffc196; font: inherit; font-size: 1rem; cursor: pointer; touch-action: manipulation; }
.scientific-keys button:hover { background: var(--key-hover); }
.scientific-keys button:active { transform: translateY(1px); }
.scientific-keys button:disabled { opacity: .4; cursor: not-allowed; }

.calculator {
  position: relative;
  padding: 18px;
  border: 1px solid rgba(255, 255, 255, .08);
  border-radius: 30px;
  background: linear-gradient(145deg, #1a1e27, #11131a 68%);
  box-shadow: var(--shadow);
}

.workspace {
  display: grid;
  grid-template-columns: minmax(340px, 430px) minmax(280px, 1fr);
  gap: 18px;
  align-items: stretch;
}

.calculator::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  border-radius: inherit;
  background: linear-gradient(135deg, rgba(255, 255, 255, .08), transparent 25%);
}

.display {
  min-height: 142px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: flex-end;
  gap: 10px;
  padding: 20px 10px 22px;
  overflow: hidden;
  font-variant-numeric: tabular-nums lining-nums;
}

.expression {
  width: 100%;
  min-height: 1.4em;
  overflow: hidden;
  color: var(--muted);
  text-align: right;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 1rem;
  letter-spacing: .02em;
}

.result {
  width: 100%;
  overflow: hidden;
  color: var(--ink);
  text-align: right;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: clamp(2.8rem, 13vw, 4.75rem);
  line-height: 1;
  letter-spacing: -.055em;
  font-weight: 650;
}

.result.error {
  color: #ffb28d;
  font-size: clamp(2rem, 9vw, 3rem);
  letter-spacing: -.03em;
}

.expression.error-message { white-space: normal; overflow-wrap: anywhere; line-height: 1.5; font-size: .875rem; }

.keys {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 11px;
}

.keys button {
  position: relative;
  min-width: 0;
  aspect-ratio: 1;
  border: 0;
  border-radius: 21px;
  background: var(--key);
  color: var(--ink);
  font: inherit;
  font-size: 1.45rem;
  font-weight: 650;
  cursor: pointer;
  touch-action: manipulation;
  box-shadow: inset 0 1px rgba(255, 255, 255, .08), 0 7px 16px rgba(0, 0, 0, .18);
  transition: transform 120ms ease, background-color 120ms ease, box-shadow 120ms ease;
  -webkit-tap-highlight-color: transparent;
}

.keys button:hover { background: var(--key-hover); }

.keys button:active,
.keys button.is-pressed {
  transform: translateY(2px) scale(.97);
  box-shadow: inset 0 1px rgba(255, 255, 255, .05), 0 3px 8px rgba(0, 0, 0, .16);
}

button:focus-visible {
  outline: 3px solid var(--ring);
  outline-offset: 3px;
}

.keys .utility {
  background: var(--utility);
  color: var(--utility-ink);
}

.keys .utility:hover { background: #eef0f4; }

.keys .operator {
  background: var(--accent);
  color: #231106;
  font-size: 1.7rem;
}

.keys .operator:hover { background: var(--accent-hover); }

.operator[aria-pressed="true"] {
  background: var(--ink);
  color: #a74100;
  box-shadow: 0 0 0 3px rgba(255, 138, 61, .3);
}

.keys .zero {
  grid-column: span 2;
  aspect-ratio: auto;
  text-align: left;
  padding-left: 28px;
}

.calculator-toolbar, .edit-actions, .input-toolbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}

.calculator-toolbar, .input-toolbar { justify-content: space-between; }
.input-toolbar { margin: 0 0 12px; }

.tool-button {
  min-height: 38px;
  border: 1px solid rgba(255, 255, 255, .12);
  border-radius: 10px;
  padding: 8px 10px;
  background: rgba(255, 255, 255, .035);
  color: var(--ink);
  font: inherit;
  font-size: .875rem;
  cursor: pointer;
  touch-action: manipulation;
}

.tool-button:hover:not(:disabled) { background: var(--key-hover); }
.tool-button:disabled { opacity: .35; cursor: not-allowed; }
.action-status { flex: 1 1 100px; min-height: 1.4em; margin: 0; color: #ffb888; font-size: .875rem; line-height: 1.4; }
.storage-note { margin: -8px 0 18px; color: var(--muted); font-size: .8125rem; line-height: 1.6; }
.shortcut-help { margin: 16px 4px 0; color: var(--muted); font-size: .875rem; line-height: 1.7; }
.shortcut-help summary { width: fit-content; padding: 4px 0; cursor: pointer; }
.shortcut-help summary:focus-visible { outline: 3px solid var(--ring); outline-offset: 3px; }
.shortcut-help p { margin: 8px 0; }
.copy-fallback { width: 100%; min-height: 44px; margin: 8px 0; padding: 10px; border: 1px solid var(--accent); border-radius: 10px; background: var(--surface); color: var(--ink); font: inherit; }

.memory-strip { margin: 0 0 14px; padding: 12px; border: 1px solid rgba(255,255,255,.08); border-radius: 16px; background: rgba(255,255,255,.025); }
.memory-readout { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 10px; color: var(--muted); font-size: .875rem; }
.memory-readout output { min-width: 0; overflow-wrap: anywhere; color: #ffc196; font-variant-numeric: tabular-nums; }
.memory-keys { display: grid; grid-template-columns: repeat(4, minmax(0,1fr)); gap: 8px; }
.memory-keys button { padding-inline: 6px; font-weight: 700; }
.history-tools { display: flex; justify-content: flex-end; flex-wrap: wrap; gap: 6px; }
.history-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 12px; }
.history-actions button { min-height: 40px; margin: 0; padding-inline: 6px; }
.use-history { border: 1px solid rgba(255,255,255,.14); border-radius: 12px; background: transparent; color: var(--ink); font: inherit; font-size: .85rem; cursor: pointer; }
.use-history:hover { background: var(--key-hover); }

.history-panel {
  display: flex;
  min-height: 0;
  max-height: 850px;
  flex-direction: column;
  padding: 24px;
  border: 1px solid rgba(255, 255, 255, .08);
  border-radius: 30px;
  background: rgba(21, 24, 32, .86);
  box-shadow: 0 22px 60px rgba(0, 0, 0, .3);
  backdrop-filter: blur(14px);
}

.history-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
}

.history-eyebrow {
  margin: 0 0 4px;
  color: var(--accent);
  font-size: .75rem;
  font-weight: 750;
  letter-spacing: .14em;
  text-transform: uppercase;
}

.history-header h2 {
  margin: 0;
  font-size: 1.35rem;
  line-height: 1.3;
}

.clear-history {
  flex: 0 0 auto;
  border: 1px solid rgba(255, 255, 255, .12);
  border-radius: 999px;
  padding: 9px 12px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: .8rem;
  cursor: pointer;
}

.clear-history:hover:not(:disabled) {
  border-color: rgba(255, 138, 61, .5);
  color: var(--ink);
}

.clear-history:disabled {
  cursor: not-allowed;
  opacity: .4;
}

.history-guide {
  margin: 16px 0 18px;
  color: var(--muted);
  font-size: .875rem;
  line-height: 1.6;
}

.history-empty {
  display: grid;
  flex: 1;
  place-content: center;
  justify-items: center;
  min-height: 220px;
  color: #747b89;
  text-align: center;
}

.history-empty[hidden] { display: none; }

.history-empty span {
  display: grid;
  width: 52px;
  height: 52px;
  place-items: center;
  margin-bottom: 10px;
  border: 1px solid rgba(255, 255, 255, .1);
  border-radius: 16px;
  color: var(--accent);
  font-size: 1.5rem;
}

.history-empty p {
  margin: 0;
  font-size: .9rem;
}

.history-list {
  display: grid;
  gap: 10px;
  margin: 0;
  padding: 0 4px 0 0;
  overflow-y: auto;
  list-style: none;
  scrollbar-color: #454b58 transparent;
}

.history-item {
  padding: 14px;
  border: 1px solid rgba(255, 255, 255, .07);
  border-radius: 18px;
  background: var(--surface-2);
}

.history-calculation {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
  font-variant-numeric: tabular-nums lining-nums;
}

.history-expression {
  min-width: 0;
  max-width: 100%;
  overflow: hidden;
  color: var(--muted);
  font-size: .85rem;
  line-height: 1.45;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-result {
  max-width: 100%;
  overflow-wrap: anywhere;
  flex: 0 0 auto;
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -.025em;
}

.restore-history {
  width: 100%;
  margin-top: 12px;
  border: 0;
  border-radius: 12px;
  padding: 9px 12px;
  background: rgba(255, 138, 61, .12);
  color: #ffad78;
  font: inherit;
  font-size: .85rem;
  font-weight: 700;
  cursor: pointer;
}

.restore-history:hover { background: rgba(255, 138, 61, .2); }

.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;
}

@media (max-width: 390px) {
  body { padding: 18px 12px; }
  .keyboard-hint { display: none; }
  .calculator { padding: 14px; border-radius: 25px; }
  .display { min-height: 128px; padding-top: 12px; }
  .keys { gap: 8px; }
  .keys button { border-radius: 18px; }
}

@media (max-width: 800px) {
  body { place-items: start center; }
  .app { width: min(100%, 430px); }
  .workspace { grid-template-columns: 1fr; }
  .history-panel { max-height: 480px; }
}

@media (min-width: 801px) {
  .keys button { aspect-ratio: auto; min-height: 72px; }
  .display { min-height: 124px; }
}

@media (prefers-reduced-motion: reduce) {
  button { transition: none; }
}

