/* --- Global Reset & Variables --- */
*,
*:before,
*:after {
  box-sizing: border-box;
}

:root {
  --bg-color: #fdfdfc;
  --text-color: #111111;
  --menu-bg: rgba(255, 255, 255, 0.95);
  --menu-border: #e0e0e0;
  --font-family:
    -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial,
    sans-serif;
  --font-size: 18px;
  --line-height: 1.6;
}

[data-theme='dark'] {
  --bg-color: #1a1a1a;
  --text-color: #e0e0e0;
  --menu-bg: rgba(30, 30, 30, 0.95);
  --menu-border: #333333;
}

[data-theme='oled'] {
  --bg-color: #000000;
  --text-color: #cccccc;
  --menu-bg: rgba(10, 10, 10, 0.95);
  --menu-border: #222222;
}

html,
body {
  overscroll-behavior-y: none;
  scroll-behavior: smooth;
  margin: 0;
  padding: 0;
  width: 100%;
}

body {
  background-color: var(--bg-color);
  color: var(--text-color);
  font-family: var(--font-family);
  font-size: var(--font-size);
  line-height: var(--line-height);
  transition:
    background-color 0.3s,
    color 0.3s;
  overflow-x: hidden;
}

/* --- View Management --- */
.view {
  display: none;
}
.view.active {
  display: block;
}

/* --- Algemene UI Helpers --- */
.btn-full-width {
  width: 100%;
  padding: 12px;
  font-weight: bold;
  border-radius: 8px;
  margin-top: 10px;
}
.error-msg {
  color: #d9534f;
  display: none;
  margin-top: 15px;
  font-size: 14px;
}

/* --- Login --- */
.login-container {
  max-width: 400px;
  margin: 80px auto;
  padding: 25px;
  text-align: center;
  background: var(--menu-bg);
  border: 1px solid var(--menu-border);
  border-radius: 12px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}
.login-container h1 {
  margin-top: 0;
  font-size: 20px;
}
.login-container input {
  width: 100%;
  padding: 10px;
  margin-bottom: 12px;
  border: 1px solid var(--menu-border);
  border-radius: 8px;
  background: var(--bg-color);
  color: var(--text-color);
  font-size: 16px;
}

/* --- Dashboard --- */
.dash-header {
  position: sticky;
  top: 0;
  z-index: 100;
  padding: 15px 20px;
  max-width: 800px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--menu-border);
  background: var(--menu-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}
.dash-header h1 {
  font-size: 20px;
  margin: 0;
  font-weight: 700;
}
.header-actions {
  position: relative;
  display: flex;
  align-items: center;
}

/* Dashboard Dropdown Menu */
.dash-dropdown {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 10px;
  background: var(--menu-bg);
  border: 1px solid var(--menu-border);
  border-radius: 12px;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
  display: none;
  flex-direction: column;
  min-width: 255px;
  z-index: 1000;
}
.dash-dropdown.active {
  display: flex;
}
.dash-dropdown-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  background: none;
  border: none;
  color: var(--text-color);
  font-size: 15px;
  cursor: pointer;
  text-align: left;
  border-bottom: 1px solid var(--menu-border);
  width: 100%;
  white-space: nowrap;
  transition: background 0.1s;
}
.dash-dropdown-item:last-child {
  border-bottom: none;
}
.dash-dropdown-item:active {
  opacity: 0.7;
}
.dash-dropdown-item.danger {
  color: #d9534f;
}

#story-list {
  max-width: 800px;
  margin: 15px auto;
  padding: 0 15px;
}

/* --- Story Card --- */
.story-card {
  background: var(--menu-bg);
  border: 1px solid var(--menu-border);
  padding: 12px 15px;
  border-radius: 12px;
  margin-bottom: 12px;
  display: flex;
  flex-direction: column;
}
.story-card-main {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  width: 100%;
}
.drag-handle {
  cursor: grab;
  color: var(--text-color);
  opacity: 0.4;
  display: flex;
  align-items: center;
  flex-shrink: 0;
}
.drag-handle:active {
  cursor: grabbing;
}
.story-card-info {
  flex-grow: 1;
  min-width: 0;
  cursor: pointer;
}
.story-card h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.btn-options {
  flex-shrink: 0;
  opacity: 0.7;
}
.story-actions {
  display: none;
  width: 100%;
  justify-content: flex-end;
  gap: 10px;
  padding-top: 12px;
  margin-top: 12px;
  border-top: 1px solid var(--menu-border);
}
.story-actions.active {
  display: flex;
}
.loading-text {
  text-align: center;
  opacity: 0.6;
  margin-top: 30px;
  font-size: 14px;
}


.draft-card {
  border-color: rgba(243, 179, 12, 0.65);
}
.draft-marker {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  background: #f3b30c;
  color: #111111;
  font-size: 13px;
  font-weight: 800;
}
.draft-status {
  display: block;
  margin-top: 3px;
  color: #9a6a00;
  font-size: 12px;
  line-height: 1.3;
}
[data-theme='dark'] .draft-status,
[data-theme='oled'] .draft-status {
  color: #f3c955;
}
.editor-save-status {
  min-height: 18px;
  font-size: 12px;
  line-height: 1.4;
  opacity: 0.72;
  text-align: center;
  flex-shrink: 0;
}
.editor-save-status[data-state='saved'] {
  color: #2f8f46;
  opacity: 1;
}
.editor-save-status[data-state='dirty'],
.editor-save-status[data-state='saving'] {
  color: #9a6a00;
  opacity: 1;
}
.editor-save-status[data-state='error'] {
  color: #d9534f;
  opacity: 1;
  font-weight: 600;
}
#btn-save-story:disabled {
  opacity: 0.55;
  cursor: wait;
}


.draft-choice-modal-content {
  width: min(92%, 440px);
  padding: 20px;
}
.draft-choice-modal-content h2 {
  margin: 0 0 10px;
  font-size: 18px;
}
.draft-choice-modal-content p {
  margin: 0 0 18px;
  font-size: 14px;
  line-height: 1.5;
  opacity: 0.8;
}
.draft-choice-actions {
  display: flex;
  flex-direction: column;
  gap: 9px;
}
button.ui-btn.draft-choice-button {
  width: 100%;
  min-height: 42px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 700;
}
button.ui-btn.draft-choice-button.danger-action {
  color: #d9534f;
  border-color: rgba(217, 83, 79, 0.45);
}

/* --- Reader View (Dynamische Viewport) --- */
#reader-view.active {
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  height: 100%;
  width: 100%;
  overflow: hidden;
  background-color: var(--bg-color);
  z-index: 50;
}

#reader-scroll-container {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  position: relative;
  scroll-behavior: auto;
  touch-action: pan-y;
}

#reader-content {
  padding: 25px 20px 80px 20px;
  max-width: 800px;
  margin: 0 auto;
  text-align: left;
  hyphens: auto;
  -webkit-hyphens: auto;
}
#reader-content h1,
#reader-content h2 {
  margin-top: 2em;
  margin-bottom: 0.5em;
  line-height: 1.2;
}
#reader-content p {
  margin-top: 0;
  margin-bottom: 1.5em;
}
#reader-content ul,
#reader-content ol {
  margin-bottom: 1.5em;
  padding-left: 20px;
}
#reader-content strong {
  font-weight: bold;
}

/* Menu balken tonen/verbergen */
#reader-view .menu-bar {
  display: none;
  flex-shrink: 0;
}
#reader-view.ui-visible .menu-bar {
  display: flex;
}

.menu-bar {
  background-color: var(--menu-bg);
  border-bottom: 1px solid var(--menu-border);
  padding: 10px 15px;
  justify-content: space-around;
  align-items: center;
  z-index: 10;
}
.menu-bar.bottom {
  border-bottom: none;
  border-top: 1px solid var(--menu-border);
}
.toc-btn {
  border: none !important;
  font-weight: bold;
  font-size: 15px !important;
}

/* --- SVG Button Styling --- */
button.ui-btn,
label.ui-btn {
  background: transparent;
  border: 1px solid var(--menu-border);
  color: var(--text-color);
  padding: 8px 14px;
  border-radius: 20px;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.2s;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
button.ui-btn.active {
  background: var(--text-color);
  color: var(--bg-color);
  border-color: var(--text-color);
}
button.ui-btn.icon-only,
label.ui-btn.icon-only {
  width: 36px;
  height: 36px;
  padding: 0;
  border-radius: 50%;
}
button.ui-btn.danger-icon {
  color: #d9534f;
  border-color: rgba(217, 83, 79, 0.3);
}

/* --- Modals --- */
.modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.6);
  z-index: 300;
  align-items: center;
  justify-content: center;
}
.modal.active {
  display: flex;
}

.modal-content {
  background: var(--bg-color);
  padding: 15px;
  border-radius: 12px;
  width: 92%;
  max-width: 500px;
  border: 1px solid var(--menu-border);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2);
}
.editor-modal-content {
  display: flex;
  flex-direction: column;
  height: 85vh;
  height: 85dvh;
  max-height: 800px;
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--menu-border);
  padding-bottom: 12px;
  margin-bottom: 12px;
  flex-shrink: 0;
}
.modal-header h2 {
  margin: 0;
  font-size: 18px;
}

.editor-body {
  display: flex;
  flex-direction: column;
  gap: 10px;
  flex-grow: 1;
  overflow: hidden;
}
.editor-body input[type='text'] {
  width: 100%;
  padding: 10px;
  border: 1px solid var(--menu-border);
  border-radius: 8px;
  background: var(--menu-bg);
  color: var(--text-color);
  font-size: 15px;
  flex-shrink: 0;
}
.editor-search-panel {
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--bg-color);
  padding: 10px;
  border: 1px solid var(--menu-border);
  border-radius: 8px;
  flex-shrink: 0;
}
.editor-search-panel.hidden {
  display: none;
}
.search-row {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: nowrap;
}
.search-row input {
  min-width: 0;
  flex: 1;
  padding: 8px;
  border: 1px solid var(--menu-border);
  border-radius: 6px;
  background: var(--menu-bg);
  color: var(--text-color);
  font-size: 14px;
}
.search-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}
.search-actions button {
  padding: 8px 12px;
  font-size: 13px;
  white-space: nowrap;
}

#edit-story-content {
  width: 100%;
  padding: 10px;
  border: 1px solid var(--menu-border);
  border-radius: 8px;
  background: var(--menu-bg);
  color: var(--text-color);
  font-size: 14px;
  font-family: inherit;
  resize: none;
  flex-grow: 1;
  min-height: 0;
}

/* --- Rules & TOC --- */
.rule-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid var(--menu-border);
}
.rule-item-text {
  font-size: 15px;
}
.add-rule-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 15px;
}
.add-rule-form input[type='text'] {
  padding: 10px;
  border: 1px solid var(--menu-border);
  border-radius: 8px;
  background: var(--menu-bg);
  color: var(--text-color);
  font-size: 15px;
}
.toc-item {
  display: block;
  padding: 10px 0;
  border-bottom: 1px solid var(--menu-border);
  color: var(--text-color);
  text-decoration: none;
  cursor: pointer;
  font-size: 15px;
  line-height: 1.4;
}
.toc-item.h2 {
  padding-left: 20px;
  font-size: 13px;
  opacity: 0.8;
}

/* --- Name Suggestions & Controls --- */
.suggestion-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
  margin-top: 4px;
}
.threshold-control {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  opacity: 0.8;
}
.threshold-control input[type='range'] {
  width: 80px;
}

.name-suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 4px;
}
.suggestion-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--text-color);
  color: var(--bg-color);
  padding: 6px 10px;
  border-radius: 14px;
  font-size: 13px;
  font-weight: 600;
  transition:
    opacity 0.2s,
    transform 0.1s;
}
.suggestion-chip-text {
  cursor: pointer;
}
.suggestion-chip-text:active {
  opacity: 0.7;
  transform: scale(0.95);
}
.suggestion-chip-ignore {
  cursor: pointer;
  opacity: 0.6;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  font-size: 12px;
  line-height: 1;
  background: rgba(128, 128, 128, 0.3);
  transition: all 0.2s;
}
.suggestion-chip-ignore:hover,
.suggestion-chip-ignore:active {
  opacity: 1;
  background: #d9534f;
  color: white;
}

/* Voor genegeerde chips in de lijst onderaan */
.suggestion-chip.ignored-chip {
  background: transparent;
  border: 1px dashed var(--menu-border);
  color: var(--text-color);
  opacity: 0.7;
  padding: 4px 10px;
  font-weight: normal;
}
.suggestion-chip.ignored-chip .suggestion-chip-ignore {
  background: transparent;
  color: var(--text-color);
  border: 1px solid var(--menu-border);
}

/* --- Compact Grouped Name Suggestions --- */
.suggestion-group {
  width: 100%;
  padding: 9px 0;
  border-bottom: 1px solid var(--menu-border);
}

.suggestion-group:last-child {
  border-bottom: none;
}

.suggestion-group-header {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0;
  margin: 0;
  border: 0;
  background: transparent;
  color: var(--text-color);
  cursor: pointer;
  text-align: left;
}

.suggestion-group-title {
  min-width: 0;
  font-size: 13px;
  font-weight: 700;
  opacity: 0.78;
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.suggestion-group-toggle {
  flex-shrink: 0;
  font-size: 11px;
  font-weight: 700;
  opacity: 0.6;
  padding-left: 8px;
}

.suggestion-group-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding-top: 8px;
}

.suggestion-group.collapsed .suggestion-group-chips {
  display: none;
}

.grouped-suggestion-chip {
  flex-wrap: nowrap;
  max-width: 100%;
  background: var(--text-color);
  color: var(--bg-color);
}

.suggestion-chip-label {
  opacity: 0.65;
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
}

.grouped-suggestion-chip .suggestion-chip-text {
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

@media (max-width: 420px) {
  .grouped-suggestion-chip .suggestion-chip-text {
    max-width: 160px;
  }
}


/* --- Rules Modal Scrollable Lists --- */
#rules-modal .modal-content {
  max-height: 92vh;
  max-height: 92dvh;
  overflow: hidden;
}


#rules-list {
  max-height: min(24vh, 220px);
  max-height: min(24dvh, 220px);
  overflow-y: auto;
  overflow-x: hidden;
  padding-right: 4px;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
}

#rules-list .rule-item:first-child {
  padding-top: 0;
}

#rules-list .rule-item:last-child {
  padding-bottom: 0;
}

#rules-modal .add-rule-form {
  min-height: 0;
}

#name-suggestions {
  max-height: min(38vh, 320px);
  max-height: min(38dvh, 320px);
  overflow-y: auto;
  overflow-x: hidden;
  padding-right: 4px;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
}

#name-suggestions .suggestion-group:first-child {
  padding-top: 0;
}

#name-suggestions .suggestion-group:last-child {
  padding-bottom: 0;
}

@media (max-width: 420px) {
  #rules-modal .modal-content {
    max-height: 94vh;
    max-height: 94dvh;
  }

  #rules-list {
    max-height: min(20vh, 180px);
    max-height: min(20dvh, 180px);
  }

  #name-suggestions {
    max-height: min(34vh, 280px);
    max-height: min(34dvh, 280px);
  }
}


/* --- Optional Rule Aliases --- */
.alias-options {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px 0 2px;
}
.alias-options.hidden,
.edit-rule-cancel.hidden {
  display: none;
}
.alias-options-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  border: 1px solid var(--menu-border);
  border-radius: 10px;
  padding: 8px 10px;
  background: var(--menu-bg);
}
.alias-options-heading {
  display: flex;
  flex-direction: column;
  min-width: 0;
  gap: 2px;
}
.alias-options-title {
  font-size: 12px;
  font-weight: 700;
  opacity: 0.78;
}
.alias-options-count {
  font-size: 12px;
  opacity: 0.6;
}
.alias-options-toggle {
  flex-shrink: 0;
  border: 1px solid var(--menu-border);
  border-radius: 999px;
  padding: 6px 10px;
  background: transparent;
  color: var(--text-color);
  font: inherit;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
}
.alias-options-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: min(22vh, 210px);
  max-height: min(22dvh, 210px);
  overflow-y: auto;
  overflow-x: hidden;
  padding-right: 4px;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
}
.alias-option-row {
  display: grid;
  grid-template-columns: auto auto minmax(0, 1fr);
  align-items: center;
  gap: 8px;
  width: 100%;
  border: 1px solid var(--menu-border);
  border-radius: 10px;
  padding: 8px 10px;
  background: var(--menu-bg);
  color: var(--text-color);
  font: inherit;
  cursor: pointer;
  text-align: left;
}
.alias-option-row.selected {
  background: var(--text-color);
  color: var(--bg-color);
  border-color: var(--text-color);
}
.alias-option-check {
  width: 18px;
  text-align: center;
  font-weight: 800;
}
.alias-option-label {
  font-size: 11px;
  font-weight: 700;
  opacity: 0.65;
  white-space: nowrap;
}
.alias-option-text {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
  font-weight: 700;
}
.edit-rule-cancel {
  margin-top: 0;
  border-style: dashed !important;
}
@media (max-width: 420px) {
  .alias-options-list {
    max-height: min(18vh, 170px);
    max-height: min(18dvh, 170px);
  }
  .alias-option-row {
    grid-template-columns: auto minmax(0, 1fr);
    gap: 6px;
  }
  .alias-option-label {
    display: none;
  }
}


/* --- Rules Modal Task Modes --- */
#rules-modal .rules-mode-back,
#rules-modal .rule-form-mode-title,
#rules-modal .manual-rule-button {
  display: none;
}
#rules-modal.rules-overview-mode .manual-rule-button,
#rules-modal.rules-form-mode .rules-mode-back,
#rules-modal.rules-form-mode .rule-form-mode-title,
#rules-modal.rules-alias-mode .rules-mode-back,
#rules-modal.rules-alias-mode .rule-form-mode-title {
  display: flex;
}
#rules-modal.rules-overview-mode .add-rule-form {
  display: none;
}
#rules-modal.rules-form-mode #rules-list,
#rules-modal.rules-form-mode .suggestion-header,
#rules-modal.rules-form-mode #name-suggestions,
#rules-modal.rules-form-mode .manual-rule-button,
#rules-modal.rules-alias-mode #rules-list,
#rules-modal.rules-alias-mode .suggestion-header,
#rules-modal.rules-alias-mode #name-suggestions,
#rules-modal.rules-alias-mode .manual-rule-button {
  display: none;
}
#rules-modal.rules-alias-mode .add-rule-form > input,
#rules-modal.rules-alias-mode #btn-cancel-rule-edit,
#rules-modal.rules-alias-mode #btn-add-rule {
  display: none !important;
}
#rules-modal.rules-alias-mode #alias-options {
  padding-top: 0;
}
#rules-modal.rules-alias-mode .alias-options-list {
  max-height: none;
  overflow: visible;
  padding-right: 0;
}
#rules-modal.rules-form-mode #alias-options .alias-options-list {
  display: none;
}
.rules-mode-back {
  align-self: flex-start;
  border: 0;
  background: transparent;
  color: var(--text-color);
  opacity: 0.75;
  font: inherit;
  font-size: 13px;
  font-weight: 700;
  padding: 0 0 10px;
  cursor: pointer;
}
.rule-form-mode-title {
  font-size: 13px;
  font-weight: 800;
  opacity: 0.72;
  margin-bottom: 2px;
}
#rules-modal.rules-form-mode .modal-content,
#rules-modal.rules-alias-mode .modal-content {
  overflow-y: auto;
  overscroll-behavior: contain;
}


/* --- Rules Modal Emergency Manual Button Visibility --- */
#rules-modal.rules-overview-mode #btn-manual-rule {
  display: flex !important;
}

#rules-modal.rules-form-mode #btn-manual-rule,
#rules-modal.rules-alias-mode #btn-manual-rule {
  display: none !important;
}


/* --- Rules Modal Overview Suggestions Visibility Fix --- */
#rules-modal.rules-overview-mode .add-rule-form {
  display: flex !important;
}

#rules-modal.rules-overview-mode .add-rule-form > input,
#rules-modal.rules-overview-mode #alias-options,
#rules-modal.rules-overview-mode #btn-cancel-rule-edit,
#rules-modal.rules-overview-mode #btn-add-rule,
#rules-modal.rules-overview-mode #rule-form-mode-title,
#rules-modal.rules-overview-mode .rule-form-mode-title {
  display: none !important;
}

#rules-modal.rules-overview-mode .suggestion-header {
  display: flex !important;
}

#rules-modal.rules-overview-mode #name-suggestions {
  display: block !important;
}

#rules-modal.rules-overview-mode #btn-manual-rule {
  display: flex !important;
}


/* --- Rules Modal Final Layout Stabilization --- */
#rules-modal .modal-content {
  height: min(86vh, 650px);
  height: min(86dvh, 650px);
  max-height: min(86vh, 650px);
  max-height: min(86dvh, 650px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#rules-modal .modal-header {
  flex-shrink: 0;
  gap: 10px;
}

#rules-modal .modal-header h2 {
  flex: 1;
}

#rules-modal .modal-header .rules-mode-back {
  display: none;
  flex-shrink: 0;
}

#rules-modal.rules-form-mode .modal-header .rules-mode-back,
#rules-modal.rules-alias-mode .modal-header .rules-mode-back {
  display: inline-flex !important;
}

#rules-modal .modal-header .rules-mode-back.hidden,
#rules-modal.rules-overview-mode .modal-header .rules-mode-back {
  display: none !important;
}

#rules-modal.rules-overview-mode .add-rule-form {
  display: flex !important;
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
}

#rules-modal.rules-overview-mode .suggestion-header {
  flex-shrink: 0;
}

#rules-modal.rules-overview-mode #name-suggestions {
  display: block !important;
  flex: 1 1 auto;
  min-height: 0;
  max-height: none;
  overflow-y: auto;
  overflow-x: hidden;
}

#rules-modal.rules-overview-mode #rules-list {
  flex: 0 0 auto;
  max-height: min(20vh, 180px);
  max-height: min(20dvh, 180px);
}

#rules-modal.rules-overview-mode #btn-manual-rule {
  flex-shrink: 0;
}

#rules-modal.rules-form-mode .add-rule-form,
#rules-modal.rules-alias-mode .add-rule-form {
  display: flex !important;
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
}

#rules-modal.rules-form-mode #alias-options {
  flex-shrink: 0;
}

#rules-modal.rules-alias-mode #alias-options {
  display: flex !important;
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
}

#rules-modal.rules-alias-mode .alias-options-list {
  flex: 1 1 auto;
  min-height: 0;
  max-height: none;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
}

@media (max-width: 520px) {
  #rules-modal .modal-content {
    height: 92vh;
    height: 92dvh;
    max-height: 92vh;
    max-height: 92dvh;
  }
}

/* --- Character Profiles & Compact Suggestion Actions --- */
.suggestion-group-action-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
}
.suggestion-group-title-button {
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--text-color);
  cursor: pointer;
  text-align: left;
}
.suggestion-group-title-button svg {
  flex-shrink: 0;
  opacity: 0.55;
  transition: transform 0.15s ease;
}
.suggestion-group:not(.collapsed) .suggestion-group-title-button svg {
  transform: rotate(90deg);
}
.suggestion-group-actions,
.character-item-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}
button.ui-btn.suggestion-action-button {
  width: 34px;
  height: 34px;
}
.rules-overview-section,
.rules-section-heading {
  display: none;
}
#rules-modal.rules-overview-mode .rules-overview-section,
#rules-modal.rules-overview-mode .rules-section-heading {
  display: block;
}
.rules-overview-section h3,
.rules-section-heading {
  margin: 0 0 6px;
  font-size: 12px;
  font-weight: 800;
  opacity: 0.65;
}
#characters-list {
  max-height: min(18vh, 150px);
  max-height: min(18dvh, 150px);
  overflow-y: auto;
  margin-bottom: 10px;
}
.character-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 7px 0;
  border-bottom: 1px solid var(--menu-border);
}
.character-item-text {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 14px;
}
.empty-section-text {
  margin: 0 0 10px;
  font-size: 13px;
  opacity: 0.6;
}
.character-modal-content {
  display: flex;
  flex-direction: column;
  height: min(88vh, 720px);
  height: min(88dvh, 720px);
  overflow: hidden;
}
.character-form-scroll {
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
}
.character-form-section {
  display: grid;
  gap: 10px;
  margin-bottom: 20px;
}
.character-form-section h3 {
  margin: 0;
  font-size: 15px;
}
.character-form-section p {
  margin: -5px 0 0;
  font-size: 12px;
  line-height: 1.4;
  opacity: 0.65;
}
.character-form-section label {
  display: grid;
  gap: 4px;
  font-size: 12px;
  font-weight: 700;
  opacity: 0.82;
}
.character-form-section input {
  width: 100%;
  padding: 10px;
  border: 1px solid var(--menu-border);
  border-radius: 8px;
  background: var(--menu-bg);
  color: var(--text-color);
  font: inherit;
  font-size: 15px;
}
@media (max-width: 420px) {
  button.ui-btn.suggestion-action-button {
    width: 32px;
    height: 32px;
  }
  .suggestion-group-action-row {
    gap: 6px;
  }
  .character-modal-content {
    height: 94vh;
    height: 94dvh;
  }
}

/* --- Sprint 1.4b: Character Variant Mapping --- */
.character-item-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.character-item-text small {
  font-size: 11px;
  font-weight: 500;
  opacity: 0.6;
}
.character-variants-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}
.character-variant-count {
  flex-shrink: 0;
  padding: 3px 8px;
  border: 1px solid var(--menu-border);
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  opacity: 0.75;
}
.character-variants-list {
  display: flex;
  flex-direction: column;
  gap: 7px;
}
.character-variant-row {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 9px;
  padding: 9px;
  border: 1px solid var(--menu-border);
  border-radius: 10px;
  opacity: 0.7;
}
.character-variant-row.active {
  opacity: 1;
  border-color: var(--text-color);
}
.character-variant-toggle {
  display: flex !important;
  align-items: flex-start;
  padding-top: 3px;
}
.character-variant-toggle input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}
.character-variant-toggle span {
  width: 20px;
  height: 20px;
  display: grid;
  place-items: center;
  border: 1px solid var(--menu-border);
  border-radius: 6px;
}
.character-variant-toggle input:checked + span {
  background: var(--text-color);
  color: var(--bg-color);
  border-color: var(--text-color);
}
.character-variant-toggle input:checked + span::after {
  content: '✓';
  font-size: 13px;
  font-weight: 800;
}
.character-variant-fields {
  min-width: 0;
  display: grid;
  gap: 6px;
}
.character-variant-source {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-width: 0;
}
.character-variant-source strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
}
.character-variant-status {
  flex-shrink: 0;
  font-size: 10px;
  font-weight: 700;
  opacity: 0.6;
}
.character-variant-fields input.character-variant-replace {
  padding: 7px 8px;
  font-size: 13px;
}


/* --- Sprint 1.6d-2: Delen en rollenbeheer --- */
.story-role {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 7px;
  border: 1px solid var(--menu-border);
  border-radius: 999px;
  font-size: 10px;
  font-weight: 700;
  line-height: 1.3;
  opacity: 0.72;
  margin-right: auto;
}
.story-role-owner {
  opacity: 0.55;
}
.story-role-editor,
.story-role-reader {
  border-style: dashed;
}
.sharing-modal-content {
  display: flex;
  flex-direction: column;
  width: min(92%, 560px);
  height: min(86vh, 650px);
  height: min(86dvh, 650px);
  max-height: 650px;
  overflow: hidden;
}
.sharing-modal-content .modal-header,
.sharing-explanation,
.sharing-feedback,
.sharing-add-area {
  flex-shrink: 0;
}
.sharing-modal-content .modal-header > div { min-width: 0; }
.sharing-story-title {
  margin: 3px 0 0;
  max-width: 420px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 12px;
  opacity: 0.65;
}
.sharing-explanation {
  margin: 0 0 10px;
  font-size: 12px;
  line-height: 1.45;
  opacity: 0.7;
}
.sharing-feedback {
  min-height: 18px;
  margin-bottom: 6px;
  font-size: 12px;
  color: #2f7d32;
}
.sharing-feedback.error,
.sharing-error { color: #d9534f; }
.sharing-add-area {
  padding: 4px 0 12px;
  border-bottom: 1px solid var(--menu-border);
}
button.ui-btn.sharing-add-toggle {
  gap: 7px;
  border-style: dashed;
  font-weight: 700;
}
.sharing-add-toggle span {
  font-size: 20px;
  line-height: 0.8;
}
.sharing-add-form {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  padding: 10px;
  border: 1px solid var(--menu-border);
  border-radius: 10px;
  background: var(--menu-bg);
}
.sharing-add-form.hidden,
.sharing-add-toggle.hidden { display: none; }
.sharing-add-form label {
  display: grid;
  gap: 5px;
  font-size: 11px;
  font-weight: 700;
}
.sharing-add-form select,
.sharing-role-select {
  width: 100%;
  padding: 9px 30px 9px 10px;
  border: 1px solid var(--menu-border);
  border-radius: 8px;
  background: var(--menu-bg);
  color: var(--text-color);
  font: inherit;
  font-size: 13px;
}
.sharing-add-actions {
  grid-column: 1 / -1;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}
.sharing-users {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
}
.sharing-user-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(120px, 155px) 38px;
  align-items: center;
  gap: 9px;
  padding: 10px 0;
  border-bottom: 1px solid var(--menu-border);
}
.sharing-user-row:last-child { border-bottom: 0; }
.sharing-user-info {
  min-width: 0;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 5px 7px;
}
.sharing-user-info strong {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 14px;
}
.sharing-user-info span {
  flex-shrink: 0;
  padding: 2px 6px;
  border-radius: 999px;
  background: var(--text-color);
  color: var(--bg-color);
  font-size: 10px;
  font-weight: 700;
}
.sharing-user-info small {
  flex-basis: 100%;
  font-size: 10px;
  font-weight: 700;
  opacity: 0.55;
}
.sharing-revoke:disabled,
.sharing-role-select:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}
.sharing-empty,
.sharing-error {
  margin: 12px 0;
  font-size: 13px;
}
@media (max-width: 420px) {
  .sharing-modal-content {
    width: 94%;
    height: 92vh;
    height: 92dvh;
    max-height: 92dvh;
  }
  .sharing-user-row {
    grid-template-columns: minmax(0, 1fr) 112px 36px;
    gap: 6px;
  }
  .sharing-add-form { grid-template-columns: 1fr; }
  .sharing-add-actions { grid-column: 1; }
}


/* --- Sprint 1.9d-1f: Consistent Modal and Menu Behavior --- */
.profile-avatar-btn {
  flex: 0 0 36px;
  width: 36px !important;
  min-width: 36px;
  height: 36px !important;
  min-height: 36px;
  padding: 0 !important;
  aspect-ratio: 1 / 1;
  border: 1px solid var(--menu-border) !important;
  border-radius: 50% !important;
  background: transparent !important;
  color: var(--text-color) !important;
  font-size: 14px !important;
  font-weight: 400 !important;
  line-height: 1;
  text-transform: uppercase;
}
.profile-avatar-btn > span {
  display: block;
  font: inherit;
  line-height: 1;
}
#dashboard-view .header-actions { gap: 8px; }
#reader-view .profile-avatar-btn { flex-basis: 36px; }
.profile-menu {
  display: none;
  position: fixed;
  width: min(280px, calc(100vw - 24px));
  flex-direction: column;
  z-index: 1200;
  overflow: hidden;
  border: 1px solid var(--menu-border);
  border-radius: 12px;
  background: var(--menu-bg);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}
.profile-menu.active { display: flex; }
.profile-menu-item {
  display: grid;
  grid-template-columns: 28px minmax(0, 1fr);
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 48px;
  padding: 11px 15px;
  border: 0;
  border-bottom: 1px solid var(--menu-border);
  background: transparent;
  color: var(--text-color);
  font: inherit;
  font-size: 14px;
  text-align: left;
  cursor: pointer;
}
.profile-menu-item:last-child { border-bottom: 0; }
.profile-menu-item:active { opacity: 0.7; }
.profile-menu-item.danger { color: #d9534f; }
.profile-menu-icon {
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  font-size: 13px;
  font-weight: 800;
}
.profile-detail-content {
  display: flex;
  flex-direction: column;
  width: min(92%, 460px);
  min-height: 310px;
  max-height: min(82vh, 520px);
  max-height: min(82dvh, 520px);
  overflow-y: auto;
  overscroll-behavior: contain;
}
.profile-detail-content .modal-header { margin-bottom: 14px; }
.profile-detail-content .profile-modal-close { flex-shrink: 0; }
.profile-data-row {
  display: grid;
  grid-template-columns: minmax(110px, 0.8fr) minmax(0, 1.2fr);
  gap: 12px;
  align-items: center;
  padding: 9px 0;
  font-size: 13px;
}
.profile-data-row span { opacity: 0.65; }
.profile-data-row strong, .profile-data-row code { min-width: 0; overflow-wrap: anywhere; text-align: right; }
.profile-fieldset { margin: 0 0 16px; padding: 0; border: 0; }
.profile-fieldset legend, .profile-font-setting > span { display: block; margin-bottom: 8px; font-size: 12px; font-weight: 700; opacity: 0.75; }
.profile-theme-options { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
button.ui-btn.profile-theme-btn { width: 100%; border-radius: 10px; }
.profile-font-setting { display: block; }
.profile-font-setting > span { display: flex; justify-content: space-between; }
#profile-font-size { width: 100%; }
.profile-settings-feedback { min-height: 18px; margin-top: 7px; font-size: 12px; }
.profile-settings-feedback[data-state='saved'] { color: #2f8f46; }
.profile-settings-feedback[data-state='saving'] { color: #9a6a00; }
.profile-settings-feedback[data-state='error'] { color: #d9534f; }
.notification-explanation, .notification-privacy { margin: 0; font-size: 13px; line-height: 1.5; opacity: 0.72; }
.notification-status { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 12px; align-items: flex-start; margin: 16px 0 10px; padding: 13px; border: 1px solid var(--menu-border); border-radius: 12px; background: var(--menu-bg); }
.notification-status-indicator { width: 12px; height: 12px; margin-top: 4px; border-radius: 50%; background: #9a6a00; }
.notification-status[data-state='allowed'] .notification-status-indicator { background: #2f8f46; }
.notification-status[data-state='blocked'] .notification-status-indicator, .notification-status[data-state='unsupported'] .notification-status-indicator { background: #d9534f; }
.notification-status strong { display: block; font-size: 14px; }
.notification-status p { margin: 3px 0 0; font-size: 12px; line-height: 1.45; opacity: 0.72; }
.notification-feedback { min-height: 20px; margin: 4px 0; font-size: 12px; line-height: 1.45; }
.notification-feedback[data-state='info'] { color: #9a6a00; }
[data-theme='dark'] .notification-feedback[data-state='info'], [data-theme='oled'] .notification-feedback[data-state='info'] { color: #f3c955; }
.notification-actions { margin: 4px 0 14px; }
.notification-actions .ui-btn:disabled { cursor: not-allowed; opacity: 0.45; }
@media (max-width: 420px) {
  .profile-data-row { grid-template-columns: 1fr; gap: 2px; }
  .profile-data-row strong, .profile-data-row code { text-align: left; }
}

.profile-menu-icon svg {
  display: block;
}
.technical-version-list {
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px solid var(--menu-border);
}
.technical-version-list .profile-data-row {
  padding: 6px 0;
}

#technical-modal .profile-data-row { grid-template-columns: minmax(105px, 0.72fr) minmax(0, 1.28fr); }
#profile-user-id { font-size: 11px; line-height: 1.35; letter-spacing: -0.01em; word-break: break-all; }
#reader-view .menu-bar.top { justify-content: flex-start; gap: 10px; }
#reader-view .menu-bar.top #btn-toc { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#reader-view .menu-bar.top #btn-rules,
#reader-view .menu-bar.top .profile-avatar-btn,
#reader-view .menu-bar.top #btn-back { flex-shrink: 0; }

#dashboard-view #dash-dropdown {
  right: 44px;
}
