/* ==========================================================================
   Avezz — Word-Style Markdown Editor (1:1 UI/markdown-editor.html)
   Used by Text/Page.php (/write, /article/{id}/edit) with $minimalShell = true.
   ========================================================================== */

:root {
  --md-desk: #eceef2;
  --md-chrome: #f8f9fb;
  --md-paper: #ffffff;
  --md-blue: #185abd;
  --md-blue-hover: #144ca3;
  --md-blue-soft: rgba(24, 90, 189, 0.11);
  --md-shadow-paper: 0 1px 3px rgba(15, 23, 42, 0.08), 0 12px 34px rgba(15, 23, 42, 0.07);
  --md-zoom: 1;
}

[data-theme="dark"] {
  --md-desk: #0d0e11;
  --md-chrome: #16181d;
  --md-paper: #1d2026;
  --md-blue: #4c9aff;
  --md-blue-hover: #6cb0ff;
  --md-blue-soft: rgba(76, 154, 255, 0.16);
  --md-shadow-paper: 0 2px 6px rgba(0, 0, 0, 0.45), 0 16px 40px rgba(0, 0, 0, 0.45);
}

body.scroll-body,
body.md-editor-body {
  padding-top: 0;
  background: var(--md-desk);
  color: var(--text);
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.md-shell {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  width: 100%;
  background: var(--md-desk);
  overflow: hidden;
}

.md-shell form.md-form-wrap {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
}

/* ==========================================================================
   TITLE BAR (Cover + Title + Categories + +18 | View Toggle | Actions)
   ========================================================================== */
.md-titlebar {
  position: relative;
  z-index: 30;
  background: var(--md-chrome);
  border-bottom: 1px solid var(--border);
  padding: 7px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px 10px;
  flex-shrink: 0;
  flex-wrap: wrap;
}

.md-title-left {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  flex: 1 1 280px;
}

.md-fields-pair {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 8px;
  flex: 1 1 0;
  min-width: 0;
  align-items: center;
}

.md-title-input {
  background: transparent;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 4px 8px;
  font-size: 14.5px;
  font-weight: 600;
  color: var(--text);
  min-width: 0;
  width: 100%;
  outline: none;
}

.md-title-input:hover {
  border-color: var(--border);
}

.md-title-input:focus {
  border-color: var(--md-blue);
  background: var(--md-paper);
}

.md-cat-input {
  background: var(--surface-muted);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 8px;
  font-size: 12.5px;
  color: var(--text);
  min-width: 0;
  width: 100%;
  outline: none;
}

.md-cat-input:focus {
  border-color: var(--md-blue);
  background: var(--md-paper);
}

.md-fields-pair .cat-pills-wrap {
  width: 100%;
  min-width: 0;
}

.md-fields-pair .cat-pills-box {
  min-height: 30px;
  padding: 2px 6px;
  border-radius: 6px;
  font-size: 12.5px;
}

.md-adult-toggle {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
}

.md-title-center {
  display: flex;
  align-items: center;
  flex-shrink: 0;
}

.md-mode-seg {
  display: inline-flex;
  padding: 2px;
  border-radius: 8px;
  background: var(--surface-muted);
  gap: 2px;
}

.md-mode-seg button {
  padding: 4px 10px;
  border-radius: 6px;
  border: 0;
  background: transparent;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-secondary);
  cursor: pointer;
  white-space: nowrap;
}

.md-mode-seg button.active {
  background: var(--md-paper);
  color: var(--text);
  font-weight: 600;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.md-title-right {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.md-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  height: 30px;
  padding: 0 10px;
  border-radius: 7px;
  border: 0;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.14s ease, color 0.14s ease;
}

.md-btn-primary {
  background: var(--md-blue);
  color: #fff;
}

.md-btn-primary:hover {
  background: var(--md-blue-hover);
}

.md-btn-secondary {
  background: var(--surface-muted);
  color: var(--text);
  border: 1px solid var(--border);
}

.md-btn-secondary:hover {
  background: var(--surface-hover);
}

.md-btn-danger {
  background: var(--negative-bg);
  color: var(--negative);
}

.md-btn-danger:hover {
  background: var(--negative);
  color: #fff;
}

.md-btn-icon {
  width: 30px;
  padding: 0;
}

.md-mobile-history-btn {
  display: none;
}

/* ==========================================================================
   WORD RIBBON TOOLBAR
   ========================================================================== */
.md-ribbon {
  background: var(--md-chrome);
  border-bottom: 1px solid var(--border);
  padding: 6px 14px;
  display: flex;
  align-items: center;
  gap: 6px;
  overflow-x: auto;
  flex-shrink: 0;
  scrollbar-width: thin;
}

.rib-group {
  display: flex;
  align-items: center;
  gap: 2px;
  padding-right: 8px;
  margin-right: 2px;
  border-right: 1px solid var(--border);
  flex-shrink: 0;
}

.rib-group:last-child {
  border-right: 0;
  padding-right: 0;
}

.rib-select {
  height: 30px;
  padding: 0 8px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--md-paper);
  color: var(--text);
  font-size: 12.5px;
  font-weight: 500;
  outline: none;
  cursor: pointer;
}

.rib-select:focus {
  border-color: var(--md-blue);
}

.rib-btn {
  height: 30px;
  min-width: 30px;
  padding: 0 7px;
  border-radius: 6px;
  border: 0;
  background: transparent;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text);
  cursor: pointer;
  transition: background 0.12s ease, color 0.12s ease;
}

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

.rib-btn.active {
  background: var(--md-blue-soft);
  color: var(--md-blue);
}

/* Table Grid Picker Popover */
.md-popover {
  position: fixed;
  z-index: 80;
  background: var(--md-paper);
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  padding: 12px;
  box-shadow: var(--shadow-popover);
  display: none;
  gap: 8px;
}

.md-popover.open {
  display: grid;
}

.table-grid {
  display: grid;
  grid-template-columns: repeat(6, 20px);
  gap: 3px;
}

.table-cell {
  width: 20px;
  height: 20px;
  border-radius: 3px;
  border: 1px solid var(--border-strong);
  background: var(--surface-muted);
  cursor: pointer;
}

.table-cell.lit {
  background: var(--md-blue-soft);
  border-color: var(--md-blue);
}

/* ==========================================================================
   MAIN BODY (Workspace + Optional Desktop Right Panel)
   ========================================================================== */
.md-main-row {
  flex: 1;
  min-height: 0;
  display: flex;
  overflow: hidden;
}

.md-workspace {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr;
  overflow: hidden;
}

.md-workspace.split {
  grid-template-columns: 1fr 1fr;
}

.visual-pane {
  overflow-y: auto;
  padding: 22px 20px 64px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

.md-workspace.md-only .visual-pane {
  display: none;
}

.meta-cover-drawer {
  width: 100%;
  background: var(--md-chrome);
  border-bottom: 1px solid var(--border);
  padding: 10px 14px;
  flex-shrink: 0;
}

.meta-cover-drawer.hide {
  display: none;
}

/* A4 Document Sheet */
.page-sheet {
  width: 100%;
  max-width: calc(816px * var(--md-zoom));
  min-height: 820px;
  background: var(--md-paper);
  border: 1px solid var(--border);
  border-radius: 4px;
  box-shadow: var(--md-shadow-paper);
  padding: 52px 64px;
  outline: none;
  font-size: calc(15.5px * var(--md-zoom));
  line-height: 1.72;
  color: var(--text);
}

.page-sheet h1 { font-size: 1.95em; font-weight: 700; letter-spacing: -0.02em; margin: 0 0 0.55em; line-height: 1.25; }
.page-sheet h2 { font-size: 1.42em; font-weight: 650; letter-spacing: -0.015em; margin: 1.25em 0 0.45em; padding-bottom: 0.2em; border-bottom: 1px solid var(--border); line-height: 1.3; }
.page-sheet h3 { font-size: 1.15em; font-weight: 650; margin: 1.1em 0 0.35em; line-height: 1.35; }
.page-sheet p { margin: 0 0 0.85em; }
.page-sheet ul, .page-sheet ol { margin: 0 0 0.95em 1.5em; }
.page-sheet li { margin: 0.25em 0; }
.page-sheet blockquote {
  margin: 1em 0;
  padding: 10px 16px;
  border-left: 3.5px solid var(--md-blue);
  background: var(--md-blue-soft);
  border-radius: 0 8px 8px 0;
  color: var(--text-secondary);
}
.page-sheet code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.88em;
  padding: 2px 5px;
  border-radius: 5px;
  background: var(--surface-muted);
}
.page-sheet pre {
  margin: 1em 0;
  padding: 14px 16px;
  border-radius: 8px;
  background: var(--surface-muted);
  border: 1px solid var(--border);
  overflow-x: auto;
}
.page-sheet pre code { background: none; padding: 0; font-size: 0.86em; }
.page-sheet hr { border: 0; border-top: 1px solid var(--border-strong); margin: 1.5em 0; }
.page-sheet a { color: var(--md-blue); text-decoration: underline; }
.page-sheet img { max-width: 100%; border-radius: 8px; margin: 1em 0; }
.page-sheet table { width: 100%; border-collapse: collapse; margin: 1em 0; font-size: 0.92em; }
.page-sheet th, .page-sheet td { border: 1px solid var(--border-strong); padding: 8px 11px; text-align: left; }
.page-sheet th { background: var(--surface-muted); font-weight: 600; }

/* Raw Markdown Pane */
.md-pane {
  display: none;
  flex-direction: column;
  border-left: 1px solid var(--border);
  background: var(--md-chrome);
  min-height: 0;
}

.md-workspace.split .md-pane,
.md-workspace.md-only .md-pane {
  display: flex;
}

.md-workspace.md-only .md-pane {
  border-left: 0;
}

.md-pane-head {
  padding: 8px 14px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
}

.md-textarea {
  flex: 1;
  width: 100%;
  border: 0;
  outline: none;
  resize: none;
  padding: 20px 24px;
  background: transparent;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13.5px;
  line-height: 1.65;
  color: var(--text);
}

/* ==========================================================================
   RIGHT PANEL (Desktop Suggestions & History) + Mobile Modal
   ========================================================================== */
.md-side-panel {
  width: 310px;
  flex-shrink: 0;
  background: var(--md-chrome);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.md-side-head {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.md-side-tabs {
  display: inline-flex;
  padding: 2px;
  border-radius: 8px;
  background: var(--surface-muted);
  gap: 2px;
  flex: 1;
}

.md-side-tab {
  flex: 1;
  padding: 5px 8px;
  border-radius: 6px;
  border: 0;
  background: transparent;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  cursor: pointer;
}

.md-side-tab.active {
  background: var(--md-paper);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.md-side-close {
  display: none;
}

.md-side-body {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  font-size: 13px;
}

.md-side-pane.hide {
  display: none;
}

.md-side-empty {
  color: var(--text-tertiary);
  font-size: 12.5px;
  text-align: center;
  padding: 24px 12px;
}

/* Mini Insert Dialog */
.mini-backdrop {
  position: fixed;
  inset: 0;
  z-index: 120;
  background: rgba(0, 0, 0, 0.45);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.mini-backdrop.open {
  display: flex;
}

.mini-dialog {
  width: 100%;
  max-width: 390px;
  background: var(--md-paper);
  border: 1px solid var(--border-strong);
  border-radius: 14px;
  padding: 18px;
  display: grid;
  gap: 12px;
  box-shadow: var(--shadow-popover);
}

.mini-dialog h3 {
  font-size: 15px;
  font-weight: 650;
}

.mini-input {
  width: 100%;
  padding: 8px 11px;
  border-radius: 8px;
  border: 1px solid var(--border-strong);
  background: var(--md-chrome);
  color: var(--text);
  outline: none;
}

.mini-input:focus {
  border-color: var(--md-blue);
}

@media (max-width: 1024px) {
  .md-workspace.split {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr 1fr;
  }
  .md-mobile-history-btn {
    display: inline-flex;
  }
  .md-side-panel {
    display: none;
    position: fixed;
    inset: 0;
    width: 100%;
    z-index: 100;
    border-left: 0;
  }
  .md-side-panel.is-open {
    display: flex;
  }
  .md-side-close {
    display: inline-flex;
  }
}

@media (max-width: 768px) {
  .md-titlebar {
    padding: 8px 10px;
    gap: 8px;
  }
  .md-title-left {
    flex: 1 0 100%;
    flex-wrap: nowrap;
    gap: 6px;
  }
  .md-fields-pair {
    gap: 6px;
  }
  .md-title-right {
    flex: 1 0 100%;
    flex-wrap: nowrap;
    justify-content: flex-end;
    gap: 6px;
  }
  .md-title-right .md-adult-toggle {
    margin-right: auto;
  }
  .md-title-center {
    flex: 1 0 100%;
    justify-content: center;
  }
  .md-mode-seg {
    width: 100%;
  }
  .md-mode-seg button {
    flex: 1;
    text-align: center;
  }
  .page-sheet {
    padding: 26px 20px;
    min-height: 520px;
  }
  .md-btn span.md-hide-sm {
    display: none;
  }
}
