:root {
  --bg: #f5f4f0;
  --surface: #ffffff;
  --surface-2: #eeece6;
  --text: #1d1f24;
  --muted: #6b6f78;
  --border: #dcd9d0;
  --accent: #1f2a44;
  --accent-text: #ffffff;
  --accent-soft: #e3e7f1;
  --danger: #a12828;
  --done: #8a8d94;
  --radius: 8px;
  --shadow: 0 8px 30px rgba(20, 22, 30, 0.12);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171c;
    --surface: #1d2027;
    --surface-2: #262a33;
    --text: #e7e8eb;
    --muted: #9a9fa9;
    --border: #333844;
    --accent: #8fa6e0;
    --accent-text: #11141b;
    --accent-soft: #283149;
    --danger: #e07070;
    --done: #6d717a;
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.45);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

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

input, select, textarea {
  width: 100%;
  padding: 7px 9px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
}

textarea { resize: vertical; }

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

label { display: block; color: var(--muted); font-size: 12px; margin-bottom: 10px; }
label > input, label > select, label > textarea { margin-top: 3px; color: var(--text); font-size: 14px; }

.btn {
  padding: 7px 14px;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 6px;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.1); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 3px 10px; font-size: 12px; display: inline-block; margin: 0; color: var(--text); }

.icon-btn {
  width: 26px; height: 26px;
  border: none; background: transparent; border-radius: 6px;
  font-size: 18px; line-height: 1; cursor: pointer; color: var(--muted);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

/* ---- Layout ------------------------------------------------------------- */

.layout {
  display: grid;
  grid-template-columns: 250px minmax(0, 1fr) auto;
  height: 100vh;
}

.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 14px 10px;
  overflow-y: auto;
}

.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 18px; padding: 2px 6px 14px; }
.logout {
  margin-left: auto; border: none; background: transparent; cursor: pointer;
  color: var(--muted); font-size: 12px; font-weight: 400; padding: 3px 6px; border-radius: 6px;
}
.logout:hover { background: var(--surface-2); color: var(--text); }

/* ---- Login -------------------------------------------------------------- */

.login-page { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 340px; background: var(--surface);
  border: 1px solid var(--border); border-radius: 12px; padding: 24px;
}
.login-card .brand { padding: 0 0 18px; }
.login-card .btn { width: 100%; margin-top: 14px; }
.login-error { color: var(--danger); font-size: 13px; margin: 10px 0 0; }
.login-card p { font-size: 14px; line-height: 1.5; margin: 0 0 12px; }
.consent-target { color: var(--muted); font-size: 13px !important; }
.consent-actions { display: flex; gap: 8px; margin-top: 14px; }
.consent-actions .btn { flex: 1; margin-top: 0; width: auto; }

#search { margin-bottom: 12px; }

.nav-list { list-style: none; margin: 0 0 8px; padding: 0; }
.nav-list li button {
  display: flex; justify-content: space-between; gap: 8px;
  width: 100%; padding: 6px 8px;
  border: none; background: transparent; border-radius: 6px;
  text-align: left; cursor: pointer;
}
.nav-list li button:hover { background: var(--surface-2); }
.nav-list li button.active { background: var(--accent-soft); font-weight: 600; }
.nav-list .count { color: var(--muted); font-size: 12px; font-weight: 400; }

.nav-heading {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 8px 4px;
  color: var(--muted); font-size: 11px; font-weight: 600;
  text-transform: uppercase; letter-spacing: 0.06em;
  cursor: default;
}
details summary.nav-heading { cursor: pointer; list-style: none; justify-content: flex-start; gap: 4px; }
details summary.nav-heading::-webkit-details-marker { display: none; }
details summary.nav-heading::before { content: "▸"; }
details[open] summary.nav-heading::before { content: "▾"; }

.main { overflow-y: auto; padding: 24px 32px 60px; }

.view-header h1 { margin: 0 0 2px; font-size: 22px; }
.view-header .sub { color: var(--muted); display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.view-header .summary {
  margin: 14px 0 0; padding: 12px 14px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  white-space: pre-wrap;
}
.view-header .summary.empty { color: var(--muted); font-style: italic; }
.view-header .head-row { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; }
.head-actions { display: flex; gap: 8px; flex: none; }
.status-select { width: auto; }
.section-label { color: var(--muted); font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; margin-bottom: 4px; }
.nav-list .case-status, .case-hits .case-status { display: block; color: var(--muted); font-size: 11px; font-weight: 400; }
#nav-archive details { margin-top: 2px; }

.case-hits { list-style: none; margin: 14px 0 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.case-hits button {
  width: 100%; text-align: left; cursor: pointer;
  padding: 10px 14px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
.case-hits button:hover { border-color: var(--accent); }
.case-hits .case-status { display: inline; margin-left: 8px; }

/* Case content pasted from the RP's WYSIWYG editor */
.content-empty { margin-top: 14px; display: flex; flex-direction: column; align-items: flex-start; gap: 6px; }
.paste-zone {
  width: 100%; min-height: 90px; padding: 18px;
  border: 2px dashed var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--muted); cursor: text;
  display: flex; align-items: center; justify-content: center; text-align: center;
}
.paste-zone:empty::before { content: attr(data-placeholder); }
.paste-zone:focus { outline: none; border-color: var(--accent); background: var(--accent-soft); }

.case-content { margin-top: 14px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.case-content > summary {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 14px; cursor: pointer; list-style: none;
  color: var(--muted); font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em;
}
.case-content > summary::before { content: "▸"; margin-right: 6px; }
.case-content[open] > summary::before { content: "▾"; }
.case-content > summary > span { flex: 1; }
.case-content[open] > summary { border-bottom: 1px solid var(--border); }
.case-content > .case-content-body { padding: 4px 16px 12px; max-height: 60vh; overflow: auto; }

.case-content-body { overflow-wrap: anywhere; }
.case-content-body img { max-width: 100%; height: auto; }
.case-content-body table { border-collapse: collapse; margin: 8px 0; }
.case-content-body td, .case-content-body th { border: 1px solid var(--border); padding: 4px 8px; vertical-align: top; }
.case-content-body h1, .case-content-body h2, .case-content-body h3 { margin: 14px 0 6px; line-height: 1.3; }
.case-content-body h1 { font-size: 20px; } .case-content-body h2 { font-size: 17px; } .case-content-body h3 { font-size: 15px; }
.case-content-body p { margin: 6px 0; }

dialog.wide { width: min(980px, calc(100vw - 32px)); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 12px; }
.dialog-head h2 { margin: 0; }
.rich-editor {
  min-height: 45vh; max-height: 62vh; overflow: auto;
  padding: 10px 14px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg);
}
.rich-editor:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
#content-html { font: 12px/1.5 ui-monospace, "SF Mono", Consolas, monospace; min-height: 45vh; }

.field-grid.three { grid-template-columns: 1fr 1fr 1fr; }
.title-parts { margin-top: -4px; }
.title-parts input { font-size: 13px; padding: 5px 8px; background: var(--bg); }


.quick {
  display: flex; gap: 10px; align-items: stretch;
  margin: 20px 0 16px;
}
.quick textarea { flex: 1; }
.quick-actions { display: flex; flex-direction: column; gap: 6px; width: 150px; }

.toolbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 10px; }
.tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.tabs button {
  border: 1px solid transparent; background: transparent;
  padding: 4px 10px; border-radius: 999px; cursor: pointer; color: var(--muted);
}
.tabs button:hover { color: var(--text); }
.tabs button.active { background: var(--accent-soft); color: var(--text); border-color: var(--border); }
.new-entry { display: flex; gap: 6px; }
.new-entry select { width: auto; }

/* ---- Entries ------------------------------------------------------------ */

.entries { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.entries .empty { color: var(--muted); padding: 30px 0; text-align: center; }

.entry {
  display: flex; gap: 12px; align-items: flex-start;
  padding: 12px 14px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  cursor: pointer;
}
.entry:hover { border-color: var(--accent); }
.entry.selected { outline: 2px solid var(--accent); outline-offset: -1px; }
.entry input[type="checkbox"] { width: 18px; height: 18px; margin-top: 2px; flex: none; cursor: pointer; }
.entry .content { min-width: 0; flex: 1; }
.entry .title-row { display: flex; gap: 8px; align-items: baseline; }
.entry .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.entry .details { color: var(--muted); font-size: 12px; margin-top: 1px; }
.entry .snippet { margin-top: 4px; color: var(--text); opacity: 0.85; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; white-space: pre-line; }
.entry .date { margin-left: auto; color: var(--muted); font-size: 12px; white-space: nowrap; flex: none; }
.entry.done .title { text-decoration: line-through; color: var(--done); }

.badge {
  display: inline-block; flex: none;
  padding: 1px 8px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted);
  font-size: 11px; font-weight: 600;
}

/* ---- Editor ------------------------------------------------------------- */

.editor {
  width: 460px;
  background: var(--surface);
  border-left: 1px solid var(--border);
  overflow: hidden;
}
.editor[hidden] { display: none; }
.editor form { display: flex; flex-direction: column; height: 100%; }
.editor-header { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.editor-body { flex: 1; overflow-y: auto; padding: 16px; }
.editor-footer { display: flex; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--border); }
.editor-footer .danger { margin-left: auto; }

.field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }
.field-grid .wide { grid-column: 1 / -1; }
.field-grid label.check { display: flex; align-items: center; gap: 8px; color: var(--text); font-size: 14px; padding-top: 18px; }
.field-grid label.check input { width: auto; margin: 0; }
.hint { color: var(--muted); font-size: 12px; margin: 6px 0 0; }
.meta { color: var(--muted); font-size: 12px; }

.attachments { margin: 6px 0 10px; }
.attachments-head { display: flex; justify-content: space-between; align-items: center; color: var(--muted); font-size: 12px; margin-bottom: 6px; }
.thumbs { display: flex; flex-wrap: wrap; gap: 8px; }
.thumb { position: relative; width: 96px; height: 96px; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; background: var(--surface-2); }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb.file a {
  display: flex; flex-direction: column; justify-content: center; align-items: center; gap: 2px;
  height: 100%; padding: 6px; text-decoration: none; color: var(--text); text-align: center;
}
.thumb.file .ext { font-weight: 700; font-size: 13px; text-transform: uppercase; color: var(--accent); }
.thumb.file .name { font-size: 11px; line-height: 1.2; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; word-break: break-all; }
.thumb.file .text-state { font-size: 10px; color: var(--muted); }
.editor.drop { outline: 3px dashed var(--accent); outline-offset: -6px; }
.thumb button {
  position: absolute; top: 3px; right: 3px;
  width: 22px; height: 22px; border-radius: 50%;
  border: none; background: rgba(0, 0, 0, 0.6); color: #fff; cursor: pointer;
}

/* ---- Dialogs & toast ---------------------------------------------------- */

dialog {
  width: min(640px, calc(100vw - 32px));
  border: 1px solid var(--border); border-radius: 12px;
  background: var(--surface); color: var(--text);
  box-shadow: var(--shadow); padding: 20px 22px;
}
dialog::backdrop { background: rgba(10, 12, 18, 0.45); }
dialog h2 { margin: 0 0 14px; font-size: 18px; }
.dialog-footer { display: flex; gap: 8px; margin-top: 8px; }
.spacer { flex: 1; }
#render-output {
  max-height: 60vh; overflow: auto;
  background: var(--bg); border: 1px solid var(--border); border-radius: 6px;
  padding: 14px; white-space: pre-wrap; font: 13px/1.5 ui-monospace, "SF Mono", Consolas, monospace;
}

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg);
  padding: 8px 16px; border-radius: 8px; box-shadow: var(--shadow);
}
.toast.error { background: var(--danger); color: #fff; }

/* ---- Narrow windows ----------------------------------------------------- */

@media (max-width: 1100px) {
  .editor { position: fixed; inset: 0 0 0 auto; width: min(460px, 100vw); box-shadow: var(--shadow); z-index: 10; }
}

@media (max-width: 720px) {
  .layout { grid-template-columns: 1fr; height: auto; }
  .sidebar { border-right: none; border-bottom: 1px solid var(--border); max-height: 40vh; }
  .main { padding: 16px; overflow: visible; }
  .quick { flex-direction: column; }
  .quick-actions { width: 100%; flex-direction: row; }
  .field-grid, .field-grid.three { grid-template-columns: 1fr; }
  .view-header .head-row { flex-direction: column; }
}
