:root {
  --bg-color: #000000;
  --text-primary: #ffffff;
  --text-secondary: #8e8e93;
  --btn-bg: #1c1c1e;
  --btn-bg-alt: #2c2c2e;
  --op-color: #26e07f;
  --equals-bg: #4f80e1;
  --equals-bg-alt: #26e07f;
  --danger-color: #ff453a;
  --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

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

html { height: 100%; }

body {
  background-color: var(--bg-color);
  color: var(--text-primary);
  font-family: var(--font-family);
  /* --app-height is set in JS from the real visible viewport (fixes
     bottom-row cutoff behind Brave's bottom bar). Fallbacks first. */
  height: 100vh;
  height: 100svh;
  height: var(--app-height, 100svh);
  overflow: hidden;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

/* --- Header & Navigation --- */
.top-nav {
  display: flex;
  justify-content: space-between;
  padding: 16px 24px;
  flex: 0 0 auto;
}

.icon-btn {
  background: none;
  border: none;
  color: var(--text-primary);
  font-size: 1.2rem;
  cursor: pointer;
  padding: 8px;
}

/* Install pill (Stage 1). Hidden until pwa.js reveals it on
   `beforeinstallprompt`. Reuses the equals-key colour treatment so it reads
   as the primary action and recolours with the theme. */
.nav-right { display: flex; align-items: center; gap: 8px; }

.install-pill {
  background: var(--op-color);
  color: var(--bg-color);
  border: none;
  border-radius: 16px;
  padding: 6px 14px;
  font-family: var(--font-family);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
}
.install-pill:active { filter: brightness(1.1); }
.install-pill:focus-visible { outline: 2px solid var(--op-color); outline-offset: 2px; }

#converter-nav {
  display: flex;
  gap: 16px;
  padding: 0 24px 16px;
  overflow-x: auto;
  white-space: nowrap;
  flex: 0 0 auto;
}

.cat-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 1rem;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 20px;
  flex: 0 0 auto;
}

.cat-btn.active {
  background-color: var(--btn-bg-alt);
  color: var(--text-primary);
}

/* --- Layout Architecture --- */
#main-container {
  display: flex;
  flex: 1;
  overflow: hidden;
  flex-direction: column; /* Portrait default */
  min-height: 0;
}

#left-pane {
  display: none; /* Hidden in portrait standard mode */
  flex-direction: column;
  padding: 0 24px;
  overflow: hidden;
  min-height: 0;
}

#right-pane {
  display: flex;
  flex-direction: column;
  flex: 1;
  padding: 0 24px 24px;
  justify-content: flex-end;
  min-height: 0;
}

/* --- Displays --- */
#calc-display-wrap {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: flex-end;
  flex: 1;
  min-height: 0;
  margin-bottom: 24px;
  overflow: hidden;
}

#calc-input {
  font-size: 2.5rem;
  font-weight: 300;
  text-align: right;
  line-height: 1.2;
  width: 100%;
  white-space: nowrap;      /* single line; JS shrinks the font to fit (§F1) */
  overflow-x: auto;         /* past the shrink floor, scroll horizontally */
  overflow-y: hidden;
  scrollbar-width: none;    /* hide the scrollbar (Firefox) */
}
#calc-input::-webkit-scrollbar { display: none; } /* hide the scrollbar (WebKit) */

#calc-expression {
  font-size: 1.5rem;
  color: var(--text-secondary);
  min-height: 2rem;
  margin-top: 8px;
}

/* After '=': expression dims on top, result grows below. */
#calc-display-wrap.evaluated #calc-input {
  font-size: 1.4rem;
  color: var(--text-secondary);
}
#calc-display-wrap.evaluated #calc-expression {
  font-size: 2.5rem;
  color: var(--text-primary);
  text-align: right;
  white-space: nowrap;
  overflow-x: auto;
  max-width: 100%;
  scrollbar-width: none;
}
#calc-display-wrap.evaluated #calc-expression::-webkit-scrollbar { display: none; }

.op-color { color: var(--op-color); }
.text-red { color: var(--danger-color) !important; }

.cursor {
  display: inline-block;
  width: 2px;
  color: var(--op-color);
  animation: blink 1s step-end infinite;
}
@keyframes blink { 50% { opacity: 0; } }

/* --- Buttons & Grids --- */
.grid-standard {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  width: 100%;
  max-width: 400px;
  margin: 0 auto;
  flex: 0 0 auto;
}

/* Portrait scientific: 6-column grid (2 sci + 4 numpad, then a 6-wide sci row)
   sized to sit inside the basic numpad's footprint. aspect-ratio:1 keeps every
   key circular; the whole cluster is smaller than the standard keypad so it
   always fits. */
.grid-scientific {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 8px;
  width: 100%;
  max-width: 400px;
  margin: 0 auto;
  flex: 0 0 auto;
}
.grid-scientific .btn { font-size: 1.1rem; }
.grid-scientific .sci-btn { font-size: 0.82rem; }

.btn {
  background-color: var(--btn-bg);
  color: var(--text-primary);
  border: none;
  border-radius: 50%;
  aspect-ratio: 1; /* NO SQUISH */
  font-size: 1.5rem;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: filter 0.1s;
  width: 100%;
}

.btn:active { filter: brightness(1.5); }
.op-btn { background-color: var(--btn-bg-alt); }
.equals-btn { background-color: var(--op-color); color: var(--bg-color); }

/* Bold numpad symbols (leaves the scientific labels at normal weight). */
.num-btn, .op-btn, .action-btn, .equals-btn { font-weight: 700; }

.btn.disabled {
  opacity: 0.3;
  pointer-events: none;
}

/* --- Scientific pad (portrait: 4-col grid above the numpad) --- */
#sci-pad {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  width: 100%;
  max-width: 400px;
  margin: 0 auto 12px;
  overflow-y: auto;
  min-height: 0;
}

/* Both scientific layouts use the operator grey. The colour is un-scoped so the
   portrait merged-grid .sci-btn keys match the operator keys; the size caps
   below stay scoped to the landscape dual-pane pad. */
.sci-btn { background-color: var(--btn-bg-alt); }

#sci-pad .sci-btn {
  font-size: 1rem;
  max-width: 62px;
  max-height: 62px;
  margin: 0 auto;
}

/* --- Converter View --- */
#converter-view {
  display: flex;
  flex-direction: column;
  justify-content: center;
  flex: 1;
  min-height: 0;
  padding: 12px 0;
}

.converter-field {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

select {
  border: none;
  color: var(--text-primary);
  font-size: 1.2rem;
  margin-bottom: 8px;
  outline: none;
  appearance: none;
  /* appearance:none strips the native arrow — draw our own chevron so users
     can tell the unit is a dropdown. Grey reads on both themes. */
  padding-right: 22px;
  background-color: transparent;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%238e8e93' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'><polyline points='6 9 12 15 18 9'/></svg>");
  background-repeat: no-repeat;
  background-position: right 0 center;
  background-size: 12px;
}

.unit-val {
  font-size: 1.5rem;
  color: var(--text-secondary);
  align-self: flex-end;
  max-width: 100%;
  overflow-x: auto;
  white-space: nowrap;
  scrollbar-width: none;
  transition: color 0.2s, font-size 0.2s;
}
.unit-val::-webkit-scrollbar { display: none; }

.unit-val.active-val {
  color: var(--text-primary);
  font-size: 1.9rem;
}

.divider {
  border: 0;
  height: 1px;
  background: var(--btn-bg-alt);
  margin: 16px 0;
}

/* --- History slide-in panel --- */
.history-panel {
  position: fixed;
  top: 0; right: 0; bottom: 0;
  width: 82%;
  max-width: 340px;
  background: var(--btn-bg);
  display: flex;
  flex-direction: column;
  padding: 24px 0 calc(16px + env(safe-area-inset-bottom, 0px));
  transform: translateX(100%);
  visibility: hidden;
  transition: transform 0.24s ease, visibility 0.24s ease;
  z-index: 50;
  box-shadow: -2px 0 24px rgba(0, 0, 0, 0.5);
}
.history-panel.open { transform: translateX(0); visibility: visible; }

.history-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 20px 16px;
  border-bottom: 1px solid var(--btn-bg-alt);
}
.history-head .title { font-size: 1.2rem; font-weight: 500; }
.history-head .modal-close { position: static; }

#history-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px 20px;
}

.history-item {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  padding: 8px 4px;
  border-bottom: 1px solid var(--btn-bg-alt);
  width: 100%;
}
.history-item button {
  background: none;
  border: none;
  cursor: pointer;
  width: 100%;
  text-align: right;
  padding: 3px 0;
  font-family: var(--font-family);
}
.history-item button:active { filter: brightness(1.6); }

.history-item .h-expr { color: var(--text-secondary); font-size: 1.1rem; }
.history-item .h-res { color: var(--text-primary); font-size: 1.6rem; }
.history-empty { color: var(--text-secondary); padding: 16px 4px; }

#clear-history {
  align-self: center;
  margin: 12px 20px 0;
  background: var(--btn-bg-alt);
  color: var(--text-primary);
  border: none;
  border-radius: 20px;
  padding: 10px 20px;
  font-size: 0.95rem;
  cursor: pointer;
  flex: 0 0 auto;
}

/* --- Landscape Overrides --- */
@media (orientation: landscape) {
  #main-container {
    flex-direction: row;
    padding-bottom: 12px;
  }

  #left-pane {
    display: flex;
    flex: 1;
    justify-content: center;
    border-right: 1px solid var(--btn-bg-alt);
  }

  #right-pane {
    flex: 1;
    padding-left: 24px;
    justify-content: center;
  }

  #calc-display-wrap {
    flex: 0 0 auto;
    margin-bottom: 12px;
  }

  #sci-pad {
    align-content: center;
    margin: 0;
    max-width: none;
    height: 100%;
  }

  #sci-pad .sci-btn { max-width: 60px; max-height: 60px; }

  .btn {
    font-size: 1.2rem;
    max-height: 60px;
    max-width: 60px;
    margin: 0 auto;
  }
}

.hidden { display: none !important; }

/* ============================================================
   Portrait layout fixes (mode-scoped via #app[data-mode]).
============================================================ */
@media (orientation: portrait) {
  /* Converter: numpad keeps its natural height; the value fields take the rest.
     Anchor the fields to the TOP of the pane (with a little clearance under the
     tab nav) instead of centring — centring let the taller active value push
     the first unit label up under the tabs and clip. A trimmed keypad gap and
     tighter divider give both fields room to sit without clipping either edge. */
  #app[data-mode="converter"] #left-pane { flex: 1 1 0; min-height: 0; }
  #app[data-mode="converter"] #right-pane { flex: 0 0 auto; }
  #app[data-mode="converter"] #converter-view {
    justify-content: flex-start;
    padding-top: 8px;
  }
  #app[data-mode="converter"] .divider { margin: 12px 0; }
  #app[data-mode="converter"] #keypad { margin-top: 12px; }

  /* Scientific (portrait): the merged 6-col grid IS the numpad footprint, so
     the display simply absorbs the slack above it. No pane juggling needed. */
  #app[data-mode="sci"] #calc-display-wrap { flex: 1 1 0; min-height: 0; margin-bottom: 8px; }
  #app[data-mode="sci"] #right-pane { flex: 1 1 0; min-height: 0; }
}

/* ============================================================
   Header brand, slide-out menu (§C) and info modal (§D).
   All colours pulled from the existing tokens so the new chrome
   stays native to the Samsung-dark calculator.
============================================================ */

/* --- Header (§B) --- */
.nav-left { display: flex; align-items: center; gap: 12px; min-width: 0; }
.nav-left .icon-btn { padding: 4px; }
.icon-btn svg { display: block; }

.brand { display: flex; align-items: baseline; gap: 8px; min-width: 0; }
.brand-mark { flex: 0 0 auto; align-self: center; }
.brand-mark rect { fill: var(--op-color); }
.brand-mark text { fill: var(--bg-color); font-size: 13px; font-weight: 700; font-family: var(--font-family); }

.brand-name { font-size: 1.25rem; font-weight: 500; letter-spacing: -0.01em; }
.brand-name.lg { font-size: 1.9rem; }
.brand-version { font-size: 0.8rem; font-style: italic; color: var(--text-secondary); }

/* --- Shared overlay (backdrop) --- */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s ease, visibility 0.2s ease;
  z-index: 40;
}
.overlay.show { opacity: 1; visibility: visible; }

/* --- Slide-out menu (§C) --- */
.menu-panel {
  position: fixed;
  top: 0; left: 0; bottom: 0;
  width: 78%;
  max-width: 300px;
  background: var(--btn-bg);
  display: flex;
  flex-direction: column;
  padding: 24px 0 calc(16px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-100%);
  visibility: hidden;
  transition: transform 0.24s ease, visibility 0.24s ease;
  z-index: 50;
  box-shadow: 2px 0 24px rgba(0, 0, 0, 0.5);
}
.menu-panel.open { transform: translateX(0); visibility: visible; }

.menu-head {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 8px 24px 20px;
  border-bottom: 1px solid var(--btn-bg-alt);
}

.menu-items { display: flex; flex-direction: column; padding: 8px 0; }

.menu-item {
  background: none;
  border: none;
  border-left: 3px solid transparent;
  color: var(--text-primary);
  font-family: var(--font-family);
  font-size: 1.1rem;
  text-align: left;
  padding: 16px 24px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.menu-item:active { background: var(--btn-bg-alt); }
.menu-item.active { color: var(--op-color); border-left-color: var(--op-color); }
.menu-item.is-soon { color: var(--text-secondary); cursor: default; }
.menu-item.is-soon:active { background: none; }

.menu-item .soon {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-secondary);
  border: 1px solid var(--btn-bg-alt);
  border-radius: 10px;
  padding: 2px 8px;
}

/* --- Info modal (§D) --- */
.modal {
  position: fixed;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%) scale(0.96);
  width: calc(100% - 48px);
  max-width: 360px;
  max-height: calc(100% - 48px);
  overflow-y: auto;
  background: var(--btn-bg);
  border-radius: 20px;
  padding: 28px 24px 24px;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease, visibility 0.2s ease;
  z-index: 60;
}
.modal.open {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
  transform: translate(-50%, -50%) scale(1);
}

.modal-close {
  position: absolute;
  top: 10px; right: 14px;
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 1.7rem;
  line-height: 1;
  cursor: pointer;
  padding: 4px;
}

.modal-s1 { text-align: center; padding-bottom: 16px; border-bottom: 1px solid var(--btn-bg-alt); }
.modal-s1 .brand { justify-content: center; }
.modal-s1 .brand-version { position: static; }
.modal-by { font-style: italic; color: var(--text-secondary); font-size: 0.9rem; margin-top: 6px; }

.modal-s2 { padding: 18px 0; }
.modal-s2 h3 {
  font-size: 0.8rem;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
  margin-bottom: 12px;
}
.modal-s2 ul { list-style: none; display: flex; flex-direction: column; gap: 10px; }
.modal-s2 li { font-size: 0.98rem; line-height: 1.3; padding-left: 18px; position: relative; }
.modal-s2 li::before {
  content: "";
  position: absolute;
  left: 0; top: 0.5em;
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--op-color);
}

.modal-s3 { border-top: 1px solid var(--btn-bg-alt); padding-top: 16px; }
.modal-soon { color: var(--text-secondary); font-size: 0.9rem; }

/* --- Quality floor: visible focus + reduced motion --- */
.icon-btn:focus-visible,
.menu-item:focus-visible,
.modal-close:focus-visible {
  outline: 2px solid var(--op-color);
  outline-offset: 2px;
  border-radius: 8px;
}

@media (prefers-reduced-motion: reduce) {
  .overlay, .menu-panel, .modal { transition: none; }
}

/* ============================================================
   Light theme (§E) — derived from the dark tokens, not a separate
   palette. Because every component reads these variables, the whole
   UI (keypad, menu, modals) recolours from this one block.
============================================================ */
:root[data-theme="light"] {
  --bg-color: #ececf1;
  --text-primary: #1c1c1e;
  --text-secondary: #8a8a8e;
  --btn-bg: #ffffff;
  --btn-bg-alt: #d1d1d6;
  --op-color: #12a35a;      /* darkened green — the neon reads too pale on white */
  --equals-bg: #4f80e1;
  --equals-bg-alt: #12a35a;
  --danger-color: #ff3b30;
}
/* Equals keeps white text on blue in both themes. */
/* A touch of lift so white keys separate from the light surface. */
:root[data-theme="light"] .btn { box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08); }

/* ============================================================
   Settings modal (§E) — reuses the shared .overlay / .modal chrome.
============================================================ */
.modal-title { font-size: 1.3rem; font-weight: 500; margin-bottom: 20px; }

.set-group { margin-bottom: 20px; }
.set-group:last-child { margin-bottom: 4px; }
.set-group h3 {
  font-size: 0.78rem;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
  margin-bottom: 10px;
}

.seg { display: flex; gap: 6px; }
.seg button {
  flex: 1;
  padding: 10px 6px;
  border: 1px solid var(--btn-bg-alt);
  border-radius: 10px;
  background: none;
  color: var(--text-primary);
  font-family: var(--font-family);
  font-size: 0.9rem;
  cursor: pointer;
  white-space: nowrap;
}
.seg button.active {
  background: var(--op-color);
  border-color: var(--op-color);
  color: var(--bg-color);
}
.seg button:focus-visible { outline: 2px solid var(--op-color); outline-offset: 2px; }

/* --- Info modal: "What's new" list (§D S3) --- */
.modal-s3 h3 {
  font-size: 0.78rem;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
  margin-bottom: 12px;
}
.modal-list { list-style: none; display: flex; flex-direction: column; gap: 10px; }
.modal-list li { font-size: 0.98rem; line-height: 1.3; padding-left: 18px; position: relative; }
.modal-list li::before {
  content: "";
  position: absolute;
  left: 0; top: 0.5em;
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--op-color);
}
