/* ---------------------------------------------------------------
   Tokens
--------------------------------------------------------------- */
:root {
  color-scheme: light;

  --paper:        #eee9dd;
  --card:         #fbf9f3;
  --ink:          #211f1a;
  --ink-soft:     #6f6a5c;
  --ink-faint:    #a49f8f;
  --line:         #d9d2bf;
  --line-soft:    #e7e1d0;
  --dot:          rgba(33, 31, 26, 0.05);
  --accent:       #2f6459;
  --accent-hover: #244f46;
  --accent-wash:  #dfe9e4;
  --danger:       #a8432b;
  --danger-wash:  #f1ddd4;
  --shadow: 0 1px 2px rgba(33, 31, 26, 0.06), 0 6px 16px -8px rgba(33, 31, 26, 0.18);
  --shadow-lg: 0 12px 36px -12px rgba(33, 31, 26, 0.35);

  --font-display: "Lora", Georgia, serif;
  --font-body: "Inter", system-ui, sans-serif;
  --font-mono: "IBM Plex Mono", ui-monospace, monospace;

  --radius: 3px;
  --sidebar-w: 300px;
}

[data-theme="dark"] {
  color-scheme: dark;

  --paper:        #1b1a17;
  --card:         #242320;
  --ink:          #ece7da;
  --ink-soft:     #a9a290;
  --ink-faint:    #726c5c;
  --line:         #3a3733;
  --line-soft:    #2e2c28;
  --accent:       #5fb3a1;
  --accent-hover: #7cc4b4;
  --accent-wash:  #24413b;
  --danger:       #e08a6d;
  --danger-wash:  #3a231c;
  --dot:          rgba(236, 231, 218, 0.04);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.35), 0 6px 16px -8px rgba(0, 0, 0, 0.5);
  --shadow-lg: 0 12px 36px -12px rgba(0, 0, 0, 0.65);
}

@media (prefers-color-scheme: dark) {
  [data-theme="system"] {
    color-scheme: dark;
    
    --paper:        #1b1a17;
    --card:         #242320;
    --ink:          #ece7da;
    --ink-soft:     #a9a290;
    --ink-faint:    #726c5c;
    --line:         #3a3733;
    --line-soft:    #2e2c28;
    --accent:       #5fb3a1;
    --accent-hover: #7cc4b4;
    --accent-wash:  #24413b;
    --danger:       #e08a6d;
    --danger-wash:  #3a231c;
    --dot:          rgba(236, 231, 218, 0.04);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.35), 0 6px 16px -8px rgba(0, 0, 0, 0.5);
    --shadow-lg: 0 12px 36px -12px rgba(0, 0, 0, 0.65);
  }
}

/* Theme switch — segmented control matching the app's existing look */
.theme-switch {
  display: flex;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
.theme-option {
  position: relative;
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.4rem 0.3rem;
  font-family: var(--font-mono);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--ink-soft);
  cursor: pointer;
  border-right: 1px solid var(--line);
  transition: background-color 120ms ease, color 120ms ease;
}
.theme-option:last-child { border-right: none; }
.theme-option:hover { background: var(--line-soft); }
.theme-option input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}
.theme-option:has(input:checked) {
  background: var(--accent);
  color: #f6f4ec;
}
.theme-option:has(input:focus-visible) {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

* { box-sizing: border-box; }

/* The `hidden` attribute must always win over component-level `display`
   rules (e.g. .modal{display:flex}, .icon-btn{display:inline-flex}) — without
   this, elements marked hidden in markup can still render because an author
   rule setting `display` on the same element beats the UA's [hidden] rule. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-body);
  -webkit-font-smoothing: antialiased;
}

body {
  background-image:
    radial-gradient(circle at 1px 1px, var(--dot) 1px, transparent 0);
  background-size: 22px 22px;
}

button, input, textarea {
  font-family: inherit;
  color: inherit;
}

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  white-space: nowrap;
}

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

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

/* ---------------------------------------------------------------
   Buttons
--------------------------------------------------------------- */
.btn {
  font-family: var(--font-body);
  font-weight: 600;
  font-size: 0.85rem;
  letter-spacing: 0.01em;
  border-radius: var(--radius);
  border: 1px solid transparent;
  padding: 0.6rem 1rem;
  cursor: pointer;
  transition: background-color 120ms ease, border-color 120ms ease, transform 80ms ease;
}
.btn:active { transform: translateY(1px); }

.btn-primary {
  background: var(--accent);
  color: #f6f4ec;
}
.btn-primary:hover { background: var(--accent-hover); }

.btn-ghost {
  background: transparent;
  border-color: var(--line);
  color: var(--ink-soft);
}
.btn-ghost:hover { border-color: var(--ink-faint); color: var(--ink); }

.btn-danger {
  background: var(--danger);
  color: #fbf3ee;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  cursor: pointer;
  color: var(--ink-soft);
  border-radius: var(--radius);
  transition: background-color 120ms ease, color 120ms ease;
}
.icon-btn:hover { color: var(--ink); background: var(--line-soft); }
.icon {
  font-size: 1.05rem;
  line-height: 1;
  font-style: normal;
  pointer-events: none;
}

/* ---------------------------------------------------------------
   Top bar (mobile only)
--------------------------------------------------------------- */
.topbar {
  display: none;
  align-items: center;
  gap: 0.75rem;
  padding: 0.7rem 0.9rem;
  background: var(--card);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 30;
}
.topbar-title {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1.05rem;
  flex: 1;
}
.burger {
  width: 38px; height: 38px;
  flex-direction: column;
  gap: 4px;
}
.burger span {
  display: block;
  width: 18px; height: 2px;
  background: var(--ink);
  border-radius: 2px;
}
.add-btn-mobile {
  width: 34px; height: 34px;
  border-radius: 50%;
  background: var(--accent);
  color: #f6f4ec;
  font-size: 1.2rem;
  font-weight: 500;
  line-height: 1;
}
.add-btn-mobile:hover { background: var(--accent-hover); color: #f6f4ec; }

/* ---------------------------------------------------------------
   App shell
--------------------------------------------------------------- */
.app {
  display: flex;
  align-items: flex-start;
  min-height: 100vh;
}

.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  min-height: 100vh;
  background: var(--card);
  border-right: 1px solid var(--line);
  padding: 1.5rem 1.25rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1.4rem;
  position: sticky;
  top: 0;
}

.sidebar-header {
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

.brand {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1.35rem;
  line-height: 1.15;
  margin: 0;
}

.add-btn-desktop { width: 100%; padding: 0.7rem 1rem; }

.field-block {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}
.field-block-grow { flex: 1; min-height: 0; }

.field-label {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--ink-faint);
}

.text-input {
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--ink);
  border-radius: var(--radius);
  padding: 0.5rem 0.65rem;
  font-size: 0.9rem;
}
.text-input:focus { border-color: var(--accent); }

.date-range { display: flex; align-items: center; gap: 0.4rem; }
.date-input { flex: 1; min-width: 0; font-size: 0.8rem; padding: 0.45rem 0.4rem; }
.date-sep { color: var(--ink-faint); }

.tag-checklist {
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  padding-right: 0.25rem;
}
.tag-empty { color: var(--ink-faint); font-size: 0.85rem; margin: 0.2rem 0; }

.tag-check {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  padding: 0.35rem 0.3rem;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.87rem;
}
.tag-check:hover { background: var(--line-soft); }
.tag-check input { accent-color: var(--accent); }
.tag-check .tag-name { user-select: none; flex: 1; font-family: var(--font-mono); font-size: 0.82rem; }
.tag-check .tag-count { user-select: none; color: var(--ink-faint); font-family: var(--font-mono); font-size: 0.75rem; }

.clear-btn { width: 100%; }

.sidebar-scrim {
  display: none;
}

/* ---------------------------------------------------------------
   Main / card grid
--------------------------------------------------------------- */
.main {
  flex: 1;
  min-width: 0;
  padding: 2rem 2.25rem 4rem;
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 1.15rem;
}

.empty-state {
  text-align: center;
  padding: 4rem 1rem;
  color: var(--ink-soft);
}
.empty-title { font-family: var(--font-display); font-size: 1.1rem; margin: 0 0 0.3rem; }
.empty-sub { margin: 0; font-size: 0.9rem; color: var(--ink-faint); }

/* ---------------------------------------------------------------
   Card (grid item + shared look with modal card)
--------------------------------------------------------------- */
.card {
  position: relative;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.1rem 1.2rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  cursor: pointer;
  transition: transform 120ms ease, box-shadow 120ms ease;
  text-align: left;
  max-height: 25em;
}
.card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg);
}

.card-title {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1.08rem;
  line-height: 1.3;
  margin: 0;
  word-break: break-word;
}

.card-rule {
  height: 1px;
  background: repeating-linear-gradient(to right, var(--line) 0 6px, transparent 6px 10px);
  margin: 0 0 0 0;
}

.card-content {
  font-size: 0.88rem;
  line-height: 1.55;
  color: var(--ink);
  word-break: break-word;
  flex: 1;
}
.card-grid .card-content {
  display: block;
  -webkit-line-clamp: 6;
  -webkit-box-orient: vertical;
  overflow: scroll;
}

/* Typography for rendered Markdown content (card content is stored as raw
   Markdown and rendered to HTML client-side — see renderMarkdown() in app.js) */
.card-content > *:first-child { margin-top: 0; }
.card-content > *:last-child { margin-bottom: 0; }
.card-content p { margin: 0 0 0.65em; }
.card-content ul, .card-content ol { margin: 0 0 0.65em; padding-left: 1.25em; }
.card-content li { margin: 0.15em 0; }
.card-content li > p { margin: 0; }
.card-content h1, .card-content h2, .card-content h3,
.card-content h4, .card-content h5, .card-content h6 {
  font-family: var(--font-display);
  font-weight: 600;
  line-height: 1.25;
  margin: 0.7em 0 0.3em;
}
.card-content h1 { font-size: 1.25em; }
.card-content h2 { font-size: 1.12em; }
.card-content h3, .card-content h4, .card-content h5, .card-content h6 { font-size: 1.02em; }
.card-content code {
  font-family: var(--font-mono);
  font-size: 0.85em;
  background: var(--line-soft);
  padding: 0.1em 0.35em;
  border-radius: 3px;
}
.card-content pre {
  font-family: var(--font-mono);
  font-size: 0.82em;
  background: var(--line-soft);
  padding: 0.65em 0.8em;
  border-radius: 4px;
  overflow-x: auto;
  margin: 0 0 0.65em;
}
.card-content pre code { background: none; padding: 0; }
.card-content blockquote {
  margin: 0 0 0.65em;
  padding-left: 0.8em;
  border-left: 2px solid var(--line);
  color: var(--ink-soft);
}
.card-content a {
  color: var(--accent);
  text-decoration: underline;
  text-decoration-color: var(--accent-wash);
  text-underline-offset: 2px;
}
.card-content hr { border: none; border-top: 1px solid var(--line); margin: 0.8em 0; }
.card-content img { max-width: 100%; border-radius: 3px; }
.card-content table { border-collapse: collapse; margin: 0 0 0.65em; font-size: 0.92em; }
.card-content th, .card-content td { border: 1px solid var(--line); padding: 0.3em 0.55em; }

/* Fallback rendering if the Markdown library failed to load (see app.js) */
.card-content .md-fallback {
  white-space: pre-wrap;
  word-break: break-word;
  font-family: inherit;
  font-size: inherit;
  margin: 0;
}

/* KaTeX math ($inline$ / $$display$$) — KaTeX's own stylesheet handles glyph
   layout; this just keeps wide formulas (big fractions, matrices) from
   breaking card width, and stops them being clipped in the grid preview. */
.card-content .katex-display {
  margin: 0.5em 0 0.65em;
  overflow-x: auto;
  overflow-y: hidden;
  padding-bottom: 2px; /* room for descenders when a scrollbar appears */
}
.card-content .katex { font-size: 1em; }

/* Mermaid diagrams (```mermaid fences). Only the open card modal actually
   renders these (see renderMermaidIn() in app.js) — in the grid preview
   they're left un-rendered on purpose, so style the raw-text state to look
   like an intentional "open to view diagram" placeholder rather than a
   broken code block. Once Mermaid runs, it replaces this element's content
   with an <svg>, so these rules only ever apply to the un-rendered state. */
.card-content .mermaid {
  display: block;
  font-family: var(--font-mono);
  font-size: 0.78em;
  white-space: pre-wrap;
  word-break: break-word;
  background: var(--line-soft);
  border: 1px dashed var(--line);
  border-radius: 4px;
  padding: 0.6em 0.75em;
  margin: 0 0 0.65em;
  color: var(--ink-soft);
}
.card-content .mermaid svg { max-width: 100%; height: auto; }

/* Once rendered, the diagram itself should look like a normal card element
   again, not a dashed placeholder box. */
.card-content .mermaid:has(svg) {
  display: flex;
  justify-content: center;
  background: none;
  border: none;
  padding: 0.4em 0;
  overflow-x: auto;
}

.card-footer {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 0.6rem;
  flex-wrap: wrap;
}

.tag-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  flex: 1;
  min-width: 0;
}

.tag-chip {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  background: var(--accent-wash);
  color: var(--accent-hover);
  border: 1px solid transparent;
  border-radius: 999px;
  padding: 0.18rem 0.6rem;
  cursor: pointer;
  line-height: 1.5;
}
.tag-chip:hover { border-color: var(--accent); }
.tag-chip.is-active { background: var(--accent); color: #f6f4ec; }

.card-date {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  color: var(--ink-faint);
  white-space: nowrap;
}

/* ---------------------------------------------------------------
   Modal
--------------------------------------------------------------- */
.modal-scrim {
  position: fixed;
  inset: 0;
  background: rgba(28, 26, 21, 0.42);
  z-index: 40;
}

.modal {
  position: fixed;
  z-index: 50;
  top: 6vh;
  left: 50%;
  transform: translateX(-50%);
  width: min(620px, 92vw);
  max-height: 88vh;
  max-height: 88dvh; /* overrides the line above on browsers that support dvh */
  display: flex;
  flex-direction: column;
  background: var(--card);
  border-radius: 6px;
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}

.modal-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 0.6rem;
  border-bottom: 1px solid var(--line-soft);
  flex-shrink: 0;
}
.modal-toolbar-left { display: flex; gap: 0.15rem; }
.toolbar-btn { width: 36px; height: 36px; }
.toolbar-btn.confirm-delete { color: var(--danger); background: var(--danger-wash); }

.modal-card {
  position: relative;
  padding: 0.5rem 1.75rem 1.75rem;
  overflow-y: auto;
  flex: 1;
}

.card-view .card-title { font-size: 1.5rem; }
.card-view .card-content { font-size: 0.95rem; margin-top: 0.9rem; }
.card-view .card-footer { margin-top: 1.4rem; }

/* Edit mode fields, styled to match the read view */
.card-title-input {
  width: 100%;
  border: none;
  background: transparent;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1.5rem;
  padding: 0;
}
.card-title-input::placeholder { color: var(--ink-faint); }

.card-content-input {
  width: 100%;
  border: none;
  background: transparent;
  resize: vertical;
  min-height: 140px;
  font-family: var(--font-body);
  font-size: 0.95rem;
  line-height: 1.6;
  margin-top: 0.9rem;
  padding: 0;
}
.card-content-input::placeholder { color: var(--ink-faint); }

.card-edit .card-footer-edit { margin-top: 1.2rem; padding-bottom: 3.5rem; }

.tag-row-edit .tag-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding-right: 0.4rem;
}
.tag-remove {
  border: none;
  background: none;
  color: var(--danger);
  cursor: pointer;
  font-family: var(--font-mono);
  font-size: 0.85rem;
  line-height: 1;
  padding: 0;
  opacity: 0.8;
}
.tag-remove:hover { opacity: 1; }

.tag-input {
  border: none;
  border-bottom: 1px dashed var(--line);
  background: transparent;
  font-family: var(--font-mono);
  font-size: 0.78rem;
  padding: 0.18rem 0.1rem;
  width: 90px;
}
.tag-input:focus { border-bottom-color: var(--accent); }

.modal-fab-row {
  position: sticky;
  bottom: 0;
  display: flex;
  justify-content: flex-end;
  gap: 0.6rem;
  margin: 0.85rem -1.75rem -1.75rem;
  padding: 0.85rem 1.75rem 1.4rem;
  background: linear-gradient(to top, var(--card) 65%, transparent);
  flex-shrink: 0;
}
.fab {
  border-radius: 999px;
  padding: 0.65rem 1.3rem;
  box-shadow: var(--shadow);
}

/* ---------------------------------------------------------------
   Toast
--------------------------------------------------------------- */
.toast {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--paper);
  font-size: 0.85rem;
  padding: 0.55rem 1rem;
  border-radius: var(--radius);
  z-index: 60;
  box-shadow: var(--shadow-lg);
}

/* ---------------------------------------------------------------
   Responsive — collapsible sidebar under 860px
--------------------------------------------------------------- */
@media (max-width: 860px) {
  .topbar { display: flex; }

  .app { display: block; }

  .sidebar {
    position: fixed;
    top: 0; left: 0;
    height: 100vh;
    z-index: 41;
    transform: translateX(-100%);
    transition: transform 200ms ease;
    box-shadow: var(--shadow-lg);
  }
  .sidebar.is-open { transform: translateX(0); }

  .add-btn-desktop { display: none; }

  .sidebar-scrim {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(28, 26, 21, 0.42);
    z-index: 40;
    opacity: 0;
    pointer-events: none;
    transition: opacity 200ms ease;
  }
  .sidebar-scrim.is-visible { opacity: 1; pointer-events: auto; }

  .main { padding: 1.25rem 1rem 3rem; }
}

@media (max-width: 480px) {
  .modal {
    top: 0;
    width: 100vw;
    max-height: 100vh;
    max-height: 100dvh;
    height: 100vh;
    height: 100dvh;
    border-radius: 0;
  }
}
