:root {
  --gold: #f8b700;
  --gold-dim: #c79c3e;
  --gold-dark: #6b4f1d;
  --ink: #1a0f06;
  --parchment: #e8d5a9;
  --stone: #17120d;
  --panel: rgba(20, 14, 9, 0.92);
  --text: #f0e6d0;
  --muted: #b3a27d;
  --red: #b5261e;
  --alliance: #3a6ed8;
  --horde: #c4231b;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--text);
  font: 18px/1.4 'Crimson Text', Georgia, serif;
  background: #120d09;
  -webkit-text-size-adjust: 100%;
}
body::before {
  content: '';
  position: fixed;
  inset: 0;
  z-index: -1;
  background:
    radial-gradient(ellipse at top, rgba(248, 183, 0, 0.12), transparent 60%),
    radial-gradient(ellipse at bottom, rgba(120, 30, 10, 0.25), transparent 60%),
    repeating-linear-gradient(45deg, #120d09 0 12px, #15100b 12px 24px);
}

h1, h2, th, .btn { font-family: 'Cinzel', Georgia, serif; }

header { text-align: center; padding: 2rem 1rem 1rem; }
h1 {
  margin: 0;
  font-size: clamp(1.6rem, 10vw, 4rem);
  white-space: nowrap;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  background: linear-gradient(#fff3b0, var(--gold) 45%, #9a5b00);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 3px 0 #000) drop-shadow(0 0 18px rgba(248, 183, 0, 0.35));
}

main {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 270px;
  gap: 1.5rem;
  max-width: 1440px;
  margin: 0 auto;
  padding: 1rem;
}
/* Sidebar drops below the roster once the full table no longer fits beside it */
@media (max-width: 1260px) { main { grid-template-columns: minmax(0, 1fr); max-width: 1000px; } }

.panel {
  background: var(--panel);
  border: 2px solid var(--gold-dark);
  border-radius: 6px;
  padding: 1.25rem;
  box-shadow:
    inset 0 0 0 1px #000,
    inset 0 0 0 3px rgba(199, 156, 62, 0.35),
    0 10px 30px rgba(0, 0, 0, 0.6);
}
.panel h2 {
  margin: 0 0 0.75rem;
  color: var(--gold);
  font-size: 1.25rem;
  letter-spacing: 0.05em;
  text-shadow: 0 2px 0 #000;
}
.panel-head { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; }
.panel-head h2 { margin: 0; }

.filters { display: flex; gap: 0.5rem; margin: 1rem 0 0.5rem; }
#sort-select { display: none; width: auto; flex: none; }
td.meta, td.notes { display: none; }
.note {
  max-width: 220px;
  margin-top: 0.15rem;
  font-size: 0.9rem;
  font-weight: 400;
  font-style: italic;
  line-height: 1.25;
  color: var(--muted);
  white-space: normal;
  overflow-wrap: anywhere;
}

td.profs { min-width: 190px; max-width: 230px; }
.chip {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  margin: 2px 6px 2px 0;
  padding: 2px 0.5rem 2px 2px;
  font-size: 0.9rem;
  line-height: 1;
  white-space: nowrap;
  border: 1px solid currentColor;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.45);
  cursor: default;
  outline: none;
}
.chip img {
  width: 22px;
  height: 22px;
  border-radius: 4px;
  box-shadow: 0 0 0 1px #000;
}
.chip b { color: #fff; font-weight: 600; }
.chip.secondary { border-style: dashed; font-size: 0.8rem; padding: 1px 0.4rem 1px 1px; }
.chip.secondary img { width: 18px; height: 18px; }
.prof-line { display: flex; flex-wrap: wrap; }
.chip.apprentice { color: #9d9d9d; }
.chip.journeyman { color: #1eff00; }
.chip.expert { color: #0070dd; }
.chip.artisan { color: #a335ee; }
.chip.maxed { color: #ff8000; box-shadow: 0 0 6px rgba(255, 128, 0, 0.5); }
/* Tooltip with the profession name; tap (focus) works on phones */
.chip::after {
  content: attr(data-name);
  position: absolute;
  left: 0;
  bottom: calc(100% + 6px);
  padding: 0.3rem 0.55rem;
  font-size: 0.85rem;
  color: #fff;
  background: #0b0805;
  border: 1px solid var(--gold-dim);
  border-radius: 4px;
  box-shadow: 0 4px 10px #000;
  white-space: nowrap;
  pointer-events: none;
  display: none; /* not just invisible: hidden tooltips must not widen the table */
  z-index: 5;
}
.chip:hover::after, .chip:focus::after { display: block; }

.prof-fields { border: 1px solid var(--gold-dark); border-radius: 4px; padding: 0.5rem 0.75rem 0.75rem; margin: 0; display: grid; gap: 0.5rem; }
.prof-fields legend { color: var(--gold-dim); font-family: 'Cinzel', serif; font-size: 0.85rem; padding: 0 0.3rem; }
.prof-fields .hint { font-family: 'Crimson Text', serif; color: var(--muted); font-size: 0.8rem; font-style: italic; }
#secondary-profs { display: grid; gap: 0.5rem; }
.prof-row { display: grid; grid-template-columns: minmax(0, 1fr) 5.5rem; gap: 0.5rem; align-items: center; }
.prof-pick { display: flex; align-items: center; gap: 0.5rem; min-width: 0; }
.prof-pick select { flex: 1; min-width: 0; }
.prof-icon { width: 30px; height: 30px; border-radius: 4px; border: 1px solid var(--gold-dark); flex: none; }
.prof-name { display: flex; align-items: center; gap: 0.5rem; }
dialog label.prof-row { display: grid; grid-template-columns: minmax(0, 1fr) 5.5rem; color: var(--text); font-size: 1rem; }
td.class { white-space: nowrap; }
td.race { min-width: 6.5rem; }
/* Long names wrap under themselves, keeping the faction dot on the first line */
.toon-line { display: flex; align-items: baseline; gap: 0.4rem; min-width: 7rem; }
.toon-line .faction { flex: none; margin: 0; position: relative; top: -0.1em; }

input, select, textarea {
  width: 100%;
  padding: 0.5rem 0.65rem;
  font: inherit;
  color: var(--text);
  background: #0b0805;
  border: 1px solid var(--gold-dark);
  border-radius: 4px;
}
textarea { resize: vertical; min-height: 4.6em; line-height: 1.3; }
.label-row { display: flex; justify-content: space-between; align-items: baseline; }
.label-row .hint { font-size: 0.8rem; font-style: italic; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--gold); box-shadow: 0 0 8px rgba(248, 183, 0, 0.4); }

.btn {
  font-size: 0.85rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  padding: 0.5rem 1rem;
  color: var(--gold);
  background: linear-gradient(#3a0d08, #1c0604);
  border: 1px solid var(--gold-dim);
  border-radius: 4px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.12), 0 2px 4px #000;
  cursor: pointer;
  text-shadow: 0 1px 0 #000;
}
.btn:hover:not(:disabled) { filter: brightness(1.35); }
.btn:disabled { opacity: 0.35; cursor: default; }
.btn:active { transform: translateY(1px); }
.btn.gold { background: linear-gradient(#a3120b, #5b0905); color: #ffe27a; }
.btn.danger { color: #ff8f86; border-color: var(--red); }
.btn.small { padding: 0.25rem 0.55rem; font-size: 0.75rem; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th {
  text-align: left;
  font-size: 0.75rem;
  color: var(--gold-dim);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: 0.5rem;
  border-bottom: 1px solid var(--gold-dark);
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}
th[data-sort]:hover { color: var(--gold); }
th.sorted::after { content: ' ▾'; }
th.sorted.asc::after { content: ' ▴'; }
td { padding: 0.55rem 0.5rem; border-bottom: 1px solid rgba(107, 79, 29, 0.35); vertical-align: middle; }
tbody tr:hover { background: rgba(248, 183, 0, 0.06); }

.rank { color: var(--muted); width: 2rem; }
.rank.top { color: var(--gold); font-weight: 600; }
.toon { font-weight: 600; font-size: 1.1rem; }
.faction { display: inline-block; width: 0.6rem; height: 0.6rem; border-radius: 50%; margin-right: 0.4rem; vertical-align: middle; }
.faction.alliance { background: var(--alliance); box-shadow: 0 0 6px var(--alliance); }
.faction.horde { background: var(--horde); box-shadow: 0 0 6px var(--horde); }
.faction.neutral { background: #999; }

.level-cell { min-width: 110px; }
.level-num { font-family: 'Cinzel', serif; font-weight: 800; font-size: 1.2rem; color: #fff; }
.xp {
  height: 8px;
  margin-top: 3px;
  background: #000;
  border: 1px solid #3d2d10;
  border-radius: 4px;
  overflow: hidden;
}
.xp > span { display: block; height: 100%; background: linear-gradient(#b67cf2, #6a2fb0); }
.xp.max > span { background: linear-gradient(#ffe27a, #c78a00); }

.armory {
  display: block;
  margin-top: 0.3rem;
  font-size: 0.8rem;
  line-height: 1.2;
  white-space: nowrap;
  color: var(--muted);
  text-decoration: none;
}
a.armory:hover { color: var(--gold); text-decoration: underline; }
.armory.missing { color: #e0a040; }
.armory.failed { opacity: 0.7; }

.row-actions { text-align: right; }
.row-actions .btn { display: block; width: 100%; }
.row-actions .btn + .btn { margin-top: 0.3rem; }

.empty { color: var(--muted); font-style: italic; text-align: center; padding: 1.5rem; }

.dings ol { list-style: none; padding: 0; margin: 0; }
.dings li { padding: 0.5rem 0; border-bottom: 1px solid rgba(107, 79, 29, 0.35); }
.dings li:last-child { border-bottom: none; }
.dings .when { display: block; font-size: 0.85rem; color: var(--muted); }
.dings .lvl { color: var(--gold); font-weight: 600; }

dialog {
  max-height: 95vh;
  padding: 0;
  border: none;
  background: transparent;
  color: var(--text);
  width: min(460px, 92vw);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.7); backdrop-filter: blur(2px); }
dialog form { display: grid; gap: 0.75rem; max-height: inherit; overflow-y: auto; }
dialog label { display: grid; gap: 0.25rem; font-size: 0.9rem; color: var(--muted); }
dialog .row { display: grid; grid-template-columns: 1fr 1fr; gap: 0.75rem; }
dialog .actions { display: flex; gap: 0.5rem; margin-top: 0.5rem; }
dialog .spacer { flex: 1; }
.error { color: #ff8f86; margin: 0; min-height: 1.2em; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(2rem + env(safe-area-inset-bottom));
  width: max-content;
  max-width: 90vw;
  text-align: center;
  transform: translateX(-50%);
  padding: 0.75rem 1.5rem;
  font-family: 'Cinzel', serif;
  font-weight: 800;
  font-size: 1.3rem;
  color: #ffe27a;
  background: rgba(0, 0, 0, 0.85);
  border: 2px solid var(--gold);
  border-radius: 6px;
  text-shadow: 0 0 10px rgba(248, 183, 0, 0.8);
  animation: toast 2.4s ease forwards;
  pointer-events: none;
}
@keyframes toast {
  0% { opacity: 0; transform: translate(-50%, 20px) scale(0.9); }
  12% { opacity: 1; transform: translate(-50%, 0) scale(1.05); }
  20% { transform: translate(-50%, 0) scale(1); }
  80% { opacity: 1; }
  100% { opacity: 0; }
}

/* ---------- Narrow screens: roster rows become cards (2 per row on tablets) ---------- */
@media (max-width: 1000px) {
  #sort-select { display: block; }
  .btn.small { min-height: 40px; padding: 0.4rem 0.9rem; font-size: 0.8rem; }

  thead { display: none; }
  table { display: block; }
  tbody { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); column-gap: 1.5rem; }
  tbody tr {
    display: grid;
    grid-template-columns: 1.75rem minmax(0, 1fr) auto;
    grid-template-areas:
      "rank toon  lvl"
      "rank meta  lvl"
      ".    notes notes"
      ".    profs profs"
      "xp   xp    xp"
      "arm  arm   arm"
      "act  act  act";
    column-gap: 0.6rem;
    row-gap: 0.15rem;
    padding: 0.85rem 0.25rem;
    border-bottom: 1px solid rgba(107, 79, 29, 0.45);
  }
  tbody tr:hover { background: none; }
  td { padding: 0; border: none; }
  td.player, td.race, td.class { display: none; }

  .rank { grid-area: rank; align-self: center; font-size: 1.1rem; text-align: center; width: auto; }
  .toon { grid-area: toon; min-width: 0; }
  .toon-line { min-width: 0; overflow-wrap: anywhere; }
  td.meta { grid-area: meta; display: block; font-size: 0.95rem; line-height: 1.25; color: var(--muted); }
  .toon .note { display: none; }
  td.notes { grid-area: notes; display: block; font-size: 0.95rem; font-style: italic; line-height: 1.25; color: var(--muted); overflow-wrap: anywhere; }
  td.notes:not(:empty) { margin-top: 0.3rem; }
  td.profs { grid-area: profs; min-width: 0; }
  td.profs:not(:empty) { margin-top: 0.3rem; }
  .level-cell { display: contents; }
  .level-num { grid-area: lvl; align-self: center; font-size: 1.9rem; line-height: 1; }
  .xp { grid-area: xp; height: 10px; margin-top: 0.5rem; }
  .armory { grid-area: arm; justify-self: end; }
  .row-actions { grid-area: act; display: flex; gap: 0.5rem; margin-top: 0.6rem; }
  .row-actions .btn { flex: 1; }
  .row-actions .btn + .btn { margin: 0; }

}

/* ---------- Phones ---------- */
@media (max-width: 700px) {
  header { padding: 1.25rem 1rem 0.25rem; }
  main { padding: 0.75rem max(0.75rem, env(safe-area-inset-left)) calc(1.5rem + env(safe-area-inset-bottom)); gap: 1rem; }
  .panel { padding: 1rem; }
  .panel-head h2 { font-size: 1.1rem; }

  .btn { min-height: 44px; padding: 0.5rem 1rem; }

  input, select, textarea { min-height: 44px; font-size: 16px; } /* 16px stops iOS zooming on focus */

  tbody { grid-template-columns: minmax(0, 1fr); }
  /* Give long toon names the full line: no faction dot, narrower rank column */
  tbody tr { grid-template-columns: 1.1rem minmax(0, 1fr) auto; column-gap: 0.5rem; }
  .toon-line .faction { display: none; }

  dialog { width: 100vw; max-width: 100vw; max-height: 100dvh; margin: auto 0 0; }
  dialog form { border-radius: 10px 10px 0 0; padding-bottom: calc(1.25rem + env(safe-area-inset-bottom)); }
  dialog .row { grid-template-columns: 3fr 2fr; }
  dialog .actions .btn { flex: 1; }
  dialog .spacer { display: none; }
}
