* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

/* ── Theme variables ── */

:root {
  --bg:            #000;
  --text:          #fff;
  --text-expr:     rgba(255,255,255,0.55);
  --text-mode:     rgba(255,255,255,0.9);
  --icon-color:    rgba(255,255,255,0.55);
  --icon-hover:    rgba(255,255,255,0.9);
  --sess-color:    rgba(255,255,255,0.45);
  --sess-hover:    rgba(255,255,255,0.75);
  --sess-sep:      rgba(255,255,255,0.18);
  --cumul-color:   rgba(255,255,255,0.85);
  --cumul-reset:   rgba(255,255,255,0.45);
  --cumul-hover:   rgba(255,255,255,0.8);
  --key-digit-bg:  #3a3a3c;
  --key-gray-bg:   #636366;
  --key-color:     #fff;
  --border:        #222;
  --sheet-bg:      #1c1c1e;
  --sheet-sep:     rgba(255,255,255,0.1);
  --kbd-bg:        rgba(255,255,255,0.12);
  --kbd-text:      rgba(255,255,255,0.85);
  --sc-section:    rgba(255,255,255,0.35);
}

html[data-theme="light"] {
  --bg:            #f2f2f7;
  --text:          #000;
  --text-expr:     rgba(0,0,0,0.55);
  --text-mode:     rgba(0,0,0,0.9);
  --icon-color:    rgba(0,0,0,0.55);
  --icon-hover:    rgba(0,0,0,0.9);
  --sess-color:    rgba(0,0,0,0.45);
  --sess-hover:    rgba(0,0,0,0.75);
  --sess-sep:      rgba(0,0,0,0.15);
  --cumul-color:   rgba(0,0,0,0.85);
  --cumul-reset:   rgba(0,0,0,0.45);
  --cumul-hover:   rgba(0,0,0,0.8);
  --key-digit-bg:  #d9d9d9;
  --key-gray-bg:   #bdbdc2;
  --key-color:     #000;
  --border:        #d1d1d6;
  --sheet-bg:      #f2f2f7;
  --sheet-sep:     rgba(0,0,0,0.1);
  --kbd-bg:        rgba(0,0,0,0.08);
  --kbd-text:      rgba(0,0,0,0.7);
  --sc-section:    rgba(0,0,0,0.35);
}

/* ── Base ── */

html, body {
  height: 100%;
  background: var(--bg);
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Rounded', system-ui, sans-serif;
  overflow: hidden;
}

.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;
}

.calc-app {
  max-width: 430px;
  margin: 0 auto;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  position: relative;
}

/* ── Display ── */

.calc-display {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 0 16px;
  min-height: 0;
  overflow: hidden;
}

.calc-topbar {
  display: flex;
  gap: 0;
  padding-top: 14px;
  padding-bottom: 4px;
  flex-shrink: 0;
}

@supports (padding-top: env(safe-area-inset-top)) {
  .calc-topbar {
    padding-top: calc(14px + env(safe-area-inset-top));
  }
}

.icon-btn {
  background: none;
  border: none;
  cursor: pointer;
  padding: 8px;
  font-size: 20px;
  color: var(--icon-color);
  line-height: 1;
  border-radius: 8px;
  transition: color 0.15s;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.icon-btn:hover { color: var(--icon-hover); }

.calc-display-main {
  flex: 1;
  display: flex;
  align-items: flex-end;
  gap: 12px;
  padding-bottom: 20px;
  min-height: 0;
}

/* Session history – left column */

.session-history-wrap {
  width: 128px;
  flex-shrink: 0;
  max-height: 200px;
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-width: none;
}

.session-history-wrap::-webkit-scrollbar { display: none; }

.session-history {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.session-entry {
  background: none;
  border: none;
  text-align: left;
  font-family: inherit;
  font-size: 12px;
  font-weight: 500;
  color: var(--sess-color);
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 1px 0;
  line-height: 1.5;
  width: 100%;
  transition: color 0.1s;
}

.session-entry:hover { color: var(--sess-hover); }

.session-sep {
  border: none;
  border-top: 1px dashed var(--sess-sep);
  margin: 2px 0;
}

/* Main display – right column */

.display-right {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
  min-width: 0;
}

.cumul-row {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 15px;
  font-weight: 500;
  color: var(--cumul-color);
  line-height: 1;
}

.cumul-row.hidden { display: none; }

.cumul-reset {
  background: none;
  border: none;
  color: var(--cumul-reset);
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
  padding: 0;
  display: flex;
  align-items: center;
}

.cumul-reset:hover { color: var(--cumul-hover); }

.mode-label {
  font-size: 17px;
  font-weight: 600;
  color: var(--text-mode);
  line-height: 1;
}

.expression-line {
  font-size: 21px;
  font-weight: 500;
  color: var(--text-expr);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
  line-height: 1.2;
  min-height: 1.2em;
}

.expression-line.error { color: #ff3b30; }

.display-value-row {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  min-width: 0;
  max-width: 100%;
}

.display-value {
  color: var(--text);
  font-weight: 300;
  line-height: 1;
  cursor: default;
  user-select: none;
  text-align: right;
  white-space: nowrap;
  overflow: hidden;
  min-width: 0;
  flex: 1;
  font-size: 72px;
  transition: font-size 0.1s;
}

.display-value.error { color: #ff3b30; }

.live-conversion {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-expr);
  line-height: 1.2;
  min-height: 1.2em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

/* ── Keypad ── */

.calc-keypad {
  padding: 0 10px 10px;
  display: grid;
  grid-template-rows: repeat(6, 1fr);
  gap: 10px;
  flex: 0 0 62%;
}

@supports (padding-bottom: env(safe-area-inset-bottom)) {
  .calc-keypad {
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
  }
}

.key-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}

.key-empty { /* spacer */ }

.key {
  border: none;
  border-radius: 16px;
  cursor: pointer;
  color: var(--key-color);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 2px;
  font-family: inherit;
  font-size: 28px;
  font-weight: 500;
  user-select: none;
  -webkit-user-select: none;
  transition: transform 0.1s ease, filter 0.1s ease;
  width: 100%;
  height: 100%;
}

.key:active {
  transform: scale(0.93);
  filter: brightness(1.1);
}

.key-digit  { background: var(--key-digit-bg); }
.key-gray   { background: var(--key-gray-bg); }
.key-op     { background: #ff9500; color: #fff; }
.key-equals { background: #ff6a00; color: #fff; }
.key-indigo { background: #5856d6; color: #fff; }

.key.key-disabled {
  opacity: 0.38;
  pointer-events: none;
}

.key-icon {
  font-size: 20px;
  font-weight: 600;
  line-height: 1;
}

.key-sub {
  font-size: 12px;
  font-weight: 600;
  line-height: 1;
}

/* On very small screens, tighten spacing */
@media (max-height: 640px) {
  .calc-keypad { gap: 7px; padding: 0 8px 8px; }
  .key-row     { gap: 7px; }
  .key         { font-size: 23px; border-radius: 14px; }
  .key-icon    { font-size: 17px; }
}

/* On wide / landscape */
@media (min-width: 500px) {
  .calc-app { border-left: 1px solid var(--border); border-right: 1px solid var(--border); }
}

/* ── Copy toast ── */
.copy-toast {
  position: fixed;
  bottom: 66%;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(50,50,50,0.88);
  color: #fff;
  padding: 8px 18px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 600;
  pointer-events: none;
  white-space: nowrap;
  z-index: 99;
  animation: toast-in 0.18s ease, toast-out 0.28s ease 1.1s forwards;
}

@keyframes toast-in {
  from { opacity: 0; transform: translateX(-50%) translateY(6px); }
  to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}
@keyframes toast-out {
  to { opacity: 0; }
}

/* ── Shortcuts modal ── */
.shortcuts-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 200;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}

.shortcuts-overlay.open {
  opacity: 1;
  pointer-events: auto;
}

.shortcuts-sheet {
  width: 100%;
  max-width: 430px;
  background: var(--sheet-bg);
  border-radius: 16px 16px 0 0;
  padding-bottom: env(safe-area-inset-bottom, 0px);
  transform: translateY(100%);
  transition: transform 0.26s cubic-bezier(0.32,0.72,0,1);
  max-height: 75vh;
  overflow-y: auto;
  scrollbar-width: none;
}

.shortcuts-sheet::-webkit-scrollbar { display: none; }

.shortcuts-overlay.open .shortcuts-sheet { transform: translateY(0); }

.shortcuts-header {
  display: flex;
  align-items: center;
  padding: 16px 20px 14px;
  border-bottom: 1px solid var(--sheet-sep);
}

.shortcuts-title {
  flex: 1;
  font-size: 17px;
  font-weight: 600;
  color: var(--text);
}

.shortcuts-close {
  background: var(--kbd-bg);
  border: none;
  color: var(--text);
  width: 28px;
  height: 28px;
  border-radius: 50%;
  font-size: 13px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.sc-section {
  padding: 12px 20px 4px;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--sc-section);
}

.sc-row {
  display: flex;
  align-items: center;
  padding: 10px 20px;
  border-bottom: 1px solid var(--sheet-sep);
}

.sc-row:last-child { border-bottom: none; }

.sc-label {
  flex: 1;
  font-size: 15px;
  color: var(--text);
}

.sc-keys {
  display: flex;
  align-items: center;
  gap: 4px;
}

kbd {
  background: var(--kbd-bg);
  color: var(--kbd-text);
  border-radius: 5px;
  padding: 2px 8px;
  font-size: 13px;
  font-family: inherit;
  font-weight: 500;
  white-space: nowrap;
}

/* ── Focus visible ── */
.icon-btn:focus-visible,
.key:focus-visible,
.session-entry:focus-visible,
.cumul-reset:focus-visible,
.shortcuts-close:focus-visible { outline: 2px solid var(--text); outline-offset: 2px; border-radius: 4px; }

/* Raccourcis : fenêtre centrée plutôt que bottom-sheet sur grand écran.
   Placé après les règles de base pour les emporter (un media query
   n'augmente pas la spécificité — c'est l'ordre source qui tranche). */
@media (min-width: 500px) {
  .shortcuts-overlay { align-items: center; }
  .shortcuts-sheet {
    border-radius: 16px;
    transform: translateY(20px);
    box-shadow: 0 20px 60px rgba(0,0,0,0.35);
    padding-bottom: 0;
  }
  .shortcuts-overlay.open .shortcuts-sheet { transform: translateY(0); }
}
