/* Cosmos — Graphing Calculator
   Professional, refined, scientific aesthetic */

:root {
  --bg-deep: #0a0e14;
  --bg-panel: #12171f;
  --bg-elevated: #1a212c;
  --border: rgba(255,255,255,0.08);
  --text: #e6edf3;
  --text-muted: #8b949e;
  --accent: #58a6ff;
  --accent-dim: rgba(88, 166, 255, 0.5);
  --success: #3fb950;
  --display-bg: #0d1117;
  --btn-bg: #21262d;
  --btn-hover: #30363d;
  --graph-1: #58a6ff;
  --graph-2: #f85149;
  --graph-3: #3fb950;
  --graph-4: #a371f7;
  --radius: 6px;
  --font-mono: 'IBM Plex Mono', 'SF Mono', Consolas, monospace;
  --font-sans: 'DM Sans', -apple-system, BlinkMacSystemFont, sans-serif;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font-sans);
  background: var(--bg-deep);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
}

.app {
  max-width: 1200px;
  margin: 0 auto;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* Header */
.header {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 16px 24px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
}

.logo {
  font-family: var(--font-mono);
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--accent);
  letter-spacing: 0.02em;
}

.mode-tabs {
  display: flex;
  gap: 4px;
}

.tab {
  padding: 8px 16px;
  font-family: var(--font-sans);
  font-size: 0.875rem;
  font-weight: 500;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius);
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.15s;
}

.tab:hover {
  color: var(--text);
}

.tab.active {
  background: var(--bg-elevated);
  color: var(--accent);
  border-color: var(--border);
}

.header-controls {
  margin-left: auto;
}

.mode-btn {
  padding: 6px 12px;
  font-family: var(--font-mono);
  font-size: 0.75rem;
  background: var(--btn-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  cursor: pointer;
}

.mode-btn:hover {
  background: var(--btn-hover);
}

/* Mode panels */
.mode-panel {
  display: none;
  flex: 1;
  padding: 24px;
  flex-direction: column;
  gap: 20px;
}

.mode-panel.active {
  display: flex;
}

/* Calculator display */
.display-panel {
  background: var(--display-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px 24px;
  min-height: 88px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: flex-end;
}

.ans-indicator {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  color: var(--text-muted);
  min-height: 16px;
  width: 100%;
  text-align: right;
}

.expr-display {
  font-family: var(--font-mono);
  font-size: 0.9rem;
  color: var(--text-muted);
  min-height: 24px;
  width: 100%;
  text-align: right;
}

.result-display {
  font-family: var(--font-mono);
  font-size: 1.75rem;
  font-weight: 500;
  color: var(--text);
  margin-top: 4px;
}

/* Keypad */
.keypad {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
}

.btn {
  padding: 14px 10px;
  font-family: var(--font-mono);
  font-size: 0.9rem;
  font-weight: 500;
  background: var(--btn-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  cursor: pointer;
  transition: all 0.12s;
}

.btn:hover {
  background: var(--btn-hover);
  border-color: rgba(255,255,255,0.15);
}

.btn:active {
  transform: scale(0.98);
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--bg-deep);
}

.btn.primary:hover {
  background: #79b8ff;
}

.btn.small {
  padding: 8px 12px;
  font-size: 0.8rem;
}

.btn-num { font-size: 1rem; }
.btn-op { color: var(--accent); }
.btn-sci { color: var(--success); font-size: 0.8rem; }
.btn-const { color: #f0b429; }

/* Graph layout */
.graph-layout {
  display: grid;
  grid-template-columns: 1fr 260px;
  gap: 20px;
  flex: 1;
  min-height: 460px;
}

@media (max-width: 768px) {
  .graph-layout {
    grid-template-columns: 1fr;
    min-height: 400px;
  }
}

.graph-section {
  position: relative;
  background: var(--display-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  min-height: 420px;
  display: flex;
  flex-direction: column;
}

#graphCanvas {
  width: 100%;
  flex: 1;
  min-height: 380px;
  display: block;
  cursor: crosshair;
  outline: none;
}

#graphCanvas:focus, #graphCanvas.graph-focused {
  box-shadow: inset 0 0 0 2px var(--accent);
}

.trace-tooltip {
  position: fixed;
  background: rgba(10, 14, 20, 0.97);
  color: var(--accent);
  padding: 12px 16px;
  border-radius: 8px;
  font-family: var(--font-mono);
  font-size: 14px;
  font-weight: 500;
  pointer-events: none;
  z-index: 100;
  border: 1px solid var(--accent);
  box-shadow: 0 4px 20px rgba(0,0,0,0.4);
  line-height: 1.6;
  white-space: pre;
}

.trace-tooltip.hidden { display: none; }

.graph-hint {
  position: absolute;
  bottom: 12px;
  left: 12px;
  font-size: 10px;
  color: var(--text-muted);
  pointer-events: none;
}

.coord-display {
  position: absolute;
  bottom: 12px;
  right: 16px;
  font-family: var(--font-mono);
  font-size: 13px;
  font-weight: 500;
  color: var(--accent);
  background: rgba(10, 14, 20, 0.9);
  padding: 8px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
}

.graph-sidebar {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow-y: auto;
  max-height: 100%;
}

.function-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 320px;
  overflow-y: auto;
}

.graph-sidebar h3,
.stats-tab-panel h3,
.stats-output h3,
.stats-plot h3 {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.function-row { display: flex; align-items: center; gap: 8px; }
.func-label { font-family: var(--font-mono); font-size: 0.85rem; min-width: 28px; color: var(--accent); }

.func-input {
  flex: 1;
  padding: 8px 12px;
  font-family: var(--font-mono);
  font-size: 0.85rem;
  background: var(--display-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  outline: none;
}

.func-input:focus { border-color: var(--accent); }

.graph-controls { display: flex; gap: 8px; }

/* Statistics */
.stats-layout {
  display: grid;
  grid-template-columns: 1fr 320px;
  grid-template-rows: 1fr auto;
  gap: 20px;
  flex: 1;
  min-height: 0;
}

@media (max-width: 900px) {
  .stats-layout {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }
}

.stats-left {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
}

.stats-tabs {
  display: flex;
  gap: 4px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 12px;
  margin-bottom: 4px;
}

.stats-tab {
  padding: 8px 16px;
  font-size: 0.8rem;
  font-weight: 500;
  background: transparent;
  border: none;
  border-radius: var(--radius);
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.15s;
}

.stats-tab:hover { color: var(--text); }

.stats-tab.active {
  background: var(--bg-elevated);
  color: var(--accent);
}

.stats-tab-panels { flex: 1; min-height: 0; overflow-y: auto; }

.stats-tab-panel {
  display: none;
  flex-direction: column;
  gap: 12px;
}

.stats-tab-panel.active { display: flex; }

.stats-tab-panel h3 {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 4px;
}

.panel-desc {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin: -4px 0 8px;
}

.stats-actions-grid {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.stat-action-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.stat-action-row select {
  padding: 6px 10px;
  font-family: var(--font-mono);
  font-size: 0.8rem;
  background: var(--display-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  min-width: 52px;
}

.input-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.input-row input {
  padding: 8px 12px;
  font-family: var(--font-mono);
  font-size: 0.85rem;
  background: var(--display-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  width: 70px;
}

.formulas-details { margin-top: 8px; }
.formulas-details summary {
  cursor: pointer;
  font-size: 0.75rem;
  color: var(--accent);
  user-select: none;
}

.formulas-ref {
  font-size: 0.75rem;
  font-family: var(--font-mono);
  color: var(--text-muted);
  line-height: 1.5;
  margin-top: 8px;
}

.formulas-ref p { margin: 4px 0; }

.list-editor { margin: 8px 0; }
.list-editor.list-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

@media (max-width: 900px) {
  .list-editor.list-grid { grid-template-columns: repeat(2, 1fr); }
}

.list-col {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.list-col label {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  color: var(--text-muted);
}

.list-col textarea {
  padding: 10px;
  font-family: var(--font-mono);
  font-size: 0.8rem;
  background: var(--display-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  resize: vertical;
  outline: none;
  min-height: 56px;
}

.list-col textarea:focus { border-color: var(--accent); }

.stats-output {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  grid-column: 2;
  grid-row: 1;
}

@media (max-width: 900px) {
  .stats-output { grid-column: 1; grid-row: 2; }
}

.stats-output h3,
.stats-plot h3 {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 8px;
}

.stats-results {
  font-family: var(--font-mono);
  font-size: 0.8rem;
  color: var(--text);
  white-space: pre-wrap;
  word-break: break-all;
  line-height: 1.6;
  max-height: 180px;
  overflow-y: auto;
}

.stats-plot {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  grid-column: 2;
  grid-row: 2;
}

@media (max-width: 900px) {
  .stats-plot { grid-column: 1; grid-row: 3; }
}

.plot-type { display: flex; gap: 8px; margin-bottom: 12px; }
.plot-type .btn.active { background: var(--accent); color: var(--bg-deep); border-color: var(--accent); }

#statsCanvas {
  width: 100%;
  height: 260px;
  background: var(--display-bg);
  border-radius: var(--radius);
  display: block;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  backdrop-filter: blur(4px);
}

.modal.hidden { display: none; }

.modal-content {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 24px;
  width: 90%;
  max-width: 360px;
}

.modal-content h2 { font-size: 1rem; margin-bottom: 16px; }

.window-inputs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 20px;
}

.window-inputs label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.window-inputs input {
  padding: 8px 12px;
  font-family: var(--font-mono);
  background: var(--display-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
}

.modal-actions { display: flex; gap: 8px; }
