/* =====================================================
   Shared Note App - Styles
   ===================================================== */

/* -----------------------------------------------------
   CSS Variables / Theme
   ----------------------------------------------------- */
:root {
  --bg-deep: #0a0a0f;
  --bg-surface: #12121a;
  --bg-elevated: #1a1a24;
  --border: #2a2a3a;
  --text-primary: #e8e8ed;
  --text-muted: #6a6a7a;
  --accent: #4ade80;
  --accent-dim: #22543d;
  --warning: #f59e0b;
  --danger: #ef4444;
}

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

html, body {
  height: 100%;
  height: 100dvh;
}

body {
  font-family: 'Outfit', sans-serif;
  background: var(--bg-deep);
  color: var(--text-primary);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* -----------------------------------------------------
   Login Screen
   ----------------------------------------------------- */
.login-screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-deep);
  z-index: 100;
}

.login-screen.hidden {
  display: none;
}

.login-box {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 48px 40px;
  width: 100%;
  max-width: 380px;
  text-align: center;
}

.login-box h1 {
  font-size: 1.5rem;
  font-weight: 600;
  margin-bottom: 8px;
  color: var(--text-primary);
}

.login-box p {
  color: var(--text-muted);
  font-size: 0.9rem;
  margin-bottom: 32px;
}

.login-box input {
  width: 100%;
  padding: 14px 18px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text-primary);
  font-family: 'JetBrains Mono', monospace;
  font-size: 1rem;
  outline: none;
  transition: border-color 0.2s;
}

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

.login-box input::placeholder {
  color: var(--text-muted);
}

.login-box button {
  width: 100%;
  padding: 14px;
  margin-top: 16px;
  background: var(--accent);
  color: var(--bg-deep);
  border: none;
  border-radius: 10px;
  font-family: 'Outfit', sans-serif;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.2s;
}

.login-box button:hover {
  opacity: 0.9;
}

.login-error {
  color: #ef4444;
  font-size: 0.85rem;
  margin-top: 16px;
  display: none;
}

.login-error.show {
  display: block;
}

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

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

.logo {
  width: 32px;
  height: 32px;
  background: linear-gradient(135deg, var(--accent) 0%, #22c55e 100%);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  color: var(--bg-deep);
}

.title {
  font-size: 1.1rem;
  font-weight: 600;
}

.header-center {
  display: flex;
  gap: 4px;
  background: var(--bg-elevated);
  padding: 4px;
  border-radius: 8px;
}

.view-btn {
  padding: 6px 12px;
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-family: 'Outfit', sans-serif;
  font-size: 0.8rem;
  cursor: pointer;
  border-radius: 6px;
  transition: all 0.2s;
}

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

.view-btn:hover:not(.active) {
  color: var(--text-primary);
}

.status {
  display: flex;
  align-items: center;
  gap: 16px;
}

.user-count {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--text-muted);
  font-size: 0.85rem;
}

.user-count .dot {
  width: 8px;
  height: 8px;
  background: var(--accent);
  border-radius: 50%;
  animation: pulse 2s infinite;
}

.save-status {
  font-size: 0.8rem;
  color: var(--text-muted);
  padding: 6px 12px;
  background: var(--bg-elevated);
  border-radius: 6px;
}

.save-status.saving {
  color: var(--warning);
}

.save-status.saved {
  color: var(--accent);
}

/* -----------------------------------------------------
   Active Users & Cursors
   ----------------------------------------------------- */
.active-users {
  display: flex;
  align-items: center;
  gap: -8px;
}

.user-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--bg-deep);
  border: 2px solid var(--bg-surface);
  margin-left: -8px;
  cursor: default;
  position: relative;
}

.user-avatar:first-child {
  margin-left: 0;
}

.user-avatar:hover::after {
  content: attr(data-nickname);
  position: absolute;
  bottom: -28px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-elevated);
  color: var(--text-primary);
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 0.7rem;
  white-space: nowrap;
  z-index: 100;
  border: 1px solid var(--border);
}

.remote-cursor {
  position: absolute;
  width: 2px;
  pointer-events: none;
  z-index: 10;
}

.remote-cursor-line {
  width: 2px;
  height: 1.2em;
}

.remote-cursor-label {
  position: absolute;
  top: -18px;
  left: 0;
  padding: 2px 6px;
  border-radius: 3px;
  font-size: 0.65rem;
  font-family: 'Outfit', sans-serif;
  font-weight: 500;
  white-space: nowrap;
  color: var(--bg-deep);
}

.remote-selection {
  position: absolute;
  opacity: 0.3;
  pointer-events: none;
  z-index: 5;
}

.self-cursor .remote-cursor-label {
  opacity: 0.8;
  font-style: italic;
}

/* -----------------------------------------------------
   Tab Bar
   ----------------------------------------------------- */
.tab-bar {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 16px;
  background: var(--bg-deep);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.tab-bar-spacer {
  flex: 1 1 auto;
  min-width: 8px;
}

.tab-bar::-webkit-scrollbar {
  height: 4px;
}

.tab-bar::-webkit-scrollbar-track {
  background: var(--bg-deep);
}

.tab-bar::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 2px;
}

.tab {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-muted);
  font-family: 'Outfit', sans-serif;
  font-size: 0.85rem;
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
  flex-shrink: 0;
}

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

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

.tab-name {
  max-width: 150px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tab-close {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  border-radius: 4px;
  font-size: 14px;
  line-height: 1;
  opacity: 0.5;
  transition: all 0.2s;
}

.tab-close:hover {
  opacity: 1;
  background: var(--danger);
  color: white;
}

.tab-new {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  background: transparent;
  border: 1px dashed var(--border);
  border-radius: 8px;
  color: var(--text-muted);
  font-size: 18px;
  cursor: pointer;
  transition: all 0.2s;
  flex-shrink: 0;
}

.tab-new:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-dim);
}

.tab-export {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 12px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-muted);
  font-family: 'Outfit', sans-serif;
  font-size: 0.8rem;
  cursor: pointer;
  transition: all 0.2s;
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

.tab-export:hover,
.tab-export:active {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-dim);
}

.tab-export svg {
  flex-shrink: 0;
  pointer-events: none;
}

.tab-export span {
  pointer-events: none;
}

/* -----------------------------------------------------
   Editor Container & Split Pane
   ----------------------------------------------------- */
.editor-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 16px;
  gap: 12px;
  min-height: 0;
  overflow: hidden;
}

.editor-container.hidden {
  display: none;
}

.split-pane {
  flex: 1;
  display: flex;
  gap: 0;
  min-height: 0;
}

.pane {
  position: relative;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  min-height: 0;
  min-width: 100px;
}

.pane.editor {
  flex: 1 1 50%;
}

.pane.preview {
  flex: 1 1 50%;
}

.pane.hidden {
  display: none;
}

/* Resizer */
.resizer {
  flex-shrink: 0;
  width: 16px;
  cursor: col-resize;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

.resizer::before {
  content: '';
  width: 4px;
  height: 48px;
  background: var(--border);
  border-radius: 2px;
  transition: background 0.2s, height 0.2s;
}

.resizer:hover::before,
.resizer.dragging::before {
  background: var(--accent);
  height: 64px;
}

.resizer.dragging {
  cursor: col-resize;
}

/* View modes */
.split-pane.edit-only .pane.preview,
.split-pane.edit-only .resizer {
  display: none;
}

.split-pane.edit-only .pane.editor {
  flex: 1 1 100%;
}

.split-pane.preview-only .pane.editor,
.split-pane.preview-only .resizer {
  display: none;
}

.split-pane.preview-only .pane.preview {
  flex: 1 1 100%;
}

/* -----------------------------------------------------
   Editor & Preview
   ----------------------------------------------------- */
#editor {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  padding: 20px;
  background: transparent;
  border: none;
  color: var(--text-primary);
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.9rem;
  line-height: 1.7;
  resize: none;
  outline: none;
  -webkit-overflow-scrolling: touch;
}

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

#preview {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 20px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

/* -----------------------------------------------------
   Markdown Styles
   ----------------------------------------------------- */
.markdown-body {
  font-family: 'Source Serif 4', Georgia, serif;
  font-size: 1rem;
  line-height: 1.8;
  color: var(--text-primary);
}

.markdown-body h1, .markdown-body h2, .markdown-body h3,
.markdown-body h4, .markdown-body h5, .markdown-body h6 {
  font-family: 'Outfit', sans-serif;
  font-weight: 600;
  margin: 1.5em 0 0.5em;
  line-height: 1.3;
}

.markdown-body h1 { font-size: 2em; border-bottom: 1px solid var(--border); padding-bottom: 0.3em; }
.markdown-body h2 { font-size: 1.5em; border-bottom: 1px solid var(--border); padding-bottom: 0.3em; }
.markdown-body h3 { font-size: 1.25em; }
.markdown-body h4 { font-size: 1em; }

.markdown-body p { margin: 1em 0; }

.markdown-body a {
  color: var(--accent);
  text-decoration: none;
}

.markdown-body a:hover {
  text-decoration: underline;
}

.markdown-body code {
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.85em;
  background: var(--bg-elevated);
  padding: 0.2em 0.4em;
  border-radius: 4px;
}

.markdown-body pre {
  background: #0d1117;
  padding: 16px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 1em 0;
  border: 1px solid var(--border);
}

.markdown-body pre code {
  background: none;
  padding: 0;
  font-size: 0.9em;
  line-height: 1.6;
}

.markdown-body pre code.hljs {
  background: transparent;
  padding: 0;
}

.markdown-body blockquote {
  border-left: 4px solid var(--accent);
  margin: 1em 0;
  padding-left: 1em;
  color: var(--text-muted);
  font-style: italic;
}

.markdown-body ul, .markdown-body ol {
  margin: 1em 0;
  padding-left: 2em;
}

.markdown-body li { margin: 0.5em 0; }

.markdown-body hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 2em 0;
}

.markdown-body table {
  width: 100%;
  border-collapse: collapse;
  margin: 1em 0;
}

.markdown-body th, .markdown-body td {
  border: 1px solid var(--border);
  padding: 8px 12px;
  text-align: left;
}

.markdown-body th {
  background: var(--bg-elevated);
  font-weight: 600;
}

.markdown-body img {
  max-width: 100%;
  border-radius: 8px;
}

/* -----------------------------------------------------
   Runnable Code Blocks (Python)
   ----------------------------------------------------- */
.code-block-wrapper {
  margin: 1em 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}

.code-block-wrapper pre {
  margin: 0;
  border: none;
  border-radius: 0;
}

.code-block-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  background: #161b22;
  border-bottom: 1px solid var(--border);
}

.code-lang {
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.7rem;
  font-weight: 500;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 1.5px;
  opacity: 0.7;
}

.run-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  background: var(--accent);
  color: var(--bg-deep);
  border: none;
  border-radius: 6px;
  font-family: 'Outfit', sans-serif;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.run-btn:hover { opacity: 0.9; }
.run-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.run-btn.running { background: var(--warning); }
.run-btn.running .run-icon { animation: spin 1s linear infinite; }
.run-icon { font-size: 0.7rem; }

.code-output {
  background: #0d1117;
  border-top: 1px solid var(--border);
}

.code-output.hidden { display: none; }

.output-header {
  padding: 8px 12px;
  background: #161b22;
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  border-bottom: 1px solid var(--border);
}

.output-content {
  margin: 0;
  padding: 12px 16px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.85rem;
  line-height: 1.5;
  color: var(--text-primary);
  white-space: pre-wrap;
  word-break: break-word;
  background: transparent;
  border: none;
  border-radius: 0;
}

.output-content.error { color: var(--danger); }

.output-duration {
  padding: 4px 12px 8px;
  font-size: 0.7rem;
  color: var(--text-muted);
}

.output-image {
  padding: 12px;
  display: flex;
  justify-content: center;
  background: #0d1117;
}

.output-image img {
  max-width: 100%;
  height: auto;
  border-radius: 4px;
}

/* -----------------------------------------------------
   KaTeX Overrides
   ----------------------------------------------------- */
.katex-display {
  margin: 1em 0;
  overflow-x: auto;
  overflow-y: hidden;
}

.katex-error {
  color: #ef4444;
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.85em;
}

/* -----------------------------------------------------
   Footer
   ----------------------------------------------------- */
.footer-hint {
  flex-shrink: 0;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.75rem;
  padding-bottom: 4px;
}

/* -----------------------------------------------------
   Overlays & Modals
   ----------------------------------------------------- */
.connection-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 10, 15, 0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}

.connection-overlay.hidden { display: none; }

.connection-message {
  text-align: center;
  color: var(--text-muted);
}

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto 16px;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 10, 15, 0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}

.modal-overlay.hidden { display: none; }

.modal {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 24px;
  width: 100%;
  max-width: 340px;
}

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

.modal input {
  width: 100%;
  padding: 12px 14px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-primary);
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.9rem;
  outline: none;
}

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

.modal-buttons {
  display: flex;
  gap: 8px;
  margin-top: 16px;
}

.modal-buttons button {
  flex: 1;
  padding: 10px;
  border: none;
  border-radius: 8px;
  font-family: 'Outfit', sans-serif;
  font-size: 0.9rem;
  cursor: pointer;
  transition: opacity 0.2s;
}

.modal-buttons button:hover { opacity: 0.9; }
.modal-buttons .btn-cancel { background: var(--bg-elevated); color: var(--text-muted); }
.modal-buttons .btn-confirm { background: var(--accent); color: var(--bg-deep); font-weight: 600; }
.modal-buttons .btn-danger { background: var(--danger); color: white; font-weight: 600; }

/* -----------------------------------------------------
   Animations
   ----------------------------------------------------- */
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

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

/* -----------------------------------------------------
   Mobile Responsive
   ----------------------------------------------------- */
@media (max-width: 768px) {
  .split-pane {
    flex-direction: column;
  }

  .split-pane.split .pane {
    flex: 1 1 50% !important;
  }

  .resizer {
    width: 100%;
    height: 16px;
    cursor: row-resize;
  }

  .resizer::before {
    width: 48px;
    height: 4px;
  }

  .resizer:hover::before,
  .resizer.dragging::before {
    width: 64px;
    height: 4px;
  }

  .title {
    display: none;
  }

  .header-center {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
  }

  .tab-bar {
    padding: 8px 12px;
  }

  .tab {
    padding: 6px 10px;
    font-size: 0.8rem;
  }

  .tab-name {
    max-width: 100px;
  }
}

