:root {
  color-scheme: dark;
  --bg: #050713;
  --panel: rgba(17, 23, 48, .94);
  --panel-2: #111831;
  --key: #1b2447;
  --key-hover: #263463;
  --text: #f5f8ff;
  --muted: #8d9bc7;
  --cyan: #66d9ff;
  --violet: #8c6dff;
  --danger: #ff668d;
  --shadow: rgba(0, 0, 0, .42);
  --radius: 28px;
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #eaf1ff;
  --panel: rgba(255, 255, 255, .93);
  --panel-2: #f4f7ff;
  --key: #e6ebfa;
  --key-hover: #d5def5;
  --text: #10152a;
  --muted: #5b6585;
  --cyan: #007bb8;
  --violet: #6547df;
  --danger: #d3295f;
  --shadow: rgba(53, 67, 110, .22);
}

* { box-sizing: border-box; }
html { min-height: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  font-family: Inter, ui-rounded, "Segoe UI", system-ui, sans-serif;
  color: var(--text);
  background:
    radial-gradient(circle at 15% 15%, rgba(62, 182, 255, .18), transparent 35%),
    radial-gradient(circle at 85% 85%, rgba(143, 86, 255, .20), transparent 38%),
    var(--bg);
  overflow-x: hidden;
}

button { font: inherit; }
button:focus-visible { outline: 3px solid rgba(102, 217, 255, .65); outline-offset: 2px; }

.app-shell {
  width: min(100%, 1040px);
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  padding: clamp(14px, 3vw, 34px);
  display: grid;
  grid-template-columns: minmax(0, 520px) minmax(270px, 1fr);
  gap: 22px;
  align-items: center;
  justify-content: center;
}

.calculator, .history-panel {
  border: 1px solid rgba(126, 183, 255, .22);
  background: var(--panel);
  box-shadow: 0 25px 70px var(--shadow), inset 0 1px rgba(255,255,255,.08);
  backdrop-filter: blur(22px);
}

.calculator {
  width: 100%;
  border-radius: var(--radius);
  padding: clamp(16px, 4vw, 26px);
  position: relative;
  overflow: hidden;
}

.calculator::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: linear-gradient(135deg, rgba(73, 203, 255, .13), transparent 40%, rgba(131, 88, 255, .12));
}

.topbar, .brand, .top-actions, .status-row, .history-head {
  display: flex;
  align-items: center;
}
.topbar, .status-row, .history-head { justify-content: space-between; }
.topbar { gap: 14px; position: relative; z-index: 1; }
.brand { gap: 12px; min-width: 0; }
.brand-logo {
  width: 58px; height: 58px; object-fit: contain; border-radius: 16px;
  filter: drop-shadow(0 0 14px rgba(98, 207, 255, .34));
}
h1, h2, p { margin: 0; }
h1 { font-size: clamp(1.35rem, 4vw, 1.85rem); letter-spacing: -.04em; }
.brand p, .history-head p { color: var(--muted); font-size: .85rem; margin-top: 2px; }
.top-actions { gap: 8px; }
.icon-btn {
  width: 42px; height: 42px; border: 0; border-radius: 14px; cursor: pointer;
  color: var(--text); background: var(--key); box-shadow: inset 0 1px rgba(255,255,255,.08);
}
.icon-btn:hover { background: var(--key-hover); transform: translateY(-1px); }

.status-row { margin: 18px 2px 8px; min-height: 30px; position: relative; z-index: 1; }
.memory-indicator { color: var(--cyan); font-size: .82rem; font-weight: 700; }
.mode-btn {
  border: 0; background: transparent; color: var(--cyan); cursor: pointer; padding: 6px 0; font-weight: 700;
}

.display {
  min-height: 154px;
  border-radius: 22px;
  padding: 18px;
  margin-bottom: 14px;
  background: linear-gradient(180deg, rgba(5, 9, 26, .86), rgba(15, 22, 48, .92));
  border: 1px solid rgba(110, 196, 255, .20);
  box-shadow: inset 0 10px 30px rgba(0,0,0,.28);
  text-align: right;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  position: relative;
  z-index: 1;
  overflow: hidden;
}
:root[data-theme="light"] .display { background: linear-gradient(180deg, #f8fbff, #eaf0ff); }
.previous-result { min-height: 22px; color: var(--muted); font-size: .9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.expression { min-height: 32px; color: var(--muted); font-size: 1.18rem; word-break: break-all; }
.result {
  min-height: 52px; font-size: clamp(2.3rem, 9vw, 3.75rem); line-height: 1.08; font-weight: 750;
  letter-spacing: -.055em; color: var(--cyan); white-space: nowrap; overflow-x: auto; scrollbar-width: none;
}
.result::-webkit-scrollbar { display: none; }

.memory-row, .scientific-pad, .keypad { display: grid; gap: 10px; position: relative; z-index: 1; }
.memory-row { grid-template-columns: repeat(4, 1fr); margin-bottom: 10px; }
.memory-row button, .scientific-pad button {
  min-height: 38px; border: 0; border-radius: 12px; color: var(--muted); background: rgba(93, 121, 191, .12); cursor: pointer; font-weight: 700;
}
.memory-row button:hover, .scientific-pad button:hover { color: var(--cyan); background: rgba(93, 121, 191, .23); }
.scientific-pad { grid-template-columns: repeat(5, 1fr); margin-bottom: 10px; }

.keypad { grid-template-columns: repeat(4, 1fr); }
.key {
  min-height: clamp(54px, 10vw, 68px);
  border: 1px solid rgba(255,255,255,.055);
  border-radius: 18px;
  color: var(--text);
  background: linear-gradient(145deg, color-mix(in srgb, var(--key), white 4%), var(--key));
  box-shadow: 0 8px 15px rgba(0,0,0,.19), inset 0 1px rgba(255,255,255,.07);
  cursor: pointer;
  font-size: clamp(1.15rem, 4vw, 1.45rem);
  font-weight: 760;
  transition: transform .12s ease, background .12s ease, box-shadow .12s ease;
}
.key:hover { background: var(--key-hover); transform: translateY(-2px); box-shadow: 0 11px 20px rgba(0,0,0,.23); }
.key:active { transform: translateY(1px) scale(.97); }
.key-soft { color: var(--cyan); }
.key-op { color: #fff; background: linear-gradient(145deg, #5669c8, #39469d); }
.key-op:hover { background: linear-gradient(145deg, #6579dc, #4654b1); }
.key-equals {
  grid-column: 1 / -1;
  color: #08101f;
  background: linear-gradient(90deg, var(--cyan), #9c8cff);
  font-size: 1.7rem;
}
.key-equals:hover { background: linear-gradient(90deg, #8de5ff, #b2a5ff); }

footer { margin-top: 16px; text-align: center; color: var(--muted); font-size: .78rem; position: relative; z-index: 1; }

.history-panel {
  min-height: 540px;
  max-height: 78vh;
  border-radius: 24px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  transition: opacity .2s ease, transform .2s ease;
}
.history-list { margin: 18px 0; overflow: auto; flex: 1; display: grid; align-content: start; gap: 10px; padding-right: 3px; }
.history-empty { color: var(--muted); text-align: center; padding: 42px 10px; }
.history-item {
  width: 100%; border: 0; border-radius: 14px; padding: 12px 14px; text-align: right; cursor: pointer;
  color: var(--text); background: var(--panel-2);
}
.history-item span { display: block; color: var(--muted); font-size: .83rem; margin-bottom: 4px; word-break: break-all; }
.history-item strong { color: var(--cyan); font-size: 1.1rem; word-break: break-all; }
.clear-history { border: 0; border-radius: 14px; padding: 13px; color: var(--danger); background: rgba(255, 87, 132, .12); cursor: pointer; font-weight: 750; }
.clear-history:hover { background: rgba(255, 87, 132, .2); }

@media (max-width: 820px) {
  .app-shell { grid-template-columns: minmax(0, 520px); align-items: start; }
  .history-panel {
    position: fixed; z-index: 20; inset: auto 12px 12px; max-height: 72vh; min-height: 360px;
    transform: translateY(120%); opacity: 0; pointer-events: none;
  }
  .history-panel.is-open { transform: translateY(0); opacity: 1; pointer-events: auto; }
}

@media (min-width: 821px) {
  .history-panel[aria-hidden="true"] { opacity: .45; }
}

@media (max-width: 430px) {
  .app-shell { padding: 8px; }
  .calculator { border-radius: 22px; padding: 14px; }
  .brand-logo { width: 48px; height: 48px; }
  .scientific-pad { grid-template-columns: repeat(5, minmax(0,1fr)); gap: 7px; }
  .scientific-pad button { font-size: .82rem; padding: 0; }
  .keypad, .memory-row { gap: 8px; }
  .key { border-radius: 15px; min-height: 57px; }
  .display { min-height: 134px; padding: 14px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; animation: none !important; }
}

/* Optimisations smartphone et écrans tactiles */
html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  background: var(--bg);
}

body {
  overscroll-behavior-y: contain;
  -webkit-tap-highlight-color: transparent;
}

button {
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

.app-shell {
  padding-top: max(clamp(10px, 3vw, 34px), env(safe-area-inset-top));
  padding-right: max(clamp(10px, 3vw, 34px), env(safe-area-inset-right));
  padding-bottom: max(clamp(10px, 3vw, 34px), env(safe-area-inset-bottom));
  padding-left: max(clamp(10px, 3vw, 34px), env(safe-area-inset-left));
}

@media (max-width: 820px) {
  body { overflow-y: auto; }

  .app-shell {
    width: 100%;
    min-height: 100dvh;
    display: block;
  }

  .calculator {
    width: 100%;
    max-width: 560px;
    margin: 0 auto;
  }

  .history-panel {
    left: max(10px, env(safe-area-inset-left));
    right: max(10px, env(safe-area-inset-right));
    bottom: max(10px, env(safe-area-inset-bottom));
    width: auto;
    max-height: min(76dvh, 680px);
    overflow: hidden;
  }
}

@media (max-width: 480px) {
  .app-shell {
    padding-top: max(6px, env(safe-area-inset-top));
    padding-right: max(6px, env(safe-area-inset-right));
    padding-bottom: max(8px, env(safe-area-inset-bottom));
    padding-left: max(6px, env(safe-area-inset-left));
  }

  .calculator {
    border-radius: 20px;
    padding: 12px;
  }

  .topbar { gap: 8px; }
  .brand { gap: 8px; }
  .brand-logo { width: 42px; height: 42px; border-radius: 12px; }
  h1 { font-size: 1.28rem; }
  .brand p { font-size: .72rem; }
  .top-actions { gap: 6px; }
  .icon-btn { width: 38px; height: 38px; border-radius: 12px; }

  .status-row { margin: 9px 2px 5px; min-height: 25px; }
  .mode-btn, .memory-indicator { font-size: .75rem; }

  .display {
    min-height: 116px;
    margin-bottom: 9px;
    padding: 12px;
    border-radius: 17px;
  }

  .previous-result { font-size: .76rem; min-height: 18px; }
  .expression { font-size: .98rem; min-height: 25px; }
  .result { font-size: clamp(2rem, 12vw, 3rem); min-height: 44px; }

  .memory-row,
  .scientific-pad,
  .keypad { gap: 6px; }

  .memory-row { margin-bottom: 7px; }
  .memory-row button,
  .scientific-pad button {
    min-height: 34px;
    border-radius: 10px;
    font-size: .75rem;
  }

  .scientific-pad { margin-bottom: 7px; }

  .key {
    min-width: 0;
    min-height: clamp(48px, 13vw, 58px);
    border-radius: 14px;
    font-size: clamp(1.05rem, 5vw, 1.3rem);
  }

  .key-equals { min-height: 51px; font-size: 1.5rem; }
  footer { margin-top: 10px; font-size: .68rem; }
}

@media (max-width: 340px) {
  .calculator { padding: 9px; }
  .brand-logo { width: 36px; height: 36px; }
  h1 { font-size: 1.1rem; }
  .brand p { display: none; }
  .icon-btn { width: 34px; height: 34px; }
  .scientific-pad button { font-size: .68rem; }
  .key { min-height: 44px; }
}

/* Smartphone en mode paysage : interface compacte et entièrement visible */
@media (max-height: 520px) and (orientation: landscape) and (max-width: 950px) {
  .app-shell {
    padding-top: max(5px, env(safe-area-inset-top));
    padding-bottom: max(5px, env(safe-area-inset-bottom));
  }

  .calculator {
    max-width: 900px;
    padding: 9px 12px;
  }

  .topbar { position: absolute; top: 10px; left: 14px; right: 14px; }
  .brand-logo { width: 34px; height: 34px; }
  .brand p, footer { display: none; }
  h1 { font-size: 1.05rem; }
  .icon-btn { width: 34px; height: 34px; }

  .status-row { margin: 0 82px 4px 150px; min-height: 30px; }
  .display { min-height: 82px; padding: 8px 12px; margin-bottom: 6px; }
  .previous-result { min-height: 15px; font-size: .7rem; }
  .expression { min-height: 18px; font-size: .85rem; }
  .result { min-height: 35px; font-size: clamp(1.8rem, 6vw, 2.6rem); }

  .memory-row, .scientific-pad, .keypad { gap: 5px; }
  .memory-row { margin-bottom: 5px; }
  .memory-row button, .scientific-pad button { min-height: 28px; font-size: .7rem; }
  .scientific-pad { margin-bottom: 5px; }
  .key { min-height: 36px; border-radius: 10px; font-size: 1rem; }
  .key-equals { min-height: 37px; font-size: 1.25rem; }

  .history-panel {
    inset: max(6px, env(safe-area-inset-top)) max(6px, env(safe-area-inset-right)) max(6px, env(safe-area-inset-bottom)) auto;
    width: min(42vw, 360px);
    min-height: 0;
    max-height: none;
  }
}
