:root {
  --bg: #0b0e13;
  --panel: #141922;
  --panel-2: #1a2029;
  --border: #262e3a;
  --text: #e8ebef;
  --text-dim: #9aa4b2;
  --accent: #2dd4bf;
  --danger: #ef4444;
  --radius: 12px;
  font-family: 'Inter', system-ui, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 28px;
  border-bottom: 1px solid var(--border);
  background: #0d1117;
  position: sticky;
  top: 0;
  z-index: 10;
  flex-wrap: wrap;
  gap: 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-family: 'Rajdhani', sans-serif;
  font-weight: 700;
  font-size: 20px;
  letter-spacing: 0.02em;
}
.brand-mark { color: var(--accent); font-size: 22px; }
.brand strong { color: var(--accent); font-weight: 800; }

.backup-actions { display: flex; align-items: center; gap: 8px; }
.backup-actions .btn-secondary { padding: 7px 12px; font-size: 12.5px; }

.tabs { display: flex; gap: 6px; }
.tab-btn {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-dim);
  padding: 9px 16px;
  border-radius: 999px;
  cursor: pointer;
  font-family: 'Inter', sans-serif;
  font-weight: 600;
  font-size: 14px;
  transition: all .15s;
}
.tab-btn:hover { color: var(--text); border-color: var(--border); }
.tab-btn.active { background: var(--accent); color: #06231f; }

main { padding: 24px 28px 60px; max-width: 1400px; margin: 0 auto; }

.tab-panel { display: none; }
.tab-panel.active { display: block; }

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}

.designer-grid {
  display: grid;
  grid-template-columns: minmax(340px, 460px) 1fr;
  gap: 20px;
  align-items: start;
}

@media (max-width: 900px) {
  .designer-grid { grid-template-columns: 1fr; }
}

.form-panel { display: flex; flex-direction: column; gap: 16px; max-height: calc(100vh - 120px); overflow-y: auto; }

fieldset {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
legend {
  padding: 0 6px;
  font-weight: 700;
  font-family: 'Rajdhani', sans-serif;
  font-size: 15px;
  letter-spacing: 0.04em;
  color: var(--accent);
  text-transform: uppercase;
}

label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  color: var(--text-dim);
  font-weight: 500;
}
label.grow { flex: 1; }
label.narrow { width: 90px; }

input[type="text"], input[type="number"], select, input[type="range"] {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 9px 10px;
  border-radius: 8px;
  font-family: 'Inter', sans-serif;
  font-size: 14px;
}
input[type="color"] {
  height: 38px;
  padding: 2px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

.checkbox-label { flex-direction: row; align-items: center; gap: 8px; font-size: 13px; }
.checkbox-label input { width: auto; }

.field-row { display: flex; gap: 12px; flex-wrap: wrap; }
.field-row > label:not(.grow):not(.narrow) { flex: 1 1 120px; min-width: 100px; }

.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 14px;
}
.stat-grid .hint { grid-column: 1 / -1; margin: -2px 0 2px; }
.stat-grid .hint:empty { display: none; }
.stat-grid-actions { grid-column: 1 / -1; display: flex; justify-content: flex-end; margin: -8px 0 -2px; }
.stat-reset-btn { padding: 5px 10px; font-size: 11.5px; }
.stat-group-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  font-weight: 700;
}
.stat-row {
  grid-column: span 1;
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 8px;
}
.stat-row span:first-child { flex: 1; font-size: 13px; font-weight: 600; }
.stat-row .d { color: var(--text-dim); font-size: 12px; }
.stat-row input[type="number"] { width: 44px; padding: 5px 6px; text-align: center; }
.stat-row select { width: 56px; padding: 5px 4px; }

.ability-item {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  background: var(--panel-2);
  position: relative;
}

.ability-name-wrap { position: relative; }

.ability-suggestions {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  background: var(--panel);
  border: 1px solid var(--accent);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.5);
  z-index: 30;
  max-height: 240px;
  overflow-y: auto;
}
.ability-suggestions .sugg-item {
  padding: 8px 10px;
  cursor: pointer;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  border-bottom: 1px solid var(--border);
}
.ability-suggestions .sugg-item:last-child { border-bottom: none; }
.ability-suggestions .sugg-item:hover,
.ability-suggestions .sugg-item.active {
  background: rgba(45,212,191,0.14);
}
.ability-suggestions .sugg-name { font-weight: 600; font-size: 13px; color: var(--text); }
.ability-suggestions .sugg-level {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-dim);
  flex-shrink: 0;
}
.ability-suggestions .sugg-text {
  display: block;
  font-size: 11px;
  color: var(--text-dim);
  font-weight: 400;
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.ability-suggestions .sugg-empty {
  padding: 10px;
  font-size: 12px;
  color: var(--text-dim);
}
.ability-item input, .ability-item textarea {
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 7px 9px;
  font-family: 'Inter', sans-serif;
  font-size: 13px;
  width: 100%;
}
.ability-item textarea { resize: vertical; min-height: 44px; }
.ability-item textarea[readonly] {
  background: var(--panel-2);
  border-style: dashed;
  cursor: default;
  resize: none;
}
.ability-text-locked-note {
  font-size: 10.5px;
  color: var(--text-dim);
  margin: -2px 0 0;
}
.ability-rename-note {
  font-size: 11px;
  color: var(--text-dim);
  margin-top: 2px;
}
.ability-rename-note strong { color: var(--text); font-weight: 600; }
.ability-reset-name { color: var(--accent); text-decoration: none; }
.ability-reset-name:hover { text-decoration: underline; }
.ability-remove {
  align-self: flex-end;
  background: transparent;
  border: none;
  color: var(--danger);
  cursor: pointer;
  font-size: 12px;
  font-weight: 600;
}

.btn-primary, .btn-secondary, .btn-ghost {
  border-radius: 8px;
  padding: 10px 16px;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  font-family: 'Inter', sans-serif;
  border: 1px solid transparent;
}
.btn-primary { background: var(--accent); color: #06231f; }
.btn-primary:hover { filter: brightness(1.08); }
.btn-secondary { background: var(--panel-2); border-color: var(--border); color: var(--text); }
.btn-secondary:hover { border-color: var(--accent); color: var(--accent); }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--text-dim); }
.btn-ghost:hover { color: var(--text); }

.hint { color: var(--text-dim); font-size: 12px; margin: 2px 0 0; }

.preview-panel { display: flex; flex-direction: column; align-items: center; gap: 16px; position: sticky; top: 84px; }
.card-stage {
  background: repeating-conic-gradient(#11151c 0% 25%, #14181f 0% 50%) 50% / 24px 24px;
  border-radius: 16px;
  padding: 20px;
  display: flex;
  justify-content: center;
}
#card-canvas {
  width: min(360px, 78vw);
  height: auto;
  border-radius: 18px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.5);
  cursor: grab;
  touch-action: none;
}
#card-canvas:active { cursor: grabbing; }

.preview-actions { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }

/* Gallery */
.gallery-toolbar { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 12px; margin-bottom: 16px; }
.gallery-toolbar h2 { font-family: 'Rajdhani', sans-serif; margin: 0; font-size: 22px; }
.gallery-meta { display: flex; align-items: center; gap: 14px; }
#selected-count { color: var(--text-dim); font-size: 13px; }

.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 16px;
}
.gallery-card {
  background: var(--panel-2);
  border: 2px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  cursor: pointer;
  transition: border-color .15s;
  position: relative;
}
.gallery-card.selected { border-color: var(--accent); }
.gallery-card img { width: 100%; display: block; }
.gallery-card .gc-name {
  padding: 8px 10px;
  font-size: 13px;
  font-weight: 600;
}
.gallery-card .gc-actions {
  position: absolute; top: 8px; right: 8px;
  display: flex; gap: 6px;
}
.gallery-card .gc-actions button {
  background: rgba(11,14,19,0.85);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  width: 28px; height: 28px;
  cursor: pointer;
  font-size: 13px;
}
.gallery-card .gc-check {
  position: absolute; top: 8px; left: 8px;
  width: 22px; height: 22px;
  border-radius: 6px;
  background: rgba(11,14,19,0.85);
  border: 1px solid var(--border);
  display: flex; align-items: center; justify-content: center;
  font-size: 13px;
  color: var(--accent);
}

/* Print sheet */
.sheet-stage {
  display: flex;
  justify-content: center;
  background: #1a2029;
  border-radius: 12px;
  padding: 20px;
  overflow: auto;
}
#sheet-canvas {
  width: min(700px, 100%);
  height: auto;
  background: white;
  box-shadow: 0 8px 30px rgba(0,0,0,0.4);
}

/* League Roster */
.slot-meter { margin: 4px 0 20px; }
.slot-meter-bar {
  height: 10px;
  border-radius: 999px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  overflow: hidden;
}
.slot-meter-fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width .2s;
}
.slot-meter-fill.over { background: var(--danger); }
.slot-meter-label { margin-top: 6px; font-size: 12px; color: var(--text-dim); font-weight: 600; }

.roster-warnings { display: flex; flex-direction: column; gap: 6px; margin-bottom: 4px; }
.roster-warning {
  background: rgba(239,68,68,0.12);
  border: 1px solid rgba(239,68,68,0.4);
  color: #ffb4b4;
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 12.5px;
}

.roster-columns {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 20px;
  margin-top: 8px;
}
@media (max-width: 1100px) {
  .roster-columns { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 700px) {
  .roster-columns { grid-template-columns: 1fr; }
}
.roster-column-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
  flex-wrap: wrap;
}
.roster-column-header h3 {
  margin: 0;
  font-family: 'Rajdhani', sans-serif;
  font-size: 16px;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.roster-list { display: flex; flex-direction: column; gap: 8px; }
.roster-row {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
}
.roster-row-thumb {
  width: 34px;
  height: 48px;
  object-fit: cover;
  border-radius: 4px;
  flex-shrink: 0;
  background: var(--panel);
}
.roster-row-body { flex: 1; min-width: 0; }
.roster-row-name { font-weight: 600; font-size: 13.5px; }
.roster-row-meta { font-size: 11px; color: var(--text-dim); margin-top: 1px; }
.roster-row-slots {
  font-family: 'Rajdhani', sans-serif;
  font-weight: 700;
  font-size: 13px;
  color: var(--accent);
  flex-shrink: 0;
  white-space: nowrap;
}
.roster-row-remove {
  background: transparent;
  border: none;
  color: var(--danger);
  cursor: pointer;
  font-size: 15px;
  flex-shrink: 0;
  padding: 4px;
}

.associates-list { display: flex; flex-direction: column; gap: 10px; }
.associate-item {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--panel-2);
}
.associate-item-top { display: flex; align-items: center; gap: 8px; }
.associate-name-input {
  flex: 1;
  min-width: 0;
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 7px 9px;
  font-family: 'Inter', sans-serif;
  font-size: 13px;
}
.associate-ability-row { display: flex; flex-direction: column; gap: 4px; }
.associate-ability-select {
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 6px 8px;
  font-family: 'Inter', sans-serif;
  font-size: 12.5px;
  width: 100%;
}
.associate-ability-text { font-size: 11.5px; color: var(--text-dim); line-height: 1.4; margin: 0; }

/* Ability Library modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(4,6,9,0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 24px;
}
.modal-overlay.hidden { display: none; }

.modal-panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  width: min(720px, 100%);
  max-height: min(760px, 90vh);
  display: flex;
  flex-direction: column;
  box-shadow: 0 24px 64px rgba(0,0,0,0.55);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 22px;
  border-bottom: 1px solid var(--border);
}
.modal-header h2 {
  margin: 0;
  font-family: 'Rajdhani', sans-serif;
  font-size: 21px;
}
.modal-close {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 8px;
  width: 32px;
  height: 32px;
  cursor: pointer;
  font-size: 14px;
}
.modal-close:hover { color: var(--text); border-color: var(--accent); }

.modal-toolbar {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px 22px;
  border-bottom: 1px solid var(--border);
}
.level-filter { display: flex; gap: 6px; flex-wrap: wrap; }
.level-btn {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 6px 13px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  font-family: 'Inter', sans-serif;
}
.level-btn:hover { color: var(--text); border-color: var(--accent); }
.level-btn.active { background: var(--accent); color: #06231f; border-color: var(--accent); }

#library-search {
  width: 100%;
}

.library-list {
  overflow-y: auto;
  padding: 8px 22px 20px;
  flex: 1;
}
.library-level-heading {
  position: sticky;
  top: 0;
  background: var(--panel);
  padding: 10px 0 6px;
  font-family: 'Rajdhani', sans-serif;
  font-weight: 700;
  font-size: 13px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--accent);
  border-bottom: 1px solid var(--border);
  margin-bottom: 4px;
}
.library-item {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 10px 4px;
  border-bottom: 1px solid var(--border);
}
.library-item:last-child { border-bottom: none; }
.library-item-body { flex: 1; min-width: 0; }
.library-item-name { font-weight: 700; font-size: 14px; color: var(--text); }
.library-item-level {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-dim);
  margin-left: 8px;
  font-weight: 600;
}
.library-item-text { font-size: 12.5px; color: var(--text-dim); margin-top: 3px; line-height: 1.4; }
.library-item-overcap { opacity: 0.55; }
.library-item-overcap-tag {
  font-size: 9.5px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: #ffb4b4;
  margin-left: 8px;
  font-weight: 700;
}
.library-item-thumb {
  width: 40px;
  height: 56px;
  object-fit: cover;
  border-radius: 5px;
  flex-shrink: 0;
  background: var(--panel-2);
}
.library-add-btn {
  flex-shrink: 0;
  width: 34px;
  height: 34px;
  border-radius: 8px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--accent);
  font-size: 18px;
  font-weight: 700;
  cursor: pointer;
  line-height: 1;
}
.library-add-btn:hover { background: var(--accent); color: #06231f; border-color: var(--accent); }
.library-add-btn.added { background: var(--accent); color: #06231f; border-color: var(--accent); }
.library-empty { padding: 30px 4px; text-align: center; color: var(--text-dim); font-size: 13px; }

@media print {
  header.topbar, .panel > .gallery-toolbar, main > .panel > p.hint, #tab-designer, #tab-gallery { display: none !important; }
  body { background: white; }
  main { padding: 0; max-width: none; }
  .panel { border: none; padding: 0; background: white; }
  .sheet-stage { padding: 0; background: white; }
  #sheet-canvas { width: 210mm; height: 297mm; box-shadow: none; }
}
