/* ==========================================================================
   FirewallFlow Calculator — calculator.css
   Design language: cool blue→indigo→cyan gradient, glassmorphism, premium
   desktop-app feel. Supports light & dark themes via [data-theme].
   ========================================================================== */

:root{
  --build-version: "2026-07-26-d";

  --brand-blue:   #2563EB;
  --brand-indigo: #4F46E5;
  --brand-cyan:   #06B6D4;
  --brand-gradient: linear-gradient(135deg, var(--brand-blue) 0%, var(--brand-indigo) 55%, var(--brand-cyan) 100%);

  --radius-lg: 22px;
  --radius-md: 14px;
  --radius-sm: 10px;

  --shadow-soft: 0 10px 30px rgba(30, 41, 90, .12), 0 2px 8px rgba(30, 41, 90, .06);
  --shadow-key: 0 2px 4px rgba(20, 30, 60, .08);
  --shadow-key-hover: 0 6px 16px rgba(37, 99, 235, .18);

  --font-display: 'Segoe UI', -apple-system, BlinkMacSystemFont, 'Inter', sans-serif;
  --font-mono: 'SF Mono', 'Cascadia Code', 'Consolas', ui-monospace, monospace;

  --transition-fast: 120ms cubic-bezier(.4,0,.2,1);
  --transition-med: 220ms cubic-bezier(.4,0,.2,1);
}

/* -------------------------- Light theme (default) ------------------------ */
[data-theme="light"]{
  --bg-app: #eef2fb;
  --bg-app-2: #e3ebfa;
  --surface: rgba(255,255,255,.72);
  --surface-solid: #ffffff;
  --surface-strong: rgba(255,255,255,.92);
  --border-soft: rgba(37,99,235,.14);
  --text-primary: #10192e;
  --text-secondary: #5b6785;
  --text-faint: #97a2c0;
  --key-bg: rgba(255,255,255,.85);
  --key-bg-hover: #ffffff;
  --key-util-bg: rgba(37,99,235,.08);
  --key-util-text: #1d3fae;
  --display-bg: rgba(255,255,255,.55);
  --divider: rgba(37,99,235,.14);
  --accent-text: #2547d0;
  --danger: #e0435c;
}

/* -------------------------------- Dark theme ------------------------------ */
[data-theme="dark"]{
  --bg-app: #0f1729;
  --bg-app-2: #131c33;
  --surface: rgba(24,33,58,.72);
  --surface-solid: #182238;
  --surface-strong: rgba(24,33,58,.92);
  --border-soft: rgba(99,140,255,.16);
  --text-primary: #eef2ff;
  --text-secondary: #9aa7d1;
  --text-faint: #5d6790;
  --key-bg: rgba(38,49,82,.75);
  --key-bg-hover: rgba(52,66,106,.95);
  --key-util-bg: rgba(79,70,229,.22);
  --key-util-text: #b6c2ff;
  --display-bg: rgba(18,25,45,.65);
  --divider: rgba(99,140,255,.16);
  --accent-text: #7fa4ff;
  --danger: #ff6b81;
}

*, *::before, *::after{ box-sizing: border-box; }

html, body{
  margin: 0;
  background: var(--bg-app);
  overscroll-behavior: none;
  overflow-x: hidden;
}

body{
  font-family: var(--font-display);
  background:
    radial-gradient(1200px 800px at 15% -10%, rgba(6,182,212,.16), transparent 60%),
    radial-gradient(1000px 700px at 110% 10%, rgba(79,70,229,.16), transparent 55%),
    linear-gradient(180deg, var(--bg-app) 0%, var(--bg-app-2) 100%);
  color: var(--text-primary);
  min-height: 100vh;
  display: flex;
  align-items: stretch;
  justify-content: center;
  overscroll-behavior: none;
  transition: background var(--transition-med), color var(--transition-med);
}

.app{
  width: 100%;
  max-width: 480px;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  position: relative;
  background: var(--bg-app);
  overflow-x: hidden;
  overscroll-behavior: none;
  padding-bottom: env(safe-area-inset-bottom, 0);
}

/* Only lock out scrolling entirely once installed as a standalone app —
   there's no address bar there, so the layout should always fit exactly.
   In a normal browser tab we deliberately do NOT manage height/overflow
   manually — native document scrolling handles the address bar showing
   and hiding far more reliably than a fixed-height scroll container. */
@media (display-mode: standalone){
  html, body{ height: 100%; overflow-y: hidden; }
  .app{ overflow-y: hidden; }
}

/* On wide screens, present as a contained "app window" rather than full bleed */
@media (min-width: 640px){
  body{ padding: 28px 16px; align-items: center; min-height: 100vh; }
  .app{
    min-height: min(860px, 94vh);
    border-radius: 28px;
    box-shadow: var(--shadow-soft), 0 0 0 1px var(--border-soft);
    background: var(--surface);
    backdrop-filter: blur(24px);
  }
}

/* ============================== TOP BAR ================================= */
.topbar{
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px 10px;
  flex-wrap: wrap;
}

.brand{
  margin: 0;
  font-size: 15px;
  font-weight: 700;
  letter-spacing: .2px;
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-primary);
  flex: 0 0 auto;
}
.brand-icon{
  flex: 0 0 auto;
  color: var(--brand-indigo);
}
.brand-titles{
  display: flex;
  flex-direction: column;
  line-height: 1.15;
}
.brand-main{
  font-size: 16px;
  font-weight: 800;
  color: var(--text-primary);
}
.brand-sub{
  font-size: 9.5px;
  font-weight: 700;
  letter-spacing: .5px;
  text-transform: uppercase;
  background: var(--brand-gradient);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.mode-tabs{
  display: flex;
  gap: 4px;
  background: var(--key-util-bg);
  padding: 4px;
  border-radius: 999px;
  flex: 1 1 100%;
  order: 3;
}

.mode-tab{
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 12.5px;
  font-weight: 600;
  padding: 7px 6px;
  border-radius: 999px;
  cursor: pointer;
  transition: color var(--transition-fast), background var(--transition-fast), box-shadow var(--transition-fast);
  white-space: nowrap;
}
.mode-tab:hover{ color: var(--text-primary); }
.mode-tab.is-active{
  background: var(--brand-gradient);
  color: #fff;
  box-shadow: var(--shadow-key-hover);
}

.topbar-actions{ display: flex; gap: 6px; margin-left: auto; }

.icon-btn{
  display: inline-flex; align-items: center; gap: 6px;
  border: none;
  background: var(--key-util-bg);
  color: var(--key-util-text);
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: transform var(--transition-fast), background var(--transition-fast);
}
.icon-btn:hover{ background: var(--brand-gradient); color: #fff; transform: translateY(-1px); }
.icon-btn:active{ transform: translateY(0); }
.icon-btn span{ font-size: 12px; }

/* ============================== CALCULATOR BODY ========================== */
.calculator{
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 6px 14px 14px;
  min-height: 0;
}

/* ------------------------------- Display --------------------------------- */
.display-panel{
  background: var(--display-bg);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-lg);
  padding: 26px 18px 24px;
  text-align: right;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.3);
  flex: 0 0 auto;
  min-height: 128px;
}
.display-top-row{
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 8px;
  min-height: 18px;
}
.expression-prev{
  font-size: 13px;
  color: var(--text-faint);
  font-family: var(--font-mono);
  min-height: 16px;
  overflow-wrap: anywhere;
}
.result-actions{
  display: flex;
  justify-content: flex-end;
  gap: 14px;
  margin-top: 6px;
}
.result-actions[hidden]{ display: none; }
.memory-indicator{
  font-size: 11px;
  font-weight: 700;
  color: var(--accent-text);
  background: var(--key-util-bg);
  padding: 1px 7px;
  border-radius: 6px;
}
.expression-current{
  font-family: var(--font-mono);
  font-size: clamp(28px, 8vw, 42px);
  font-weight: 600;
  line-height: 1.15;
  color: var(--text-primary);
  overflow-wrap: anywhere;
  outline: none;
  margin-top: 2px;
  min-height: 2.3em;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.expression-current.shrink-1{ font-size: clamp(22px, 6vw, 32px); }
.expression-current.shrink-2{ font-size: clamp(17px, 4.6vw, 24px); }
.expression-current.has-error{ color: var(--danger); }

.expression-error{
  font-size: 12px;
  font-weight: 700;
  color: var(--danger);
  margin-top: 5px;
  min-height: 15px;
}
.expression-error[hidden]{ display: none; }

@keyframes shake-x{
  10%, 90% { transform: translateX(-1px); }
  20%, 80% { transform: translateX(2px); }
  30%, 50%, 70% { transform: translateX(-4px); }
  40%, 60% { transform: translateX(4px); }
}
.expression-current.shake{ animation: shake-x 420ms cubic-bezier(.36,.07,.19,.97); }

/* ------------------------------ Function area ----------------------------- */
.function-area{
  flex: 0 0 auto;
  display: grid;
  gap: 6px;
  min-height: 0;
}
.function-area:empty{ display: none; }

.function-area.grid-4{ grid-template-columns: repeat(4, 1fr); }
.function-area.grid-5{ grid-template-columns: repeat(5, 1fr); }
.function-area.grid-6{ grid-template-columns: repeat(6, 1fr); }
.function-area.grid-7{ grid-template-columns: repeat(7, 1fr); }
.function-area.grid-2{ grid-template-columns: repeat(2, 1fr); }

.fn-btn{
  border: none;
  background: var(--key-util-bg);
  color: var(--key-util-text);
  font-size: 11.5px;
  font-weight: 600;
  padding: 7px 2px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--transition-fast), color var(--transition-fast), transform var(--transition-fast);
  font-family: var(--font-display);
}
.fn-btn sup{ font-size: 8px; }
.fn-btn:hover{ background: var(--brand-gradient); color: #fff; transform: translateY(-1px); }
.fn-btn:active{ transform: translateY(0) scale(.96); }
.fn-btn.is-active{ background: var(--brand-gradient); color: #fff; }
.fn-btn.wide{ grid-column: span 2; }

/* Consolidated Root / log / Trig dropdown buttons */
.fn-menu-btn{ display: flex; align-items: center; justify-content: center; gap: 2px; }
.menu-caret{ font-size: 7px; opacity: .75; transform: translateY(1px); }

.fn-menu{
  position: absolute;
  z-index: 45;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 6px;
  min-width: 132px;
  background: var(--surface-strong);
  backdrop-filter: blur(20px);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-soft);
  animation: modal-in var(--transition-fast);
}
.fn-menu-item{
  border: none;
  background: var(--key-util-bg);
  color: var(--key-util-text);
  font-size: 13px;
  font-weight: 600;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  text-align: left;
  font-family: var(--font-display);
  transition: background var(--transition-fast), color var(--transition-fast);
}
.fn-menu-item:hover{ background: var(--brand-gradient); color: #fff; }

/* Version info popover */
.version-panel{
  position: absolute;
  z-index: 50;
  right: 14px;
  top: 58px;
  min-width: 240px;
  max-width: 280px;
  padding: 14px 16px;
  background: var(--surface-strong);
  backdrop-filter: blur(20px);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-soft);
  animation: modal-in var(--transition-fast);
}
.version-panel h3{
  margin: 0 0 10px;
  font-size: 12.5px;
  font-weight: 700;
  color: var(--text-primary);
  letter-spacing: .02em;
}
.version-row{
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 6px 0;
  border-top: 1px solid var(--border-soft);
  font-size: 12px;
}
.version-row:first-of-type{ border-top: none; }
.version-row .v-label{ color: var(--text-secondary); font-weight: 600; }
.version-row .v-value{
  font-family: var(--font-mono);
  color: var(--text-primary);
  font-weight: 700;
  background: var(--key-util-bg);
  padding: 2px 7px;
  border-radius: 6px;
}
.version-panel .v-hint{
  margin-top: 10px;
  font-size: 10.5px;
  line-height: 1.4;
  color: var(--text-secondary);
}

/* -------------------------------- Memory row ------------------------------ */
.memory-row{
  display: flex;
  gap: 5px;
  flex: 0 0 auto;
  align-items: stretch;
}
.mem-btn{
  flex: 1;
  border: none;
  background: var(--surface);
  border: 1px solid var(--border-soft);
  color: var(--text-secondary);
  font-size: 10.5px;
  font-weight: 700;
  line-height: 1.15;
  padding: 7px 1px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--transition-fast), color var(--transition-fast), transform var(--transition-fast);
}
.mem-btn:hover{ color: var(--accent-text); border-color: var(--brand-blue); }
.mem-btn:active{ transform: scale(.94); }
.mem-btn.has-value{ color: #fff; background: var(--brand-gradient); border-color: transparent; }
.mem-divider{ width: 1px; background: var(--divider); margin: 2px 2px; }

/* --------------------------------- Keypad --------------------------------- */
.keypad{
  flex: 1;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-auto-rows: 1fr;
  gap: 6px;
  min-height: 148px;
}

.key{
  border: none;
  border-radius: var(--radius-md);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  color: var(--text-primary);
  background: var(--key-bg);
  box-shadow: var(--shadow-key);
  transition: background var(--transition-fast), transform 90ms ease, box-shadow var(--transition-fast);
  font-family: var(--font-display);
}
.key:hover{ background: var(--key-bg-hover); box-shadow: var(--shadow-key-hover); transform: translateY(-1px); }
.key:active{ transform: translateY(0) scale(.95); }

.key-digit{ font-weight: 600; }
.key-util{ background: var(--key-util-bg); color: var(--key-util-text); font-size: 14px; }
.key-op{ background: rgba(6,182,212,.18); color: #05748a; font-size: 17px; }
[data-theme="dark"] .key-op{ background: rgba(6,182,212,.26); color: #5fdfef; }
.key-percent{ background: rgba(6,182,212,.18); color: #05748a; font-size: 15.5px; }
[data-theme="dark"] .key-percent{ background: rgba(6,182,212,.26); color: #5fdfef; }
.key-equals{
  grid-column: span 2;
  background: var(--brand-gradient);
  color: #fff;
  font-size: 18px;
  box-shadow: 0 8px 20px rgba(37,99,235,.35);
}
.key-equals:hover{ filter: brightness(1.08); box-shadow: 0 10px 26px rgba(37,99,235,.45); }
.key.is-disabled{ opacity: .32; pointer-events: none; }
.key:focus-visible, .fn-btn:focus-visible, .mem-btn:focus-visible, .mode-tab:focus-visible, .icon-btn:focus-visible, .base-row:focus-visible{
  outline: 2.5px solid var(--brand-cyan);
  outline-offset: 2px;
}

/* ============================== HISTORY DRAWER =========================== */
.history-drawer{
  position: fixed;
  top: 0; right: 0;
  height: 100%;
  width: min(340px, 88vw);
  background: var(--surface-strong);
  backdrop-filter: blur(20px);
  border-left: 1px solid var(--border-soft);
  box-shadow: -12px 0 40px rgba(20,30,60,.18);
  z-index: 40;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform var(--transition-med);
}
.history-drawer.is-open{ transform: translateX(0); }

.drawer-header{
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 16px 10px;
  border-bottom: 1px solid var(--divider);
}
.drawer-header h2{ margin: 0; font-size: 16px; }
.drawer-actions{ display: flex; align-items: center; gap: 10px; }
.text-btn{
  background: none; border: none; color: var(--accent-text);
  font-size: 12px; font-weight: 700; cursor: pointer;
}
.text-btn:hover{ text-decoration: underline; }

.history-list{
  list-style: none;
  margin: 0; padding: 8px 10px;
  overflow-y: auto;
  flex: 1;
}
.history-item{
  padding: 10px 10px;
  border-radius: var(--radius-sm);
  margin-bottom: 6px;
  cursor: pointer;
  transition: background var(--transition-fast);
  position: relative;
}
.history-item:hover{ background: var(--key-util-bg); }
.history-item .h-expr{ font-size: 11.5px; color: var(--text-faint); font-family: var(--font-mono); }
.history-item .h-result{ font-size: 17px; font-weight: 700; font-family: var(--font-mono); }
.history-item .h-time{ font-size: 10px; color: var(--text-faint); margin-top: 2px; }
.history-item .h-actions{ position: absolute; top: 8px; right: 8px; display: flex; gap: 4px; opacity: 0; transition: opacity var(--transition-fast); }
.history-item:hover .h-actions{ opacity: 1; }
.h-actions button{
  border: none; background: var(--surface-solid); color: var(--text-secondary);
  width: 22px; height: 22px; border-radius: 6px; font-size: 11px; cursor: pointer;
}
.h-actions button:hover{ background: var(--brand-gradient); color: #fff; }
.history-empty{ text-align: center; color: var(--text-faint); font-size: 13px; padding: 30px 20px; }
.history-empty[hidden]{ display: none; }

.overlay{
  position: fixed; inset: 0; background: rgba(10,15,30,.35);
  backdrop-filter: blur(2px);
  z-index: 30;
  opacity: 0; pointer-events: none;
  transition: opacity var(--transition-med);
}
.overlay.is-open{ opacity: 1; pointer-events: auto; }

/* ================================== MODAL ================================ */
.modal{
  position: fixed; inset: 0;
  z-index: 50;
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.modal[hidden]{ display: none; }
.modal-card{
  width: min(380px, 100%);
  max-height: 84vh;
  overflow-y: auto;
  background: var(--surface-strong);
  backdrop-filter: blur(24px);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-soft);
  animation: modal-in var(--transition-med);
}
@keyframes modal-in{ from{ opacity: 0; transform: translateY(10px) scale(.98);} to{ opacity: 1; transform: none; } }
.modal-header{
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 18px; border-bottom: 1px solid var(--divider);
}
.modal-header h2{ margin: 0; font-size: 16px; }
.modal-body{ padding: 16px 18px 20px; display: flex; flex-direction: column; gap: 12px; }

.field{ display: flex; flex-direction: column; gap: 5px; }
.field label{ font-size: 11.5px; font-weight: 700; color: var(--text-secondary); }
.field input, .field select{
  font-family: var(--font-mono);
  font-size: 15px;
  padding: 9px 11px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-soft);
  background: var(--surface-solid);
  color: var(--text-primary);
}
.field input:focus, .field select:focus{ outline: 2px solid var(--brand-blue); }
.tool-result{
  background: var(--key-util-bg);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  font-family: var(--font-mono);
  font-size: 18px;
  font-weight: 700;
  color: var(--accent-text);
  word-break: break-word;
}
.tool-actions{ display: flex; gap: 8px; }
.btn{
  flex: 1;
  border: none;
  padding: 10px;
  border-radius: var(--radius-sm);
  font-weight: 700;
  font-size: 13px;
  cursor: pointer;
  background: var(--surface);
  border: 1px solid var(--border-soft);
  color: var(--text-primary);
}
.btn.primary{ background: var(--brand-gradient); color: #fff; border: none; }
.swap-btn{ align-self: center; background: var(--key-util-bg); border: none; color: var(--accent-text); border-radius: 50%; width: 30px; height: 30px; cursor: pointer; font-size: 14px; }

/* ================================== TOAST ================================ */
.toast{
  position: fixed;
  bottom: 24px; left: 50%;
  transform: translate(-50%, 20px);
  background: var(--text-primary);
  color: var(--bg-app);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--transition-med), transform var(--transition-med);
  z-index: 60;
  box-shadow: var(--shadow-soft);
}
.toast.is-visible{ opacity: 1; transform: translate(-50%, 0); }

/* ============================ RESPONSIVE TWEAKS =========================== */
@media (max-width: 380px){
  .brand-main{ font-size: 14px; }
  .brand-sub{ font-size: 8.5px; }
  .icon-btn span{ display: none; }
  .key{ font-size: 16px; }
  .expression-current{ font-size: clamp(24px, 9vw, 34px); }
}

@media (min-width: 640px) and (min-height: 760px){
  .keypad{ min-height: 260px; }
}

/* Landscape phones: tighten vertical rhythm so nothing scrolls */
@media (max-height: 480px) and (orientation: landscape){
  .topbar{ padding: 8px 14px 6px; }
  .calculator{ padding: 4px 14px 10px; gap: 6px; }
  .display-panel{ padding: 8px 14px 10px; }
  .expression-current{ font-size: clamp(22px, 6vw, 30px); }
  .keypad{ min-height: 140px; gap: 5px; }
  .key{ font-size: 15px; }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce){
  *{ transition-duration: 0ms !important; animation-duration: 1ms !important; }
}
