:root,
[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0b0e21;
  --bg-elevated: #12152c;
  --surface: #1a1c3a;
  --surface-2: #232548;
  --num: #1e2040;
  --op: #2a2d55;
  --op-text: #c5cbff;
  --op-accent: #4050d2;
  --op-accent-text: #ffffff;
  --ac: #9bb0ff;
  --ac-text: #0b0e21;
  --eq: #f2b8c6;
  --eq-text: #31111d;
  --sci: #2c2f58;
  --sci-text: #d5d8ff;
  --text: #e8e8f0;
  --text-dim: #9aa0b8;
  --result: #f2b8c6;
  --accent: #c4c0ff;
  --danger: #ffb4ab;
  --history-chip: #3b3f6b;
  --history-border: rgba(255, 255, 255, 0.06);
  --menu-hover: rgba(255, 255, 255, 0.06);
  --shadow: 0 8px 28px rgba(0, 0, 0, 0.35);
  --theme-color: #0b0e21;
}

[data-theme="light"] {
  color-scheme: light;
  --bg: #f7f5fa;
  --bg-elevated: #ffffff;
  --surface: #ece8f0;
  --surface-2: #ffffff;
  --num: #e7e2eb;
  --op: #d9d2e3;
  --op-text: #3d3a55;
  --op-accent: #4a56d6;
  --op-accent-text: #ffffff;
  --ac: #4a56d6;
  --ac-text: #ffffff;
  --eq: #7a4e8c;
  --eq-text: #ffffff;
  --sci: #ddd6e6;
  --sci-text: #3d3a55;
  --text: #1c1b1f;
  --text-dim: #5f5b66;
  --result: #7a4e8c;
  --accent: #4a56d6;
  --danger: #b3261e;
  --history-chip: #e0dae8;
  --history-border: rgba(0, 0, 0, 0.08);
  --menu-hover: rgba(0, 0, 0, 0.05);
  --shadow: 0 8px 28px rgba(40, 30, 60, 0.12);
  --theme-color: #f7f5fa;
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]),
  [data-theme="system"] {
    color-scheme: light;
    --bg: #f7f5fa;
    --bg-elevated: #ffffff;
    --surface: #ece8f0;
    --surface-2: #ffffff;
    --num: #e7e2eb;
    --op: #d9d2e3;
    --op-text: #3d3a55;
    --op-accent: #4a56d6;
    --op-accent-text: #ffffff;
    --ac: #4a56d6;
    --ac-text: #ffffff;
    --eq: #7a4e8c;
    --eq-text: #ffffff;
    --sci: #ddd6e6;
    --sci-text: #3d3a55;
    --text: #1c1b1f;
    --text-dim: #5f5b66;
    --result: #7a4e8c;
    --accent: #4a56d6;
    --danger: #b3261e;
    --history-chip: #e0dae8;
    --history-border: rgba(0, 0, 0, 0.08);
    --menu-hover: rgba(0, 0, 0, 0.05);
    --shadow: 0 8px 28px rgba(40, 30, 60, 0.12);
    --theme-color: #f7f5fa;
  }
}

:root {
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --font: "Google Sans", "Product Sans", "Segoe UI", system-ui, -apple-system, sans-serif;
  --pad-gap: clamp(8px, 1.4vw, 14px);
  --key-max: 120px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overflow: hidden;
  user-select: none;
}

body {
  min-height: 100dvh;
}

.app {
  width: 100%;
  height: 100dvh;
  max-height: 100dvh;
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: auto minmax(100px, 1fr) auto;
  grid-template-areas:
    "top"
    "display"
    "keypad";
  padding:
    calc(8px + var(--safe-top))
    calc(14px + var(--safe-right))
    calc(12px + var(--safe-bottom))
    calc(14px + var(--safe-left));
  gap: 6px;
  background: var(--bg);
  position: relative;
}

.app.history-open {
  grid-template-rows: auto minmax(0, 1.1fr) auto auto;
  grid-template-areas:
    "top"
    "history"
    "display"
    "keypad";
}

.top-bar {
  grid-area: top;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: relative;
  min-height: 44px;
  padding: 0 2px;
  z-index: 5;
}

.icon-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--accent);
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: background 0.15s ease;
}

.icon-btn:hover,
.icon-btn[aria-expanded="true"] {
  background: var(--menu-hover);
}

.menu-panel {
  position: absolute;
  top: 44px;
  right: 0;
  min-width: 220px;
  background: var(--surface-2);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 8px;
  z-index: 20;
  display: grid;
  gap: 2px;
  border: 1px solid var(--history-border);
}

.menu-panel[hidden] {
  display: none;
}

.menu-panel button {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text);
  text-align: left;
  padding: 12px 14px;
  border-radius: 8px;
  font: inherit;
  font-size: 0.95rem;
  cursor: pointer;
}

.menu-panel button:hover {
  background: var(--menu-hover);
}

.menu-footer {
  margin-top: 4px;
  padding-top: 4px;
  border-top: 1px solid var(--history-border);
}

.menu-footer-row {
  display: block;
  padding: 12px 14px;
  text-align: left;
  border-radius: 8px;
}

.menu-link {
  display: block;
  color: var(--accent);
  font: inherit;
  font-size: 0.95rem;
  line-height: 1.25;
  text-decoration: none;
}

.menu-link:hover {
  text-decoration: underline;
}

.menu-footer-row:has(.menu-link):hover {
  background: var(--menu-hover);
}

.menu-build {
  color: var(--text-dim);
  font-size: 0.95rem;
  line-height: 1.25;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.02em;
  user-select: text;
  cursor: text;
  -webkit-user-select: text;
}

.history-panel {
  grid-area: history;
  min-height: 0;
  overflow: hidden;
  border-bottom: 1px solid var(--history-border);
  margin: 0 2px 4px;
  animation: slideIn 0.2s ease;
}

.history-panel[hidden] {
  display: none;
}

.history-scroll {
  height: 100%;
  overflow-y: auto;
  padding: 8px 4px 12px;
  scrollbar-width: thin;
  scrollbar-color: var(--surface) transparent;
}

.history-day {
  color: var(--text-dim);
  font-size: 0.78rem;
  margin: 10px 4px 8px;
  letter-spacing: 0.02em;
}

.history-item {
  width: 100%;
  appearance: none;
  border: 0;
  background: transparent;
  color: inherit;
  text-align: right;
  padding: 10px 10px;
  border-radius: 12px;
  cursor: pointer;
  display: grid;
  gap: 2px;
  position: relative;
}

.history-item:hover,
.history-item.selected {
  background: var(--menu-hover);
}

.history-expr {
  color: var(--text-dim);
  font-size: 0.95rem;
  word-break: break-all;
  -webkit-user-select: text;
  user-select: text;
  cursor: text;
}

.history-result {
  color: var(--text);
  font-size: 1.35rem;
  font-weight: 500;
  -webkit-user-select: text;
  user-select: text;
  cursor: text;
}

.history-actions {
  position: absolute;
  top: -6px;
  right: 10px;
  display: flex;
  gap: 6px;
}

.history-actions button {
  appearance: none;
  border: 0;
  background: var(--history-chip);
  color: var(--accent);
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
}

.history-empty {
  color: var(--text-dim);
  text-align: center;
  padding: 28px 12px;
  font-size: 0.95rem;
}

.display {
  grid-area: display;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  min-height: 0;
  padding: 4px 8px 10px;
  gap: 4px;
}

.current-label {
  color: var(--text-dim);
  font-size: 0.8rem;
  opacity: 0;
  transition: opacity 0.15s ease;
  align-self: flex-start;
}

.app.history-open .current-label,
.app.wide .current-label {
  opacity: 1;
}

.keypad-toolbar {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  min-height: 28px;
  padding: 0 2px;
}

.expand-btn svg {
  transition: transform 0.2s ease;
}

.expand-btn[aria-expanded="true"] svg {
  transform: rotate(180deg);
}

.expression-wrap {
  overflow: hidden;
  width: 100%;
}

.expression {
  font-size: clamp(1.8rem, 7vw, 3.25rem);
  font-weight: 400;
  line-height: 1.15;
  text-align: right;
  white-space: nowrap;
  outline: none;
  letter-spacing: 0.01em;
  transform-origin: right center;
  transition: font-size 0.12s ease;
  caret-color: var(--accent);
  -webkit-user-select: text;
  user-select: text;
  cursor: text;
}

.expression.small {
  font-size: clamp(1.25rem, 4.5vw, 2rem);
}

.result {
  min-height: 1.6em;
  text-align: right;
  color: var(--result);
  font-size: clamp(1.15rem, 4vw, 1.85rem);
  font-weight: 500;
  opacity: 0.95;
  -webkit-user-select: text;
  user-select: text;
  cursor: text;
}

.result.error {
  color: var(--danger);
  font-size: 1rem;
}

.keypad {
  grid-area: keypad;
  display: grid;
  gap: var(--pad-gap);
  padding: 4px 2px 0;
  width: min(100%, 560px);
  margin-inline: auto;
  align-content: end;
}

/* When scientific is open in portrait, match stock: pill-shaped main keys */
.app.scientific:not(.wide) .main-pad .key {
  border-radius: 999px;
  aspect-ratio: 1.55 / 1;
  max-width: none;
  max-height: none;
  width: 100%;
  height: auto;
}

.app.scientific .display {
  min-height: 5.5rem;
}

.sci-pad,
.main-pad {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: var(--pad-gap);
  align-items: stretch;
}

.sci-pad[hidden] {
  display: none;
}

.key {
  appearance: none;
  border: 0;
  border-radius: 50%;
  aspect-ratio: 1 / 1;
  width: 100%;
  height: auto;
  max-width: var(--key-max);
  max-height: var(--key-max);
  justify-self: center;
  font: inherit;
  font-size: clamp(1.2rem, 4.5vw, 1.7rem);
  font-weight: 500;
  color: var(--text);
  background: var(--num);
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: transform 0.08s ease, filter 0.08s ease, background 0.15s ease, color 0.15s ease;
  -webkit-user-select: none;
  user-select: none;
}

.key:active {
  transform: scale(0.96);
  filter: brightness(1.08);
}

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

.key.op {
  background: var(--op);
  color: var(--op-text);
}

.key.op.accent {
  background: var(--op-accent);
  color: var(--op-accent-text);
  font-size: clamp(1.35rem, 5vw, 1.85rem);
}

.key.ac {
  background: var(--ac);
  color: var(--ac-text);
  font-weight: 700;
}

.key.eq {
  background: var(--eq);
  color: var(--eq-text);
  font-size: clamp(1.5rem, 5.5vw, 2rem);
  font-weight: 700;
}

.key.sci {
  background: var(--sci);
  color: var(--sci-text);
  font-size: clamp(0.85rem, 2.8vw, 1.1rem);
  font-weight: 600;
  aspect-ratio: 1.7 / 1;
  border-radius: 999px;
  max-width: none;
  max-height: none;
  width: 100%;
}

.key.sci.active {
  background: var(--ac);
  color: var(--ac-text);
}

@media (max-height: 720px) {
  .sci-pad,
  .main-pad {
    gap: 8px;
  }

  .key {
    font-size: clamp(1.05rem, 3.8vw, 1.35rem);
  }
}

@media (max-height: 640px) {
  .app.history-open:not(.wide) .history-panel {
    max-height: 28dvh;
  }

  .key.sci {
    aspect-ratio: 1.85 / 1;
  }
}

/*
 * Phone landscape — matches stock Google Calculator:
 * 5 columns: [7 8 9] [AC ×] etc., pill keys (portrait stays 4-col circles).
 */
@media (orientation: landscape) and (max-height: 560px) {
  .app,
  .app.history-open {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr) auto;
    grid-template-areas:
      "top"
      "display"
      "keypad";
    padding:
      calc(4px + var(--safe-top))
      calc(12px + var(--safe-right))
      calc(6px + var(--safe-bottom))
      calc(12px + var(--safe-left));
    gap: 2px;
  }

  .app.history-open {
    grid-template-rows: auto minmax(0, 0.7fr) minmax(0, 0.55fr) auto;
    grid-template-areas:
      "top"
      "history"
      "display"
      "keypad";
  }

  .display {
    padding: 0 8px 4px;
  }

  .expression {
    font-size: clamp(1.4rem, 6vh, 2.2rem);
  }

  .result {
    font-size: clamp(1rem, 4vh, 1.35rem);
    min-height: 1.2em;
  }

  .keypad {
    width: 100%;
    max-width: none;
    margin: 0;
    --pad-gap: clamp(6px, 1.2vh, 10px);
  }

  .main-pad {
    grid-template-columns: repeat(5, minmax(0, 1fr));
    grid-template-rows: repeat(4, minmax(0, 1fr));
    height: min(42dvh, 280px);
  }

  .main-pad .key {
    border-radius: 999px;
    aspect-ratio: auto;
    max-width: none;
    max-height: none;
    width: 100%;
    height: 100%;
    justify-self: stretch;
    font-size: clamp(1.1rem, 4.2vh, 1.55rem);
  }

  .main-pad .key.op.accent,
  .main-pad .key.eq {
    font-size: clamp(1.2rem, 4.6vh, 1.7rem);
  }

  /* Stock landscape order */
  .main-pad [data-key="7"] { grid-area: 1 / 1; }
  .main-pad [data-key="8"] { grid-area: 1 / 2; }
  .main-pad [data-key="9"] { grid-area: 1 / 3; }
  .main-pad [data-key="AC"] { grid-area: 1 / 4; }
  .main-pad [data-key="×"] { grid-area: 1 / 5; }

  .main-pad [data-key="4"] { grid-area: 2 / 1; }
  .main-pad [data-key="5"] { grid-area: 2 / 2; }
  .main-pad [data-key="6"] { grid-area: 2 / 3; }
  .main-pad [data-key="()"] { grid-area: 2 / 4; }
  .main-pad [data-key="−"] { grid-area: 2 / 5; }

  .main-pad [data-key="1"] { grid-area: 3 / 1; }
  .main-pad [data-key="2"] { grid-area: 3 / 2; }
  .main-pad [data-key="3"] { grid-area: 3 / 3; }
  .main-pad [data-key="%"] { grid-area: 3 / 4; }
  .main-pad [data-key="+"] { grid-area: 3 / 5; }

  .main-pad [data-key="0"] { grid-area: 4 / 1; }
  .main-pad [data-key="."] { grid-area: 4 / 2; }
  .main-pad [data-key="back"] { grid-area: 4 / 3; }
  .main-pad [data-key="÷"] { grid-area: 4 / 4; }
  .main-pad [data-key="="] { grid-area: 4 / 5; }

  .sci-pad {
    grid-template-columns: repeat(6, minmax(0, 1fr));
  }

  .sci-pad .key.sci {
    aspect-ratio: auto;
    height: clamp(34px, 7vh, 44px);
  }
}

/*
 * Tablet / desktop wide: history column + keypad fills the screen.
 */
@media (min-width: 900px) and (min-aspect-ratio: 1/1) {
  .app,
  .app.history-open {
    grid-template-columns: minmax(260px, 0.9fr) minmax(420px, 1.35fr);
    grid-template-rows: auto minmax(0, 1fr) auto;
    grid-template-areas:
      "top top"
      "history display"
      "history keypad";
    column-gap: clamp(16px, 3vw, 40px);
    row-gap: 8px;
    padding:
      calc(12px + var(--safe-top))
      calc(24px + var(--safe-right))
      calc(16px + var(--safe-bottom))
      calc(20px + var(--safe-left));
  }

  .history-panel,
  .app.history-open .history-panel {
    display: block;
    border-bottom: 0;
    border-right: 1px solid var(--history-border);
    margin: 0;
    padding-right: clamp(12px, 2vw, 24px);
    animation: none;
  }

  .history-toggle-wide-hide {
    visibility: hidden;
    pointer-events: none;
  }

  .display {
    padding: 8px 12px 16px;
  }

  .expression {
    font-size: clamp(2.4rem, 4.5vw, 4rem);
  }

  .result {
    font-size: clamp(1.35rem, 2.2vw, 2rem);
  }

  .keypad {
    width: 100%;
    max-width: none;
    margin: 0;
    align-content: end;
    --pad-gap: clamp(10px, 1.2vw, 16px);
  }

  .main-pad {
    grid-template-columns: repeat(5, minmax(0, 1fr));
    grid-template-rows: repeat(4, minmax(0, 1fr));
    min-height: min(46dvh, 360px);
  }

  .main-pad .key {
    border-radius: 999px;
    aspect-ratio: auto;
    max-width: none;
    max-height: none;
    width: 100%;
    height: 100%;
    justify-self: stretch;
    font-size: clamp(1.35rem, 2.2vw, 1.9rem);
  }

  .main-pad .key.op.accent,
  .main-pad .key.eq {
    font-size: clamp(1.5rem, 2.4vw, 2.1rem);
  }

  .main-pad [data-key="7"] { grid-area: 1 / 1; }
  .main-pad [data-key="8"] { grid-area: 1 / 2; }
  .main-pad [data-key="9"] { grid-area: 1 / 3; }
  .main-pad [data-key="AC"] { grid-area: 1 / 4; }
  .main-pad [data-key="×"] { grid-area: 1 / 5; }

  .main-pad [data-key="4"] { grid-area: 2 / 1; }
  .main-pad [data-key="5"] { grid-area: 2 / 2; }
  .main-pad [data-key="6"] { grid-area: 2 / 3; }
  .main-pad [data-key="()"] { grid-area: 2 / 4; }
  .main-pad [data-key="−"] { grid-area: 2 / 5; }

  .main-pad [data-key="1"] { grid-area: 3 / 1; }
  .main-pad [data-key="2"] { grid-area: 3 / 2; }
  .main-pad [data-key="3"] { grid-area: 3 / 3; }
  .main-pad [data-key="%"] { grid-area: 3 / 4; }
  .main-pad [data-key="+"] { grid-area: 3 / 5; }

  .main-pad [data-key="0"] { grid-area: 4 / 1; }
  .main-pad [data-key="."] { grid-area: 4 / 2; }
  .main-pad [data-key="back"] { grid-area: 4 / 3; }
  .main-pad [data-key="÷"] { grid-area: 4 / 4; }
  .main-pad [data-key="="] { grid-area: 4 / 5; }

  .sci-pad {
    grid-template-columns: repeat(6, minmax(0, 1fr));
  }

  .sci-pad .key.sci {
    aspect-ratio: auto;
    height: 48px;
  }
}

@media (min-width: 1400px) {
  .app,
  .app.history-open {
    grid-template-columns: minmax(300px, 420px) minmax(560px, 820px);
    justify-content: center;
    column-gap: 48px;
  }
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + var(--safe-bottom));
  transform: translateX(-50%);
  background: var(--surface-2);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 0.9rem;
  box-shadow: var(--shadow);
  z-index: 50;
  border: 1px solid var(--history-border);
}

.toast[hidden] {
  display: none;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(-6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation: none !important;
    transition: none !important;
  }
}
