/* Palette by analogy with raccounting's Tailwind config (ink/brand/money) and its html.dark
   overrides — same hex values, just expressed as custom properties instead of utility classes,
   since this project has no build step to run Tailwind's CDN scanner against. */
:root {
  color-scheme: light dark;
  --font-sans: system-ui, -apple-system, 'Segoe UI', sans-serif;
  --font-mono: ui-monospace, 'SF Mono', Menlo, Consolas, monospace;

  --surface-0: #ffffff;
  --surface-1: #f8fafc;
  --surface-2: #f1f5f9;
  --border: #e2e8f0;
  --text: #14213d;
  --text-dim: #64748b;
  --accent: #2563eb;
  --danger: #dc2626;
  --warning-bg: #fef2f2;
  --warning-text: #b91c1c;
}

/* Dark theme: applied when the OS prefers dark and the user hasn't pinned "light" (see
   js/data/theme.js), or unconditionally once they've pinned "dark". */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --surface-0: #141d2e;
    --surface-1: #0b1120;
    --surface-2: #212c42;
    --border: #29354c;
    --text: #e7edf5;
    --text-dim: #94a3b8;
    --accent: #2563eb;
    --danger: #dc2626;
    --warning-bg: rgba(220, 38, 38, 0.18);
    --warning-text: #fca5a5;
    color-scheme: dark;
  }
}

:root[data-theme='dark'] {
  --surface-0: #141d2e;
  --surface-1: #0b1120;
  --surface-2: #212c42;
  --border: #29354c;
  --text: #e7edf5;
  --text-dim: #94a3b8;
  --accent: #2563eb;
  --danger: #dc2626;
  --warning-bg: rgba(220, 38, 38, 0.18);
  --warning-text: #fca5a5;
  color-scheme: dark;
}

:root[data-theme='light'] {
  color-scheme: light;
}

* {
  box-sizing: border-box;
}

html,
body,
#app {
  height: 100%;
  margin: 0;
}

body {
  font-family: var(--font-sans);
  background: var(--surface-0);
  color: var(--text);
}

button,
input {
  font-family: inherit;
}

button {
  -webkit-appearance: none;
  appearance: none;
}

input:focus-visible,
button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.app-shell {
  display: flex;
  height: 100vh;
}

/* Sidebar (note-list.js) */

.sidebar {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 280px;
  flex: none;
  border-right: 1px solid var(--border);
  background: var(--surface-1);
}

.sidebar-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.9rem 1rem 0.6rem;
}

.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: none;
  border: none;
  background: var(--accent);
  color: white;
  width: 1.8rem;
  height: 1.8rem;
  border-radius: 6px;
  cursor: pointer;
}

.icon-btn:hover {
  background: #3b82f6;
}

.search {
  flex: 1;
  min-width: 0;
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-2);
  color: inherit;
}

.tag-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  padding: 0 1rem 0.7rem;
  margin-bottom: 0.4rem;
  border-bottom: 1px solid var(--border);
}

.tag-chip {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-dim);
  border-radius: 999px;
  padding: 0.1rem 0.55rem;
  font-size: 0.75rem;
  cursor: pointer;
}

.tag-chip.active {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

.hint {
  padding: 0 1rem;
  color: var(--text-dim);
  font-size: 0.85rem;
}

.hint.error {
  color: var(--danger);
}

.note-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
}

.note-item {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.6rem 1rem;
  cursor: pointer;
  border-left: 3px solid transparent;
}

.note-item:hover {
  background: var(--surface-2);
}

.note-item.active {
  background: var(--surface-2);
  border-left-color: var(--accent);
}

.note-item-main {
  flex: 1;
  min-width: 0;
}

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

.note-meta {
  font-size: 0.75rem;
  color: var(--text-dim);
  margin-top: 0.15rem;
}

.note-delete {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 1.6rem;
  height: 1.6rem;
  border: none;
  background: transparent;
  color: var(--text-dim);
  border-radius: 6px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.1s ease;
}

.note-item:hover .note-delete,
.note-delete:focus-visible {
  opacity: 1;
}

.note-delete:hover {
  background: var(--surface-0);
  color: var(--danger);
}

/* Workspace (note-view.js) */

.workspace {
  display: flex;
  flex-direction: column;
  height: 100%;
  flex: 1;
  min-width: 0;
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.6rem 1rem;
  border-bottom: 1px solid var(--border);
  gap: 1rem;
}

.title-input {
  border: none;
  background: transparent;
  font-size: 1.05rem;
  font-weight: 600;
  color: inherit;
  flex: 1;
  min-width: 0;
}

/* Only shown on narrow screens (see the responsive layout section below), which show either the
   note list or the open note, never both — this is how you get back to the list. */
.back-btn {
  display: none;
  align-items: center;
  justify-content: center;
  flex: none;
  border: none;
  background: transparent;
  color: var(--text);
  width: 1.8rem;
  height: 1.8rem;
  border-radius: 6px;
  cursor: pointer;
}

.back-btn:hover {
  background: var(--surface-2);
}

.toolbar-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.save-state {
  font-size: 0.75rem;
  color: var(--text-dim);
  min-width: 5.5rem;
  text-align: right;
}

.mode-switch {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
}

.mode-switch button {
  border: none;
  background: transparent;
  color: inherit;
  padding: 0.4rem 0.8rem;
  font-size: 0.85rem;
  cursor: pointer;
  white-space: nowrap;
}

.mode-switch button.active {
  background: var(--accent);
  color: white;
}

.conflict-banner {
  background: var(--warning-bg);
  color: var(--warning-text);
  padding: 0.5rem 1rem;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  font-size: 0.85rem;
}

.conflict-banner button {
  border: 1px solid currentColor;
  background: transparent;
  color: inherit;
  border-radius: 4px;
  padding: 0.15rem 0.5rem;
  cursor: pointer;
}

.panes {
  flex: 1;
  display: flex;
  min-height: 0;
}

.panes.edit .pane,
.panes.preview .pane {
  flex: 1 1 100%;
}

.pane {
  flex: 1 1 50%;
  min-width: 0;
  height: 100%;
}

.editor-pane {
  border-right: 1px solid var(--border);
}

.empty-state {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
}

/* CodeMirror host + toolbar (editor.js) */

.cm-wrapper {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.cm-toolbar {
  display: flex;
  align-items: center;
  gap: 0.15rem;
  padding: 0.3rem 0.5rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface-1);
  flex: none;
}

.cm-toolbar button {
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  color: var(--text);
  min-width: 1.7rem;
  height: 1.7rem;
  padding: 0 0.3rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.8rem;
  line-height: 1;
}

.cm-toolbar button:hover {
  background: var(--surface-2);
}

.cm-toolbar .glyph-bold {
  font-weight: 700;
}

.cm-toolbar .glyph-italic {
  font-style: italic;
}

.cm-toolbar .glyph-strike {
  text-decoration: line-through;
}

.cm-toolbar .glyph-mono {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  font-weight: 600;
}

.cm-toolbar-sep {
  width: 1px;
  height: 1.1rem;
  background: var(--border);
  margin: 0 0.25rem;
}

.cm-host {
  flex: 1;
  min-height: 0;
  overflow: auto;
}

.cm-host .cm-editor {
  height: 100%;
  font-size: 0.95rem;
}

.cm-host .cm-scroller {
  font-family: var(--font-mono);
  padding: 1rem 1.25rem;
}

.cm-host .cm-gutters {
  background: transparent;
  border-right: none;
}

/* Markdown preview (preview.js) */

.markdown-preview {
  padding: 1.5rem 2rem;
  line-height: 1.6;
  overflow-y: auto;
  height: 100%;
}

.markdown-preview pre {
  background: var(--surface-2);
  padding: 0.75rem 1rem;
  border-radius: 6px;
  overflow-x: auto;
}

.markdown-preview code {
  font-family: var(--font-mono);
}

.markdown-preview table {
  border-collapse: collapse;
  margin: 0.5rem 0;
}

.markdown-preview th,
.markdown-preview td {
  border: 1px solid var(--border);
  padding: 0.4rem 0.75rem;
  text-align: left;
}

.markdown-preview th {
  background: var(--surface-1);
  font-weight: 600;
}

/* Boot screen (app.js) — shown only for the instant it takes /api/v1/auth/me to answer */

.boot-screen {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.5rem;
}

/* Login screen (login-view.js) — by analogy with raccounting's login view: a plain brand line above
   a white card, an in-card heading + subtitle, labeled fields with placeholder examples. */

.login-screen {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface-1);
  padding: 0 1rem;
}

.login-wrap {
  width: 100%;
  max-width: 24rem;
}

.login-outer-brand {
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--text);
  text-align: center;
  margin-bottom: 1rem;
}

.login-card {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding: 1.25rem;
  background: var(--surface-0);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

@media (min-width: 768px) {
  .login-card {
    padding: 1.5rem;
  }
}

.login-title {
  font-size: 1.125rem;
  font-weight: 600;
  color: var(--text);
  margin: 0;
}

.login-field {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-dim);
}

.login-field input {
  padding: 0.625rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-0);
  color: var(--text);
  font-size: 0.875rem;
}

.login-field input::placeholder {
  color: var(--text-dim);
}

.login-error {
  margin: 0;
  color: var(--danger);
  font-size: 0.875rem;
}

.login-submit {
  border: none;
  background: var(--accent);
  color: white;
  padding: 0.625rem;
  border-radius: 8px;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.15s ease;
}

.login-submit:hover:not(:disabled) {
  background: #3b82f6;
}

.login-submit:disabled {
  opacity: 0.5;
  cursor: default;
}

/* Sidebar footer / user menu (note-list.js) — theme, language and sign-out all live behind one
   trigger here, instead of being split between a header icon and a separate bottom row. */

.sidebar-footer {
  padding: 0.6rem;
  border-top: 1px solid var(--border);
  flex: none;
}

.user-menu {
  position: relative;
}

.user-menu-trigger {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  border: none;
  background: transparent;
  padding: 0.35rem 0.4rem;
  border-radius: 8px;
  cursor: pointer;
  color: inherit;
  font: inherit;
  text-align: left;
}

.user-menu-trigger:hover {
  background: var(--surface-2);
}

.user-avatar {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 1.7rem;
  height: 1.7rem;
  border-radius: 999px;
  background: var(--accent);
  color: white;
  font-size: 0.75rem;
  font-weight: 600;
}

.sidebar-user {
  flex: 1;
  min-width: 0;
  font-size: 0.8rem;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-menu-trigger .chevron {
  flex: none;
  color: var(--text-dim);
}

.menu-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
}

.user-menu-panel {
  position: absolute;
  bottom: calc(100% + 0.4rem);
  left: 0;
  right: 0;
  z-index: 50;
  background: var(--surface-0);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
  padding: 0.35rem;
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
}

.user-menu-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  width: 100%;
  border: none;
  background: transparent;
  color: var(--text);
  padding: 0.5rem 0.6rem;
  border-radius: 6px;
  font-size: 0.85rem;
  cursor: pointer;
  text-align: left;
}

.user-menu-item:hover {
  background: var(--surface-2);
}

.user-menu-lang select {
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-0);
  color: var(--text);
  font-size: 0.8rem;
  padding: 0.2rem 0.35rem;
}

.user-menu-sep {
  height: 1px;
  background: var(--border);
  margin: 0.25rem 0.2rem;
}

.user-menu-danger {
  color: var(--danger);
}

/* Credentials modal (credentials-modal.js) — reuses .login-field/.login-error/.login-submit from
   the login screen, so the form looks consistent wherever it appears. */

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.modal-card {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  width: 100%;
  max-width: 22rem;
  padding: 1.25rem;
  background: var(--surface-0);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.25);
}

.modal-title {
  font-size: 1.125rem;
  font-weight: 600;
  color: var(--text);
  margin: 0;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
}

.modal-cancel {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  padding: 0.55rem 0.9rem;
  border-radius: 8px;
  font-size: 0.875rem;
  cursor: pointer;
}

.modal-cancel:hover {
  background: var(--surface-2);
}

.modal-body {
  margin: 0;
  color: var(--text);
  font-size: 0.9rem;
}

.modal-danger {
  border: none;
  background: var(--danger);
  color: white;
  padding: 0.55rem 0.9rem;
  border-radius: 8px;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.15s ease;
}

.modal-danger:hover {
  background: #b91c1c;
}

/* Responsive layout — phones and narrow/portrait tablets.
   Below 768px there isn't room for the sidebar and the open note side by side, so .app-shell shows
   one full-width pane at a time: the note list, or the open note (with a .back-btn to return),
   toggled by app.js's `note-open` class, which mirrors whether a note is currently selected. */
@media (max-width: 768px) {
  .sidebar {
    width: 100%;
    border-right: none;
  }

  .app-shell.note-open .sidebar {
    display: none;
  }

  .app-shell:not(.note-open) .workspace,
  .app-shell:not(.note-open) .empty-state {
    display: none;
  }

  .back-btn {
    display: flex;
  }

  /* Split ("Оба") mode stacks the editor above the preview instead of side by side — at this width
     two side-by-side columns would be too narrow to write or read in. */
  .panes.split {
    flex-direction: column;
  }

  .panes.split .pane {
    flex: 1 1 50%;
    min-height: 0;
    width: 100%;
  }

  .panes.split .editor-pane {
    border-right: none;
    border-bottom: 1px solid var(--border);
  }

  /* The toolbar's title and mode-switch/save-state group no longer fit on one line next to the
     back button — let the second group wrap onto its own row instead of squeezing everything down
     to illegible widths. */
  .toolbar {
    flex-wrap: wrap;
    row-gap: 0.5rem;
  }

  .toolbar-actions {
    flex: 1 1 100%;
    justify-content: space-between;
  }

  /* The formatting toolbar's buttons don't need to wrap (which would eat vertical space on an
     already-short mobile screen) — they scroll horizontally instead, same as a native app's
     accessory toolbar. */
  .cm-toolbar {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
}

/* Touch targets: a mouse can hover to reveal .note-delete, but a touch screen can't — on any
   hover-incapable pointer (phones, tablets, and touchscreen laptops alike) it stays visible so it's
   actually reachable, regardless of viewport width. */
@media (hover: none) {
  .note-delete {
    opacity: 1;
  }
}
