:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --border: #d9dde3;
  --text: #1f2430;
  --muted: #6b7280;
  --primary: #2563eb;
  --primary-text: #ffffff;
  --danger: #dc2626;
  --hl-in: #fde68a;   /* keywords in the original text */
  --hl-out: #bfdbfe;  /* replacements in the anonymized text */
  --sidebar-bg: #1f2430;
  --sidebar-text: #e5e7eb;
  --sidebar-active: #2563eb;
  --radius: 6px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  font-family: var(--font);
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
}

body {
  display: flex;
  min-height: 100vh;
}

/* ---------- Sidebar ---------- */
.sidebar {
  width: 260px;
  flex: 0 0 260px;
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  display: flex;
  flex-direction: column;
  height: 100vh;
  position: sticky;
  top: 0;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-bottom: 1px solid rgba(255,255,255,0.1);
}

.sidebar-header h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}

.workspace-list {
  list-style: none;
  margin: 0;
  padding: 8px;
  overflow-y: auto;
  flex: 1;
}

.workspace-list li {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  border-radius: var(--radius);
  cursor: pointer;
  white-space: nowrap;
  margin-bottom: 2px;
}
.workspace-list li .ws-list-name { flex: 0 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; }

.workspace-list li:hover { background: rgba(255,255,255,0.08); }
.workspace-list li.active { background: var(--sidebar-active); color: #fff; }
.workspace-list li .count { color: rgba(255,255,255,0.55); font-size: 12px; flex: none; }
.workspace-list li.active .count { color: rgba(255,255,255,0.8); }
.workspace-list li .demo-badge {
  flex: none;
  margin-left: auto;
  padding: 1px 6px;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: rgba(255,255,255,0.7);
  border: 1px solid rgba(255,255,255,0.25);
  border-radius: 999px;
}
.workspace-list li.active .demo-badge { color: #fff; border-color: rgba(255,255,255,0.5); }

.demo-note {
  margin: -8px 0 20px;
  padding: 10px 14px;
  background: #eff6ff;
  border: 1px solid #bfdbfe;
  border-radius: var(--radius);
  font-size: 13px;
  color: #1e3a8a;
}
.demo-note strong { display: block; margin-bottom: 4px; }
.demo-note ul { margin: 0; padding-left: 18px; }
.demo-note li { margin: 2px 0; }

.sidebar-footer {
  padding: 12px 16px 16px;
  border-top: 1px solid rgba(255,255,255,0.1);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sidebar-footer .btn { background: rgba(255,255,255,0.08); color: var(--sidebar-text); border-color: rgba(255,255,255,0.15); }
.sidebar-footer .btn:hover { background: rgba(255,255,255,0.14); }
.sidebar-footer .btn-danger { color: #fca5a5; }
.sidebar-footer .hint { color: rgba(255,255,255,0.5); }
/* Links in the dark sidebar footer: one light blue, always underlined. */
.sidebar-footer .link-btn-light,
.sidebar-footer .legal-links a { color: #93c5fd; text-decoration: underline; }
.sidebar-footer .link-btn-light:hover,
.sidebar-footer .legal-links a:hover { color: #bfdbfe; }
.legal-links { margin-top: 4px; }
.help-body .hint a { color: var(--primary); }

/* ---------- Main ---------- */
.main {
  flex: 1;
  padding: 24px;
  overflow-y: auto;
  min-width: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.workspace-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.empty-state {
  color: var(--muted);
  padding: 48px;
  text-align: center;
}

.ws-header {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 24px;
  flex-wrap: wrap;
}

.ws-name {
  font-size: 20px;
  font-weight: 600;
  border: 1px solid transparent;
  background: transparent;
  padding: 6px 8px;
  border-radius: var(--radius);
  flex: 1;
  min-width: 200px;
  color: var(--text);
}
.ws-name-wrap { display: flex; align-items: center; gap: 4px; flex: 1; min-width: 200px; cursor: text; }
.ws-name-wrap .ws-name { flex: 1; min-width: 0; }
.ws-name-pencil { color: var(--muted); opacity: 0.5; flex: none; }
.ws-name-wrap:hover .ws-name-pencil, .ws-name-wrap:focus-within .ws-name-pencil { opacity: 1; color: var(--primary); }
.ws-name:hover { border-color: var(--border); }
.ws-name:focus { border-color: var(--primary); outline: none; background: var(--panel); }

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}
.section-title h2 { margin: 0; font-size: 15px; font-weight: 600; }
.section-actions { display: flex; align-items: center; gap: 10px; }
.segmented-sm .seg { padding: 4px 10px; font-size: 13px; }

/* ---------- Compact keyword view ---------- */
.compact-entry {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}
.compact-entry input[type="text"] {
  flex: 1 1 180px;
  min-width: 120px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: var(--mono);
  font-size: 13px;
  color: var(--text);
}
.compact-entry input[type="text"]:focus { border-color: var(--primary); outline: none; }
.compact-entry .arrow { color: var(--muted); }
.opt-check { font-family: var(--mono); font-size: 12px; color: var(--muted); padding: 0 4px; gap: 3px; }
.opt-check:has(input:checked) { color: var(--primary); }

.pills {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 8px;
}
.pill {
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: #f0f2f5;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1;
  overflow: hidden;
}
.pill.editing { border-color: var(--primary); box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.2); }
.pill.invalid { border-color: var(--danger); }
.pill > button {
  font: inherit;
  border: none;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  padding: 6px 6px;
  line-height: 1;
}
.pill-main { padding-left: 10px !important; display: inline-flex; gap: 6px; align-items: center; }
.pill-main:hover { background: #e4e8ee; }
.pill-kw { background: var(--hl-in); border-radius: 3px; padding: 1px 4px; }
.pill-rp { background: var(--hl-out); border-radius: 3px; padding: 1px 4px; }
.pill-arrow { color: var(--muted); }
.pill-empty { color: var(--muted); font-style: italic; }
.pill-opt { color: var(--muted) !important; opacity: 0.4; font-weight: 600; padding: 5px 3px !important; display: inline-flex; align-items: center; }
.pill-opt.on { opacity: 1; color: var(--primary) !important; }
.pill-opt:hover { opacity: 1; }
.pill-opt:disabled { opacity: 0.15; cursor: not-allowed; }
.pill-opt:disabled:hover { opacity: 0.15; }
.pill-hits { color: var(--muted); font-size: 11px; padding-left: 2px; }
.pill-del { color: var(--muted) !important; padding: 5px 9px 5px 4px !important; display: inline-flex; align-items: center; }
.pill-del:hover { color: var(--danger) !important; }

/* ---------- Rules table ---------- */
.rules { margin-bottom: 24px; }
.rules.collapsed { margin-bottom: 16px; }

.collapse-btn {
  font: inherit;
  font-weight: 600;
  font-size: 15px;
  color: var(--text);
  background: transparent;
  border: none;
  padding: 2px 4px 2px 0;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.collapse-btn:hover { color: var(--primary); }
.collapse-btn .chevron { display: inline-block; transition: transform 0.15s; font-size: 14px; }
.rules.collapsed .collapse-btn .chevron,
.mappings.collapsed .collapse-btn .chevron { transform: rotate(-90deg); }
/* Collapsed sections show only their title. */
.rules.collapsed .section-actions,
.mappings.collapsed .section-actions { display: none; }

.rules-scroll {
  overflow-y: auto;
  resize: vertical;         /* drag the handle to set the height manually */
  min-height: 60px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.rules-scroll:empty { resize: none; }

.rules-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  background: var(--panel);
}
.rules-table thead th {
  position: sticky;
  top: 0;
  z-index: 1;
}

.rules-table th, .rules-table td {
  padding: 6px 8px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: middle;
}
.rules-table th { background: #f0f2f5; font-weight: 600; font-size: 12px; color: var(--muted); }
.rules-table tr:last-child td { border-bottom: none; }

.link-btn {
  font: inherit;
  font-size: 12px;
  color: var(--primary);
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  text-decoration: underline;
}
.rules-table .col-opt { width: 96px; text-align: center; }
.rules-table .col-hits { width: 48px; text-align: right; }
.rules-table td.col-hits { text-align: right; }
.rules-table .hits { font-family: var(--mono); font-size: 12px; color: var(--muted); }
.rules-table .col-del { width: 44px; }
.rules-table input[type="checkbox"]:disabled { opacity: 0.3; cursor: not-allowed; }
.rules-table tr.is-pattern input.kw, .rules-table tr.is-pattern input.rp { background: #fbfaf5; }
.rules-table td.col-opt { text-align: center; }
.rules-table input[type="text"] {
  width: 100%;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: var(--mono);
  font-size: 13px;
  color: var(--text);
}
.rules-table input[type="text"]:focus { border-color: var(--primary); outline: none; }
.rules-table input[type="checkbox"] { width: 16px; height: 16px; cursor: pointer; }
.rules-table tr.invalid input.kw { border-color: var(--danger); }
.rules-table tr.warn input.kw, .rules-table tr.warn input.rp { border-color: #f59e0b; background: #fffbeb; }
.rules-table tr.derived input.kw { border-style: dashed; }
.pill.warn { border-color: #f59e0b; background: #fffbeb; }
.pill.derived .pill-kw { outline: 1px dashed #b45309; outline-offset: -1px; }

/* ---------- Wildcard mappings ---------- */
.mappings { margin-bottom: 24px; }
.mappings.collapsed { margin-bottom: 16px; }
.mappings .rules-scroll { max-height: 220px; }
.mappings-table td { font-family: var(--mono); font-size: 13px; overflow-wrap: anywhere; }
.mappings-table .map-from { background: #fffbeb; }
.mappings-table .map-to { background: #eff6ff; }
.mappings-table th { width: 50%; }
.mappings-table th.col-del { width: 44px; }
.seed-label { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.seed-input {
  width: 160px;
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: var(--mono);
  font-size: 12px;
  color: var(--text);
}
.seed-input:focus { border-color: var(--primary); outline: none; }
.hint code { font-family: var(--mono); background: #eef1f5; padding: 0 3px; border-radius: 3px; }

/* ---------- Input / output ---------- */
.io {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 360px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px 16px;
  gap: 10px;
}

.io-toolbar {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
}

.io-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.io-actions-right { margin-left: auto; }

.segmented {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.seg {
  font: inherit;
  padding: 6px 14px;
  border: none;
  background: var(--panel);
  color: var(--muted);
  cursor: pointer;
}
.seg + .seg { border-left: 1px solid var(--border); }
.seg:hover { background: #eef1f5; }
.seg.active { background: var(--primary); color: var(--primary-text); }

.io-columns {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  min-height: 0;
}
@media (max-width: 900px) { .io-columns { grid-template-columns: 1fr; } }

.io-col {
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 6px;
  min-height: 0;
}
.io-col h2 { margin: 0; font-size: 13px; font-weight: 600; color: var(--muted); }

/* The textarea is transparent and sits on top of a backdrop that renders the
   same text (invisible) with <mark> elements for keyword highlighting. */
.io-field {
  position: relative;
  min-height: 0;
  background: var(--panel);
  border-radius: var(--radius);
}
.io-field-readonly { background: #fafbfc; }

.io-col textarea,
.io-highlights {
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.45;
  padding: 10px;
  margin: 0;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: normal;
  tab-size: 4;
}

.io-col textarea {
  position: relative;
  z-index: 1;
  display: block;
  width: 100%;
  height: 100%;
  min-height: 200px;
  resize: vertical;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  background: transparent;
}
.io-col textarea:focus { border-color: var(--primary); outline: none; }

.io-backdrop {
  position: absolute;
  top: 1px;   /* textarea border width */
  left: 1px;
  overflow: hidden;
  pointer-events: none;
  z-index: 0;
  border-radius: var(--radius);
}
.io-highlights {
  color: transparent;
  min-height: 100%;
}
.io-highlights mark {
  color: transparent;
  background: var(--hl-in);
  border-radius: 2px;
}
.io-highlights.hl-out mark { background: var(--hl-out); }

.count { color: var(--muted); font-size: 12px; }

/* Hidden mirror of the input text used to locate the selection. */
.io-measure {
  position: absolute;
  top: 1px;
  left: 1px;
  visibility: hidden;
  pointer-events: none;
  z-index: 0;
  min-height: 0;
}
.io-measure-marker { display: inline-block; width: 0; }

/* Popup under a selection in the original text. */
.sel-popup {
  position: absolute;
  z-index: 3;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 6px 20px rgba(0,0,0,0.18);
  max-width: calc(100% - 8px);
}
.sel-popup input.sel-popup-kw {
  width: 170px;
  background: var(--hl-in);
  border-color: transparent;
}
.sel-popup input.sel-popup-kw:focus { border-color: var(--primary); }
.sel-popup-del:hover { color: var(--danger); }
.sel-popup.editing { border-color: var(--primary); }
.sel-popup .arrow { color: var(--muted); }
.sel-popup input[type="text"] {
  width: 130px;
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: var(--mono);
  font-size: 12px;
  color: var(--text);
}
.sel-popup input[type="text"]:focus { border-color: var(--primary); outline: none; }
.sel-popup .btn-primary { padding: 5px 10px; font-size: 13px; }
.sel-popup-pt {
  font: inherit;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: transparent;
  cursor: pointer;
  padding: 4px 5px !important;
  display: inline-flex;
  align-items: center;
}
.sel-popup-pt.on { border-color: var(--primary); background: #eff6ff; }

/* ---------- Icons (inline SVG sprite, see index.html) ---------- */
.icon {
  width: 16px;
  height: 16px;
  fill: currentColor;
  display: inline-block;
  vertical-align: middle;
  flex: none;
}
.btn-icon .icon { width: 15px; height: 15px; }
.pill-opt .icon { width: 15px; height: 15px; display: block; }
.opt-check .icon { width: 17px; height: 17px; }

/* ---------- Controls ---------- */
.btn {
  font: inherit;
  padding: 6px 12px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  border-radius: var(--radius);
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: #eef1f5; }
.btn-primary { background: var(--primary); color: var(--primary-text); border-color: var(--primary); }
.btn-primary:hover { background: #1d4ed8; }
.btn:disabled, .btn:disabled:hover { opacity: 0.45; cursor: not-allowed; background: var(--panel); color: var(--muted); border-color: var(--border); }
.btn-primary:disabled, .btn-primary:disabled:hover { background: var(--primary); color: var(--primary-text); border-color: var(--primary); }
.btn-danger { color: var(--danger); }
.btn-danger:hover { background: #fee2e2; }
.btn-icon { padding: 4px 8px; border: none; background: transparent; color: var(--muted); font-size: 16px; line-height: 1; display: inline-flex; align-items: center; }
.btn-icon:hover { color: var(--danger); background: transparent; }

.checkbox { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; user-select: none; }

.hint { color: var(--muted); font-size: 12px; margin: 8px 0 0; }

.badge {
  display: inline-block;
  margin-left: 8px;
  padding: 1px 6px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.04em;
  color: #92400e;
  background: #fef3c7;
  border: 1px solid #fcd34d;
  border-radius: 4px;
  vertical-align: middle;
  cursor: help;
}

body.overwrite .rules-table input[type="text"]:focus { caret-shape: block; }

/* ---------- Help dialog ---------- */
.btn-help { display: inline-flex; align-items: center; gap: 4px; color: var(--muted); }
.btn-help:hover { color: var(--primary); }
.help {
  width: min(880px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 12px 40px rgba(0,0,0,0.25);
  display: none;
  flex-direction: column;
}
.help[open] { display: flex; }
.help::backdrop { background: rgba(15, 23, 42, 0.45); }
.help-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px 12px 24px;
  border-bottom: 1px solid var(--border);
}
.help-head h2 { margin: 0; font-size: 16px; }
.help-body { padding: 8px 24px 24px; overflow-y: auto; line-height: 1.5; }
.help-body h3 { font-size: 14px; margin: 20px 0 6px; }
.help-body p { margin: 6px 0; }
.help-body code { font-family: var(--mono); font-size: 12.5px; background: #eef1f5; padding: 1px 4px; border-radius: 3px; white-space: nowrap; }
.help-body .icon { color: var(--primary); }
.help-table { width: 100%; border-collapse: collapse; margin: 6px 0; font-size: 13px; }
.help-table th, .help-table td { text-align: left; vertical-align: top; padding: 6px 8px; border-bottom: 1px solid var(--border); }
.help-table th { font-size: 12px; color: var(--muted); background: #f0f2f5; }
.help-compact { width: min(680px, calc(100vw - 32px)); }
.help-compact ul { margin: 4px 0; padding-left: 20px; }
.help-compact li { margin: 4px 0; }

/* ---------- Export / import dialogs ---------- */
.pick-all { font-weight: 600; margin: 8px 0 4px; }
.pick-list { list-style: none; margin: 0 0 8px; padding: 0; border: 1px solid var(--border); border-radius: var(--radius); max-height: 320px; overflow-y: auto; }
.pick-list li { border-bottom: 1px solid var(--border); }
.pick-list li:last-child { border-bottom: none; }
.pick-list label { display: flex; align-items: center; gap: 8px; padding: 7px 10px; cursor: pointer; }
.pick-list label:hover { background: #f6f7f9; }
.pick-list .count { margin-left: auto; }
.pick-list .demo-badge { font-size: 10px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); border: 1px solid var(--border); border-radius: 999px; padding: 1px 6px; }
.dialog-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 12px; }
.import-table select { font: inherit; font-size: 13px; padding: 3px 6px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--panel); }
.import-table .status-new { color: #047857; }
.import-table .status-exists { color: #b45309; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: #fff;
  padding: 10px 16px;
  border-radius: var(--radius);
  box-shadow: 0 4px 16px rgba(0,0,0,0.25);
  z-index: 10;
}

/* ---------- Small screens ---------- */
@media (max-width: 760px) {
  body { flex-direction: column; }
  .sidebar { width: 100%; flex: none; height: auto; position: static; max-height: 45vh; }
  .main { padding: 16px; height: auto; }
}
