:root {
  --paper: #f5efe1;
  --paper-dark: #ebe3cd;
  --ink: #2b2118;
  --ink-soft: #6b5b4a;
  --line: rgba(92, 71, 45, 0.18);
  --key: #fbf7ec;
  --key-hover: #f1e9d4;
  --key-shadow: rgba(92, 71, 45, 0.25);
  --fn: #e6dcc3;
  --accent: #b5452b;
  --accent-soft: #d9735a;
  --teal: #2f6f6a;
  --page-bg: url("background.svg");
  --shadow: 0 2px 0 rgba(92,71,45,0.15), 0 18px 40px rgba(60, 40, 20, 0.22);
  --grain: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='160' height='160'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='3' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 0.35  0 0 0 0 0.25  0 0 0 0 0.15  0 0 0 0.28 0'/></filter><rect width='100%' height='100%' filter='url(%23n)'/></svg>");
}

[data-theme="dark"] {
  --paper: #171b26;
  --paper-dark: #0e1119;
  --ink: #e9edf7;
  --ink-soft: #8d96ab;
  --line: rgba(160, 175, 210, 0.10);
  --key: #222938;
  --key-hover: #2c3547;
  --key-shadow: rgba(0, 0, 0, 0.6);
  --fn: #1b2130;
  --accent: #ff8a65;
  --accent-soft: #ffb199;
  --teal: #4fd1c5;
  --shadow: 0 1px 0 rgba(255,255,255,0.04), 0 14px 30px rgba(0, 0, 0, 0.45);
}

/* Portrait phones get the tall version so the margin and tally marks stay on screen */
@media (max-width: 600px) and (orientation: portrait) {
  :root { --page-bg: url("background-mobile.svg"); }
}

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

/* ---------- Welcome overlay ---------- */
.welcome {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 24px;
  background-color: var(--paper-dark);
  transition: opacity 0.9s ease, visibility 0.9s ease;
}
.welcome::before {
  content: "";
  position: absolute;
  inset: 0;
  background-image: var(--grain);
  opacity: 0.6;
  pointer-events: none;
}
.welcome.leaving {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}
.welcome-card {
  position: relative;
  width: 100%;
  max-width: 400px;
  padding: 40px 28px 32px;
  text-align: center;
  background: var(--key);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow);
  animation: rise 0.7s ease;
}
.welcome-mark {
  display: block;
  width: 72px;
  height: auto;
  margin: 0 auto 16px;
}
.welcome-card h1 {
  font-size: 2.3rem;
  font-weight: 700;
  color: var(--ink);
  letter-spacing: -0.01em;
}
.welcome-sub {
  margin-top: 10px;
  font-family: "Courier New", monospace;
  font-size: 0.75rem;
  letter-spacing: 3px;
  text-transform: uppercase;
  color: var(--ink-soft);
}
.welcome-btn {
  margin-top: 28px;
  padding: 13px 26px;
  font: inherit;
  font-size: 1rem;
  color: var(--paper);
  background: var(--accent);
  border: 1.5px solid var(--ink);
  border-radius: 999px;
  cursor: pointer;
  box-shadow: 3px 4px 0 var(--key-shadow);
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}
@media (hover: hover) {
  .welcome-btn:hover {
    transform: translate(-1px, -1px);
    box-shadow: 4px 5px 0 var(--key-shadow);
  }
}
.welcome-btn:active {
  transform: translate(2px, 2px);
  box-shadow: 1px 1px 0 var(--key-shadow);
}
.welcome-btn:focus-visible {
  outline: 2px solid var(--teal);
  outline-offset: 3px;
}
[data-theme="dark"] .welcome-mark rect { fill: #222938; stroke: #8d96ab; }
[data-theme="dark"] .welcome-mark .welcome-ticks { stroke: #e9edf7; }
[data-theme="dark"] .welcome-mark .welcome-strike { stroke: #ff8a65; }

@media (prefers-reduced-motion: reduce) {
  .welcome, .welcome-btn { transition: none; }
  .welcome-card { animation: none; }
}

html {
  background-color: var(--paper-dark);
}

/* Page background: the paper SVG, fixed to the viewport so it doesn't scroll
   (fixed works on iOS, unlike background-attachment: fixed) */
body {
  min-height: 100vh;
  display: grid;
  place-items: center;
  position: relative;
  isolation: isolate;
  font-family: "Georgia", "Palatino Linotype", "Book Antiqua", serif;
  color: var(--ink);
  padding: 20px;
}
body::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: -2;
  background: var(--page-bg) center / cover no-repeat;
  pointer-events: none;
}
/* Dark mode: a dark wash over the same paper so the texture still shows */
[data-theme="dark"] body::after {
  content: "";
  position: fixed;
  inset: 0;
  z-index: -1;
  background: var(--paper-dark);
  opacity: 0.6;
  pointer-events: none;
}

.app {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 22px;
  width: 100%;
}

.logo-wrap {
  width: 100%;
  display: flex;
  justify-content: center;
  animation: rise 0.6s ease;
}

.logo {
  width: 220px;
  height: auto;
  filter: drop-shadow(2px 3px 0 rgba(92, 71, 45, 0.25));
}

/* Dark logo: cool slate cards, light ink, coral strike, muted tagline */
[data-theme="dark"] .logo rect[fill="#fbf7ec"] { fill: #222938; stroke: #8d96ab; }
[data-theme="dark"] .logo rect[fill="#ebe3cd"] { fill: #1b2130; stroke: #8d96ab; }
[data-theme="dark"] .logo g[stroke="#2b2118"] line { stroke: #e9edf7; }
[data-theme="dark"] .logo g line[stroke="#b5452b"] { stroke: #ff8a65; }
[data-theme="dark"] .logo text[fill="#2b2118"] { fill: #e9edf7; }
[data-theme="dark"] .logo text[fill="#6b5b4a"] { fill: #8d96ab; }
[data-theme="dark"] .logo path[stroke="#b5452b"] { stroke: #ff8a65; }
[data-theme="dark"] .logo {
  filter: none;
}

/* ---------- Layout: calculator + history tape side by side ---------- */
.workspace {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  width: 100%;
}

.calculator {
  position: relative;
  width: 100%;
  max-width: 380px;
  padding: 24px 22px 26px;
  background-color: var(--paper);
  background-image: var(--grain);
  border-radius: 6px 6px 10px 10px;
  box-shadow: var(--shadow);
  animation: rise 0.6s ease;
  transition: max-width 0.3s ease;
}
.calculator.wide { max-width: 520px; }

/* notebook margin + ruled lines */
.calculator::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  pointer-events: none;
  background:
    linear-gradient(90deg, transparent 52px, rgba(181, 69, 43, 0.22) 52px, rgba(181, 69, 43, 0.22) 53px, transparent 53px),
    repeating-linear-gradient(to bottom, transparent 0 27px, var(--line) 27px 28px);
  background-position: 0 0, 0 40px;
  opacity: 0.7;
  mix-blend-mode: multiply;
}
[data-theme="dark"] .calculator::before { mix-blend-mode: normal; opacity: 0.35; }
[data-theme="dark"] .calculator { border: 1px solid rgba(255,255,255,0.05); }
[data-theme="dark"] .key { box-shadow: 0 4px 10px rgba(0,0,0,0.35), inset 0 1px 0 rgba(255,255,255,0.05); border-color: rgba(233,237,247,0.22); }
[data-theme="dark"] .key.op { box-shadow: 2px 3px 0 rgba(0,0,0,0.45); color: #1a0f0b; }
[data-theme="dark"] .key.eq { box-shadow: 2px 3px 0 rgba(0,0,0,0.45); color: #0b1a19; }
[data-theme="dark"] .key.sci.active { box-shadow: 1px 2px 0 rgba(0,0,0,0.45); }
[data-theme="dark"] .display { border-bottom-color: rgba(233,237,247,0.25); }
[data-theme="dark"] .theme-toggle.on { color: #1b0f0a; }
[data-theme="dark"] .logo { filter: drop-shadow(1px 2px 0 rgba(0,0,0,0.45)); }

/* perforated top edge */
.calculator::after {
  content: "";
  position: absolute;
  left: 0; right: 0; top: -6px;
  height: 12px;
  background: radial-gradient(circle at 8px 6px, var(--paper-dark) 3px, transparent 3.5px) 0 0 / 22px 12px repeat-x;
  pointer-events: none;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(16px) rotate(-0.4deg); }
  to { opacity: 1; transform: none; }
}

.top-bar {
  position: relative;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
  padding-left: 30px;
}

.brand {
  font-family: "Courier New", Courier, monospace;
  font-weight: 700;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  font-size: 0.8rem;
  color: var(--ink-soft);
}
.brand::after {
  content: " ✎";
  color: var(--accent);
}

.theme-toggle {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 1.5px solid var(--ink-soft);
  background: var(--key);
  color: var(--ink);
  cursor: pointer;
  font-size: 0.85rem;
  font-family: "Courier New", monospace;
  box-shadow: 1px 2px 0 var(--key-shadow);
  transition: transform 0.2s ease, background 0.2s ease;
}
@media (hover: hover) {
  .theme-toggle:hover { transform: rotate(20deg); background: var(--key-hover); }
}
.theme-toggle.on { background: var(--accent); color: #fdf6ea; border-color: var(--accent); }

.top-actions { display: flex; gap: 6px; }

/* ---------- Display ---------- */
.display {
  position: relative;
  text-align: right;
  padding: 18px 10px 22px 40px;
  min-height: 130px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  overflow: hidden;
  border-bottom: 2px solid var(--ink);
  margin-bottom: 14px;
}

.indicators {
  position: absolute;
  top: 6px;
  left: 40px;
  display: flex;
  gap: 8px;
  font-family: "Courier New", monospace;
  font-size: 0.65rem;
  letter-spacing: 0.1em;
  color: var(--ink-soft);
}
.ind { opacity: 0.25; transition: opacity 0.2s ease; }
.ind.lit { opacity: 1; color: var(--accent); }

.expression {
  font-family: "Courier New", Courier, monospace;
  color: var(--ink-soft);
  font-size: 0.95rem;
  min-height: 1.4em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.result {
  font-family: "Georgia", serif;
  font-size: 3.2rem;
  font-weight: 400;
  line-height: 1.1;
  word-break: break-all;
  color: var(--ink);
  transition: font-size 0.15s ease;
}

/* ---------- Keys ---------- */
.keypad,
.sci-panel {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
}

.memory-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 12px;
}

.sci-panel {
  margin-bottom: 14px;
  padding-bottom: 14px;
  border-bottom: 1px dashed var(--line);
}

.key {
  aspect-ratio: 1;
  border: 1.5px solid var(--ink);
  border-radius: 50%;
  background: var(--key);
  color: var(--ink);
  font-family: "Georgia", serif;
  font-size: 1.35rem;
  cursor: pointer;
  box-shadow: 2px 3px 0 var(--key-shadow);
  transition: transform 0.1s ease, background 0.2s ease, box-shadow 0.1s ease;
  user-select: none;
}

@media (hover: hover) {
  .key:hover { background: var(--key-hover); }
}
.key:active { transform: translate(2px, 3px); box-shadow: 0 0 0 var(--key-shadow); }

.key.fn {
  background: var(--fn);
  font-family: "Courier New", monospace;
  font-weight: 700;
  font-size: 1.1rem;
}

.key.op {
  background: var(--accent);
  color: #fdf6ea;
  border-color: var(--accent);
  font-size: 1.5rem;
  box-shadow: 2px 3px 0 rgba(120, 40, 20, 0.4);
}
@media (hover: hover) {
  .key.op:hover { background: var(--accent-soft); }
}

.key.eq {
  background: var(--teal);
  color: #f7f3e6;
  border-color: var(--teal);
  box-shadow: 2px 3px 0 rgba(30, 70, 66, 0.4);
}
@media (hover: hover) {
  .key.eq:hover { filter: brightness(1.1); }
}

.key.mem,
.key.sci {
  aspect-ratio: auto;
  border-radius: 999px;
  height: 40px;
  font-family: "Courier New", monospace;
  font-size: 0.85rem;
  font-weight: 700;
  background: var(--fn);
  border-width: 1px;
  box-shadow: 1px 2px 0 var(--key-shadow);
}
.key.sci.active { background: var(--accent); color: #fdf6ea; border-color: var(--accent); }

.key.zero {
  grid-column: span 2;
  aspect-ratio: 2.1 / 1;
  border-radius: 999px;
  justify-self: stretch;
}

.key.active-op {
  background: #fdf6ea;
  color: var(--accent);
  border-color: var(--accent);
}

.key:focus-visible {
  outline: 2px dashed var(--accent);
  outline-offset: 3px;
}

/* ---------- History tape ---------- */
.history-panel {
  width: 100%;
  max-width: 380px;
  max-height: 170px;
  display: flex;
  flex-direction: column;
  padding: 14px 16px;
  background-color: var(--paper);
  background-image: var(--grain);
  border-radius: 4px;
  box-shadow: var(--shadow);
  animation: rise 0.4s ease;
}

.sheet-grab { display: none; }
.sheet-backdrop { display: none; }

.history-actions { display: flex; gap: 10px; }
.history-panel .mini-btn { display: none; }
.history-panel #clearHistory { display: inline; }

.history-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 8px;
  border-bottom: 2px solid var(--ink);
  font-family: "Courier New", monospace;
  font-weight: 700;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  font-size: 0.75rem;
  color: var(--ink-soft);
}

.mini-btn {
  background: none;
  border: none;
  font-family: "Courier New", monospace;
  font-size: 0.7rem;
  color: var(--accent);
  cursor: pointer;
  text-decoration: underline;
}

.history-list {
  list-style: none;
  overflow-y: auto;
  padding-top: 8px;
  flex: 1;
}

.history-list li {
  border-bottom: 1px dashed var(--line);
}
.history-item {
  display: block;
  width: 100%;
  padding: 8px 0;
  border: none;
  background: none;
  font-family: "Courier New", monospace;
  text-align: right;
  color: var(--ink);
  cursor: pointer;
  transition: background 0.15s ease, transform 0.15s ease;
}
.history-item:focus-visible { outline: 2px dashed var(--accent); outline-offset: -2px; }
@media (hover: hover) {
  .history-item:hover { background: var(--key-hover); }
}
.history-list .h-expr { font-size: 0.72rem; color: var(--ink-soft); word-break: break-all; }
.history-list .h-res { font-size: 1.05rem; color: var(--ink); font-family: "Georgia", serif; }

.history-empty {
  font-family: "Courier New", monospace;
  font-size: 0.8rem;
  color: var(--ink-soft);
  text-align: center;
  padding: 24px 0;
  font-style: italic;
}

/* ---------- UI touch-ups ---------- */
.calculator .display {
  transition: transform 0.2s ease;
}
.result.flip { animation: flip 0.25s ease; }
@keyframes flip {
  0% { transform: translateY(-4px); opacity: 0.4; }
  100% { transform: none; opacity: 1; }
}

.key {
  -webkit-tap-highlight-color: transparent;
  position: relative;
  overflow: hidden;
  touch-action: manipulation;
}
.key .ripple {
  position: absolute;
  border-radius: 50%;
  width: 20px;
  height: 20px;
  transform: translate(-50%, -50%) scale(0);
  background: rgba(43, 33, 24, 0.25);
  pointer-events: none;
  animation: ripple 0.5s ease-out forwards;
}
@keyframes ripple {
  to { transform: translate(-50%, -50%) scale(7); opacity: 0; }
}

.theme-toggle:active { transform: scale(0.92); }
.theme-toggle:focus-visible,
.mini-btn:focus-visible {
  outline: 2px dashed var(--accent);
  outline-offset: 3px;
}

.calculator::before { will-change: auto; }

.history-item:active { transform: scale(0.98); }

/* ---------- Footer ---------- */
.app-footer {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  margin-top: 4px;
  padding-bottom: 8px;
  font-family: "Courier New", Courier, monospace;
  color: #000;
  animation: rise 0.6s ease 0.15s both;
}
[data-theme="dark"] .app-footer { color: #fff; }

.made-in {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 1.08rem;
  font-weight: 700;
  letter-spacing: 0.06em;
}

.footer-logo-link {
  display: inline-flex;
  border-radius: 8px;
  transition: transform 0.2s ease;
}
@media (hover: hover) {
  .footer-logo-link:hover { transform: rotate(-4deg) scale(1.06); }
}
.footer-logo-link:focus-visible { outline: 2px dashed currentColor; outline-offset: 4px; }

.footer-logo {
  height: 77px;
  width: auto;
  max-width: 176px;
  display: block;
  object-fit: contain;
  overflow: visible;
}

.model-line {
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

/* ---------- Responsive ---------- */
@media (max-width: 760px) {
  body {
    place-items: start center;
    padding: 14px 12px 24px;
  }
  .app { gap: 14px; }
  .logo { width: 180px; }

  .workspace {
    flex-direction: column;
    align-items: center;
    gap: 14px;
  }

  /* on mobile the tape becomes a popup sheet, hidden until opened */
  .history-panel {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 20;
    max-width: none;
    max-height: 55vh;
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -10px 40px rgba(0, 0, 0, 0.3);
    transform: translateY(105%);
    opacity: 0;
    pointer-events: none;
    transition: transform 0.3s ease, opacity 0.3s ease;
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
    animation: none;
  }
  .history-panel.open {
    transform: translateY(0);
    opacity: 1;
    pointer-events: auto;
  }
  .sheet-grab {
    display: block;
    width: 40px;
    height: 4px;
    margin: 0 auto 10px;
    border-radius: 2px;
    background: var(--ink-soft);
    opacity: 0.5;
  }
  .history-panel .mini-btn { display: inline; }

  /* dimmed backdrop behind the mobile sheet; tapping it closes the sheet */
  .sheet-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 15;
    background: rgba(20, 14, 8, 0.35);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
  }
  .sheet-backdrop.open { opacity: 1; pointer-events: auto; }

  .calculator {
    max-width: 420px;
    padding: 20px 16px 22px;
  }
  .calculator.wide { max-width: 420px; }

  .calculator::before { background-position: 0 0, 0 40px; }

  .display {
    min-height: 110px;
    padding: 16px 8px 18px 36px;
  }
  .result { font-size: 2.8rem; }

  .keypad,
  .sci-panel {
    gap: 9px;
  }
  .memory-row { gap: 6px; margin-bottom: 10px; }
  .key.mem,
  .key.sci { height: 36px; font-size: 0.8rem; }
  .key { font-size: 1.25rem; }
  .key.op { font-size: 1.45rem; }
}

@media (max-width: 420px) {
  .calculator { padding: 18px 12px 20px; }
  .keypad,
  .sci-panel { gap: 7px; }
  .memory-row { gap: 5px; }
  .result { font-size: 2.4rem; }
  .key { font-size: 1.1rem; }
  .key.op { font-size: 1.3rem; }
  .key.mem, .key.sci { height: 34px; font-size: 0.75rem; }
  .brand { font-size: 0.72rem; }
  .theme-toggle { width: 30px; height: 30px; font-size: 0.78rem; }
  .top-bar { padding-left: 24px; }
}

/* short phones: keep the full keypad visible without scrolling */
@media (max-height: 700px) and (max-width: 760px) {
  .display { min-height: 92px; }
  .result { font-size: 2.2rem; }
  .key { font-size: 1.05rem; }
  .key.op { font-size: 1.25rem; }
}

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