:root {
  --bg: #ffffff;
  --fg: #1b1b1b;
  --muted: #5b6470;
  --border: #d0d7de;
  --card: #f6f8fa;
  --surface: #ffffff;
  --sidebar: #f3f4f6;
  --accent: #2f6fed;
  --accent-soft: #e7effd;
  --accent-fg: #ffffff;
  --danger: #b42318;
  --focus: #2f6fed;
  --sidebar-w: 232px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.1);
}

[data-theme="dark"] {
  --bg: #14181f;
  --fg: #e6e9ef;
  --muted: #9aa4b2;
  --border: #2c333d;
  --card: #1b2129;
  --surface: #1b2129;
  --sidebar: #10141a;
  --accent: #6ea8ff;
  --accent-soft: #1d2a3f;
  --accent-fg: #10141a;
  --danger: #ff6b6b;
  --focus: #6ea8ff;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
}

@media (prefers-color-scheme: dark) {
  [data-theme="system"] {
    --bg: #14181f;
    --fg: #e6e9ef;
    --muted: #9aa4b2;
    --border: #2c333d;
    --card: #1b2129;
    --surface: #1b2129;
    --sidebar: #10141a;
    --accent: #6ea8ff;
    --accent-soft: #1d2a3f;
    --accent-fg: #10141a;
    --danger: #ff6b6b;
    --focus: #6ea8ff;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }

html { font-family: system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  line-height: 1.5;
  font-size: 15px;
}

a { color: var(--accent); }

a:focus-visible,
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
[tabindex]:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 1px;
}

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  background: var(--accent);
  color: var(--accent-fg);
  padding: 8px 12px;
  z-index: 100;
}
.skip-link:focus { left: 8px; top: 8px; }

/* --- App shell --- */

.nav-toggle { position: absolute; opacity: 0; pointer-events: none; width: 0; height: 0; }

.mobilebar {
  display: none;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 30;
}
.mobilebar .brand { flex: 1; }
.backdrop { display: none; }

.sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  width: var(--sidebar-w);
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  padding: 16px 12px;
  overflow-y: auto;
  z-index: 40;
}

.workspace {
  margin-left: var(--sidebar-w);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

main { padding: 20px 24px 56px; width: 100%; }

.side-nav { display: flex; flex-direction: column; min-height: 100%; gap: 4px; }
.side-brand { font-weight: 700; font-size: 1.1rem; text-decoration: none; color: var(--fg); padding: 4px 8px 12px; }
.side-new {
  display: flex; align-items: center; gap: 8px;
  background: var(--accent); color: var(--accent-fg);
  text-decoration: none; font-weight: 600;
  padding: 9px 12px; border-radius: 8px; margin-bottom: 10px;
}
.side-new:hover { filter: brightness(1.05); }
.side-group { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.side-label { color: var(--muted); font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.06em; margin: 14px 8px 4px; }
.side-spacer { flex: 1; }
.side-footer { border-top: 1px solid var(--border); padding-top: 8px; margin-top: 8px; }

.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; border-radius: 8px;
  color: var(--fg); text-decoration: none; font: inherit;
  background: none; border: 0; width: 100%; text-align: left; cursor: pointer;
}
.nav-item:hover { background: var(--accent-soft); }
.nav-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav-item .icon { flex: 0 0 auto; }
.side-logout { margin: 0; }

.icon { display: inline-block; vertical-align: middle; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 36px; min-height: 36px; padding: 4px;
  border: 1px solid transparent; border-radius: 8px;
  background: none; color: inherit; cursor: pointer; text-decoration: none;
}
.icon-btn:hover { background: var(--accent-soft); }

/* --- Flash --- */

.flash {
  margin: 16px 24px 0;
  padding: 10px 14px;
  background: var(--accent-soft);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.offline-banner {
  margin: 12px 24px 0;
  padding: 8px 14px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--fg);
}
.error { color: var(--danger); }

/* --- Notes three-pane --- */

.notes-layout {
  display: grid;
  grid-template-columns: 340px 1fr;
  gap: 16px;
  align-items: start;
}
.list-pane {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  padding: 12px;
  position: sticky;
  top: 20px;
  max-height: calc(100vh - 40px);
  overflow: auto;
}
.list-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.list-title { font-weight: 700; font-size: 1.05rem; }
.list-search input {
  width: 100%; min-height: 40px; padding: 8px 12px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg); color: var(--fg); font: inherit;
  margin-bottom: 10px;
}
.filter-note { margin: -4px 0 10px; font-size: 0.85rem; color: var(--muted); }

.composer {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  padding: 10px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}
.composer-title { width: 100%; border: 0; background: none; color: var(--fg); font: inherit; font-weight: 600; padding: 4px 2px; }
.composer-body { width: 100%; border: 0; background: none; color: var(--fg); font: inherit; resize: vertical; padding: 4px 2px; }
.composer-title:focus-visible, .composer-body:focus-visible { outline: none; }
.composer-main:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
.composer-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 6px; }
.composer-tags { flex: 1; min-width: 120px; min-height: 36px; padding: 6px 8px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); color: var(--fg); font: inherit; }
.composer select { min-height: 36px; }

.note-cards { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.note-card {
  position: relative;
  border: 1px solid transparent;
  border-radius: 8px;
}
.note-card.pinned { background: var(--card); }
.note-card:hover { border-color: var(--border); }
.note-card-link { display: flex; flex-direction: column; gap: 3px; padding: 10px; text-decoration: none; color: var(--fg); border-radius: 8px; }
.note-card-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.note-card-snippet { color: var(--muted); font-size: 0.86rem; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.note-card-meta { display: flex; align-items: center; gap: 6px; }
.note-card-tags { display: flex; flex-wrap: wrap; gap: 4px; padding: 0 10px 8px; }
.note-dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; border: 1px solid rgba(0,0,0,0.15); }
.note-color-yellow { background: #f5d76e; }
.note-color-blue { background: #8fc1f0; }
.note-color-green { background: #9fd8a6; }
.note-color-pink { background: #f3a6c4; }
.note-color-purple { background: #c3a6ef; }
.note-color-neutral { background: #d0d7de; }
.note-card-action { position: absolute; top: 6px; right: 6px; opacity: 0; }
.note-card:hover .note-card-action, .note-card:focus-within .note-card-action { opacity: 1; }

.empty-state { color: var(--muted); padding: 16px 8px; }

.detail-pane { min-width: 0; }
.detail-empty { color: var(--muted); border: 1px dashed var(--border); border-radius: 12px; padding: 40px; text-align: center; }
.detail-head { display: flex; flex-wrap: wrap; gap: 8px; align-items: flex-start; justify-content: space-between; margin-bottom: 8px; }
.detail-title { font-size: 1.5rem; margin: 0; word-break: break-word; }
.detail-tools { display: flex; flex-wrap: wrap; gap: 6px; }
.detail-extra { margin-top: 18px; border-top: 1px solid var(--border); padding-top: 10px; }
.detail-extra summary { cursor: pointer; font-weight: 600; }
.link-list { list-style: none; padding: 0; margin: 6px 0; display: flex; flex-direction: column; gap: 6px; }
.link-list li { display: flex; align-items: center; gap: 8px; }
.mobile-only { display: none; }

.note-form .field-row { display: flex; flex-wrap: wrap; gap: 12px; }
.field-row { display: flex; flex-wrap: wrap; gap: 12px; }
.form-actions { display: flex; gap: 8px; margin-top: 8px; }

.settings-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 14px; }
.settings-grid .card { max-width: none; margin: 0; }

/* --- Tasks --- */

.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.pill-tabs { display: flex; gap: 6px; margin-bottom: 14px; flex-wrap: wrap; }
.pill { padding: 6px 12px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface); color: var(--fg); text-decoration: none; }
.pill:hover { border-color: var(--accent); }
.pill.active { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }

.task-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; }
.task-item { display: flex; align-items: center; gap: 10px; padding: 10px 6px; border-bottom: 1px solid var(--border); }
.task-item:last-child { border-bottom: 0; }
.task-check-form { margin: 0; display: flex; padding: 8px 4px; }
.task-check {
  width: 24px; height: 24px; border-radius: 50%;
  border: 2px solid var(--border); background: transparent;
  color: var(--accent-fg); cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 13px; line-height: 1; padding: 0;
}
.task-check:hover { border-color: var(--accent); }
.task-item.done .task-check { background: var(--accent); border-color: var(--accent); }
.task-title { flex: 1; color: var(--fg); text-decoration: none; overflow-wrap: anywhere; }
.task-title:hover { text-decoration: underline; }
.task-item.done .task-title { color: var(--muted); text-decoration: line-through; }
.task-meta { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.task-due { color: var(--muted); font-size: 0.85rem; }
.task-trash { margin: 0; opacity: 0; }
.task-item:hover .task-trash, .task-item:focus-within .task-trash { opacity: 1; }

/* --- Generic content --- */

h1 { font-size: 1.4rem; margin: 0 0 12px; }
h2 { font-size: 1.1rem; }
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
  max-width: 560px;
  margin: 0 0 14px;
}
.field { margin-bottom: 12px; display: block; }
.field > label { display: block; margin-bottom: 4px; font-weight: 600; }
input[type="text"], input[type="email"], input[type="password"], input:not([type]), input[list] {
  width: 100%;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--fg);
  font: inherit;
  min-height: 44px;
}
fieldset { border: 0; padding: 0; margin: 0 0 12px; }
fieldset label { display: inline-flex; align-items: center; gap: 6px; margin-right: 12px; min-height: 44px; }
.help { color: var(--muted); font-size: 0.85rem; margin: 4px 0 0; }
.field-error { color: var(--danger); margin: 4px 0 0; font-size: 0.9rem; }

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 9px 14px;
  min-height: 44px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card);
  color: var(--fg);
  font: inherit;
  cursor: pointer;
  text-decoration: none;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.btn.danger { color: var(--danger); }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; background: var(--accent); color: var(--accent-fg); font-size: 0.78rem; }
.badge.muted { background: var(--border); color: var(--fg); }
.chip { display: inline-block; padding: 1px 8px; border-radius: 999px; background: var(--card); border: 1px solid var(--border); font-size: 0.78rem; }
.muted { color: var(--muted); font-size: 0.9rem; }
.tags { margin: 8px 0; display: flex; flex-wrap: wrap; gap: 4px; }

.table { width: 100%; border-collapse: collapse; margin: 12px 0; }
.table caption { text-align: left; color: var(--muted); padding-bottom: 6px; }
.table th, .table td { text-align: left; padding: 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
.ua { max-width: 220px; overflow-wrap: anywhere; }
.actions { display: flex; flex-wrap: wrap; gap: 6px; align-items: flex-start; }
.inline { display: inline-flex; gap: 4px; align-items: center; flex-wrap: wrap; }
.inline input[type="password"] { width: 140px; }
.stack { margin-top: 12px; }

textarea {
  width: 100%;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--fg);
  font: inherit;
}
select { min-height: 44px; padding: 6px; font: inherit; }

.note-body { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 16px; overflow-wrap: anywhere; }
.note-body pre { overflow-x: auto; background: var(--bg); padding: 8px; border-radius: 6px; }
.note-body code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.conflict { display: flex; flex-wrap: wrap; gap: 12px; }
.conflict > section { flex: 1 1 280px; }
.snippet { color: var(--muted); margin: 4px 0 0; }
.meta { display: grid; grid-template-columns: max-content 1fr; gap: 4px 12px; }
.meta dt { font-weight: 600; }
.board { position: relative; min-height: 400px; border: 1px solid var(--border); border-radius: 10px; margin: 12px 0; background: var(--card); overflow: auto; }
.board-card { position: absolute; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); padding: 8px; overflow: hidden; cursor: grab; touch-action: none; }
.board-card.dragging { cursor: grabbing; box-shadow: var(--shadow); }
.board-card a { text-decoration: none; color: var(--fg); }
@media (max-width: 700px) { .board { display: none; } }
@media (min-width: 701px) { .board-stack, .stack-heading { display: none; } }
.geom { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.geom label { display: inline-flex; gap: 4px; align-items: center; }
.geom input { width: 72px; min-height: 36px; }
.codes { list-style: none; padding: 0; display: flex; flex-wrap: wrap; gap: 8px; }
.codes li code { font-size: 1rem; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}

/* --- Auth pages --- */

.auth-body .workspace { margin-left: 0; }
.auth-body main { max-width: 420px; margin: 8vh auto 0; padding: 16px; }

/* --- Responsive --- */

@media (max-width: 900px) {
  .notes-layout { grid-template-columns: 1fr; }
  .notes-layout.has-selection .list-pane { display: none; }
  .detail-pane { border: 0; padding: 0; }
  .list-pane { position: static; max-height: none; }
  .mobile-only { display: inline-flex; }
}

@media (max-width: 760px) {
  .mobilebar { display: flex; }
  .sidebar {
    transform: translateX(-100%);
    transition: transform 0.18s ease;
    box-shadow: var(--shadow);
  }
  .nav-toggle:checked ~ .sidebar { transform: translateX(0); }
  .nav-toggle:checked ~ .backdrop {
    display: block; position: fixed; inset: 0; background: rgba(0,0,0,0.4); z-index: 35;
  }
  .workspace { margin-left: 0; }
  main { padding: 14px 14px 48px; }
  .flash { margin: 12px 14px 0; }
  .table, .table thead, .table tbody, .table th, .table td, .table tr { display: block; }
  .table thead { display: none; }
  .table td { border: 0; padding: 4px 0; }
  .table tr { border: 1px solid var(--border); border-radius: 8px; padding: 8px; margin-bottom: 8px; }
}
