:root {
  --bg: #0f1117;
  --bg-soft: #1a1d27;
  --bg-elev: #232735;
  --border: #2d3142;
  --text: #e6e8ef;
  --text-dim: #8a90a4;
  --accent: #6aa3ff;
  --accent-hover: #4a8df0;
  --danger: #ef5b6b;
  --success: #4ec9a6;
  --warning: #f0c674;
  --code-bg: #0c0e15;
  --shadow: 0 4px 20px rgba(0, 0, 0, 0.35);
  --radius: 8px;
  --mono: ui-monospace, SFMono-Regular, "JetBrains Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f7f8fb;
    --bg-soft: #ffffff;
    --bg-elev: #ffffff;
    --border: #e3e6ee;
    --text: #1d2030;
    --text-dim: #6b7188;
    --accent: #2c6cf6;
    --accent-hover: #1c5be0;
    --danger: #e23a4d;
    --success: #1a9c75;
    --warning: #c98a18;
    --code-bg: #f1f3f8;
    --shadow: 0 4px 16px rgba(20, 30, 60, 0.08);
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.55;
  min-height: 100vh;
}

a {
  color: var(--accent);
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
}

button {
  font-family: inherit;
  font-size: 14px;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--text);
  padding: 6px 12px;
  border-radius: 6px;
  transition: background-color 0.15s, border-color 0.15s, color 0.15s;
}
button:hover {
  border-color: var(--accent);
  color: var(--accent);
}
button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
button.primary:hover {
  background: var(--accent-hover);
  color: #fff;
}
button.danger {
  color: var(--danger);
}
button.danger:hover {
  border-color: var(--danger);
  background: rgba(239, 91, 107, 0.1);
}
button.ghost {
  background: transparent;
  border-color: transparent;
}
button.ghost:hover {
  background: var(--bg-elev);
}
button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

input[type="text"] {
  font-family: inherit;
  font-size: 14px;
  background: var(--bg-soft);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 12px;
  outline: none;
}
input[type="text"]:focus {
  border-color: var(--accent);
}

/* ----- Top navbar ----- */
.navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 24px;
  background: var(--bg-soft);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
  backdrop-filter: blur(8px);
}
.navbar .brand {
  font-weight: 700;
  font-size: 18px;
  letter-spacing: 0.3px;
  display: flex;
  align-items: center;
  gap: 10px;
}
.navbar .brand .dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 8px var(--accent);
}
.navbar .actions {
  display: flex;
  gap: 8px;
  align-items: center;
}
.save-status {
  font-size: 13px;
  color: var(--text-dim);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-width: 80px;
}
.save-status .pulse {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-dim);
}
.save-status.saving .pulse {
  background: var(--warning);
  animation: pulse 1.2s ease-in-out infinite;
}
.save-status.saved .pulse {
  background: var(--success);
}
.save-status.error .pulse {
  background: var(--danger);
}
@keyframes pulse {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.3;
  }
}

/* ----- Home page -----
 * Use the full viewport width but cap the side gutters at 100px so very wide
 * monitors don't waste horizontal space, while small screens shrink down to a
 * comfortable 16px padding.
 */
.container {
  max-width: none;
  margin: 0;
  padding: 32px clamp(16px, 6vw, 100px) 80px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
.section-header h1 {
  margin: 0;
  font-size: 22px;
}
.section-header .count {
  color: var(--text-dim);
  font-weight: 400;
  font-size: 14px;
  margin-left: 8px;
}

.page-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
}
.page-card {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  transition: border-color 0.15s, transform 0.15s, box-shadow 0.15s;
  display: flex;
  flex-direction: column;
  min-height: 130px;
  position: relative;
  cursor: pointer;
}
.page-card:hover {
  border-color: var(--accent);
  transform: translateY(-2px);
  box-shadow: var(--shadow);
}
.page-card .title {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--text);
  word-break: break-all;
}
.page-card .meta {
  font-size: 12px;
  color: var(--text-dim);
  margin-top: auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}
.page-card .meta .badges {
  display: flex;
  gap: 6px;
}
.page-card .badge {
  display: inline-block;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 2px 8px;
  font-size: 11px;
}
.page-card .row-actions {
  position: absolute;
  top: 10px;
  right: 10px;
  display: flex;
  gap: 4px;
  opacity: 0;
  transition: opacity 0.15s;
}
.page-card:hover .row-actions {
  opacity: 1;
}
.page-card .row-actions button {
  padding: 4px 8px;
  font-size: 12px;
}

.empty {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-dim);
}
.empty .big {
  font-size: 48px;
  margin-bottom: 12px;
  opacity: 0.4;
}

/* ----- Editor page ----- */
.editor-shell {
  max-width: none;
  margin: 0;
  padding: 24px clamp(16px, 6vw, 100px) 120px;
}
.editor-title {
  font-size: 24px;
  font-weight: 700;
  background: transparent;
  border: 1px solid transparent;
  color: var(--text);
  padding: 6px 10px;
  border-radius: 6px;
  width: 100%;
  outline: none;
  font-family: inherit;
  margin-bottom: 6px;
}
.editor-title:hover,
.editor-title:focus {
  border-color: var(--border);
  background: var(--bg-soft);
}
.editor-meta {
  color: var(--text-dim);
  font-size: 13px;
  margin-bottom: 20px;
  padding: 0 10px;
}

.cell {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-soft);
  margin-bottom: 14px;
  position: relative;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.cell:hover {
  border-color: #3a4055;
}
.cell.focused {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(106, 163, 255, 0.12);
}

.cell-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elev);
  border-top-left-radius: var(--radius);
  border-top-right-radius: var(--radius);
  font-size: 12px;
  color: var(--text-dim);
  gap: 8px;
}
.cell-toolbar .left {
  display: flex;
  align-items: center;
  gap: 8px;
}
.cell-toolbar .mode-tag {
  font-family: var(--mono);
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-soft);
  color: var(--text-dim);
}
.cell-toolbar .mode-tag.edit {
  border-color: var(--accent);
  color: var(--accent);
}
.cell-toolbar .right {
  display: flex;
  gap: 4px;
}
.cell-toolbar button {
  padding: 3px 8px;
  font-size: 12px;
}
.cell-toolbar .icon-btn {
  background: transparent;
  border-color: transparent;
  color: var(--text-dim);
}
.cell-toolbar .icon-btn:hover {
  background: var(--bg-soft);
  color: var(--text);
  border-color: var(--border);
}

.cell-body {
  padding: 14px 18px;
  min-height: 56px;
}
.cell-body.empty-hint {
  color: var(--text-dim);
  font-style: italic;
  cursor: text;
}

.cell-editor {
  width: 100%;
  min-height: 80px;
  background: transparent;
  color: var(--text);
  border: none;
  outline: none;
  resize: vertical;
  font-family: var(--mono);
  font-size: 14px;
  line-height: 1.6;
  padding: 0;
  display: block;
  tab-size: 2;
}

/* Markdown preview */
.markdown-body {
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.7;
  word-wrap: break-word;
}
.markdown-body :first-child {
  margin-top: 0;
}
.markdown-body :last-child {
  margin-bottom: 0;
}
.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4 {
  font-weight: 700;
  line-height: 1.25;
  margin-top: 1.4em;
  margin-bottom: 0.5em;
}
.markdown-body h1 {
  font-size: 1.7em;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.3em;
}
.markdown-body h2 {
  font-size: 1.4em;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.3em;
}
.markdown-body h3 {
  font-size: 1.2em;
}
.markdown-body p,
.markdown-body ul,
.markdown-body ol,
.markdown-body blockquote {
  margin: 0.6em 0;
}
.markdown-body code {
  font-family: var(--mono);
  font-size: 0.92em;
  background: var(--code-bg);
  padding: 1px 6px;
  border-radius: 4px;
}
.markdown-body pre {
  background: var(--code-bg);
  border: 1px solid var(--border);
  padding: 12px 14px;
  border-radius: 6px;
  overflow-x: auto;
}
.markdown-body pre code {
  background: transparent;
  padding: 0;
  font-size: 13px;
}
.markdown-body blockquote {
  border-left: 3px solid var(--accent);
  padding: 4px 12px;
  color: var(--text-dim);
  background: var(--bg-elev);
  border-radius: 0 6px 6px 0;
}
.markdown-body table {
  border-collapse: collapse;
  margin: 1em 0;
}
.markdown-body th,
.markdown-body td {
  border: 1px solid var(--border);
  padding: 6px 12px;
}
.markdown-body th {
  background: var(--bg-elev);
}
.markdown-body img {
  max-width: 100%;
  border-radius: 6px;
}
.markdown-body a {
  color: var(--accent);
}
.markdown-body hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 1.4em 0;
}
.markdown-body ul,
.markdown-body ol {
  padding-left: 1.6em;
}

/* Add cell button between cells */
.add-cell-row {
  display: flex;
  justify-content: center;
  margin: -4px 0 10px;
  height: 16px;
  align-items: center;
  position: relative;
  opacity: 0;
  transition: opacity 0.15s;
}
.add-cell-row:hover {
  opacity: 1;
}
.add-cell-row::before {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: 50%;
  height: 1px;
  background: var(--border);
}
.add-cell-row button {
  position: relative;
  background: var(--bg);
  font-size: 12px;
  padding: 2px 10px;
  border-radius: 12px;
  color: var(--text-dim);
}

.editor-footer-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-top: 24px;
}

/* Toast */
.toast-area {
  position: fixed;
  right: 16px;
  bottom: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 1000;
  pointer-events: none;
}
.toast {
  pointer-events: auto;
  background: var(--bg-elev);
  color: var(--text);
  padding: 10px 14px;
  border-left: 3px solid var(--accent);
  border-radius: 6px;
  box-shadow: var(--shadow);
  font-size: 13px;
  min-width: 200px;
  max-width: 360px;
  animation: toast-in 0.18s ease-out;
}
.toast.error {
  border-left-color: var(--danger);
}
.toast.success {
  border-left-color: var(--success);
}
@keyframes toast-in {
  from {
    transform: translateY(8px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

/* Modal */
.modal-mask {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
}
.modal {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: 360px;
  padding: 20px;
  box-shadow: var(--shadow);
}
.modal h3 {
  margin: 0 0 12px 0;
  font-size: 16px;
}
.modal input {
  width: 100%;
  margin-bottom: 16px;
}
.modal .actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.spinner {
  width: 14px;
  height: 14px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  display: inline-block;
  animation: spin 0.7s linear infinite;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}
