/* ── Design tokens ── */
:root {
  --bg: #1C1917;
  --bg-2: #241f1c;
  --card: rgba(255,255,255,.045);
  --card-solid: #221d1a;
  --key: rgba(255,255,255,.06);
  --bg-hover: rgba(255,255,255,.10);
  --border: rgba(255,255,255,.08);
  --border-strong: rgba(255,255,255,.18);
  --text: #FFFFFF;
  --text-muted: #A79C93;
  --primary: #EA580C;
  --secondary: #F97316;
  --accent: #2563EB;
  --success: #4ADE80;
  --danger: #DC2626;
  --radius-sm: 10px;
  --radius: 16px;
  --radius-lg: 24px;
  --shadow: 0 4px 24px rgba(0,0,0,.45);
  --shadow-lg: 0 18px 60px rgba(0,0,0,.55);
  --ease: cubic-bezier(.22,.61,.36,1);
  --font: 'Inter', 'IBM Plex Sans Arabic', ui-sans-serif, system-ui, sans-serif;
}
html[data-theme="light"] {
  --bg: #FAF7F4;
  --bg-2: #F3ECE6;
  --card: rgba(255,255,255,.72);
  --card-solid: #FFFFFF;
  --key: rgba(255,255,255,.9);
  --bg-hover: rgba(28,25,23,.06);
  --border: rgba(28,25,23,.09);
  --border-strong: rgba(28,25,23,.18);
  --text: #1C1917;
  --text-muted: #7A6E66;
  --shadow: 0 4px 20px rgba(28,25,23,.08);
  --shadow-lg: 0 18px 50px rgba(28,25,23,.14);
}
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  min-height: 100dvh;
  font-family: var(--font);
  color: var(--text);
  background: var(--bg);
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  transition: background .4s var(--ease), color .4s var(--ease);
}
button { font: inherit; color: inherit; background: none; border: none; cursor: pointer; }
.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}
.glow {
  position: fixed; inset: -30% -20% auto -20%; height: 65vh; pointer-events: none; z-index: 0;
  background:
    radial-gradient(ellipse at 25% 0%, rgba(234,88,12,.30), transparent 60%),
    radial-gradient(ellipse at 80% 10%, rgba(37,99,235,.18), transparent 60%);
  filter: blur(10px);
}

/* ── Top bar ── */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: .7rem clamp(.9rem, 3vw, 2rem);
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: .7rem; min-width: 0; }
.brand-icon {
  display: grid; place-items: center; width: 38px; height: 38px; flex: none;
  border-radius: 12px; color: #fff;
  background: linear-gradient(135deg, var(--primary), var(--secondary));
  box-shadow: 0 6px 22px rgba(234,88,12,.35);
}
.brand-icon svg { width: 20px; height: 20px; }
.brand-text { display: flex; flex-direction: column; line-height: 1.2; min-width: 0; }
.brand-text strong { font-size: 1rem; font-weight: 700; letter-spacing: -.02em; }
.brand-sub {
  font-size: .72rem; color: var(--text-muted); white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis;
}
.actions { display: flex; gap: .4rem; flex: none; }
.icon-btn {
  display: grid; place-items: center; width: 40px; height: 40px;
  border-radius: 12px; border: 1px solid var(--border); color: var(--text-muted);
  background: var(--card); backdrop-filter: blur(10px);
  transition: transform .18s var(--ease), color .18s, background .18s, box-shadow .18s;
}
.icon-btn:hover { color: var(--text); background: var(--bg-hover); transform: translateY(-2px); }
.icon-btn:active { transform: translateY(0) scale(.96); }
.icon-btn svg { width: 19px; height: 19px; }
.icon-btn.on { color: var(--primary); border-color: color-mix(in srgb, var(--primary) 45%, transparent); }

/* ── Layout ── */
.wrap {
  position: relative; z-index: 1;
  display: grid; gap: 1.25rem;
  grid-template-columns: minmax(0, 460px) minmax(0, 1fr);
  align-items: start;
  max-width: 1040px; margin: 0 auto; padding: clamp(1rem, 3vw, 2.25rem) clamp(.9rem, 3vw, 2rem) 3rem;
}
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  backdrop-filter: blur(14px);
  box-shadow: var(--shadow);
}

/* ── Calculator shell ── */
.calc { padding: clamp(.9rem, 2.5vw, 1.25rem); display: flex; flex-direction: column; gap: .75rem; }

/* ── Display ── */
.display {
  position: relative; overflow: hidden;
  padding: 1rem 1.1rem 1.15rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: linear-gradient(160deg, color-mix(in srgb, var(--primary) 14%, transparent), transparent 55%), var(--key);
}
.display-top { display: flex; align-items: center; gap: .4rem; min-height: 20px; }
.flag {
  font-size: .64rem; font-weight: 700; letter-spacing: .08em;
  padding: .12rem .42rem; border-radius: 6px;
  border: 1px solid var(--border-strong); color: var(--text-muted);
}
.mem-flag { color: var(--primary); border-color: color-mix(in srgb, var(--primary) 50%, transparent); }
.angle-flag { margin-inline-start: auto; }
.expr {
  min-height: 22px; font-size: .86rem; color: var(--text-muted);
  font-variant-numeric: tabular-nums; direction: ltr; text-align: end;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.result-row { display: flex; align-items: flex-end; gap: .6rem; }
.result {
  flex: 1; min-width: 0; direction: ltr; text-align: end;
  font-size: clamp(1.9rem, 8vw, 2.9rem); font-weight: 700; letter-spacing: -.03em;
  font-variant-numeric: tabular-nums; line-height: 1.15;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  transition: color .2s;
}
.result.error { color: var(--danger); font-size: clamp(1.2rem, 4.6vw, 1.6rem); }
.copy-btn {
  flex: none; display: grid; place-items: center; width: 34px; height: 34px;
  border-radius: 10px; color: var(--text-muted); border: 1px solid var(--border);
  transition: transform .18s var(--ease), color .18s, background .18s;
}
.copy-btn:hover { color: var(--primary); background: var(--bg-hover); transform: translateY(-2px); }
.copy-btn svg { width: 16px; height: 16px; }
.hint { margin-top: .5rem; font-size: .68rem; color: var(--text-muted); opacity: .75; }

/* ── Pad groups (memory / scientific) ── */
.pad-group { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: .45rem; }
.pad-group.sci { grid-template-columns: repeat(5, minmax(0, 1fr)); }
.pad-group.sci.collapsed { display: none; }

/* ── Keypad ── */
.keypad { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: .5rem; }
.key {
  position: relative; display: grid; place-items: center;
  min-height: clamp(48px, 12vw, 62px);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--key);
  color: var(--text);
  font-size: clamp(1rem, 3.6vw, 1.3rem); font-weight: 600;
  font-variant-numeric: tabular-nums;
  user-select: none;
  transition: transform .14s var(--ease), box-shadow .2s, background .2s, color .2s, border-color .2s;
}
.key:hover { background: var(--bg-hover); transform: translateY(-2px); box-shadow: 0 10px 26px rgba(0,0,0,.28); }
.key:active, .key.flash { transform: translateY(0) scale(.95); }
.key svg { width: 20px; height: 20px; }
.key-mini {
  min-height: 38px; border-radius: 12px;
  font-size: .8rem; font-weight: 600; color: var(--text-muted);
}
.key-mini:hover { color: var(--text); }
.key-mini.active { color: var(--primary); border-color: color-mix(in srgb, var(--primary) 45%, transparent); }
.key.op { color: var(--primary); font-size: clamp(1.15rem, 4vw, 1.5rem); }
.key.fn { color: var(--text-muted); font-size: clamp(.9rem, 3.2vw, 1.05rem); }
.key.fn.warn { color: var(--danger); }
.key.eq {
  color: #fff; border-color: transparent;
  background: linear-gradient(135deg, var(--primary), var(--secondary));
  box-shadow: 0 8px 26px rgba(234,88,12,.35);
}
.key.eq:hover { box-shadow: 0 14px 36px rgba(234,88,12,.5); }
.key:focus-visible, .icon-btn:focus-visible, .ghost-btn:focus-visible, .copy-btn:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}

/* ── History ── */
.history { padding: 1.1rem 1.15rem; display: flex; flex-direction: column; gap: .8rem; min-height: 240px; }
.hist-head { display: flex; align-items: center; justify-content: space-between; gap: .6rem; }
.hist-head h2 { display: flex; align-items: center; gap: .5rem; font-size: .95rem; font-weight: 700; letter-spacing: -.01em; }
.hist-head h2 svg { width: 18px; height: 18px; color: var(--primary); }
.ghost-btn {
  display: inline-flex; align-items: center; gap: .35rem;
  padding: .35rem .7rem; border-radius: 999px; font-size: .74rem; font-weight: 600;
  color: var(--text-muted); border: 1px solid var(--border);
  transition: color .2s, background .2s, transform .18s var(--ease);
}
.ghost-btn:hover { color: var(--danger); background: var(--bg-hover); transform: translateY(-1px); }
.ghost-btn svg { width: 14px; height: 14px; }
.hist-list { list-style: none; display: flex; flex-direction: column; gap: .45rem; max-height: 420px; overflow-y: auto; }
.hist-item {
  display: flex; flex-direction: column; gap: .15rem; width: 100%; text-align: start;
  padding: .6rem .75rem; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--key);
  transition: transform .16s var(--ease), border-color .2s, background .2s;
  animation: fadeUp .35s var(--ease) both;
}
.hist-item:hover { transform: translateX(-3px); border-color: var(--border-strong); }
.hist-expr { font-size: .74rem; color: var(--text-muted); direction: ltr; text-align: end; font-variant-numeric: tabular-nums; }
.hist-res { font-size: 1.05rem; font-weight: 700; direction: ltr; text-align: end; font-variant-numeric: tabular-nums; }
.hist-empty { font-size: .8rem; color: var(--text-muted); text-align: center; padding: 2rem .5rem; }
[hidden] { display: none !important; }

/* ── Toasts ── */
.toasts { position: fixed; inset-inline-end: 1rem; bottom: 1rem; z-index: 60; display: flex; flex-direction: column; gap: .5rem; }
.toast {
  display: flex; align-items: center; gap: .5rem;
  padding: .65rem 1rem; border-radius: 999px; font-size: .82rem; font-weight: 600;
  background: var(--card-solid); border: 1px solid var(--border-strong);
  box-shadow: var(--shadow-lg); animation: fadeUp .3s var(--ease) both;
}
.toast svg { width: 15px; height: 15px; }
.toast.ok { color: var(--success); }
.toast.err { color: var(--danger); }

@keyframes fadeUp { from { opacity: 0; transform: translateY(16px); } to { opacity: 1; transform: none; } }
.fade-up { animation: fadeUp .5s var(--ease) both; }
.delay-1 { animation-delay: .08s; }
@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

/* ── Responsive ── */
@media (max-width: 880px) {
  .wrap { grid-template-columns: minmax(0, 1fr); max-width: 560px; }
  .history { min-height: 0; }
  .history.collapsed { display: none; }
  .hist-list { max-height: 260px; }
}
@media (max-width: 420px) {
  .brand-sub { display: none; }
  .pad-group.sci { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .wrap { padding-inline: .75rem; }
  .calc { padding: .8rem; border-radius: 20px; }
  .toasts { inset-inline: .75rem; align-items: stretch; }
}
@media (hover: none) {
  .key:hover, .icon-btn:hover, .hist-item:hover { transform: none; }
}
@media print {
  .topbar, .glow, .toasts { display: none; }
  .card { border: 1px solid #ddd; box-shadow: none; }
}

/* ── Panel visibility + safe fallbacks ── */
.history.collapsed { display: none; }
.wrap.no-side { grid-template-columns: minmax(0, 460px); justify-content: center; }
html { --topbar: rgba(28, 25, 23, .8); }
html[data-theme="light"] { --topbar: rgba(250, 247, 244, .88); }
.topbar { background: var(--topbar); background: color-mix(in srgb, var(--bg) 82%, transparent); }
.display {
  background: var(--key);
  background: linear-gradient(160deg, color-mix(in srgb, var(--primary) 14%, transparent), transparent 55%), var(--key);
}
.hist-list::-webkit-scrollbar { width: 8px; }
.hist-list::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 999px; }
.hist-list::-webkit-scrollbar-track { background: transparent; }
