* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  display: flex;
  flex-direction: column;
  max-width: 560px;
  margin: 0 auto;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  background: #2b2622;
  color: #f3ece1;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 12px;
}

.who {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  padding: 2px 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}

/* Class glyphs are black-on-transparent PNGs used as a mask, so they take the text color */
.who-icon,
.class-icon {
  flex-shrink: 0;
  background: currentColor;
  -webkit-mask: var(--glyph, none) center / contain no-repeat;
  mask: var(--glyph, none) center / contain no-repeat;
}

.who-icon {
  width: 34px;
  height: 34px;
}

.empty .who-icon {
  display: none;
}

.who-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

#who-name {
  max-width: 100%;
  overflow: hidden;
  font-size: 1.05rem;
  font-weight: 700;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#who-class {
  font-size: 0.8rem;
  opacity: 0.75;
  white-space: nowrap;
}

.actions {
  display: flex;
  flex-shrink: 0;
  gap: 8px;
}

header .actions button {
  min-width: 40px;
  padding: 6px 12px;
  border: 1px solid rgba(255, 255, 255, 0.6);
  border-radius: 999px;
  background: transparent;
  color: inherit;
  font: inherit;
  font-size: 0.9rem;
  cursor: pointer;
}

header .actions button:active,
#more[aria-pressed="true"] {
  background: rgba(255, 255, 255, 0.2);
}

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 0 6px 6px;
  min-height: 0;
  overflow-y: auto;
}

.row {
  --icon: clamp(28px, 5dvh, 44px);
  flex: 1;
  display: grid;
  grid-template-columns: repeat(3, 1fr) minmax(76px, 1.6fr) repeat(3, 1fr);
  align-items: center;
  justify-items: center;
  gap: 4px;
  min-height: 72px;
  padding: 4px 6px;
  border-radius: 10px;
  background: var(--bg);
  color: var(--ink);
  transition: background 0.2s;
}

/* Gold and XP are half-height bars; HP and summons are full cards with a conditions strip */
.gold,
.xp {
  --icon: 20px;
  flex: 1;
  min-height: 52px;
}

.unit {
  position: relative;
  flex: 2;
  min-height: 150px;
}

.unit {
  grid-template-rows: 1fr auto;
}

.expanded .unit {
  grid-template-rows: 1fr 1fr auto;
}

.gold .step,
.xp .step {
  max-width: 42px;
}

.gold .counter,
.xp .counter {
  display: grid;
  grid-template-columns: auto auto;
  align-items: center;
  column-gap: 6px;
}

.gold .symbol,
.xp .symbol {
  grid-row: 1 / span 2;
}

.gold .sub,
.xp .sub {
  display: none;
}

.hp    { --bg: #b54a3f; --ink: #fff; }
.xp    { --bg: #4f78a8; --ink: #fff; }
.gold  { --bg: #d4a83a; --ink: #33240a; }
.summon { --bg: var(--summon-bg, #3f8a7a); --ink: #fff; }

.hp.down { --bg: #5a3a36; }
.summon.down { --bg: #34413e; }

.step {
  width: 100%;
  max-width: 56px;
  aspect-ratio: 1;
  padding: 0;
  border: 2px solid rgba(255, 255, 255, 0.85);
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.3);
  color: var(--ink);
  font: inherit;
  font-size: clamp(0.95rem, 4.2vw, 1.25rem);
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.08s, background 0.08s;
}

.step:active {
  transform: scale(0.9);
  background: rgba(255, 255, 255, 0.8);
}

/* The -6..-4 / +4..+6 line under the main buttons, shown by the header's + toggle */
.extra {
  display: none;
}

.expanded .unit .extra {
  display: block;
}

.expanded .unit .step {
  max-width: clamp(30px, 6dvh, 56px);
}

.counter {
  grid-column: 4;
  grid-row: 1;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1.1;
}

.expanded .unit .counter {
  grid-row: 1 / span 2;
}

.symbol {
  font-size: var(--icon);
  line-height: 1;
}

.track-name {
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  opacity: 0.85;
}

.value {
  font-size: clamp(1.3rem, 4dvh, 2rem);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.sub {
  min-height: 1em;
  font-size: 0.72rem;
  opacity: 0.85;
  white-space: nowrap;
}

/* Running total of the taps just made, e.g. +3 +3 +1 shows "+7" */
.delta {
  position: absolute;
  top: -2px;
  right: -10px;
  padding: 1px 7px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.7);
  color: #fff;
  font-size: 0.85rem;
  font-weight: 700;
  white-space: nowrap;
  opacity: 0;
  transition: opacity 0.3s;
  pointer-events: none;
}

.delta.show {
  opacity: 1;
  transition: none;
}

/* One tap toggles a condition: grey when off, full color when on */
.conds {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  justify-items: center;
  gap: 2px;
  width: 100%;
  padding-top: 2px;
}

.cond {
  width: 100%;
  max-width: 44px;
  padding: 2px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  cursor: pointer;
  touch-action: manipulation;
}

.cond img {
  display: block;
  width: 100%;
  aspect-ratio: 1;
  filter: grayscale(1);
  opacity: 0.4;
  transition: opacity 0.15s, transform 0.15s;
}

.cond.on img {
  filter: drop-shadow(0 0 3px rgba(255, 255, 255, 0.9));
  opacity: 1;
  transform: scale(1.1);
}

/* Room above the buttons for the remove-summon corner button */
.row.summon {
  padding-top: 20px;
}

/* Remove-summon button in the card's top-right corner */
.remove {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 26px;
  height: 26px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.35);
  color: #fff;
  font: inherit;
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
}

.add-summon {
  margin: 0 8px 6px;
  padding: 9px;
  border: 2px dashed rgba(255, 255, 255, 0.45);
  border-radius: 10px;
  background: transparent;
  color: inherit;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

.add-summon:active {
  background: rgba(255, 255, 255, 0.15);
}

/* No character yet: dim the board, taps open the character sheet */
.empty main,
.empty .add-summon {
  opacity: 0.45;
}

#flash {
  position: fixed;
  top: 40%;
  left: 50%;
  padding: 10px 22px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.8);
  color: #fff;
  font-size: 1.4rem;
  font-weight: 800;
  opacity: 0;
  transform: translate(-50%, -50%);
  transition: opacity 0.4s;
  pointer-events: none;
}

#flash.show {
  opacity: 1;
  transition: none;
}

dialog {
  width: calc(100% - 32px);
  max-width: 420px;
  max-height: 80dvh;
  padding: 0;
  border: 0;
  border-radius: 12px;
  background: #f7f0e4;
  color: #2b2622;
}

dialog form {
  display: flex;
  flex-direction: column;
  max-height: 80dvh;
}

.setup-body {
  overflow-y: auto;
}

#history[open] {
  display: flex;
  flex-direction: column;
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.55);
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 1px solid rgba(43, 38, 34, 0.2);
}

.sheet-head h2 {
  margin: 0;
  font-size: 1.05rem;
}

.sheet-head button {
  padding: 6px 14px;
  border: 1px solid rgba(43, 38, 34, 0.5);
  border-radius: 999px;
  background: transparent;
  color: inherit;
  font: inherit;
  font-size: 0.9rem;
  cursor: pointer;
}

.setup-body {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 14px;
}

.setup-body label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.85rem;
  font-weight: 600;
}

.setup-body fieldset {
  margin: 0;
  padding: 0;
  border: 0;
}

.setup-body legend {
  margin-bottom: 6px;
  padding: 0;
  font-size: 0.85rem;
  font-weight: 600;
}

.hint {
  font-weight: 400;
  opacity: 0.75;
}

.class-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(56px, 1fr));
  gap: 6px;
}

.class-pick {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 0;
  aspect-ratio: 1;
  padding: 6px;
  border: 1px solid rgba(43, 38, 34, 0.25);
  border-radius: 10px;
  background: #fff;
  color: inherit;
  font: inherit;
  cursor: pointer;
}

.class-icon {
  width: 100%;
  max-width: 44px;
  aspect-ratio: 1;
}


.level-grid {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 4px;
}

.pick {
  padding: 8px 0;
  border: 1px solid rgba(43, 38, 34, 0.25);
  border-radius: 8px;
  background: #fff;
  color: inherit;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

.level-grid#summon-quick {
  row-gap: 6px;
}

.name-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.name-pick {
  padding: 8px 2px;
  font-size: 0.85rem;
  font-weight: 600;
}

.class-pick.on,
.pick.on {
  border-color: #2b2622;
  background: #2b2622;
  color: #f7f0e4;
}

.setup-body input,
.setup-body select {
  padding: 9px 10px;
  border: 1px solid rgba(43, 38, 34, 0.35);
  border-radius: 8px;
  background: #fff;
  color: inherit;
  font: inherit;
  font-size: 1rem;
  user-select: text;
}

.note {
  margin: 0;
  font-size: 0.8rem;
  opacity: 0.75;
}

.primary {
  padding: 10px;
  border: 0;
  border-radius: 999px;
  background: #2b2622;
  color: #f7f0e4;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

#history-empty {
  margin: 0;
  padding: 20px 14px;
  text-align: center;
}

#history-list {
  margin: 0;
  padding: 0;
  overflow-y: auto;
  list-style: none;
}

#history-list li {
  display: grid;
  grid-template-columns: 24px minmax(0, 1fr) auto 5ch auto;
  align-items: center;
  gap: 8px;
  padding: 6px 14px;
  font-variant-numeric: tabular-nums;
}

#history-list li + li {
  border-top: 1px solid rgba(43, 38, 34, 0.1);
}

#history-list .symbol {
  font-size: 1.1rem;
  text-align: center;
}

#history-list .symbol img {
  display: block;
  width: 22px;
  height: 22px;
}

#history-list .name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#history-list .change {
  font-weight: 800;
  text-align: right;
}

#history-list .gain {
  color: #2c6a2f;
}

#history-list .loss {
  color: #a3262f;
}

#history-list .event .change {
  grid-column: 3 / span 2;
  font-weight: 600;
}

#history-list .total {
  font-weight: 600;
}

#history-list time {
  font-size: 0.85rem;
  white-space: nowrap;
  opacity: 0.7;
}
