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

:root {
  --sidebar-w: 260px;
  --bg: #1e1e2e;
  --surface: #282840;
  --border: #3b3b5c;
  --text: #cdd6f4;
  --text-dim: #7f849c;
  --accent: #89b4fa;
  --danger: #f38ba8;
}

html, body, #app {
  height: 100%;
  font-family: -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

/* ---- Layout ---- */
.app {
  display: flex;
  height: 100%;
  overflow: hidden;
}

/* ---- Sidebar overlay (mobile only, handled by media query) ---- */
.sidebar-overlay {
  display: none;
}

/* ---- Sidebar ---- */
.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
}

.sidebar-header h1 {
  font-size: 1.1rem;
  font-weight: 600;
}

.btn-new {
  background: var(--accent);
  color: var(--bg);
  border: none;
  border-radius: 6px;
  padding: 8px 14px;
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
}

.btn-menu {
  display: none;
  background: none;
  border: none;
  color: var(--text);
  font-size: 1.4rem;
  cursor: pointer;
  padding: 4px 8px;
  flex-shrink: 0;
}

.search {
  margin: 0 12px 12px;
  padding: 10px 12px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 16px; /* prevents iOS zoom */
  outline: none;
}

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

.note-list {
  list-style: none;
  overflow-y: auto;
  flex: 1;
  -webkit-overflow-scrolling: touch;
}

.note-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px;
  cursor: pointer;
  border-left: 3px solid transparent;
  transition: background 0.15s;
  min-height: 44px;
}

.note-list li:hover {
  background: rgba(137, 180, 250, 0.08);
}

.note-list li.active {
  background: rgba(137, 180, 250, 0.12);
  border-left-color: var(--accent);
}

.note-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.9rem;
}

.btn-delete {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 1.3rem;
  cursor: pointer;
  padding: 4px 8px;
  min-width: 44px;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.btn-delete:hover {
  color: var(--danger);
}

/* ---- Editor ---- */
.editor {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

.editor.empty {
  justify-content: center;
  align-items: center;
  color: var(--text-dim);
}

.editor-toolbar {
  display: flex;
  align-items: center;
  border-bottom: 1px solid var(--border);
}

.title-input {
  flex: 1;
  padding: 14px 20px;
  font-size: 1.2rem;
  font-weight: 600;
  background: transparent;
  border: none;
  color: var(--text);
  outline: none;
  min-width: 0;
}

/* ---- Tab bar (visible on mobile only by default, shown via media query) ---- */
.tab-bar {
  display: none;
  border-bottom: 1px solid var(--border);
}

.tab-btn {
  flex: 1;
  padding: 10px;
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-dim);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  min-height: 44px;
}

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

.split-pane {
  flex: 1;
  display: flex;
  overflow: hidden;
}

.md-input {
  flex: 1;
  padding: 16px 20px;
  font-family: "SF Mono", "Fira Code", "Cascadia Code", monospace;
  font-size: 16px; /* prevents iOS zoom */
  line-height: 1.6;
  background: var(--bg);
  color: var(--text);
  border: none;
  border-right: 1px solid var(--border);
  resize: none;
  outline: none;
  tab-size: 2;
  -webkit-overflow-scrolling: touch;
}

.md-preview {
  flex: 1;
  padding: 16px 24px;
  overflow-y: auto;
  line-height: 1.7;
  font-size: 0.95rem;
  -webkit-overflow-scrolling: touch;
}

/* ---- Preview typography ---- */
.md-preview h1, .md-preview h2, .md-preview h3 {
  margin: 1em 0 0.4em;
  color: var(--accent);
}

.md-preview h1 { font-size: 1.6rem; }
.md-preview h2 { font-size: 1.3rem; }
.md-preview h3 { font-size: 1.1rem; }

.md-preview p {
  margin: 0.6em 0;
}

.md-preview code {
  background: var(--surface);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 0.85em;
}

.md-preview pre {
  background: var(--surface);
  padding: 12px 16px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 1em 0;
}

.md-preview pre code {
  background: none;
  padding: 0;
}

.md-preview blockquote {
  border-left: 3px solid var(--accent);
  padding-left: 16px;
  color: var(--text-dim);
  margin: 1em 0;
}

.md-preview ul, .md-preview ol {
  padding-left: 1.5em;
  margin: 0.6em 0;
}

.md-preview img {
  max-width: 100%;
  border-radius: 6px;
  margin: 0.6em 0;
}

.md-preview a {
  color: var(--accent);
}

.md-preview hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 1.5em 0;
}

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

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

.md-preview th {
  background: var(--surface);
}

/* ===========================================================================
   Mobile (< 768px)
   =========================================================================== */
@media (max-width: 767px) {

  /* Hamburger button visible */
  .btn-menu {
    display: block;
  }

  /* Sidebar becomes a slide-over drawer */
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 100;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    width: 80vw;
    max-width: 320px;
    min-width: unset;
  }

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

  /* Dark overlay behind sidebar */
  .sidebar-overlay {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 99;
    background: rgba(0, 0, 0, 0.5);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
  }

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

  /* Tab bar visible */
  .tab-bar {
    display: flex;
  }

  /* Only show active tab's pane */
  .hidden-mobile {
    display: none !important;
  }

  /* Stack pane full width */
  .split-pane {
    flex-direction: column;
  }

  .md-input {
    border-right: none;
  }

  .md-preview {
    padding: 16px 16px;
  }

  .title-input {
    padding: 12px 12px;
    font-size: 1.1rem;
  }
}
