/* ── Design tokens ── */
:root {
  --bg: #12100f;
  --bg-elev: #1c1917;
  --card: rgba(255, 255, 255, .045);
  --card-hover: rgba(255, 255, 255, .09);
  --border: rgba(255, 255, 255, .09);
  --border-strong: rgba(255, 255, 255, .18);
  --text: #faf6f2;
  --muted: #a49b95;
  --primary: #ea580c;
  --primary-2: #f97316;
  --primary-soft: rgba(234, 88, 12, .14);
  --accent: #3b82f6;
  --success: #4ade80;
  --danger: #f87171;
  --key-bg: rgba(255, 255, 255, .06);
  --radius-sm: 10px;
  --radius: 16px;
  --radius-lg: 24px;
  --shadow-lg: 0 24px 70px rgba(0, 0, 0, .55);
  --font: 'IBM Plex Sans Arabic', 'Inter', ui-sans-serif, system-ui, sans-serif;
}

body.light {
  --bg: #f7f4f2;
  --bg-elev: #ffffff;
  --card: rgba(255, 255, 255, .82);
  --card-hover: rgba(28, 25, 23, .05);
  --border: rgba(28, 25, 23, .1);
  --border-strong: rgba(28, 25, 23, .2);
  --text: #1b1815;
  --muted: #6d635d;
  --key-bg: rgba(255, 255, 255, .95);
  --shadow-lg: 0 24px 60px 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.6;
  -webkit-font-smoothing: antialiased;
  transition: background .35s ease, color .35s ease;
}

.glow {
  position: fixed;
  inset: -30% -20% auto -20%;
  height: 65vh;
  pointer-events: none;
  background: radial-gradient(ellipse at 50% 0%, rgba(234, 88, 12, .3), transparent 62%);
}

button { font-family: inherit; cursor: pointer; color: inherit; background: none; border: none; }

/* ── Shell ── */
.app { position: relative; z-index: 1; max-width: 1120px; margin: 0 auto; padding: 0 1.25rem 3rem; }

.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: .85rem 0;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
  margin-bottom: 2rem;
}

.brand { display: flex; align-items: center; gap: .7rem; }
.brand-mark {
  display: grid; place-items: center;
  width: 42px; height: 42px; border-radius: 13px;
  background: linear-gradient(140deg, var(--primary), var(--primary-2));
  color: #fff; box-shadow: 0 10px 26px rgba(234, 88, 12, .35);
}
.brand-mark svg { width: 22px; height: 22px; }
.brand-text { display: flex; flex-direction: column; line-height: 1.15; }
.brand-text strong { font-size: 1.05rem; font-weight: 700; letter-spacing: -.01em; }
.brand-text small { color: var(--muted); font-size: .72rem; letter-spacing: .06em; }

.topbar-actions { display: flex; align-items: center; gap: .5rem; }

.segmented {
  display: flex; gap: .2rem; padding: .22rem;
  background: var(--card); border: 1px solid var(--border); border-radius: 999px;
  backdrop-filter: blur(12px);
}
.seg-btn {
  padding: .4rem 1rem; border-radius: 999px; font-size: .82rem; font-weight: 600;
  color: var(--muted); transition: all .2s ease;
}
.seg-btn:hover { color: var(--text); }
.seg-btn.active { background: linear-gradient(135deg, var(--primary), var(--primary-2)); color: #fff; }

.icon-btn {
  display: grid; place-items: center; width: 40px; height: 40px;
  border: 1px solid var(--border); border-radius: 12px;
  background: var(--card); backdrop-filter: blur(12px); color: var(--muted);
  transition: transform .15s ease, color .2s ease, background .2s ease;
}
.icon-btn:hover { color: var(--text); background: var(--card-hover); transform: translateY(-2px); }
.icon-btn.sm { width: 32px; height: 32px; border-radius: 9px; }
.icon-btn svg { width: 19px; height: 19px; }
.icon-btn.sm svg { width: 16px; height: 16px; }

/* ── Layout ── */
.workspace { display: grid; grid-template-columns: minmax(0, 1.35fr) minmax(0, .85fr); gap: 1.5rem; align-items: start; }

.calc-card, .history-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  backdrop-filter: blur(16px);
  box-shadow: var(--shadow-lg);
  padding: 1.35rem;
  animation: fadeUp .5s ease both;
}
.history-card { padding: 1.1rem; position: sticky; top: 92px; }
.history-card { animation-delay: .1s; }

/* ── Display ── */
.display {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: linear-gradient(180deg, rgba(234, 88, 12, .07), transparent 70%), var(--key-bg);
  padding: .9rem 1.1rem 1.1rem;
  margin-bottom: 1rem;
  overflow: hidden;
}
.display-top { display: flex; align-items: center; justify-content: space-between; gap: .5rem; min-height: 30px; }

.angle-toggle { display: flex; gap: .2rem; padding: .15rem; border: 1px solid var(--border); border-radius: 999px; }
.angle-btn { padding: .18rem .6rem; font-size: .7rem; font-weight: 700; letter-spacing: .05em; color: var(--muted); border-radius: 999px; transition: all .2s ease; }
.angle-btn.active { background: var(--primary-soft); color: var(--primary-2); }

.copy-btn { display: grid; place-items: center; width: 30px; height: 30px; border-radius: 9px; color: var(--muted); transition: all .2s ease; }
.copy-btn:hover { color: var(--text); background: var(--card-hover); }
.copy-btn svg { width: 15px; height: 15px; }

.expr {
  font-family: 'Inter', monospace; font-size: clamp(1rem, 3.2vw, 1.35rem);
  color: var(--muted); text-align: left; word-break: break-all;
  min-height: 2.1rem; margin-top: .35rem;
}
.result-row { display: flex; align-items: baseline; gap: .5rem; margin-top: .15rem; }
.result-label { color: var(--primary); font-weight: 700; font-size: 1.05rem; }
.result {
  font-family: 'Inter', monospace; font-weight: 700; letter-spacing: -.02em;
  font-size: clamp(1.9rem, 7vw, 2.9rem); text-align: left; flex: 1;
  word-break: break-all; transition: color .2s ease;
}
.result.error { color: var(--danger); font-size: clamp(1.4rem, 5vw, 2rem); }
.result.flash { animation: pop .28s ease; color: var(--success); }

/* ── Keypad ── */
.pad { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: .55rem; direction: ltr; }

.key {
  display: grid; place-items: center; min-height: 58px;
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--key-bg); color: var(--text);
  font-size: 1.2rem; font-weight: 600; font-family: 'Inter', var(--font);
  transition: transform .12s ease, background .18s ease, box-shadow .18s ease, border-color .18s ease;
}
.key:hover { background: var(--card-hover); transform: translateY(-2px); box-shadow: 0 10px 24px rgba(0, 0, 0, .28); }
.key:active { transform: translateY(0) scale(.97); }
.key:focus-visible { outline: 2px solid var(--primary-2); outline-offset: 2px; }

.key.op { color: var(--primary-2); font-size: 1.35rem; background: var(--primary-soft); border-color: rgba(234, 88, 12, .22); }
.key.fn { color: var(--muted); font-size: 1rem; }
.key.fn:hover { color: var(--text); }
.key.eq {
  background: linear-gradient(140deg, var(--primary), var(--primary-2));
  color: #fff; border-color: transparent; font-size: 1.4rem;
  box-shadow: 0 12px 30px rgba(234, 88, 12, .38);
}
.key.eq:hover { box-shadow: 0 16px 38px rgba(234, 88, 12, .5); }
.key svg { width: 19px; height: 19px; }
.key.sci { font-size: .95rem; min-height: 48px; color: var(--accent); background: rgba(59, 130, 246, .1); border-color: rgba(59, 130, 246, .2); }

.pad:not(.sci-mode) .sci { display: none; }

.hint {
  display: flex; gap: .5rem; align-items: flex-start; margin-top: 1rem;
  font-size: .78rem; color: var(--muted); line-height: 1.7;
}
.hint svg { width: 16px; height: 16px; flex: none; margin-top: .22rem; color: var(--primary-2); }
.hint b { color: var(--text); font-weight: 600; }

/* ── History ── */
.history-head { display: flex; align-items: center; justify-content: space-between; padding-bottom: .7rem; border-bottom: 1px solid var(--border); }
.history-head h2 { display: flex; align-items: center; gap: .45rem; font-size: .95rem; font-weight: 700; }
.history-head svg { width: 17px; height: 17px; color: var(--primary-2); }

.history-list { display: flex; flex-direction: column; gap: .45rem; margin-top: .8rem; max-height: 46vh; overflow-y: auto; }
.history-item {
  text-align: left; direction: ltr; padding: .6rem .75rem;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, .03);
  transition: transform .15s ease, border-color .2s ease, background .2s ease;
  animation: fadeUp .3s ease both;
}
.history-item:hover { border-color: var(--border-strong); background: var(--card-hover); transform: translateX(3px); }
.history-item .h-expr { display: block; font-family: 'Inter', monospace; font-size: .78rem; color: var(--muted); word-break: break-all; }
.history-item .h-res { display: block; font-family: 'Inter', monospace; font-size: 1.02rem; font-weight: 700; }

.empty { display: grid; place-items: center; gap: .5rem; padding: 2.2rem 1rem; color: var(--muted); text-align: center; font-size: .82rem; }
.empty svg { width: 26px; height: 26px; opacity: .55; }

/* ── Toast ── */
.toast-wrap { position: fixed; bottom: 1.4rem; left: 1.4rem; z-index: 100; display: flex; flex-direction: column; gap: .5rem; }
.toast {
  padding: .7rem 1.1rem; border-radius: 12px; font-size: .85rem; font-weight: 600;
  backdrop-filter: blur(12px); box-shadow: var(--shadow-lg); animation: fadeUp .28s ease both;
}
.toast.success { background: rgba(74, 222, 128, .14); border: 1px solid rgba(74, 222, 128, .34); color: var(--success); }
.toast.error { background: rgba(248, 113, 113, .14); border: 1px solid rgba(248, 113, 113, .34); color: var(--danger); }
.toast.out { animation: fadeOut .28s ease both; }

/* ── Motion ── */
@keyframes fadeUp { from { opacity: 0; transform: translateY(16px); } to { opacity: 1; transform: none; } }
@keyframes fadeOut { to { opacity: 0; transform: translateY(10px); } }
@keyframes pop { 0% { transform: scale(.94); } 60% { transform: scale(1.03); } 100% { transform: scale(1); } }

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

/* ── Responsive ── */
@media (max-width: 900px) {
  .workspace { grid-template-columns: 1fr; }
  .history-card { position: static; }
  .history-list { max-height: 240px; }
}

@media (max-width: 640px) {
  .app { padding: 0 .8rem 2rem; }
  .topbar { margin-bottom: 1.2rem; gap: .4rem; }
  .brand-text small { display: none; }
  .seg-btn { padding: .35rem .7rem; font-size: .76rem; }
  .icon-btn { width: 36px; height: 36px; }
  .calc-card, .history-card { padding: 1rem; border-radius: 20px; }
  .key { min-height: 52px; font-size: 1.1rem; border-radius: 13px; }
  .key.sci { min-height: 42px; font-size: .85rem; }
  .pad { gap: .45rem; }
  .hint { font-size: .72rem; }
  .toast-wrap { left: .8rem; right: .8rem; bottom: .8rem; }
}

@media (max-width: 380px) {
  .key { min-height: 46px; font-size: 1rem; }
  .pad { gap: .35rem; }
}

/* ── Misc fixes ── */
.angle-toggle[hidden] { display: none; }
.key.pressed { transform: translateY(0) scale(.95); background: var(--card-hover); }
.topbar { background: rgba(18, 16, 15, .82); }
body.light .topbar { background: rgba(247, 244, 242, .85); }

@media (prefers-reduced-motion: reduce) {
  .key:hover { transform: none; }
}
