/* Нотатки PWA — Minimalism / Swiss style, warm ink + amber accent.
   Light + dark via prefers-color-scheme. System font stack for crisp
   native rendering on iPad and full offline support. */

:root {
  --accent: #D97706;
  --accent-ink: #231703;      /* near-black on amber — 4.8:1 (WCAG AA) */
  --accent-text: #B45309;     /* amber for small text on light bg — 4.7:1 */
  --bg: #FFFBEB;
  --surface: #FFFFFF;
  --surface-2: #F6F5F1;
  --fg: #0F172A;
  --muted: #475569;
  --border: #E7E2D6;
  --hover: #F1EEE4;
  --danger: #DC2626;
  --ring: rgba(217, 119, 6, 0.45);
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.06), 0 2px 8px rgba(15, 23, 42, 0.05);

  --radius: 12px;
  --radius-sm: 8px;
  --sp-1: 6px; --sp-2: 10px; --sp-3: 16px; --sp-4: 24px; --sp-5: 36px;
  --sidebar-w: 340px;
  --touch: 44px;

  --font: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, system-ui, sans-serif;
  --fs-base: 16px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --accent: #F59E0B;
    --accent-ink: #1C1917;
    --accent-text: #F59E0B;
    --bg: #1C1917;
    --surface: #292524;
    --surface-2: #211D1B;
    --fg: #FAFAF9;
    --muted: #A8A29E;
    --border: #3A3330;
    --hover: #332D2A;
    --danger: #F87171;
    --ring: rgba(245, 158, 11, 0.5);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 2px 10px rgba(0, 0, 0, 0.3);
  }
}

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

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
}

body {
  font-family: var(--font);
  font-size: var(--fs-base);
  line-height: 1.5;
  color: var(--fg);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

input, select, textarea, button { font: inherit; color: inherit; }

/* ---------- Layout ---------- */
.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100dvh;
  padding-top: env(safe-area-inset-top);
}

.sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-right: 1px solid var(--border);
  background: var(--surface-2);
  padding-left: env(safe-area-inset-left);
}

.editor {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--bg);
  padding-right: env(safe-area-inset-right);
}

/* ---------- Sidebar head ---------- */
.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-2);
  padding: var(--sp-3);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: var(--sp-2); }
.brand h1 { font-size: 1.15rem; font-weight: 700; margin: 0; letter-spacing: -0.01em; }
.brand-mark {
  width: 26px; height: 26px;
  fill: none; stroke: var(--accent); stroke-width: 1.8;
  stroke-linecap: round; stroke-linejoin: round;
}

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: var(--touch); padding: 0 var(--sp-3);
  border: 1px solid transparent; border-radius: var(--radius-sm);
  background: transparent; cursor: pointer;
  font-weight: 600; font-size: 0.95rem;
  transition: background 200ms ease, transform 120ms ease, border-color 200ms ease;
}
.btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--accent); color: var(--accent-ink); box-shadow: var(--shadow); }
.btn-primary:hover { background: color-mix(in srgb, var(--accent) 88%, #000); }
.btn-new { padding: 0 var(--sp-3); }
.btn-ghost { min-width: var(--touch); padding: 0 var(--sp-2); color: var(--muted); }
.btn-ghost:hover { background: var(--hover); color: var(--fg); }
.btn-danger:hover { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }

/* ---------- Search ---------- */
.search { position: relative; padding: var(--sp-2) var(--sp-3); }
.search-icon {
  position: absolute; left: calc(var(--sp-3) + 10px); top: 50%; transform: translateY(-50%);
  width: 18px; height: 18px; fill: none; stroke: var(--muted); stroke-width: 2;
  stroke-linecap: round; stroke-linejoin: round; pointer-events: none;
}
#search {
  width: 100%; min-height: var(--touch);
  padding: 0 var(--sp-3) 0 40px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--fg);
  transition: border-color 200ms ease, box-shadow 200ms ease;
}
#search::placeholder { color: var(--muted); }

/* ---------- Filters (folders) ---------- */
.filters {
  display: flex; flex-wrap: wrap; gap: var(--sp-1);
  padding: 0 var(--sp-3) var(--sp-2);
}
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  min-height: 34px; padding: 0 12px;
  border: 1px solid var(--border); border-radius: 999px;
  background: var(--surface); color: var(--muted);
  cursor: pointer; font-size: 0.85rem; font-weight: 500;
  transition: background 200ms ease, color 200ms ease, border-color 200ms ease;
}
.chip:hover { background: var(--hover); }
.chip[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.chip .count { opacity: 0.7; font-size: 0.78rem; }

.tagbar { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 var(--sp-3) var(--sp-2); }
.tagbar:empty { display: none; }
.tag-chip {
  min-height: 30px; padding: 0 10px; font-size: 0.8rem;
  border: 1px dashed var(--border); border-radius: 999px;
  background: transparent; color: var(--muted); cursor: pointer;
  transition: all 200ms ease;
}
.tag-chip[aria-pressed="true"] { background: color-mix(in srgb, var(--accent) 16%, transparent); color: var(--accent-text); border-style: solid; border-color: var(--accent); }

/* ---------- Note list ---------- */
.note-list {
  list-style: none; margin: 0; padding: var(--sp-1) var(--sp-2) calc(var(--sp-4) + env(safe-area-inset-bottom));
  overflow-y: auto; -webkit-overflow-scrolling: touch; flex: 1; min-height: 0;
}
.note-item {
  display: block; width: 100%; text-align: left;
  padding: var(--sp-2) var(--sp-3);
  border: 1px solid transparent; border-radius: var(--radius-sm);
  background: transparent; cursor: pointer;
  transition: background 200ms ease, border-color 200ms ease;
}
.note-item:hover { background: var(--hover); }
.note-item[aria-current="true"] { background: var(--surface); border-color: var(--border); box-shadow: var(--shadow); }
.note-item h3 {
  margin: 0 0 2px; font-size: 1rem; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.note-item .preview {
  margin: 0; font-size: 0.86rem; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.note-item .sub { display: flex; align-items: center; gap: 8px; margin-top: 4px; font-size: 0.74rem; color: var(--muted); }
.note-item .sub .folder { color: var(--accent-text); font-weight: 600; }
mark { background: color-mix(in srgb, var(--accent) 30%, transparent); color: inherit; border-radius: 3px; padding: 0 1px; }

.empty-list { padding: var(--sp-4) var(--sp-3); color: var(--muted); text-align: center; font-size: 0.9rem; }

/* ---------- Editor ---------- */
.editor-empty {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: var(--sp-2); color: var(--muted);
}
.editor-empty svg { width: 56px; height: 56px; fill: none; stroke: var(--border); stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; }

.editor-pane { display: flex; flex-direction: column; min-height: 0; flex: 1; }

.editor-topbar {
  display: flex; align-items: center; gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-3);
  border-bottom: 1px solid var(--border);
}
.btn-back { display: none; }
.title-input {
  flex: 1; min-width: 0; min-height: var(--touch);
  border: none; background: transparent; color: var(--fg);
  font-size: 1.3rem; font-weight: 700; letter-spacing: -0.01em;
}
.title-input::placeholder { color: var(--muted); }
.save-state { font-size: 0.78rem; color: var(--muted); white-space: nowrap; min-width: 54px; text-align: right; }

.meta-row { display: flex; gap: var(--sp-2); padding: var(--sp-2) var(--sp-3); border-bottom: 1px solid var(--border); }
.meta-field { display: flex; flex-direction: column; gap: 3px; }
.meta-field.grow { flex: 1; }
.meta-label { font-size: 0.72rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
#folder-select, #tags-input {
  min-height: 38px; padding: 0 10px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--fg);
}
#folder-select { min-width: 140px; }

.toolbar {
  display: flex; align-items: center; gap: 4px; flex-wrap: wrap;
  padding: var(--sp-1) var(--sp-3);
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
}
.tool {
  display: inline-flex; align-items: center; justify-content: center;
  width: var(--touch); height: var(--touch);
  border: 1px solid transparent; border-radius: var(--radius-sm);
  background: transparent; color: var(--muted); cursor: pointer;
  transition: background 180ms ease, color 180ms ease;
}
.tool svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.tool svg text { fill: currentColor; stroke: none; font-weight: 700; }
.tool:hover { background: var(--hover); color: var(--fg); }
.tool.active { background: var(--accent); color: var(--accent-ink); }
.tool-sep { width: 1px; height: 24px; background: var(--border); margin: 0 4px; }

.body {
  flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: var(--sp-4) var(--sp-4) calc(var(--sp-5) + env(safe-area-inset-bottom));
  font-size: 1.05rem; line-height: 1.7; outline: none;
}
.body:empty::before { content: "Почніть писати…"; color: var(--muted); }
.body h2 { font-size: 1.3rem; margin: 1em 0 0.4em; }
.body ul, .body ol { padding-left: 1.4em; }
.body a { color: var(--accent-text); }

/* ---------- Focus (accessibility) ---------- */
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
#search:focus-visible, #folder-select:focus-visible, #tags-input:focus-visible, .title-input:focus-visible {
  outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--ring);
}

/* ---------- Responsive: single-column on narrow screens / portrait iPad ---------- */
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { border-right: none; }
  .editor { display: none; }
  .btn-back { display: inline-flex; }

  .app[data-view="editor"] .sidebar { display: none; }
  .app[data-view="editor"] .editor { display: flex; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; }
}

/* ---------- Sync indicator ---------- */
.sync-state { display: inline-flex; align-items: center; gap: 5px; font-size: 0.72rem; color: var(--muted); }
.sync-state .sync-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.sync-state[data-state="ok"] .sync-dot { background: #16A34A; }
.sync-state[data-state="syncing"] .sync-dot { background: var(--accent); animation: pulse 1s ease-in-out infinite; }
.sync-state[data-state="offline"] .sync-dot,
.sync-state[data-state="nokey"] .sync-dot { background: #9CA3AF; }
.sync-state[data-state="error"] .sync-dot { background: var(--danger); }
.sync-state[data-state="idle"] .sync-text:empty { display: none; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.3; } }

/* ---------- Lock icon states ---------- */
#lock-btn .ico-locked { display: none; }
#lock-btn.is-locked { color: var(--accent-text); }
#lock-btn.is-locked .ico-locked { display: block; }
#lock-btn.is-locked .ico-unlocked { display: none; }

/* lock markers in list */
.note-item h3 .lock-ico, .preview.locked .lock-ico {
  width: 14px; height: 14px; vertical-align: -2px; margin-right: 4px;
  fill: none; stroke: var(--accent-text); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}
.preview.locked { color: var(--accent-text); display: flex; align-items: center; gap: 2px; }

/* ---------- Lock screen ---------- */
.lock-screen {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: var(--sp-3); color: var(--muted); padding: var(--sp-4);
}
.lock-screen svg { width: 56px; height: 56px; fill: none; stroke: var(--accent); stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; }
.lock-screen p { margin: 0; font-size: 1rem; }

/* ---------- Images in body ---------- */
.note-img {
  max-width: 100%; height: auto; border-radius: var(--radius-sm);
  margin: var(--sp-2) 0; display: block; cursor: zoom-in; box-shadow: var(--shadow);
}
.note-img.img-missing { min-height: 60px; background: var(--surface-2); }

/* ---------- Export menu ---------- */
.menu-wrap { position: relative; }
.menu {
  position: absolute; right: 0; top: calc(100% + 4px); z-index: 20;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
  box-shadow: var(--shadow); padding: 4px; min-width: 210px;
}
.menu-item {
  display: block; width: 100%; text-align: left; padding: 10px 12px;
  border: none; background: transparent; color: var(--fg); cursor: pointer;
  border-radius: 6px; font-size: 0.92rem;
}
.menu-item:hover { background: var(--hover); }

/* ---------- Password modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center;
  background: rgba(15, 23, 42, 0.5); padding: var(--sp-3); backdrop-filter: blur(2px);
}
.modal {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 10px 40px rgba(0,0,0,0.3); padding: var(--sp-4); width: min(380px, 100%);
}
.modal h2 { margin: 0 0 var(--sp-2); font-size: 1.15rem; }
.modal-note { margin: 0 0 var(--sp-2); font-size: 0.82rem; color: var(--danger); line-height: 1.4; }
.modal-input {
  width: 100%; min-height: var(--touch); margin-top: var(--sp-2); padding: 0 12px;
  border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--bg); color: var(--fg);
}
.modal-input:focus-visible { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--ring); }
.modal-error { margin: var(--sp-2) 0 0; color: var(--danger); font-size: 0.85rem; }
.modal-actions { display: flex; justify-content: flex-end; gap: var(--sp-2); margin-top: var(--sp-3); }

/* ---------- Fullscreen image viewer ---------- */
.img-viewer {
  position: fixed; inset: 0; z-index: 60; display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,0.9); padding: var(--sp-3);
}
.img-viewer img { max-width: 100%; max-height: 100%; object-fit: contain; border-radius: var(--radius-sm); }
.img-close {
  position: absolute; top: calc(env(safe-area-inset-top) + 12px); right: 16px;
  width: 44px; height: 44px; border: none; border-radius: 50%;
  background: rgba(255,255,255,0.15); color: #fff; font-size: 1.8rem; line-height: 1; cursor: pointer;
}

/* ---------- Mode switch (Keyboard / Ink) ---------- */
.mode-row { display: flex; padding: var(--sp-1) var(--sp-3); border-bottom: 1px solid var(--border); background: var(--surface-2); }
.seg {
  display: inline-flex; gap: 2px; padding: 3px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 999px;
}
.seg-btn {
  display: inline-flex; align-items: center; gap: 6px;
  min-height: 36px; padding: 0 14px;
  border: none; border-radius: 999px; background: transparent;
  color: var(--muted); font-weight: 600; font-size: 0.88rem; cursor: pointer;
  transition: background 180ms ease, color 180ms ease;
}
.seg-btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.seg-btn.active { background: var(--accent); color: var(--accent-ink); box-shadow: var(--shadow); }
.ink-badge { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); }
.seg-btn.active .ink-badge { background: var(--accent-ink); }

/* ---------- Ink: layer, toolbar, canvas ---------- */
.ink-wrap { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.ink-toolbar {
  display: flex; align-items: center; gap: 6px; flex-wrap: wrap;
  padding: var(--sp-1) var(--sp-3);
  border-bottom: 1px solid var(--border); background: var(--surface-2);
}
.ink-group { display: inline-flex; align-items: center; gap: 4px; }
.ink-group + .ink-group { padding-left: 6px; margin-left: 2px; border-left: 1px solid var(--border); }
.ink-spring { margin-left: auto; }
.ink-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 5px;
  min-width: var(--touch); height: var(--touch); padding: 0 8px;
  border: 1px solid transparent; border-radius: var(--radius-sm);
  background: transparent; color: var(--muted); cursor: pointer;
  transition: background 160ms ease, color 160ms ease, border-color 160ms ease;
}
.ink-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.ink-btn:hover { background: var(--hover); color: var(--fg); }
.ink-btn.active { background: var(--accent); color: var(--accent-ink); }
.ink-btn[disabled] { opacity: 0.4; cursor: default; }
.ink-btn.ink-wide { font-size: 0.82rem; font-weight: 600; }
.ink-size .dot { display: inline-block; border-radius: 50%; background: currentColor; }
.ink-colors { flex-wrap: wrap; }
.swatch {
  width: 26px; height: 26px; padding: 0; border-radius: 50%;
  border: 2px solid var(--border); cursor: pointer; position: relative;
  transition: transform 120ms ease, box-shadow 160ms ease;
}
.swatch:hover { transform: scale(1.08); }
.swatch[aria-pressed="true"] { box-shadow: 0 0 0 2px var(--surface-2), 0 0 0 4px var(--accent); }
.swatch.swatch-auto { background: linear-gradient(135deg, #111 0 50%, #fff 50% 100%); }
#ink-bg {
  min-height: 38px; padding: 0 8px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--fg);
}
.ink-finger { display: inline-flex; align-items: center; gap: 6px; font-size: 0.82rem; color: var(--muted); cursor: pointer; white-space: nowrap; }
.ink-finger input { width: 18px; height: 18px; accent-color: var(--accent); }
.ink-scroll {
  flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch;
  background: var(--surface-2);
  padding: var(--sp-3) var(--sp-3) calc(var(--sp-4) + env(safe-area-inset-bottom));
}
#ink-canvas {
  display: block; width: 100%;
  background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  touch-action: none; /* finger draws by default; toggled to pan-y under palm rejection */
}

/* ink marker in note list */
.ink-list-mark {
  width: 13px; height: 13px; vertical-align: -2px; margin-left: 5px;
  fill: none; stroke: var(--accent-text); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}

@media (max-width: 820px) {
  .ink-toolbar { gap: 4px; }
  .ink-finger span { display: none; }
  .ink-btn.ink-wide span { display: none; }
}

/* ---------- Print / PDF export ---------- */
#print-root { display: none; }
.print-ink { max-width: 100%; height: auto; display: block; margin: 10pt 0; border: 1px solid #ddd; border-radius: 6px; page-break-inside: avoid; }
@media print {
  html.printing .app, html.printing .modal-backdrop, html.printing .img-viewer { display: none !important; }
  html.printing #print-root { display: block; }
  @page { size: A4; margin: 18mm 16mm; }
  html.printing, html.printing body { height: auto; overflow: visible; background: #fff; color: #000; }
  .print-note { font-family: Georgia, "Times New Roman", serif; color: #000; }
  .print-note h1 { font-size: 24pt; margin: 0 0 4pt; }
  .print-note .print-date { color: #555; font-size: 10pt; margin-bottom: 18pt; }
  .print-note .print-body { font-size: 12pt; line-height: 1.6; }
  .print-note img { max-width: 100%; height: auto; page-break-inside: avoid; display: block; margin: 8pt 0; }
  .print-note h2 { font-size: 16pt; }
}
