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

:root {
  --bg: #0f0f0f;
  --surface: #1a1a1a;
  --border: rgba(255, 255, 255, 0.08);
  --text: #ffffff;
  --muted: rgba(255, 255, 255, 0.45);
  --accent: #ff6b35;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
  overflow: hidden;
}

.back-link {
  position: fixed;
  top: 1.2rem;
  left: 1.5rem;
  font-size: 0.65rem;
  font-weight: 600;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--muted);
  text-decoration: none;
  transition: color 0.2s ease;
  z-index: 10;
}

.back-link:hover {
  color: var(--text);
}

#install-btn {
  position: fixed;
  top: 1.2rem;
  right: 1.5rem;
  background: transparent;
  border: none;
  cursor: pointer;
  font-size: 0.65rem;
  font-weight: 600;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--muted);
  padding: 0;
  z-index: 10;
  transition: color 0.2s ease;
  -webkit-tap-highlight-color: transparent;
}

#install-btn:hover {
  color: var(--text);
}

/* ── Calculator layout ─────────────────────────── */

body {
  display: flex;
  flex-direction: column;
  padding-top: 3rem;
  padding-bottom: max(0.5rem, env(safe-area-inset-bottom));
}

#calc {
  flex: 1;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 500px;
  margin: 0 auto;
  overflow: hidden;
}

/* ── Display ───────────────────────────────────── */

#display {
  padding: 0.6rem 1.25rem 0.9rem;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: 0.2rem;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

#display-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  min-height: 1.5rem;
}

#expr-display {
  font-size: 0.8rem;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: right;
  flex: 1;
  margin-right: 0.75rem;
  font-variant-numeric: tabular-nums;
  direction: rtl;
  unicode-bidi: plaintext;
}

#deg-rad-btn {
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.15);
  color: var(--muted);
  font-size: 0.58rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  padding: 0.2rem 0.45rem;
  border-radius: 3px;
  cursor: pointer;
  font-family: inherit;
  transition: color 0.15s, border-color 0.15s;
  flex-shrink: 0;
}

#deg-rad-btn:hover,
#deg-rad-btn:active {
  color: var(--text);
  border-color: rgba(255, 255, 255, 0.3);
}

#result-display {
  font-size: clamp(2rem, 10vw, 3.5rem);
  font-weight: 300;
  letter-spacing: -0.03em;
  text-align: right;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
  transition: font-size 0.1s;
}

#result-display.small  { font-size: clamp(1.5rem, 6vw, 2.4rem); }
#result-display.xsmall { font-size: clamp(1.1rem, 4vw, 1.7rem); }

/* ── Button grid ───────────────────────────────── */

#buttons {
  flex: 1;
  max-height: 70vh;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(9, 1fr);
  gap: 1px;
  background: rgba(255, 255, 255, 0.05);
  overflow: hidden;
}

.btn {
  background: #181818;
  border: none;
  color: var(--text);
  font-family: inherit;
  font-size: clamp(0.9rem, 3.2vw, 1.1rem);
  cursor: pointer;
  transition: background 0.07s;
  touch-action: manipulation;
  user-select: none;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.btn:active { background: rgba(255, 255, 255, 0.14); }

.btn.sci {
  background: #111;
  color: rgba(255, 255, 255, 0.55);
  font-size: clamp(0.7rem, 2.4vw, 0.88rem);
}
.btn.sci:active { background: rgba(255, 255, 255, 0.09); }

.btn.const {
  background: #111;
  color: var(--accent);
  font-size: clamp(0.85rem, 2.8vw, 1rem);
  opacity: 0.9;
}
.btn.const:active { background: rgba(255, 255, 255, 0.09); }

.btn.op {
  background: #1c1c1c;
  color: var(--accent);
  font-size: clamp(1rem, 3.6vw, 1.25rem);
}
.btn.op:active { background: rgba(74, 222, 128, 0.12); }

.btn.del {
  background: #1c1c1c;
  color: rgba(255, 255, 255, 0.6);
}

.btn.ac {
  background: #1c1c1c;
  color: #f87171;
}
.btn.ac:active { background: rgba(248, 113, 113, 0.18); }

.btn.eq {
  background: var(--accent);
  color: #0a0a0a;
  font-size: clamp(1.1rem, 3.8vw, 1.35rem);
  font-weight: 700;
}
.btn.eq:active { background: #22c55e; }

.btn.col2 { grid-column: span 2; }
