:root {
  --bg: #f7f6f2;
  --surface: #ffffff;
  --text: #1f1f1c;
  --muted: #6b6b66;
  --border: #e2dfd6;
  --hover: rgba(0, 0, 0, 0.06);
  --accent: #2f6f5e;
  --accent-text: #ffffff;
  --danger: #b3261e;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.06), 0 4px 14px rgba(0, 0, 0, 0.06);

  --c-red: #fbd5d0;
  --c-orange: #fde0c2;
  --c-yellow: #fdf3b8;
  --c-green: #d9f2d0;
  --c-teal: #cdeee8;
  --c-blue: #d3e6f8;
  --c-purple: #e3d8f6;
  --c-pink: #f9d7e6;
  --c-gray: #e8e8e4;

  --topbar-h: 58px;
  font-family: system-ui, -apple-system, 'Segoe UI', 'Noto Sans Thai', 'Thonburi', sans-serif;
  line-height: 1.5;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17181a;
    --surface: #212326;
    --text: #e9e7e2;
    --muted: #a09f99;
    --border: #36383c;
    --hover: rgba(255, 255, 255, 0.09);
    --accent: #7fd1b9;
    --accent-text: #10201b;
    --danger: #f2877f;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 6px 18px rgba(0, 0, 0, 0.35);

    --c-red: #5c2b29;
    --c-orange: #5e3f1c;
    --c-yellow: #554a12;
    --c-green: #2d4a2b;
    --c-teal: #1f4a47;
    --c-blue: #24405e;
    --c-purple: #42305e;
    --c-pink: #5b2a45;
    --c-gray: #3c3f43;
  }
}

[data-color] { --note-bg: var(--surface); }
[data-color='red'] { --note-bg: var(--c-red); }
[data-color='orange'] { --note-bg: var(--c-orange); }
[data-color='yellow'] { --note-bg: var(--c-yellow); }
[data-color='green'] { --note-bg: var(--c-green); }
[data-color='teal'] { --note-bg: var(--c-teal); }
[data-color='blue'] { --note-bg: var(--c-blue); }
[data-color='purple'] { --note-bg: var(--c-purple); }
[data-color='pink'] { --note-bg: var(--c-pink); }
[data-color='gray'] { --note-bg: var(--c-gray); }

* { box-sizing: border-box; }
[hidden] { display: none !important; }

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

button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.muted { color: var(--muted); }

.icon {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: none;
}

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  flex: none;
}
.icon-btn:hover { background: var(--hover); color: var(--text); }
.icon-btn.on { color: var(--accent); }
.icon-btn.on .icon { fill: currentColor; }

.btn {
  border: 1px solid var(--border);
  background: transparent;
  border-radius: 8px;
  padding: 7px 16px;
  font-weight: 600;
}
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: 0.6; cursor: default; }

/* ---------- auth ---------- */

.auth {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}
.auth-card {
  width: min(380px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 32px 28px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.auth-card h1 { margin: 0; font-size: 1.5rem; }
.auth-card p { margin: 0; }

label { display: flex; flex-direction: column; gap: 6px; font-size: 0.9rem; font-weight: 600; }
label input {
  font-weight: 400;
  font-size: 1rem;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
}
.form-error { color: var(--danger); margin: 0; min-height: 1.5em; font-size: 0.9rem; }

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

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 14px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.menu-btn { display: none; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 1.1rem; width: 212px; flex: none; }
.search {
  flex: 1;
  min-width: 0;
  max-width: 640px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 14px;
  height: 40px;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--muted);
}
.search:focus-within { border-color: var(--accent); }
.search-input { flex: 1; min-width: 0; border: 0; background: transparent; outline: none; color: var(--text); }
.account { margin-left: auto; display: flex; align-items: center; gap: 2px; }
.username { color: var(--muted); font-size: 0.9rem; margin-right: 6px; max-width: 14ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.layout { display: grid; grid-template-columns: 240px minmax(0, 1fr); }
.sidebar {
  position: sticky;
  top: var(--topbar-h);
  height: calc(100dvh - var(--topbar-h));
  overflow-y: auto;
  padding: 12px 10px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 14px;
  width: 100%;
  padding: 9px 14px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  text-align: left;
}
.nav-item span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-item:hover { background: var(--hover); }
.nav-item.active { background: color-mix(in srgb, var(--accent) 18%, transparent); font-weight: 600; }
.nav-heading { padding: 14px 14px 4px; font-size: 0.75rem; font-weight: 700; letter-spacing: 0.04em; color: var(--muted); }
.nav-sep { height: 1px; background: var(--border); margin: 8px 6px; }
.scrim { display: none; }

.main { padding: 22px 24px 60px; min-width: 0; }

/* ---------- composer ---------- */

.composer {
  max-width: 600px;
  margin: 0 auto 28px;
  background: var(--note-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 4px 14px;
}
.composer input, .composer textarea, .editor input, .editor textarea {
  display: block;
  width: 100%;
  border: 0;
  background: transparent;
  outline: none;
  resize: none;
}
.composer-title, .composer-footer { display: none !important; }
.composer.open .composer-title { display: block !important; font-weight: 700; font-size: 1.05rem; padding: 10px 0 4px; }
.composer.open .composer-footer { display: flex !important; }
.composer-body { padding: 10px 0; max-height: 50dvh; overflow-y: auto; }
.composer-footer { align-items: center; gap: 8px; padding: 4px 0 8px; flex-wrap: wrap; }
.composer-footer > div:first-child { margin-right: auto; }

.swatches { display: flex; flex-wrap: wrap; gap: 6px; }
.swatch {
  width: 24px;
  height: 24px;
  padding: 0;
  border-radius: 50%;
  border: 1px solid color-mix(in srgb, var(--text) 25%, transparent);
  background: var(--note-bg);
}
.swatch.selected { box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent); }

/* ---------- notes ---------- */

.section-title { font-size: 0.75rem; font-weight: 700; letter-spacing: 0.05em; color: var(--muted); margin: 0 4px 10px; }
.grid { columns: 250px; column-gap: 14px; margin-bottom: 26px; }

.note {
  position: relative;
  break-inside: avoid;
  margin: 0 0 14px;
  padding: 14px 16px 4px;
  background: var(--note-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow-wrap: anywhere;
  transition: box-shadow 0.15s;
}
.note.editable { cursor: default; }
.note:hover, .note:focus-within { box-shadow: var(--shadow); }
.note-title { margin: 0 0 6px; font-size: 1rem; font-weight: 700; }
.note-content { font-size: 0.93rem; }
.note-content p, .editor p { margin: 0 0 4px; white-space: pre-wrap; }
.note-content .more { color: var(--muted); }

.check { flex-direction: row; align-items: flex-start; gap: 8px; font-weight: 400; font-size: inherit; padding: 1px 0; cursor: pointer; }
.check input { margin: 4px 0 0; width: 15px; height: 15px; padding: 0; accent-color: var(--accent); flex: none; }
.check.done span { text-decoration: line-through; color: var(--muted); }

.chips { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 8px; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.75rem;
  padding: 2px 10px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--text) 9%, transparent);
}
.chip-x { border: 0; background: transparent; padding: 0 0 0 2px; font-size: 1rem; line-height: 1; color: var(--muted); }
.chip-x:hover { color: var(--danger); }

.note-actions { display: flex; gap: 2px; margin: 4px -8px 0; opacity: 0; transition: opacity 0.15s; }
.note:hover .note-actions, .note:focus-within .note-actions { opacity: 1; }
@media (hover: none) { .note-actions { opacity: 1; } }

.empty { text-align: center; color: var(--muted); margin: 80px 0; }
.banner {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  flex-wrap: wrap;
  margin: 0 0 24px;
  color: var(--muted);
  font-style: italic;
}
.banner .btn { font-style: normal; }

/* ---------- dialogs ---------- */

dialog {
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 0;
  color: var(--text);
  background: var(--surface);
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.3);
  width: min(620px, calc(100vw - 28px));
  max-height: calc(100dvh - 40px);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.5); }

.editor { background: var(--note-bg); }
.editor-inner { display: flex; flex-direction: column; padding: 18px 20px 12px; max-height: calc(100dvh - 42px); }
.editor-title { font-size: 1.2rem; font-weight: 700; padding: 0 0 8px; }
.editor-body { flex: 1 1 auto; min-height: 120px; overflow-y: auto; padding: 0; }
.editor-meta { display: flex; flex-direction: column; gap: 12px; padding-top: 10px; flex: none; }
.label-input { width: 130px !important; display: inline-block !important; font-size: 0.8rem; padding: 2px 4px; }
.editor-footer { display: flex; align-items: center; gap: 2px; flex-wrap: wrap; margin-left: -8px; }
.edited { font-size: 0.75rem; margin-left: auto; padding: 0 10px; }

.form-dialog { width: min(400px, calc(100vw - 28px)); }
.form-dialog form { display: flex; flex-direction: column; gap: 14px; padding: 24px; }
.form-dialog h2 { margin: 0; font-size: 1.15rem; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  z-index: 100;
  width: max-content;
  max-width: calc(100vw - 32px);
  padding: 10px 18px;
  border-radius: 10px;
  background: var(--text);
  color: var(--bg);
  box-shadow: var(--shadow);
}

/* ---------- small screens ---------- */

@media (max-width: 800px) {
  .menu-btn { display: inline-grid; }
  .brand { width: auto; }
  .brand span, .username { display: none; }
  .layout { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: fixed;
    z-index: 30;
    left: 0;
    width: 260px;
    background: var(--bg);
    border-right: 1px solid var(--border);
    transform: translateX(-100%);
    transition: transform 0.2s;
  }
  .nav-open .sidebar { transform: none; }
  .nav-open .scrim { display: block; position: fixed; inset: var(--topbar-h) 0 0 0; z-index: 25; background: rgba(0, 0, 0, 0.4); }
  .main { padding: 16px 16px 60px; }
  .grid { columns: 160px; column-gap: 10px; }
  .note { margin-bottom: 10px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

/* ---------- images ---------- */

.note-images { margin: -14px -16px 10px; border-radius: 11px 11px 0 0; overflow: hidden; display: grid; gap: 2px; }
.note-images.many { grid-template-columns: 1fr 1fr; }
.note-images .thumb { position: relative; }
.note-images img { display: block; width: 100%; height: auto; background: var(--hover); }
.note-images.many img { aspect-ratio: 1; object-fit: cover; }
.thumb-more {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  font-size: 1.3rem;
  font-weight: 700;
}

.editor-images { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; max-height: 38dvh; overflow-y: auto; flex: none; }
.editor-images[hidden] { display: none; }
.editor-images .thumb { position: relative; }
.editor-images img { display: block; height: 120px; width: auto; max-width: 100%; object-fit: cover; border-radius: 8px; cursor: zoom-in; background: var(--hover); }
.thumb-x {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 24px;
  height: 24px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.65);
  color: #fff;
  font-size: 1rem;
  line-height: 1;
}
.thumb-x:hover { background: var(--danger); }

.lightbox { width: fit-content; max-width: 96vw; max-height: 96dvh; border: 0; background: transparent; box-shadow: none; overflow: visible; cursor: zoom-out; }
.lightbox img { display: block; max-width: 96vw; max-height: 92dvh; border-radius: 8px; }
.lightbox::backdrop { background: rgba(0, 0, 0, 0.85); }

/* ---------- edit history ---------- */

.history { width: min(820px, calc(100vw - 28px)); }
.history-inner { display: flex; flex-direction: column; gap: 14px; padding: 20px; height: min(620px, calc(100dvh - 42px)); }
.history-inner h2 { margin: 0; font-size: 1.15rem; }
.history-body { flex: 1; min-height: 0; display: grid; grid-template-columns: 250px minmax(0, 1fr); gap: 14px; }
.history-list { overflow-y: auto; display: flex; flex-direction: column; gap: 4px; }
.history-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  text-align: left;
  padding: 8px 12px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  font-size: 0.9rem;
}
.history-item span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 0.8rem; }
.history-item:hover { background: var(--hover); }
.history-item.active { background: color-mix(in srgb, var(--accent) 18%, transparent); }
.history-preview { overflow-y: auto; padding: 14px 16px; border: 1px solid var(--border); border-radius: 10px; background: var(--bg); overflow-wrap: anywhere; }
.history-preview h3 { margin: 0 0 8px; font-size: 1.05rem; }
.history-preview p { margin: 0; white-space: pre-wrap; }

@media (max-width: 640px) {
  .history-body { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(90px, 35%) minmax(0, 1fr); }
}
