/* ============================================================
   CALC PRO — iPad-Style Calculator
   Design System: Apple-inspired flat UI, dark/light mode
   ============================================================ */

/* ── Reset & Base ─────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  /* Fonts */
  --font-display: 'Inter', -apple-system, BlinkMacSystemFont, 'SF Pro Display', sans-serif;

  /* Spacing */
  --gap: 12px;
  --gap-sm: 8px;
  --radius-btn: 50%;
  --radius-panel: 20px;
  --radius-display: 16px;

  /* Animation */
  --transition: 0.18s cubic-bezier(0.4, 0, 0.2, 1);
  --spring: 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);

  /* Sizes */
  --btn-size: 76px;
  --btn-size-sci: 56px;
  --display-height: 160px;
}

/* ── Dark Theme (default) ─────────────────────────────────── */
body.dark {
  --bg-app: #000000;
  --bg-panel: #1c1c1e;
  --bg-display: #1c1c1e;
  --bg-history: #111113;
  --bg-btn-num: #333335;
  --bg-btn-func: #636366;
  --bg-btn-op: #ff9f0a;
  --bg-btn-op-active: #ffd60a;
  --bg-btn-equals: #ff9f0a;
  --bg-btn-sci: #2c2c2e;
  --bg-btn-sci-active: #3a3a3c;
  --bg-btn-inv: #ff9f0a;

  --text-primary: #ffffff;
  --text-secondary: #ebebf5cc;
  --text-muted: #ebebf566;
  --text-op: #ffffff;
  --text-func: #ffffff;
  --text-num: #ffffff;
  --text-sci: #ffffff;
  --text-display: #ffffff;
  --text-expression: #ebebf5aa;
  --text-history-expr: #ebebf5aa;
  --text-history-result: #ffffff;

  --border-color: #38383a;
  --shadow-btn: 0 2px 8px rgba(0,0,0,0.4);
  --shadow-panel: 0 8px 32px rgba(0,0,0,0.6);
  --glow-op: 0 0 20px rgba(255,159,10,0.3);

  --scrollbar-thumb: #48484a;
  --scrollbar-track: transparent;
}

/* ── Light Theme ──────────────────────────────────────────── */
body.light {
  --bg-app: #f2f2f7;
  --bg-panel: #ffffff;
  --bg-display: #ffffff;
  --bg-history: #f2f2f7;
  --bg-btn-num: #ffffff;
  --bg-btn-func: #d1d1d6;
  --bg-btn-op: #ff9f0a;
  --bg-btn-op-active: #ffb340;
  --bg-btn-equals: #ff9f0a;
  --bg-btn-sci: #e5e5ea;
  --bg-btn-sci-active: #d1d1d6;
  --bg-btn-inv: #ff9f0a;

  --text-primary: #1c1c1e;
  --text-secondary: #3c3c4399;
  --text-muted: #3c3c4366;
  --text-op: #ffffff;
  --text-func: #1c1c1e;
  --text-num: #1c1c1e;
  --text-sci: #1c1c1e;
  --text-display: #1c1c1e;
  --text-expression: #3c3c4399;
  --text-history-expr: #3c3c4399;
  --text-history-result: #1c1c1e;

  --border-color: #e5e5ea;
  --shadow-btn: 0 2px 8px rgba(0,0,0,0.08);
  --shadow-panel: 0 4px 24px rgba(0,0,0,0.1);
  --glow-op: 0 0 20px rgba(255,159,10,0.2);

  --scrollbar-thumb: #c7c7cc;
  --scrollbar-track: transparent;
}

/* ── Body & Layout ────────────────────────────────────────── */
html, body {
  height: 100%;
  width: 100%;
  overflow: hidden;
}

body {
  font-family: var(--font-display);
  background: var(--bg-app);
  color: var(--text-primary);
  transition: background var(--transition), color var(--transition);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  user-select: none;
  -webkit-user-select: none;
}

/* ── Theme Toggle ─────────────────────────────────────────── */
.theme-toggle {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 1000;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: var(--bg-btn-sci);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  transition: all var(--transition);
  box-shadow: var(--shadow-btn);
}
.theme-toggle:hover { transform: scale(1.1); }
.theme-toggle:active { transform: scale(0.95); }

/* ── App Container ────────────────────────────────────────── */
.app-container {
  display: flex;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
  position: relative;
}

/* ── History Panel ────────────────────────────────────────── */
.history-panel {
  width: 0;
  min-width: 0;
  overflow: hidden;
  background: var(--bg-history);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  transition: width 0.35s cubic-bezier(0.4, 0, 0.2, 1),
              min-width 0.35s cubic-bezier(0.4, 0, 0.2, 1);
  flex-shrink: 0;
}

.history-panel.open {
  width: 300px;
  min-width: 300px;
}

.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 20px 12px;
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

.history-title {
  font-size: 20px;
  font-weight: 600;
  color: var(--text-primary);
  white-space: nowrap;
}

.history-clear-btn {
  background: none;
  border: none;
  color: #ff9f0a;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 8px;
  transition: all var(--transition);
  white-space: nowrap;
}
.history-clear-btn:hover { background: rgba(255,159,10,0.1); }

.history-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
  scrollbar-width: thin;
  scrollbar-color: var(--scrollbar-thumb) var(--scrollbar-track);
}
.history-list::-webkit-scrollbar { width: 4px; }
.history-list::-webkit-scrollbar-thumb { background: var(--scrollbar-thumb); border-radius: 2px; }

.history-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  gap: 12px;
  color: var(--text-muted);
}
.history-empty-icon { font-size: 40px; opacity: 0.5; }
.history-empty p { font-size: 14px; white-space: nowrap; }

.history-item {
  padding: 12px 20px;
  cursor: pointer;
  border-bottom: 1px solid var(--border-color);
  transition: background var(--transition);
  animation: slideInLeft 0.25s ease;
}
.history-item:hover { background: rgba(255,159,10,0.08); }
.history-item:active { background: rgba(255,159,10,0.15); }

.history-item-expr {
  font-size: 13px;
  color: var(--text-history-expr);
  margin-bottom: 4px;
  word-break: break-all;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.history-item-result {
  font-size: 22px;
  font-weight: 500;
  color: var(--text-history-result);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.history-item-time {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 4px;
}

/* ── Calculator Panel ─────────────────────────────────────── */
.calculator-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
  padding: 0;
}

/* ── Display ──────────────────────────────────────────────── */
.display-section {
  flex-shrink: 0;
  padding: 16px 20px 8px;
  background: var(--bg-display);
  border-bottom: 1px solid var(--border-color);
  transition: background var(--transition);
}

.display-wrapper {
  position: relative;
  min-height: var(--display-height);
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: flex-end;
  gap: 4px;
}

.expression-line {
  font-size: 18px;
  font-weight: 400;
  color: var(--text-expression);
  text-align: right;
  min-height: 24px;
  word-break: break-all;
  line-height: 1.4;
  max-height: 72px;
  overflow: hidden;
  width: 100%;
  transition: color var(--transition);
}

.result-line {
  font-size: clamp(42px, 8vw, 72px);
  font-weight: 300;
  color: var(--text-display);
  text-align: right;
  line-height: 1;
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  transition: font-size 0.15s ease, color var(--transition);
  letter-spacing: -1px;
}

.result-line.error {
  color: #ff453a;
  font-size: clamp(24px, 4vw, 36px);
}

.result-line.animate-result {
  animation: popIn 0.2s var(--spring);
}

.display-actions {
  position: absolute;
  top: 0;
  right: 0;
  display: flex;
  gap: 8px;
}

.copy-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 6px;
  border-radius: 8px;
  transition: all var(--transition);
  display: flex;
  align-items: center;
  justify-content: center;
}
.copy-btn:hover { color: var(--text-secondary); background: var(--bg-btn-sci); }
.copy-btn:active { transform: scale(0.9); }

/* ── Scientific Section ───────────────────────────────────── */
.scientific-section {
  flex-shrink: 0;
  overflow: hidden;
  max-height: 0;
  transition: max-height 0.4s cubic-bezier(0.4, 0, 0.2, 1),
              padding 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border-color);
}

.scientific-section.open {
  max-height: 260px;
  padding: 10px 12px;
}

.sci-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: var(--gap-sm);
}

/* ── Keypad ───────────────────────────────────────────────── */
.keypad-section {
  flex: 1;
  padding: 12px;
  background: var(--bg-panel);
  overflow: hidden;
  display: flex;
  align-items: stretch;
  transition: background var(--transition);
}

.keypad-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(5, 1fr);
  gap: var(--gap);
  width: 100%;
}

/* ── Buttons — Base ───────────────────────────────────────── */
.btn {
  border: none;
  cursor: pointer;
  font-family: var(--font-display);
  font-weight: 400;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  outline: none;
  transition: transform var(--transition),
              background var(--transition),
              box-shadow var(--transition),
              filter var(--transition);
  -webkit-tap-highlight-color: transparent;
  border-radius: var(--radius-btn);
  box-shadow: var(--shadow-btn);
}

/* Ripple effect */
.btn::after {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(255,255,255,0.15);
  border-radius: inherit;
  opacity: 0;
  transform: scale(0);
  transition: opacity 0.3s, transform 0.3s;
}
.btn:active::after {
  opacity: 1;
  transform: scale(1);
  transition: none;
}

.btn:focus-visible {
  outline: 3px solid #ff9f0a;
  outline-offset: 2px;
}

/* Press animation */
.btn:active {
  transform: scale(0.92);
}

/* ── Button Types ─────────────────────────────────────────── */
.btn-num {
  background: var(--bg-btn-num);
  color: var(--text-num);
  font-size: clamp(22px, 3.5vw, 32px);
  font-weight: 400;
}
.btn-num:hover { filter: brightness(1.1); }

.btn-func {
  background: var(--bg-btn-func);
  color: var(--text-func);
  font-size: clamp(18px, 2.8vw, 26px);
  font-weight: 500;
}
.btn-func:hover { filter: brightness(1.1); }

.btn-op {
  background: var(--bg-btn-op);
  color: var(--text-op);
  font-size: clamp(24px, 3.8vw, 36px);
  font-weight: 300;
  box-shadow: var(--shadow-btn), var(--glow-op);
}
.btn-op:hover { filter: brightness(1.1); }
.btn-op.active {
  background: var(--text-op);
  color: var(--bg-btn-op);
}

.btn-equals {
  background: var(--bg-btn-equals);
  color: #ffffff;
  font-size: clamp(24px, 3.8vw, 36px);
  font-weight: 300;
  box-shadow: var(--shadow-btn), var(--glow-op);
}
.btn-equals:hover { filter: brightness(1.1); }

.btn-sci {
  background: var(--bg-btn-sci);
  color: var(--text-sci);
  font-size: clamp(11px, 1.6vw, 15px);
  font-weight: 500;
  border-radius: 12px;
  height: var(--btn-size-sci);
  letter-spacing: -0.3px;
}
.btn-sci:hover { background: var(--bg-btn-sci-active); }
.btn-sci.inv-active {
  background: var(--bg-btn-inv);
  color: #ffffff;
}

/* Zero button spans 2 columns */
.btn-zero {
  grid-column: span 2;
  border-radius: 40px;
  justify-content: flex-start;
  padding-left: 28px;
}

/* ── Bottom Bar ───────────────────────────────────────────── */
.bottom-bar {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-around;
  padding: 8px 12px 12px;
  background: var(--bg-panel);
  border-top: 1px solid var(--border-color);
  gap: 8px;
  transition: background var(--transition);
}

.bottom-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 8px;
  border: none;
  background: var(--bg-btn-sci);
  color: var(--text-secondary);
  border-radius: 12px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
  font-family: var(--font-display);
}
.bottom-btn:hover { background: var(--bg-btn-sci-active); color: var(--text-primary); }
.bottom-btn:active { transform: scale(0.96); }
.bottom-btn.active {
  background: rgba(255,159,10,0.15);
  color: #ff9f0a;
}

/* ── Toast ────────────────────────────────────────────────── */
.toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--bg-btn-func);
  color: var(--text-primary);
  padding: 10px 20px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 500;
  opacity: 0;
  pointer-events: none;
  transition: all 0.3s ease;
  z-index: 9999;
  white-space: nowrap;
  box-shadow: var(--shadow-panel);
}
.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ── Animations ───────────────────────────────────────────── */
@keyframes popIn {
  0% { transform: scale(0.95); opacity: 0.7; }
  60% { transform: scale(1.03); }
  100% { transform: scale(1); opacity: 1; }
}

@keyframes slideInLeft {
  from { transform: translateX(-20px); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-6px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}

.shake { animation: shake 0.4s ease; }

/* ── Scrollbar ────────────────────────────────────────────── */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: var(--scrollbar-track); }
::-webkit-scrollbar-thumb { background: var(--scrollbar-thumb); border-radius: 2px; }

/* ── Responsive: Mobile ───────────────────────────────────── */
@media (max-width: 480px) {
  :root {
    --gap: 8px;
    --gap-sm: 6px;
    --btn-size-sci: 46px;
    --display-height: 120px;
  }

  .theme-toggle { top: 8px; right: 8px; width: 34px; height: 34px; font-size: 15px; }

  .display-section { padding: 12px 16px 6px; }
  .result-line { font-size: clamp(36px, 10vw, 56px); }
  .expression-line { font-size: 15px; }

  .keypad-section { padding: 8px; }
  .keypad-grid { gap: 8px; }

  .sci-grid { grid-template-columns: repeat(6, 1fr); gap: 5px; }
  .btn-sci { font-size: 10px; height: 42px; }

  .bottom-btn { font-size: 11px; padding: 8px 4px; }
  .bottom-btn svg { width: 14px; height: 14px; }

  .history-panel.open { width: 260px; min-width: 260px; }
}

/* ── Responsive: Tablet ───────────────────────────────────── */
@media (min-width: 481px) and (max-width: 900px) {
  :root {
    --gap: 10px;
    --btn-size-sci: 52px;
  }
  .history-panel.open { width: 280px; min-width: 280px; }
}

/* ── Responsive: Desktop ──────────────────────────────────── */
@media (min-width: 901px) {
  .app-container {
    max-width: 900px;
    max-height: 780px;
    margin: auto;
    border-radius: var(--radius-panel);
    box-shadow: var(--shadow-panel);
    overflow: hidden;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }

  body {
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-app);
  }

  .theme-toggle {
    top: calc(50% - 390px + 16px);
    right: calc(50% - 450px + 16px);
  }
}

/* ── Landscape on small screens ───────────────────────────── */
@media (max-height: 500px) and (orientation: landscape) {
  :root {
    --display-height: 80px;
    --gap: 6px;
    --btn-size-sci: 38px;
  }

  .display-section { padding: 8px 16px 4px; }
  .result-line { font-size: clamp(28px, 6vw, 42px); }
  .expression-line { font-size: 13px; min-height: 18px; }
  .display-wrapper { min-height: var(--display-height); }

  .scientific-section.open { max-height: 180px; padding: 6px 10px; }
  .btn-sci { height: 36px; font-size: 10px; }

  .keypad-section { padding: 6px; }
  .keypad-grid { gap: 6px; }

  .bottom-bar { padding: 4px 8px 6px; }
  .bottom-btn { padding: 6px 4px; font-size: 11px; }
}

/* ── High contrast / accessibility ───────────────────────── */
@media (prefers-contrast: high) {
  .btn { border: 1px solid var(--border-color); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

/* ── Loading state ────────────────────────────────────────── */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  background: var(--bg-app);
  z-index: 9998;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}

/* ── Operator active glow ─────────────────────────────────── */
.btn-op.active {
  box-shadow: var(--shadow-btn), 0 0 0 3px rgba(255,159,10,0.4);
}

/* ── Scientific button hover tooltip ─────────────────────── */
.btn-sci[title]:hover::before {
  content: attr(title);
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-btn-func);
  color: var(--text-primary);
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 6px;
  white-space: nowrap;
  pointer-events: none;
  z-index: 100;
  box-shadow: var(--shadow-btn);
}

/* ── History item hover arrow ─────────────────────────────── */
.history-item::after {
  content: '↩';
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  font-size: 14px;
  opacity: 0;
  transition: opacity var(--transition);
}
.history-item {
  position: relative;
  padding-right: 36px;
}
.history-item:hover::after { opacity: 1; }

/* ── Paren counter badge ──────────────────────────────────── */
.paren-badge {
  position: absolute;
  top: 8px;
  left: 16px;
  font-size: 12px;
  color: #ff9f0a;
  font-weight: 600;
  opacity: 0;
  transition: opacity var(--transition);
}
.paren-badge.visible { opacity: 1; }

/* ── Result copy flash ────────────────────────────────────── */
.copy-btn.copied {
  color: #30d158 !important;
}

/* ── Keyboard shortcut hint ───────────────────────────────── */
.kbd-hint {
  position: absolute;
  bottom: 2px;
  right: 4px;
  font-size: 8px;
  color: var(--text-muted);
  pointer-events: none;
  opacity: 0.6;
}

/* ── Number formatting ────────────────────────────────────── */
.result-line {
  font-variant-numeric: tabular-nums;
}

/* ── Smooth panel transitions ─────────────────────────────── */
.calculator-panel {
  transition: all 0.35s cubic-bezier(0.4, 0, 0.2, 1);
}

/* ── Focus ring for accessibility ────────────────────────── */
.btn:focus-visible,
.bottom-btn:focus-visible,
.history-item:focus-visible {
  outline: 2px solid #ff9f0a;
  outline-offset: 2px;
}

/* ── Angle mode indicator ─────────────────────────────────── */
.angle-mode-btn {
  font-size: 11px !important;
  letter-spacing: 0.5px;
}
.angle-mode-btn.rad-active {
  background: rgba(255, 159, 10, 0.2) !important;
  color: #ff9f0a !important;
}