/* SketchMath v2 — Dark-first, polished UI */

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

:root {
  /* Dark theme (default) */
  --bg-primary: #0f1117;
  --bg-secondary: #1a1d27;
  --bg-tertiary: #242734;
  --bg-hover: #2d3142;
  --text-primary: #e4e6ed;
  --text-secondary: #9ba1b0;
  --text-muted: #6b7280;
  --accent: #6c8cff;
  --accent-hover: #8ba4ff;
  --accent-dim: rgba(108, 140, 255, 0.15);
  --border: #2d3142;
  --border-light: #363a4d;
  --success: #34d399;
  --warning: #fbbf24;
  --error: #f87171;
  --shadow: 0 2px 8px rgba(0,0,0,0.3);
  --radius: 8px;
  --radius-sm: 4px;
  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'JetBrains Mono', 'Fira Code', 'Consolas', monospace;
  --transition: 150ms ease;
}

[data-theme="light"] {
  --bg-primary: #ffffff;
  --bg-secondary: #f5f6f8;
  --bg-tertiary: #ebedf2;
  --bg-hover: #e2e4ea;
  --text-primary: #1a1d27;
  --text-secondary: #4b5563;
  --text-muted: #9ba1b0;
  --accent: #4f6dd9;
  --accent-hover: #3b57c4;
  --accent-dim: rgba(79, 109, 217, 0.1);
  --border: #d1d5db;
  --border-light: #e5e7eb;
  --shadow: 0 2px 8px rgba(0,0,0,0.08);
}

html, body {
  height: 100%;
  font-family: var(--font);
  background: var(--bg-primary);
  color: var(--text-primary);
  overflow: hidden;
  transition: background var(--transition), color var(--transition);
}

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh; /* dvh accounts for mobile browser chrome */
}

/* === Sidebar === */
#sidebar {
  position: fixed;
  top: 48px;
  left: 0;
  width: 280px;
  height: calc(100vh - 48px);
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  z-index: 200;
  transform: translateX(-100%);
  transition: transform 250ms ease;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#sidebar.open {
  transform: translateX(0);
}

#sidebar-overlay {
  position: fixed;
  top: 48px;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  z-index: 199;
  opacity: 0;
  pointer-events: none;
  transition: opacity 250ms ease;
}

#sidebar-overlay.open {
  opacity: 1;
  pointer-events: auto;
}

.sidebar-header {
  padding: 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.btn-new-chat {
  width: 100%;
  padding: 10px 16px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--transition);
}

.btn-new-chat:hover {
  background: var(--accent-hover);
}

#session-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
}

#session-list::-webkit-scrollbar {
  width: 4px;
}

#session-list::-webkit-scrollbar-thumb {
  background: var(--border-light);
  border-radius: 2px;
}

.session-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  margin: 2px 8px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--transition);
}

.session-item:hover {
  background: var(--bg-hover);
}

.session-item.active {
  background: var(--accent-dim);
  border-left: 3px solid var(--accent);
}

.session-info {
  flex: 1;
  min-width: 0;
  overflow: hidden;
}

.session-topic {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-date {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
}

.btn-delete-session {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 14px;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: var(--radius-sm);
  opacity: 0;
  transition: all var(--transition);
  flex-shrink: 0;
  margin-left: 4px;
}

.session-item:hover .btn-delete-session {
  opacity: 1;
}

.btn-delete-session:hover {
  color: var(--error);
  background: rgba(248, 113, 113, 0.1);
}

/* === Header === */
#header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  height: 48px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.header-left h1 {
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.3px;
}

.header-left .version {
  font-size: 11px;
  font-weight: 500;
  color: var(--accent);
  background: var(--accent-dim);
  padding: 1px 6px;
  border-radius: 4px;
  margin-left: 6px;
  vertical-align: middle;
}

.header-right {
  display: flex;
  gap: 6px;
}

/* === Buttons === */
.btn-icon {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-secondary);
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition);
}

.btn-icon:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
  border-color: var(--border-light);
}

.btn-tool {
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  padding: 4px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 11px;
  font-weight: 500;
  transition: all var(--transition);
}

.btn-tool:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.btn-primary {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 8px 20px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  transition: all var(--transition);
  flex-shrink: 0;
}

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

.btn-primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* === Main Layout — single-column chat === */
#main {
  flex: 1;
  display: flex;
  overflow: hidden;
}

#chat-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg-primary);
  min-width: 0;
}

#chat-messages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch; /* momentum scrolling on iOS */
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-width: 720px;
  width: 100%;
  margin: 0 auto;
}

/* Scrollbar styling */
#chat-messages::-webkit-scrollbar {
  width: 6px;
}

#chat-messages::-webkit-scrollbar-track {
  background: transparent;
}

#chat-messages::-webkit-scrollbar-thumb {
  background: var(--border-light);
  border-radius: 3px;
}

/* === Messages === */
.message {
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 13px;
  line-height: 1.55;
  max-width: 100%;
  word-wrap: break-word;
}

.message.system {
  background: var(--accent-dim);
  color: var(--accent);
  font-size: 13px;
}

.message.user {
  background: var(--accent);
  color: #fff;
  align-self: flex-end;
  border-bottom-right-radius: 2px;
  max-width: 85%;
}

.message.assistant {
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border-bottom-left-radius: 2px;
}

.message.error {
  background: rgba(248, 113, 113, 0.1);
  color: var(--error);
  border: 1px solid rgba(248, 113, 113, 0.2);
}
.message.error .btn-retry {
  display: inline-block;
  margin-top: 8px;
  padding: 6px 16px;
  font-size: 13px;
  color: var(--error);
  background: rgba(248, 113, 113, 0.1);
  border: 1px solid rgba(248, 113, 113, 0.3);
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.message.error .btn-retry:hover {
  background: rgba(248, 113, 113, 0.2);
  border-color: rgba(248, 113, 113, 0.5);
}

.message.status {
  background: transparent;
  color: var(--text-muted);
  font-size: 12px;
  padding: 4px 0;
  text-align: center;
}

.message .msg-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 4px;
  opacity: 0.7;
}

.message .msg-commands {
  margin-top: 8px;
  padding: 8px 10px;
  background: rgba(0,0,0,0.15);
  border-radius: var(--radius-sm);
  font-family: var(--font-mono);
  font-size: 11px;
  line-height: 1.6;
  color: var(--text-secondary);
  white-space: pre-wrap;
  max-height: 150px;
  overflow-y: auto;
}

/* === Message Tabs === */
.msg-tabs {
  margin-top: 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.tab-header {
  display: flex;
  align-items: center;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  padding: 0 4px;
}

.tab-btn {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  padding: 8px 14px;
  font-size: 12px;
  font-weight: 500;
  font-family: var(--font);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: all var(--transition);
}

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

.tab-btn.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

.btn-copy-code {
  margin-left: auto;
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 11px;
  font-family: var(--font);
  cursor: pointer;
  padding: 6px 10px;
  transition: color var(--transition);
}

.btn-copy-code:hover {
  color: var(--text-primary);
}

.tab-content {
  display: none;
}

.tab-content.active {
  display: block;
}

/* Override msg-commands inside tabs */
.tab-content .msg-commands {
  margin-top: 0;
  border-radius: 0;
  border: none;
  max-height: 300px;
}

/* Diagram inside tab */
.tab-content .inline-diagram {
  margin-top: 0;
  border: none;
  border-radius: 0;
}

[data-theme="light"] .message .msg-commands {
  background: rgba(0,0,0,0.04);
}

/* === Inline Diagram (in chat) === */
.inline-diagram {
  margin-top: 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: #fff;
}

.inline-diagram img {
  display: block;
  width: 100%;
  max-width: 560px;
  height: auto;
  background: #fff;
}

.diagram-toolbar {
  display: flex;
  gap: 6px;
  padding: 6px 8px;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
}

/* === Chat Input === */
#chat-input-area {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  background: var(--bg-secondary);
  flex-shrink: 0;
  max-width: 720px;
  width: 100%;
  margin: 0 auto;
}

#input-row {
  display: flex;
  gap: 8px;
  align-items: flex-end;
}

#input-prompt {
  flex: 1;
  resize: none;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  color: var(--text-primary);
  padding: 8px 12px;
  border-radius: var(--radius);
  font-family: var(--font);
  font-size: 13px;
  line-height: 1.4;
  transition: border-color var(--transition);
}

#input-prompt:focus {
  outline: none;
  border-color: var(--accent);
}

#input-prompt::placeholder {
  color: var(--text-muted);
}

/* === Status Bar === */
#status-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  height: 28px;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  font-size: 11px;
  color: var(--text-muted);
  flex-shrink: 0;
}

/* === Hidden GeoGebra Container (off-screen rendering) === */
#ggb-hidden {
  position: absolute;
  left: -9999px;
  top: -9999px;
  width: 800px;
  height: 600px;
  overflow: hidden;
  pointer-events: none;
}

#ggb-element {
  width: 100%;
  height: 100%;
}

/* GeoGebra iframe override */
#ggb-element > div,
#ggb-element > div > article,
#ggb-element iframe {
  width: 100% !important;
  height: 100% !important;
}

.loading-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: var(--bg-primary);
  color: var(--text-muted);
  font-size: 14px;
  z-index: 10;
  transition: opacity 300ms ease;
}

.loading-overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

.spinner {
  width: 28px;
  height: 28px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* === GeoGebra Interactive Modal === */
#ggb-modal {
  position: fixed;
  inset: 0;
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
}

#ggb-modal .modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.6);
}

#ggb-modal .modal-content {
  position: relative;
  width: 90vw;
  max-width: 900px;
  height: 80vh;
  max-height: 700px;
  background: var(--bg-secondary);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 8px 32px rgba(0,0,0,0.4);
}

#ggb-modal .modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
  font-weight: 600;
  flex-shrink: 0;
}

#ggb-modal-body {
  flex: 1;
  position: relative;
  overflow: hidden;
}

/* When ggb-element is inside the modal, make it fill the modal body */
#ggb-modal-body #ggb-element {
  width: 100%;
  height: 100%;
}

/* === Responsive === */
@media (max-width: 800px) {
  #chat-messages {
    max-width: 100%;
    padding: 12px;
  }

  #chat-input-area {
    max-width: 100%;
    padding: 10px 12px;
  }

  .message.user {
    max-width: 92%;
  }

  #sidebar {
    width: 260px;
  }

  .inline-diagram img {
    max-width: 100%;
  }

  .diagram-toolbar {
    flex-wrap: wrap;
  }

  #header h1 {
    font-size: 15px;
  }
}

/* === Utilities === */
.hidden { display: none !important; }

/* Pulse animation for thinking indicator */
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

.thinking-dots {
  display: inline-flex;
  gap: 4px;
}

.thinking-dots span {
  width: 6px;
  height: 6px;
  background: var(--accent);
  border-radius: 50%;
  animation: pulse 1.2s infinite;
}

.thinking-dots span:nth-child(2) { animation-delay: 0.2s; }
.thinking-dots span:nth-child(3) { animation-delay: 0.4s; }

/* === Send Icon Button === */
.btn-send-icon {
  background: var(--accent);
  color: #fff;
  border: none;
  width: 36px;
  height: 36px;
  border-radius: var(--radius);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background var(--transition), opacity var(--transition);
}

.btn-send-icon:hover {
  background: var(--accent-hover);
}

.btn-send-icon:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* === Empty State === */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  padding: 32px 16px;
  text-align: center;
  min-height: 300px;
}

.empty-state-title {
  font-size: 28px;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 8px;
  letter-spacing: -0.5px;
}

.empty-state-subtitle {
  font-size: 14px;
  color: var(--text-muted);
  margin-bottom: 28px;
}

/* === Suggestion Chips === */
.suggestion-chips {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  max-width: 480px;
  width: 100%;
}

.suggestion-chip {
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 13px;
  font-family: var(--font);
  line-height: 1.4;
  cursor: pointer;
  text-align: left;
  transition: all var(--transition);
}

.suggestion-chip:hover {
  background: var(--bg-hover);
  border-color: var(--accent);
  color: var(--text-primary);
}

@media (max-width: 800px) {
  .suggestion-chips {
    grid-template-columns: 1fr;
  }

  .empty-state-title {
    font-size: 22px;
  }
}
