* {
  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);
  --sign-bg:       rgba(255,255,255,0.15);
  --sign-hover:    rgba(255,255,255,0.25);
  --sign-color:    rgba(255,255,255,0.85);
  --key-digit-bg:  #3a3a3c;
  --key-gray-bg:   #636366;
  --key-color:     #fff;
  --border:        #222;
}

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);
  --sign-bg:       rgba(0,0,0,0.1);
  --sign-hover:    rgba(0,0,0,0.18);
  --sign-color:    rgba(0,0,0,0.85);
  --key-digit-bg:  #d9d9d9;
  --key-gray-bg:   #bdbdc2;
  --key-color:     #000;
  --border:        #d1d1d6;
}

/* ── Base ── */

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

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

.toggle-sign {
  background: var(--sign-bg);
  border: none;
  border-radius: 999px;
  color: var(--sign-color);
  cursor: pointer;
  font-size: 18px;
  font-family: inherit;
  font-weight: 500;
  padding: 8px 14px;
  align-self: center;
  flex-shrink: 0;
  transition: background 0.12s;
  line-height: 1;
}

.toggle-sign:hover { background: var(--sign-hover); }
.toggle-sign.hidden { display: none; }

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

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