/* Central label width for fixed-label forms */
:root {
  --form-label-width: 12rem;
}

@media (min-width: 576px) {
  .form-row-fixed {
    display: grid;
    grid-template-columns: var(--form-label-width) 1fr;
    column-gap: 1rem;
    align-items: start;
  }
  .form-row-fixed > .col-form-label {
    text-align: end;
  }
}

/* Comfortable touch targets */
.navbar .nav-link,
.dropdown-item,
.btn {
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}
.btn-sm {
  min-height: 38px;
}
.dropdown-item {
  display: flex;
}
.theme-form,
.dropdown-menu form {
  margin: 0;
}
.dropdown-menu form .dropdown-item {
  width: 100%;
}
.pagination .page-link {
  min-height: 44px;
  display: inline-flex;
  align-items: center;
}

/* Markdown editor and rendered output */
.editor-textarea {
  /* The height is set by the editor script so that the save buttons stay visible */
  resize: none;
  overflow-y: auto;
  line-height: 1.5;
  /* Soft wrap after --wrap-columns characters: the field is exactly that many characters wide
     (monospace font), plus padding, border and scrollbar. Never wider than its pane. */
  display: block;
  flex: 0 1 auto;
  min-width: 0;
  scrollbar-gutter: stable;
  width: calc(var(--wrap-columns, 80) * 1ch + 1.5rem + 2px + var(--scrollbar-width, 0px));
  max-width: 100%;
}
#previewPane {
  overflow: auto;
}
.markdown-body table {
  margin-bottom: 1rem;
}
.markdown-body th,
.markdown-body td {
  border: 1px solid var(--bs-border-color);
  padding: 0.4rem 0.6rem;
}
.markdown-body pre {
  background-color: var(--bs-tertiary-bg);
  border-radius: var(--bs-border-radius);
  padding: 0.75rem;
  overflow-x: auto;
}
.markdown-body blockquote {
  border-left: 0.25rem solid var(--bs-border-color);
  padding-left: 1rem;
  color: var(--bs-secondary-color);
}
.markdown-body > :last-child {
  margin-bottom: 0;
}

/* Version diff */
.diff-table td {
  padding: 0.1rem 0.5rem;
  border: 0;
  vertical-align: top;
}
.diff-no {
  width: 1%;
  min-width: 2.5rem;
  text-align: end;
  color: var(--bs-secondary-color);
  user-select: none;
}
.diff-sign {
  width: 1%;
  user-select: none;
}
.diff-text {
  white-space: pre-wrap;
  word-break: break-word;
}
.diff-add,
.diff-add td {
  background-color: var(--bs-success-bg-subtle);
}
.diff-del,
.diff-del td {
  background-color: var(--bs-danger-bg-subtle);
}
.diff-add .diff-word {
  background-color: var(--bs-success-border-subtle);
}
.diff-del .diff-word {
  background-color: var(--bs-danger-border-subtle);
}
.diff-word {
  padding: 0;
  color: inherit;
}
.diff-gap td {
  background-color: var(--bs-tertiary-bg);
}

/* Split editor: edit and live preview side by side (wide screens only) */
@media (min-width: 992px) {
  #editorArea.split {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
    align-items: start;
  }
  #editorArea.split #previewPane {
    position: sticky;
    top: 1rem;
    max-height: calc(100vh - 2rem);
    overflow: auto;
  }
}

/* Comments */
.comment-quote {
  border-left: 0.25rem solid var(--bs-warning-border-subtle);
  background-color: var(--bs-warning-bg-subtle);
  padding: 0.25rem 0.75rem;
  margin-bottom: 0.5rem;
  font-style: italic;
  overflow-wrap: anywhere;
}

/* Comments side pane in the editor (wide screens only) */
@media (min-width: 992px) {
  #editorLayout.with-comments {
    display: grid;
    /* wide enough for the tick, the checkbox and both buttons of a comment in one row */
    grid-template-columns: minmax(0, 1fr) minmax(16rem, 26rem);
    gap: 1rem;
    align-items: start;
  }
  #editorLayout.with-comments #commentPane {
    position: sticky;
    top: 1rem;
    max-height: calc(100vh - 2rem);
    overflow: auto;
  }
  #editorArea.split {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  }
}

/* Light scheme: sepia background everywhere. All Bootstrap surfaces (page, cards, navbar,
   inputs, dropdowns, modals, tables) derive from these variables. */
[data-bs-theme="light"] {
  --bs-body-bg: #f4ecd8;
  --bs-body-bg-rgb: 244, 236, 216;
  --bs-secondary-bg: #e8dcc0;
  --bs-secondary-bg-rgb: 232, 220, 192;
  --bs-tertiary-bg: #eee3c9;
  --bs-tertiary-bg-rgb: 238, 227, 201;
  --bs-light: #eee3c9;
  --bs-light-rgb: 238, 227, 201;
  --bs-body-color: #3b3020;
  --bs-body-color-rgb: 59, 48, 32;
  --bs-border-color: #d9cba6;
  --bs-border-color-translucent: rgba(59, 48, 32, 0.175);
}

/* Done comments are hidden until the switch is turned on */
.comment-list.hide-done .comment-card.is-done {
  display: none;
}
.comment-card.is-done {
  opacity: 0.7;
}
.comment-done-form {
  margin: 0;
}
.comment-done-label {
  cursor: pointer;
  white-space: nowrap;
}

/* Compact comments: icon-only buttons and tight spacing to save vertical space */
.btn-icon {
  /* comfortable touch target on phones, compact on larger screens */
  min-width: 44px;
  min-height: 44px;
  padding: 0;
  justify-content: center;
}
@media (min-width: 768px) {
  .btn-icon {
    min-width: 32px;
    min-height: 32px;
  }
}
.comment-card .card-body {
  padding: 0.5rem 0.75rem;
}
.comment-card {
  margin-bottom: 0.5rem !important;
}
.comment-body {
  margin-bottom: 0.25rem;
}
.comment-card .comment-quote {
  margin-bottom: 0.25rem;
  padding-top: 0.1rem;
  padding-bottom: 0.1rem;
}
.comment-meta {
  min-height: 0;
}
.comment-done-label {
  font-weight: 600;
  font-size: 0.8rem;
  letter-spacing: 0.03em;
}
.comment-quote-box {
  display: flex;
  align-items: flex-start;
  gap: 0.25rem;
}
.comment-quote-box .comment-quote {
  flex: 1 1 auto;
}
.comment-edit-form {
  display: flex;
  align-items: flex-start;
  gap: 0.25rem;
}
.comment-edit-form textarea {
  flex: 1 1 auto;
}

/* Optional line numbers next to the editor field */
.editor-frame {
  display: flex;
  align-items: flex-start;
}
.line-numbers {
  flex: 0 0 auto;
  overflow: hidden; /* scrolled by the script together with the text area */
  box-sizing: border-box;
  border: 1px solid var(--bs-border-color);
  border-right: 0;
  border-radius: var(--bs-border-radius) 0 0 var(--bs-border-radius);
  background-color: var(--bs-tertiary-bg);
  color: var(--bs-secondary-color);
  font-size: 1rem;
  line-height: 1.5;
  text-align: right;
  user-select: none;
}
.line-numbers-inner > div {
  padding-right: 0.5rem;
  padding-left: 0.5rem;
}
.editor-frame.with-line-numbers .editor-textarea {
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
}
/* Hidden helper that measures how many rows each line takes after soft wrapping */
.editor-mirror {
  position: absolute;
  left: -99999px;
  top: 0;
  visibility: hidden;
  pointer-events: none;
}

/* Contrast (WCAG AA, 4.5:1 for text). Bootstrap's outline buttons, links and red text fall
   below that on our dark and sepia backgrounds; the colours below are chosen per scheme. */
[data-bs-theme="dark"] {
  --bs-form-invalid-color: #ea868f;
}
[data-bs-theme="dark"] .btn-outline-secondary {
  --bs-btn-color: #adb5bd;
  --bs-btn-border-color: #adb5bd;
}
[data-bs-theme="dark"] .btn-outline-primary {
  --bs-btn-color: #6ea8fe;
  --bs-btn-border-color: #6ea8fe;
}
[data-bs-theme="dark"] .btn-outline-danger {
  --bs-btn-color: #ea868f;
  --bs-btn-border-color: #ea868f;
}
[data-bs-theme="dark"] .btn-outline-success {
  --bs-btn-color: #75b798;
  --bs-btn-border-color: #75b798;
}
[data-bs-theme="dark"] .text-danger {
  color: #ea868f !important;
}

[data-bs-theme="light"] {
  /* links: navy instead of Bootstrap's blue (which has only 3.8:1 on the sepia background) */
  --bs-link-color: #000080;
  --bs-link-color-rgb: 0, 0, 128;
  --bs-link-hover-color: #00005a;
  --bs-link-hover-color-rgb: 0, 0, 90;
}
[data-bs-theme="light"] .btn-outline-secondary {
  --bs-btn-color: #565e64;
  --bs-btn-border-color: #565e64;
  --bs-btn-hover-bg: #565e64;
  --bs-btn-hover-border-color: #565e64;
  --bs-btn-active-bg: #565e64;
  --bs-btn-active-border-color: #565e64;
}
[data-bs-theme="light"] .btn-outline-primary {
  --bs-btn-color: #000080;
  --bs-btn-border-color: #000080;
  --bs-btn-hover-bg: #000080;
  --bs-btn-hover-border-color: #000080;
  --bs-btn-active-bg: #000080;
  --bs-btn-active-border-color: #000080;
}
[data-bs-theme="light"] .btn-outline-danger {
  --bs-btn-color: #b02a37;
  --bs-btn-border-color: #b02a37;
  --bs-btn-hover-bg: #b02a37;
  --bs-btn-hover-border-color: #b02a37;
  --bs-btn-active-bg: #b02a37;
  --bs-btn-active-border-color: #b02a37;
}
[data-bs-theme="light"] .btn-outline-success {
  --bs-btn-color: #146c43;
  --bs-btn-border-color: #146c43;
  --bs-btn-hover-bg: #146c43;
  --bs-btn-hover-border-color: #146c43;
  --bs-btn-active-bg: #146c43;
  --bs-btn-active-border-color: #146c43;
}
[data-bs-theme="light"] .text-danger {
  color: #b02a37 !important;
}

/* Terminal scheme: green on black, in the editor and everywhere else. It is built on Bootstrap's
   dark scheme (data-bs-theme="dark" is set together with data-theme-pref="terminal"), so only
   the colours that differ are set here. Kept after the dark rules above so that it wins. */
[data-theme-pref="terminal"] {
  --bs-body-bg: #000000;
  --bs-body-bg-rgb: 0, 0, 0;
  --bs-body-color: #33ff66;
  --bs-body-color-rgb: 51, 255, 102;
  --bs-emphasis-color: #7dff9a;
  --bs-emphasis-color-rgb: 125, 255, 154;
  --bs-secondary-color: rgba(51, 255, 102, 0.75);
  --bs-tertiary-color: rgba(51, 255, 102, 0.55);
  --bs-secondary-bg: #0b2a14;
  --bs-secondary-bg-rgb: 11, 42, 20;
  --bs-tertiary-bg: #06180b;
  --bs-tertiary-bg-rgb: 6, 24, 11;
  --bs-border-color: #1f8a3f;
  --bs-border-color-translucent: rgba(51, 255, 102, 0.25);
  --bs-link-color: #9dffbb;
  --bs-link-color-rgb: 157, 255, 187;
  --bs-link-hover-color: #ffffff;
  --bs-link-hover-color-rgb: 255, 255, 255;
  --bs-focus-ring-color: rgba(51, 255, 102, 0.35);
}
[data-theme-pref="terminal"] .btn-primary {
  --bs-btn-color: #000000;
  --bs-btn-bg: #33ff66;
  --bs-btn-border-color: #33ff66;
  --bs-btn-hover-color: #000000;
  --bs-btn-hover-bg: #7dff9a;
  --bs-btn-hover-border-color: #7dff9a;
  --bs-btn-active-color: #000000;
  --bs-btn-active-bg: #7dff9a;
  --bs-btn-active-border-color: #7dff9a;
}
[data-theme-pref="terminal"] .btn-success {
  --bs-btn-color: #ffffff;
  --bs-btn-bg: #147a2e;
  --bs-btn-border-color: #33ff66;
  --bs-btn-hover-color: #000000;
  --bs-btn-hover-bg: #33ff66;
  --bs-btn-hover-border-color: #33ff66;
  --bs-btn-active-color: #000000;
  --bs-btn-active-bg: #33ff66;
  --bs-btn-active-border-color: #33ff66;
}
[data-theme-pref="terminal"] .btn-outline-secondary,
[data-theme-pref="terminal"] .btn-outline-primary,
[data-theme-pref="terminal"] .btn-outline-success {
  --bs-btn-color: #33ff66;
  --bs-btn-border-color: #1f8a3f;
  --bs-btn-hover-color: #000000;
  --bs-btn-hover-bg: #33ff66;
  --bs-btn-hover-border-color: #33ff66;
  --bs-btn-active-color: #000000;
  --bs-btn-active-bg: #33ff66;
  --bs-btn-active-border-color: #33ff66;
}
[data-theme-pref="terminal"] .btn-outline-danger {
  --bs-btn-color: #ff6b6b;
  --bs-btn-border-color: #ff6b6b;
  --bs-btn-hover-color: #000000;
  --bs-btn-hover-bg: #ff6b6b;
  --bs-btn-hover-border-color: #ff6b6b;
  --bs-btn-active-color: #000000;
  --bs-btn-active-bg: #ff6b6b;
  --bs-btn-active-border-color: #ff6b6b;
}
[data-theme-pref="terminal"] .text-danger {
  color: #ff6b6b !important;
}
[data-theme-pref="terminal"] .text-bg-secondary {
  background-color: #145a2a !important;
  color: #c9ffd9 !important;
}
[data-theme-pref="terminal"] .navbar {
  --bs-navbar-color: #33ff66;
  --bs-navbar-hover-color: #9dffbb;
  --bs-navbar-active-color: #ffffff;
  --bs-navbar-brand-color: #33ff66;
  --bs-navbar-brand-hover-color: #9dffbb;
}
[data-theme-pref="terminal"] .dropdown-menu {
  --bs-dropdown-bg: #000000;
  --bs-dropdown-border-color: #1f8a3f;
  --bs-dropdown-link-color: #33ff66;
  --bs-dropdown-link-hover-color: #7dff9a;
  --bs-dropdown-link-hover-bg: #0b2a14;
  --bs-dropdown-link-active-color: #ffffff;
  --bs-dropdown-link-active-bg: #147a2e;
  --bs-dropdown-header-color: rgba(51, 255, 102, 0.75);
  --bs-dropdown-divider-bg: #1f8a3f;
}
[data-theme-pref="terminal"] .form-control,
[data-theme-pref="terminal"] .form-select {
  background-color: #000000;
  color: #33ff66;
  border-color: #1f8a3f;
}
[data-theme-pref="terminal"] .form-control::placeholder {
  color: rgba(51, 255, 102, 0.6);
}
[data-theme-pref="terminal"] .form-control:focus,
[data-theme-pref="terminal"] .form-select:focus {
  background-color: #000000;
  color: #33ff66;
  border-color: #33ff66;
  box-shadow: 0 0 0 0.25rem rgba(51, 255, 102, 0.25);
}
[data-theme-pref="terminal"] .form-check-input {
  background-color: #000000;
  border-color: #33ff66;
}
[data-theme-pref="terminal"] .form-check-input:checked {
  background-color: #147a2e;
  border-color: #33ff66;
}
[data-theme-pref="terminal"] .form-check-input:focus {
  border-color: #33ff66;
  box-shadow: 0 0 0 0.25rem rgba(51, 255, 102, 0.25);
}
[data-theme-pref="terminal"] .comment-quote {
  border-left-color: #33ff66;
  background-color: #0b2a14;
}

/* The editor in the terminal scheme: green text on a black field */
[data-theme-pref="terminal"] .editor-textarea,
[data-theme-pref="terminal"] #previewPane {
  background-color: #000000;
  color: #33ff66;
  caret-color: #33ff66;
  border-color: #1f8a3f;
}
[data-theme-pref="terminal"] .editor-textarea::selection {
  background-color: #33ff66;
  color: #000000;
}
[data-theme-pref="terminal"] .line-numbers {
  background-color: #06180b;
  color: rgba(51, 255, 102, 0.75);
  border-color: #1f8a3f;
}

/* Project list on phones: slightly tighter cells so the action buttons fit without scrolling */
@media (max-width: 575.98px) {
  .project-table > :not(caption) > * > * {
    padding-left: 0.4rem;
    padding-right: 0.4rem;
  }
}
