:root{
  --paper:#F3EFE8;
  --paper-2:#FBF8F2;
  --ink:#111112;
  --ivory:#F7F4EC;
  --champagne:#C8A96A;
  --champ-light:#E4CF9B;
  --champ-dark:#B8934F;
  --muted:#8C8578;
  --hairline:rgba(17,17,18,0.085);
}

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

html{ -webkit-text-size-adjust:100%; }

body{
  margin:0;
  min-height:100vh;
  min-height:100dvh;
  background-color:var(--paper);
  color:var(--ink);
  font-family:'Tajawal', system-ui, -apple-system, 'Segoe UI', sans-serif;
  font-size:16px;
  line-height:1.5;
  -webkit-font-smoothing:antialiased;
  -webkit-tap-highlight-color:transparent;
  user-select:none;
  -webkit-user-select:none;
  overflow-x:hidden;
  padding: clamp(18px, 5vh, 56px) 16px 48px;
  display:flex;
  flex-direction:column;
  align-items:center;
}

/* ---------- paper texture ---------- */
.paper-noise{
  position:fixed;
  inset:0;
  z-index:0;
  pointer-events:none;
  opacity:0.045;
  background-image:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='180' height='180' filter='url(%23n)'/%3E%3C/svg%3E");
  background-size:180px 180px;
  mix-blend-mode:multiply;
}
.paper-glow{
  position:fixed;
  inset:0;
  z-index:0;
  pointer-events:none;
  background:radial-gradient(120% 68% at 50% -12%, #FDFBF6 0%, rgba(243,239,232,0) 62%);
}

/* ---------- layout ---------- */
.page{
  position:relative;
  z-index:1;
  width:100%;
  max-width:400px;
  margin:0 auto;
  display:flex;
  flex-direction:column;
  gap:16px;
}

.layout{
  display:flex;
  flex-direction:column;
  gap:20px;
  min-width:0;
}

/* ---------- top bar ---------- */
.topbar{
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap:14px;
  padding-bottom:13px;
  border-bottom:1px solid var(--hairline);
}
.brand{ min-width:0; }
.brand-name{
  font-family:'Amiri', 'Tajawal', serif;
  font-weight:700;
  font-size:31px;
  line-height:1.05;
  margin:0;
  color:var(--ink);
}
.brand-sub{
  margin:2px 0 0;
  font-size:11.5px;
  font-weight:400;
  color:#9A9286;
}
.num-toggle{
  display:inline-flex;
  align-items:center;
  gap:2px;
  padding:3px;
  height:44px;
  background:#E9E3D7;
  border:1px solid rgba(17,17,18,0.06);
  border-radius:999px;
  flex:none;
}
.num-opt{
  appearance:none;
  border:0;
  background:transparent;
  height:36px;
  min-width:48px;
  padding:0 10px;
  border-radius:999px;
  font-family:'IBM Plex Mono', ui-monospace, monospace;
  font-size:12.5px;
  font-weight:500;
  color:#7C7468;
  cursor:pointer;
  transition:background 170ms ease, color 170ms ease, box-shadow 170ms ease;
}
.num-opt[data-num="arabic"]{ font-family:'Tajawal', sans-serif; font-size:15px; }
.num-opt.is-active{
  background:var(--ink);
  color:var(--paper);
  box-shadow:0 2px 7px rgba(17,17,18,0.28);
}
.num-opt:focus-visible{ outline:2px solid var(--champagne); outline-offset:2px; }

/* ---------- calculator body ---------- */
.calc{
  position:relative;
  width:100%;
  padding:16px 16px 20px;
  border-radius:28px;
  background:linear-gradient(155deg,#1C1C1E 0%, #141416 44%, #0B0B0C 100%);
  border:1px solid rgba(230,226,218,0.20);
  box-shadow:
    0 1px 0 rgba(255,255,255,0.075) inset,
    0 -1px 0 rgba(0,0,0,0.7) inset,
    0 26px 50px -22px rgba(17,17,18,0.55),
    0 60px 110px -46px rgba(17,17,18,0.45);
  isolation:isolate;
  overflow:hidden;
}
.calc::before{
  content:'';
  position:absolute;
  inset:0;
  border-radius:inherit;
  pointer-events:none;
  background:linear-gradient(135deg, rgba(255,255,255,0.10) 0%, rgba(255,255,255,0) 34%, rgba(255,255,255,0) 68%, rgba(255,255,255,0.04) 100%);
}

/* ---------- screen ---------- */
.screen{
  position:relative;
  background:#08080A;
  border:1px solid rgba(255,255,255,0.055);
  border-radius:18px;
  padding:10px 15px 14px;
  margin-bottom:14px;
  box-shadow:
    0 2px 12px rgba(0,0,0,0.7) inset,
    0 1px 0 rgba(255,255,255,0.05);
  transition:box-shadow 240ms ease;
}
.screen.is-echo{
  box-shadow:
    0 2px 12px rgba(0,0,0,0.7) inset,
    0 0 0 1px rgba(200,169,106,0.55),
    0 0 22px -6px rgba(200,169,106,0.5);
}
.screen.shake{ animation:screenShake 420ms cubic-bezier(.36,.07,.19,.97) both; }
@keyframes screenShake{
  0%,100%{ transform:translate3d(0,0,0); }
  15%{ transform:translate3d(-7px,0,0); }
  32%{ transform:translate3d(6px,0,0); }
  48%{ transform:translate3d(-4px,0,0); }
  64%{ transform:translate3d(3px,0,0); }
  82%{ transform:translate3d(-1px,0,0); }
}

.screen-top{
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap:10px;
  min-height:36px;
}
.led{
  width:6px;
  height:6px;
  border-radius:50%;
  background:var(--champagne);
  box-shadow:0 0 9px rgba(200,169,106,0.85);
  margin-inline-start:2px;
}
.copy-btn{
  display:inline-flex;
  align-items:center;
  justify-content:center;
  gap:6px;
  height:36px;
  min-width:78px;
  padding:0 12px;
  border-radius:999px;
  background:rgba(247,244,236,0.06);
  border:1px solid rgba(247,244,236,0.10);
  color:rgba(247,244,236,0.62);
  font-family:'Tajawal', sans-serif;
  font-size:12.5px;
  cursor:pointer;
  transition:background 160ms ease, color 160ms ease, border-color 160ms ease, transform 90ms ease;
}
.copy-btn:hover{
  background:rgba(200,169,106,0.16);
  border-color:rgba(200,169,106,0.42);
  color:#F0E3C2;
}
.copy-btn:active{ transform:scale(0.96); }
.copy-btn:focus-visible{ outline:2px solid var(--champagne); outline-offset:2px; }

.expr{
  font-family:'IBM Plex Mono', ui-monospace, monospace;
  font-size:14.5px;
  line-height:22px;
  min-height:22px;
  letter-spacing:0;
  color:rgba(247,244,236,0.42);
  text-align:right;
  direction:ltr;
  white-space:nowrap;
  overflow:hidden;
  text-overflow:ellipsis;
  padding-top:2px;
}

.result{
  font-family:'IBM Plex Mono', ui-monospace, monospace;
  font-variant-numeric:tabular-nums;
  font-weight:500;
  font-size:64px;
  line-height:1.12;
  letter-spacing:-0.025em;
  color:var(--ivory);
  text-align:right;
  direction:ltr;
  white-space:nowrap;
  overflow:hidden;
  min-height:72px;
  padding-top:2px;
  transition:color 200ms ease;
}
.result.is-error{
  font-family:'Tajawal', sans-serif;
  font-size:26px;
  font-weight:500;
  letter-spacing:0;
  line-height:1.35;
  color:var(--champagne);
  min-height:72px;
  display:flex;
  align-items:center;
  justify-content:flex-end;
  text-align:right;
  direction:rtl;
}

/* arabic-indic numerals fall back to a font that actually has them */
body.nums-arabic .result,
body.nums-arabic .expr,
body.nums-arabic .key--num,
body.nums-arabic .h-expr,
body.nums-arabic .h-res{
  font-family:'Tajawal', system-ui, sans-serif;
}
body.nums-arabic .result{ letter-spacing:0; font-weight:500; }
body.nums-arabic .key--num{ font-size:21px; }

/* ---------- keys ---------- */
.keys{
  display:grid;
  grid-template-columns:repeat(4, minmax(0,1fr));
  gap:10px;
}
.key{
  appearance:none;
  -webkit-appearance:none;
  border:0;
  margin:0;
  padding:0;
  aspect-ratio:1 / 1;
  width:100%;
  border-radius:50%;
  display:flex;
  align-items:center;
  justify-content:center;
  cursor:pointer;
  font-family:'Tajawal', sans-serif;
  font-size:22px;
  font-weight:500;
  line-height:1;
  user-select:none;
  -webkit-user-select:none;
  touch-action:manipulation;
  transition:transform 90ms ease, filter 130ms ease, box-shadow 130ms ease;
}
.key:focus-visible{ outline:2px solid var(--champagne); outline-offset:3px; }

.key--num{
  font-family:'IBM Plex Mono', ui-monospace, monospace;
  font-size:22px;
  color:var(--ivory);
  background:linear-gradient(160deg, #232327 0%, #171719 100%);
  border:1px solid rgba(230,226,218,0.09);
  box-shadow:
    0 1px 0 rgba(255,255,255,0.065) inset,
    0 7px 14px -9px rgba(0,0,0,0.9);
}
.key--fn{
  font-size:16px;
  font-weight:500;
  color:#CBB98F;
  background:linear-gradient(160deg, #212125 0%, #151517 100%);
  border:1px solid rgba(230,226,218,0.075);
  box-shadow:
    0 1px 0 rgba(255,255,255,0.05) inset,
    0 7px 14px -9px rgba(0,0,0,0.9);
}
.key--fn svg{ opacity:0.9; }
.key--op{
  font-family:'IBM Plex Mono', 'Tajawal', monospace;
  font-size:25px;
  font-weight:600;
  color:#17150F;
  background:linear-gradient(135deg, #E4CF9B 0%, #C8A96A 48%, #B8934F 100%);
  border:1px solid rgba(255,255,255,0.30);
  box-shadow:
    0 1px 0 rgba(255,255,255,0.55) inset,
    0 9px 20px -11px rgba(200,169,106,0.95);
}
.key--eq{
  box-shadow:
    0 1px 0 rgba(255,255,255,0.6) inset,
    0 0 0 1px rgba(228,207,155,0.45),
    0 10px 24px -10px rgba(200,169,106,1);
}
.key:active,
.key.is-pressed{ transform:scale(0.935); filter:brightness(1.22); }
.key--op:active,
.key--op.is-pressed{ filter:brightness(1.10) saturate(1.06); }

@media (hover:hover){
  .key--num:hover{ filter:brightness(1.22); }
  .key--fn:hover{ filter:brightness(1.2); }
  .key--op:hover{ filter:brightness(1.06) saturate(1.05); }
}

/* ---------- history ---------- */
.history-card{
  background:var(--paper-2);
  border:1px solid var(--hairline);
  border-radius:22px;
  padding:16px 16px 8px;
  box-shadow:0 20px 44px -34px rgba(17,17,18,0.5);
  display:flex;
  flex-direction:column;
  min-width:0;
}
.history-head{
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap:12px;
  margin-bottom:8px;
}
.history-head h2{
  margin:0;
  font-size:14.5px;
  font-weight:700;
  color:#2A2620;
  letter-spacing:0;
}
.link-btn{
  appearance:none;
  background:transparent;
  border:0;
  border-radius:9px;
  padding:9px 6px;
  margin-inline-end:-6px;
  font-family:'Tajawal', sans-serif;
  font-size:12.5px;
  font-weight:500;
  color:#9A8A66;
  cursor:pointer;
  transition:color 150ms ease, background 150ms ease;
}
.link-btn:hover{ color:#7A6A46; background:rgba(200,169,106,0.12); }
.link-btn:focus-visible{ outline:2px solid var(--champagne); outline-offset:1px; }
.link-btn.is-danger{ color:#A6423C; font-weight:700; }
.link-btn:disabled{ opacity:0.35; cursor:default; background:transparent; }

.history-list{
  list-style:none;
  margin:0;
  padding:0;
  overflow-y:auto;
  -webkit-overflow-scrolling:touch;
  max-height:44vh;
  min-height:132px;
  overscroll-behavior:contain;
}
.history-list li:last-child .history-row{ border-bottom:0; }

.history-row{
  width:100%;
  display:flex;
  align-items:center;
  gap:9px;
  min-height:48px;
  padding:11px 8px;
  background:transparent;
  border:0;
  border-bottom:1px solid rgba(17,17,18,0.06);
  border-radius:11px;
  cursor:pointer;
  font-family:'Tajawal', sans-serif;
  text-align:right;
  transition:background 150ms ease;
}
.history-row:hover{ background:rgba(200,169,106,0.10); }
.history-row:active{ background:rgba(200,169,106,0.19); }
.history-row:focus-visible{ outline:2px solid var(--champagne); outline-offset:-2px; }
.h-expr{
  flex:1 1 auto;
  min-width:0;
  font-family:'IBM Plex Mono', ui-monospace, monospace;
  font-size:12.5px;
  color:#8C8578;
  direction:ltr;
  text-align:right;
  white-space:nowrap;
  overflow:hidden;
  text-overflow:ellipsis;
}
.h-eq{
  flex:none;
  color:var(--champagne);
  font-size:12px;
  font-weight:700;
}
.h-res{
  flex:none;
  max-width:52%;
  overflow:hidden;
  text-overflow:ellipsis;
  white-space:nowrap;
  font-family:'IBM Plex Mono', ui-monospace, monospace;
  font-size:14.5px;
  font-weight:600;
  color:#1A1712;
  direction:ltr;
  text-align:left;
}
.history-empty{
  padding:26px 10px;
  text-align:center;
  color:#9C9488;
  font-size:13px;
  line-height:1.75;
}

/* ---------- toast ---------- */
.toast{
  position:fixed;
  left:50%;
  bottom:28px;
  transform:translate(-50%, 14px);
  z-index:50;
  display:flex;
  align-items:center;
  gap:8px;
  padding:11px 20px;
  border-radius:999px;
  background:#17171A;
  color:var(--ivory);
  font-size:13px;
  font-weight:500;
  border:1px solid rgba(200,169,106,0.42);
  box-shadow:0 16px 36px -16px rgba(0,0,0,0.7);
  opacity:0;
  visibility:hidden;
  pointer-events:none;
  transition:opacity 200ms ease, transform 200ms ease, visibility 200ms;
}
.toast svg{ color:var(--champagne); flex:none; }
.toast.is-visible{
  opacity:1;
  visibility:visible;
  transform:translate(-50%, 0);
}

/* ---------- misc ---------- */
.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;
}

/* ---------- compact heights ---------- */
@media (max-height:770px){
  body{ padding-top:14px; }
  .calc{ padding:13px 13px 16px; border-radius:26px; }
  .screen{ padding:8px 13px 11px; margin-bottom:11px; }
  .result{ min-height:62px; }
  .keys{ gap:8px; }
  .key--num{ font-size:20px; }
  .key--op{ font-size:23px; }
  .brand-name{ font-size:27px; }
}

/* ---------- desktop ---------- */
@media (min-width:940px){
  .page{ max-width:812px; gap:22px; }
  .layout{
    display:grid;
    grid-template-columns:400px minmax(0,1fr);
    gap:30px;
    align-items:start;
  }
  .history-card{ min-height:520px; }
  .history-list{ max-height:430px; min-height:220px; }
  .brand-name{ font-size:34px; }
}

@media (prefers-reduced-motion:reduce){
  *{ animation-duration:0.001ms !important; animation-iteration-count:1 !important; }
  .key{ transition:filter 100ms ease; }
  .toast{ transition:opacity 120ms ease; }
}