:root {
  /* Force light theme for consistent contrast on mobile */
  color-scheme: light;
  --bg: #f5f7fb;
  --card-bg: #ffffff;
  --border: #dcdfe6;
  --text: #1f2933;
  --muted: #6b7280;
  --primary: #2563eb;
  --primary-hover: #1e40af;
  --danger: #dc2626;
  --danger-hover: #991b1b;
  --ghost: #e5e7eb;
  --editor-font-size: 15px;
  font-family: 'Segoe UI', system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

#app {
  min-height: 100vh;
  display: flex;
  align-items: stretch;
  justify-content: center;
}

.card {
  background: var(--card-bg);
  padding: 2.5rem;
  box-shadow: 0 20px 45px rgba(0, 0, 0, 0.08);
  border-radius: 1rem;
  margin: 3rem auto;
  width: min(420px, 90vw);
}

h1,
h2,
h3 {
  margin: 0;
}

label {
  display: block;
  font-weight: 600;
  margin-top: 1.5rem;
}

input[type='text'],
input[type='password'],
textarea {
  width: 100%;
  padding: 0.75rem 1rem;
  margin-top: 0.5rem;
  border-radius: 0.75rem;
  border: 1px solid var(--border);
  background: #fff;
  font-size: 0.95rem;
  font-family: inherit;
}

textarea {
  min-height: 60vh;
  resize: vertical;
}

button {
  border: none;
  border-radius: 0.75rem;
  padding: 0.65rem 1.25rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s ease;
}

button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.primary {
  background: var(--primary);
  color: #fff;
}

.primary:hover:not(:disabled) {
  background: var(--primary-hover);
}

.ghost {
  background: var(--ghost);
  color: var(--text);
}

.ghost:hover:not(:disabled) {
  background: #cfd0d5;
}

.danger {
  background: var(--danger);
  color: #fff;
}

.danger:hover:not(:disabled) {
  background: var(--danger-hover);
}

.muted {
  color: var(--muted);
  font-size: 0.85rem;
}

.tiny {
  font-size: 0.75rem;
}

.error {
  color: var(--danger);
  min-height: 1.2em;
}

.hidden {
  display: none;
}

.topbar {
  background: var(--card-bg);
  border-bottom: 1px solid var(--border);
  padding: 1rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.workspace {
  display: flex;
  flex: 1;
  min-height: calc(100vh - 68px);
}

.sidebar {
  width: 320px;
  border-right: 1px solid var(--border);
  background: #fff;
  padding: 1rem;
  overflow-y: auto;
}

.sidebar-header {
  margin-bottom: 1rem;
}

.new-file input {
  margin-top: 0.25rem;
}

#fileList {
  list-style: none;
  padding: 0;
  margin: 1.5rem 0 0;
}

#fileList li {
  margin-bottom: 0.5rem;
}

#fileList button {
  width: 100%;
  text-align: left;
  padding: 0.75rem 1rem;
  background: #f3f4f6;
}

#fileList button.active {
  background: var(--primary);
  color: #fff;
}

.editor-area {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.editor-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: #fff;
}

.editor-toolbar {
  padding: 0.75rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: #f9fafb;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.toolbar-right {
  display: flex;
  gap: 1rem;
  align-items: flex-end;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.control label {
  display: block;
  margin-bottom: 0.15rem;
}

select,
input[type='search'] {
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  padding: 0.45rem 0.65rem;
  font-size: 0.9rem;
  background: #fff;
}

.search-control .search-row {
  display: flex;
  gap: 0.35rem;
  align-items: center;
}

.search-control input {
  min-width: 160px;
}

.panes {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  padding: 1rem 1.5rem 2rem;
  flex: 1;
  overflow: hidden;
}

.pane {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 1rem;
  padding: 1rem;
  display: flex;
  flex-direction: column;
}

.pane h3 {
  margin: 0;
}

#editor {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  padding: 1rem;
  font-family: 'JetBrains Mono', 'Fira Code', monospace;
  font-size: var(--editor-font-size);
  caret-color: #e11d48; /* brighter caret for mobile */
}

#editor:focus {
  outline: none;
  box-shadow: 0 0 0 2px rgba(225, 29, 72, 0.3);
}

#editor::selection {
  background: rgba(225, 29, 72, 0.2);
}

.editor-wrapper {
  position: relative;
  flex: 1;
}

.preview {
  flex: 1;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  padding: 1rem;
  background: #fafafa;
  line-height: 1.6;
  font-size: var(--editor-font-size);
}

.toc-container {
  min-width: 180px;
  max-width: 240px;
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  padding: 0.5rem 0.75rem;
  background: #f8fafc;
}

.toc-container ul {
  max-height: 260px;
}

.pane-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.5rem;
}

.toc-title {
  font-weight: 700;
  font-size: 0.95rem;
  margin-bottom: 0.35rem;
}

#tocList {
  list-style: none;
  padding-left: 0;
  margin: 0;
  max-height: 200px;
  overflow-y: auto;
}

#tocList li {
  margin: 0.15rem 0;
}

#tocList button {
  background: none;
  border: none;
  padding: 0.25rem 0.35rem;
  text-align: left;
  width: 100%;
  cursor: pointer;
  border-radius: 0.25rem;
}

#tocList button:hover {
  background: #e5e7eb;
}

.preview img {
  max-width: 100%;
  border-radius: 0.5rem;
  border: 1px solid var(--border);
}

.image-placeholder {
  margin: 0.25rem 0;
  color: var(--muted);
}

.image-placeholder code {
  background: #f3f4f6;
  padding: 0.2rem 0.35rem;
  border-radius: 0.35rem;
  display: inline-block;
}

.fab {
  position: fixed;
  right: 16px;
  padding: 0.9rem 1.1rem;
  border-radius: 999px;
  background: var(--primary);
  color: #fff;
  border: none;
  box-shadow: 0 12px 25px rgba(0, 0, 0, 0.15);
  cursor: pointer;
  font-weight: 700;
  z-index: 50;
}

.fab:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.fab.insert {
  top: 22vh;
}

.fab.locate {
  top: 32vh;
  background: #0f766e;
}

.fab {
  position: fixed;
  right: 16px;
  padding: 0.9rem 1.1rem;
  border-radius: 999px;
  background: var(--primary);
  color: #fff;
  border: none;
  box-shadow: 0 12px 25px rgba(0, 0, 0, 0.15);
  cursor: pointer;
  font-weight: 700;
  z-index: 50;
}

.fab:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.fab.insert {
  top: 22vh;
}

.fab.locate {
  top: 32vh;
  background: #0f766e;
}

.preview pre {
  background: #111827;
  color: #f3f4f6;
  padding: 0.75rem;
  border-radius: 0.5rem;
  overflow-x: auto;
}

.preview code {
  font-family: 'JetBrains Mono', 'Fira Code', monospace;
}

@media (max-width: 960px) {
  .workspace {
    flex-direction: column;
  }

  .sidebar {
    width: 100%;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }

  .panes {
    grid-template-columns: 1fr;
  }
}
