:root {
  --blue: #334567;
  --gold: #E6C96D;
  --red: #984242;
  --bg: #f5f6f8;
  --panel: #ffffff;
  --text: #202733;
  --muted: #657083;
  --line: #d9dee8;
  --shadow: 0 14px 35px rgba(20, 28, 44, .18);
  --radius: 12px;
  --indent: 34px;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
}
button, input, select, textarea { font: inherit; }
button {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text);
  border-radius: 8px;
  padding: 8px 12px;
  cursor: pointer;
  line-height: 1.1;
}
button:hover { filter: brightness(.98); }
button:disabled { opacity: .55; cursor: not-allowed; }
button.primary { background: var(--blue); color: #fff; border-color: var(--blue); }
button.danger { background: var(--red); color: #fff; border-color: var(--red); }
button.ghost { background: transparent; }
button.small { padding: 6px 9px; font-size: 13px; }
.icon-button {
  width: 34px;
  height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  font-size: 22px;
}
.app-shell { min-height: 100%; display: flex; flex-direction: column; }
.topbar {
  background: var(--blue);
  color: #fff;
  padding: 12px 18px;
  display: flex;
  align-items: center;
  gap: 18px;
  box-shadow: 0 2px 10px rgba(0,0,0,.18);
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand { font-weight: 750; letter-spacing: .02em; font-size: 18px; white-space: nowrap; }
.top-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.topbar button { border-color: rgba(255,255,255,.32); }
.topbar button:not(.primary) { background: rgba(255,255,255,.1); color: #fff; }
.topbar button.primary { background: var(--gold); color: #172033; border-color: var(--gold); font-weight: 700; }
.workspace { padding: 22px; flex: 1; min-height: 0; }
.organizer-panel {
  max-width: 1180px;
  margin: 0 auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 6px 22px rgba(25, 35, 55, .08);
  min-height: calc(100vh - 110px);
  padding: 18px;
}
.panel-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 12px; }
h1 { margin: 0; font-size: 24px; }
.status-line { margin: 4px 0 0; color: var(--muted); font-size: 13px; }
.panel-tools { display: flex; gap: 8px; }
.empty-state {
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  padding: 36px;
  text-align: center;
  color: var(--muted);
  margin-top: 18px;
}
.empty-state h2 { color: var(--text); margin-top: 0; }
.hidden { display: none !important; }
.card-list { display: flex; flex-direction: column; gap: 6px; }
.card-row-wrap { position: relative; }
.card-row {
  min-height: 44px;
  display: grid;
  grid-template-columns: 30px 1fr auto;
  align-items: center;
  gap: 8px;
  background: #fff;
  border: 1px solid var(--line);
  border-left: 8px solid var(--blue);
  border-radius: 10px;
  padding: 6px 8px 6px 0;
  box-shadow: 0 1px 2px rgba(0,0,0,.04);
}
.card-row.dragging { opacity: .45; }
.card-row.drop-before { outline: 2px solid var(--gold); outline-offset: 2px; }
.drag-handle {
  color: #8a94a6;
  cursor: grab;
  text-align: center;
  user-select: none;
  font-weight: 800;
}
.drag-handle:active { cursor: grabbing; }
.card-main { display: flex; align-items: center; gap: 8px; min-width: 0; }
.collapse-toggle {
  width: 25px;
  height: 25px;
  border-radius: 6px;
  padding: 0;
  border-color: transparent;
  color: var(--muted);
  background: transparent;
}
.card-title {
  font-weight: 700;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.tag-list { display: inline-flex; gap: 4px; flex-wrap: nowrap; overflow: hidden; min-width: 0; }
.tag-pill {
  font-size: 11px;
  border: 1px solid #cdd4df;
  background: #f4f6fa;
  border-radius: 999px;
  padding: 2px 6px;
  color: #4d5b70;
  white-space: nowrap;
}
.card-actions { display: flex; align-items: center; gap: 4px; position: relative; }
.card-actions button { width: 30px; height: 30px; padding: 0; border-radius: 7px; }
.card-menu {
  position: absolute;
  top: 34px;
  right: 0;
  min-width: 140px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 12px 24px rgba(20, 28, 44, .18);
  padding: 6px;
  z-index: 10;
}
.card-menu button {
  width: 100%;
  height: auto;
  padding: 8px 10px;
  text-align: left;
  border: none;
  background: transparent;
}
.card-menu button:hover { background: #f2f4f8; }
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(12, 18, 30, .48);
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.modal {
  background: #fff;
  border-radius: 14px;
  width: min(620px, 100%);
  max-height: min(88vh, 900px);
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow);
  border: 1px solid rgba(255,255,255,.3);
}
.modal.large { width: min(880px, 100%); }
.modal.confirm { width: min(460px, 100%); }
.modal-header,
.modal-footer {
  padding: 14px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border-bottom: 1px solid var(--line);
}
.modal-footer { border-bottom: none; border-top: 1px solid var(--line); justify-content: flex-end; }
.modal-header h2 { margin: 0; font-size: 19px; }
.modal-body { padding: 16px; overflow: auto; }
.field-row { display: grid; gap: 5px; margin-bottom: 12px; }
.field-row > span { font-size: 13px; color: var(--muted); font-weight: 650; }
input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  background: #fff;
}
textarea { min-height: 88px; resize: vertical; }
input[type="checkbox"] { width: auto; }
.checkbox-line { display: flex; align-items: center; gap: 8px; margin: 6px 0; }
.modal-section { border-top: 1px solid var(--line); padding-top: 14px; margin-top: 16px; }
.modal-section h3 { margin: 0 0 10px; font-size: 15px; }
.section-title-row { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.field-help { margin: -2px 0 12px; font-size: 13px; }
.template-fields { display: grid; gap: 10px; }
.template-field {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  background: #fbfcfe;
}
.system-template-field {
  background: #f3f5f9;
}
.locked-field-badge {
  align-self: end;
  justify-self: start;
  border: 1px solid #cdd4df;
  color: #4d5b70;
  border-radius: 999px;
  padding: 6px 9px;
  font-size: 12px;
  font-weight: 700;
}
.template-field-grid {
  display: grid;
  grid-template-columns: 1.4fr 1fr auto auto;
  gap: 10px;
  align-items: end;
}
.template-field-options { margin-top: 8px; }
.template-field .field-row { margin-bottom: 0; }
.lock-note { color: var(--muted); font-size: 12px; margin-top: 6px; }
.card-fields { margin-top: 14px; }
.option-group {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  margin-bottom: 12px;
}
.option-group legend { padding: 0 5px; color: var(--muted); font-weight: 650; font-size: 13px; }
.muted { color: var(--muted); }
.toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  background: #172033;
  color: #fff;
  border-radius: 10px;
  padding: 11px 13px;
  box-shadow: var(--shadow);
  z-index: 80;
  max-width: min(420px, calc(100vw - 36px));
}
@media (max-width: 720px) {
  .topbar { align-items: flex-start; flex-direction: column; }
  .workspace { padding: 12px; }
  .organizer-panel { padding: 12px; }
  .panel-heading { flex-direction: column; }
  .card-row { grid-template-columns: 26px 1fr; }
  .card-actions { grid-column: 2; justify-content: flex-end; }
  .template-field-grid { grid-template-columns: 1fr; }
}

/* v3 enhancements */
button i { pointer-events: none; }
.panel-tools button { display: inline-flex; align-items: center; gap: 6px; justify-content: center; }
.select-panel {
  border: 1px solid var(--line);
  background: #fbfcfe;
  border-radius: var(--radius);
  padding: 12px;
  margin: 8px 0 14px;
}
.select-panel-top {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}
.select-filters {
  display: grid;
  grid-template-columns: repeat(4, minmax(150px, 1fr)) auto;
  gap: 10px;
  align-items: end;
}
.select-filters label { display: grid; gap: 4px; }
.select-filters label span { font-size: 12px; font-weight: 700; color: var(--muted); }
.select-filters input,
.select-filters select { min-height: 36px; }
.card-row-wrap.selected .card-row {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
  background: #fffdf4;
}
.select-check {
  color: var(--blue);
  text-align: center;
  font-size: 17px;
}
.card-menu button {
  display: flex;
  gap: 8px;
  align-items: center;
}
.warning-tag { background: #fff0f0 !important; color: var(--red) !important; border-color: #f0c7c7 !important; }
.hex-color-control {
  display: flex;
  align-items: center;
  gap: 8px;
  max-width: 190px;
}
.hex-color-control .hex-input {
  width: 110px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  text-transform: uppercase;
}
.tag-input {
  position: relative;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 5px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 5px;
  background: #fff;
}
.tag-input:focus-within {
  border-color: var(--blue);
  box-shadow: 0 0 0 3px rgba(51, 69, 103, .12);
}
.tag-input input {
  border: none;
  outline: none;
  box-shadow: none;
  flex: 1 1 180px;
  min-width: 150px;
  padding: 5px;
}
.tag-input-pills {
  display: contents;
}
.tag-pill.entity {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 7px;
}
.tag-pill.entity button {
  width: auto;
  height: auto;
  border: none;
  background: transparent;
  color: inherit;
  padding: 0;
  opacity: .8;
}
.tag-pill.entity button:hover { opacity: 1; }
.tag-suggestions {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  z-index: 70;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 12px 24px rgba(20, 28, 44, .18);
  padding: 5px;
  max-height: 220px;
  overflow: auto;
}
.tag-suggestions button {
  width: 100%;
  border: none;
  background: transparent;
  padding: 8px;
  display: flex;
  align-items: center;
  gap: 8px;
  text-align: left;
}
.tag-suggestions button:hover { background: #f2f4f8; }
.tag-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  flex: 0 0 auto;
}
@media (max-width: 920px) {
  .select-filters { grid-template-columns: 1fr 1fr; }
  .select-filters button { grid-column: span 2; }
}
@media (max-width: 620px) {
  .select-filters { grid-template-columns: 1fr; }
  .select-filters button { grid-column: auto; }
}

.fa-svg {
  width: 1em;
  height: 1em;
  display: inline-block;
  vertical-align: -0.125em;
  fill: currentColor;
  pointer-events: none;
}
.card-actions button,
.drag-handle,
.select-check,
.collapse-toggle,
.locked-field-badge,
#selectModeBtn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
}
.hex-swatch {
  width: 46px;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--blue);
}

/* v5 icon handling: use real FontAwesome when available, with a text fallback if CDN/font loading is blocked. */
.fa-solid { display: none; }
.icon-fallback { display: inline-block; font-weight: 800; line-height: 1; }
body.fa-ready .fa-solid { display: inline-block; }
body.fa-ready .icon-fallback { display: none; }
.card-actions button i,
.card-actions button .icon-fallback,
.collapse-toggle i,
.collapse-toggle .icon-fallback,
.drag-handle i,
.drag-handle .icon-fallback,
.select-check i,
.select-check .icon-fallback { pointer-events: none; }

/* v5 color control: visual picker with Hex primary and RGB secondary; no native browser HSL/RGB picker. */
.hex-color-control { display: none; }
.color-control {
  position: relative;
  display: grid;
  gap: 8px;
  max-width: 360px;
}
.color-visual-button {
  display: inline-flex;
  align-items: center;
  justify-content: flex-start;
  gap: 8px;
  width: max-content;
  max-width: 100%;
}
.color-preview {
  width: 24px;
  height: 24px;
  border-radius: 6px;
  border: 1px solid var(--line);
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.35);
}
.color-picker-popover {
  position: absolute;
  top: 42px;
  left: 0;
  z-index: 80;
  width: 260px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 12px 24px rgba(20, 28, 44, .18);
  padding: 10px;
}
.color-picker-square {
  position: relative;
  height: 150px;
  border-radius: 10px;
  border: 1px solid var(--line);
  cursor: crosshair;
  overflow: hidden;
}
.color-picker-target {
  position: absolute;
  width: 14px;
  height: 14px;
  border: 2px solid #fff;
  border-radius: 50%;
  box-shadow: 0 0 0 1px rgba(0,0,0,.45);
  transform: translate(-50%, -50%);
  pointer-events: none;
}
.color-hue-row {
  display: grid;
  grid-template-columns: 38px 1fr;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
  font-size: 12px;
  color: var(--muted);
}
.color-hue-row input { padding: 0; }
.color-value-grid {
  display: grid;
  grid-template-columns: minmax(118px, 1.25fr) repeat(3, minmax(55px, .55fr));
  gap: 8px;
  align-items: end;
}
.color-value-grid label {
  display: grid;
  gap: 3px;
  margin: 0;
}
.color-value-grid label span {
  font-size: 12px;
  color: var(--muted);
  font-weight: 650;
}
.color-value-grid input {
  min-width: 0;
  padding: 7px 8px;
}
.color-value-grid .hex-input {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  text-transform: uppercase;
}

/* v6 fixes */
#selectModeBtn.primary,
.panel-tools #selectModeBtn.primary {
  background: var(--blue);
  color: #fff;
  border-color: var(--blue);
}
#selectModeBtn.primary .fa-solid,
#selectModeBtn.primary .icon-fallback { color: #fff; }
.tag-editor-rows {
  display: grid;
  gap: 10px;
}
.tag-editor-row {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fbfcfe;
  padding: 10px;
  display: grid;
  grid-template-columns: minmax(180px, 1fr) minmax(260px, 360px);
  gap: 12px;
  align-items: start;
}
.tag-editor-row .field-row { margin-bottom: 0; }
@media (max-width: 720px) {
  .tag-editor-row { grid-template-columns: 1fr; }
}

/* v7 refinements */
.color-control {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  flex-wrap: wrap;
  max-width: none;
}
.color-control .color-visual-button {
  flex: 0 0 auto;
  min-height: 38px;
}
.color-control .color-value-grid {
  flex: 1 1 360px;
  max-width: 480px;
}
.field-row > .color-control { margin-top: 0; }
.template-field-grid {
  grid-template-columns: minmax(180px, 1.4fr) minmax(140px, .9fr) auto auto auto auto;
}
.card-main { flex-wrap: nowrap; }
.card-visible-fields {
  display: inline-flex;
  gap: 4px;
  align-items: center;
  overflow: hidden;
  min-width: 0;
}
.visible-field-pill {
  display: inline-block;
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 11px;
  border: 1px solid #dce1ea;
  background: #f8f9fb;
  border-radius: 999px;
  padding: 2px 7px;
  color: #4d5b70;
}
.visible-field-pill strong { color: #30394a; }
.view-card-body { display: grid; gap: 14px; }
.view-card-summary {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 18px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fbfcfe;
}
.view-field-list { display: grid; gap: 10px; }
.view-field {
  border-bottom: 1px solid var(--line);
  padding-bottom: 9px;
}
.view-field-label {
  font-size: 12px;
  font-weight: 750;
  color: var(--muted);
  margin-bottom: 4px;
}
.view-field-value {
  white-space: pre-wrap;
  line-height: 1.35;
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}
#editViewedCardBtn { display: inline-flex; align-items: center; gap: 7px; }
@media (max-width: 940px) {
  .template-field-grid { grid-template-columns: 1fr 1fr; }
  .template-field-grid .tf-remove { grid-column: span 2; width: max-content; }
}
@media (max-width: 720px) {
  .color-control { align-items: stretch; }
  .color-control .color-value-grid { flex-basis: 100%; max-width: none; }
  .template-field-grid { grid-template-columns: 1fr; }
  .template-field-grid .tf-remove { grid-column: auto; }
  .card-visible-fields { display: none; }
}

/* v8 refinements */
.card-main {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 3px;
  min-width: 0;
  flex: 1 1 auto;
}
.card-main-top {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}
.card-title { min-width: 0; }
.hierarchy-pill {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  color: #4d5b70;
  background: #eef2f7;
  border: 1px solid #d7deea;
  border-radius: 999px;
  padding: 2px 7px;
  white-space: nowrap;
}
.card-visible-fields {
  display: flex;
  gap: 5px;
  align-items: center;
  overflow: hidden;
  min-width: 0;
  padding-left: 33px;
  margin-top: 1px;
}
.visible-field-pill a,
.view-field-value a {
  color: var(--blue);
  text-decoration: underline;
  text-underline-offset: 2px;
}
.tag-input { overflow: visible; }
.tag-suggestions {
  z-index: 5000;
}
.tag-editor-row {
  grid-template-columns: minmax(180px, 1fr) minmax(260px, 360px) minmax(260px, 360px);
}
.card-row {
  align-items: center;
}
@media (max-width: 980px) {
  .tag-editor-row { grid-template-columns: 1fr; }
}
@media (max-width: 720px) {
  .card-visible-fields { padding-left: 0; }
}
