/* Magi Live Sheet — styles.
   Dark theme by default. Tokens live on :root so a light theme is a
   token swap later. Mobile-first: the page is built for a phone, then
   widened for a tablet with one media query. */

:root {
  --bg: #12141a;
  --bg-elevated: #1a1d26;
  --bg-card: #1f2330;
  --bg-card-alt: #262b3b;
  --border: #2e3342;
  --text: #eef0f6;
  --text-dim: #9aa0b4;
  --text-faint: #6b7185;

  --accent: #7c9eff;
  --accent-dim: #4a5b8f;
  --success: #3ecf8e;
  --danger: #f0616f;
  --warning: #f3b358;

  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
    Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";

  --tabbar-height: 64px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  overflow-x: hidden;
  width: 100%;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
}

h1,
h2,
h3,
p {
  margin: 0;
}

button,
input,
textarea {
  font: inherit;
  color: inherit;
}

input,
textarea {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
}

input:focus,
textarea:focus,
button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

button {
  background: var(--bg-card-alt);
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
  min-height: 44px;
}

.hidden {
  display: none !important;
}

/* --- Connection badge --- */

.badge {
  position: fixed;
  top: calc(8px + var(--safe-top));
  right: calc(8px + var(--safe-right));
  z-index: 50;
  padding: 5px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.02em;
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text-dim);
}

.badge[data-state="live"] {
  color: #0e1a12;
  background: var(--success);
  border-color: var(--success);
}

.badge[data-state="reconnecting"] {
  color: #241a05;
  background: var(--warning);
  border-color: var(--warning);
}

.badge[data-state="offline"] {
  color: #2a0a0d;
  background: var(--danger);
  border-color: var(--danger);
}

.badge[data-state="bridge-offline"] {
  color: #fff;
  background: var(--accent-dim);
  border-color: var(--accent);
}

.badge[data-state="fixture"] {
  color: var(--text-dim);
  background: var(--bg-card);
  border-style: dashed;
}

/* --- Pairing screen --- */

.pairing {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px calc(20px + var(--safe-right)) 24px calc(20px + var(--safe-left));
}

.pairing-box {
  width: 100%;
  max-width: 360px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px 22px;
  text-align: center;
}

.pairing-title {
  font-size: 22px;
  margin-bottom: 10px;
}

.pairing-hint {
  color: var(--text-dim);
  font-size: 14px;
  margin-bottom: 20px;
}

#pairing-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.pairing-code-input {
  font-size: 28px;
  letter-spacing: 0.3em;
  text-align: center;
  padding: 12px;
}

.pairing-error {
  color: var(--danger);
  font-size: 13px;
  min-height: 18px;
  margin-top: 12px;
}

/* --- Sheet layout --- */

.sheet {
  padding-bottom: calc(var(--tabbar-height) + var(--safe-bottom) + 12px);
}

.tab-panels {
  width: 100%;
}

.tab-panel {
  padding: calc(12px + var(--safe-top)) calc(12px + var(--safe-right)) 12px calc(12px + var(--safe-left));
  max-width: 720px;
  margin: 0 auto;
}

/* --- Cards --- */

.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px;
  margin-bottom: 12px;
}

.card-title {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
  margin-bottom: 10px;
}

.empty-note {
  color: var(--text-dim);
  text-align: center;
  padding: 24px 0;
}

/* --- Buttons --- */

.btn {
  padding: 10px 14px;
  font-weight: 600;
  font-size: 14px;
}

.btn-big {
  min-height: 52px;
  font-size: 16px;
  flex: 1;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #0e1220;
}

.btn-damage {
  background: rgba(240, 97, 111, 0.18);
  border-color: var(--danger);
  color: #ffb3ba;
}

.btn-heal {
  background: rgba(62, 207, 142, 0.18);
  border-color: var(--success);
  color: #a9f2d2;
}

.btn-roll {
  flex: 1;
}

.btn-roll.is-proficient {
  border-color: var(--accent);
  color: var(--accent);
}

/* --- Steppers --- */

.stepper {
  display: flex;
  align-items: center;
  gap: 10px;
}

.stepper-btn {
  width: 40px;
  height: 40px;
  padding: 0;
  font-size: 18px;
  border-radius: 10px;
}

.stepper-value {
  min-width: 44px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

/* --- Portrait header (Main tab) --- */

.portrait-header {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 8px 2px 14px;
}

.portrait {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--bg-card-alt);
  border: 1px solid var(--border);
  flex-shrink: 0;
}

.portrait-fallback {
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 20px;
  color: var(--text-dim);
}

.identity {
  min-width: 0;
}

.char-name {
  font-size: 20px;
  font-weight: 700;
  overflow-wrap: anywhere;
}

.char-sub {
  color: var(--text-dim);
  font-size: 14px;
  margin-top: 2px;
}

/* --- HP card --- */

.hp-readout {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 6px;
  margin-bottom: 12px;
}

.hp-num {
  width: 70px;
  text-align: center;
  font-size: 30px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  padding: 6px 4px;
}

.hp-max {
  color: var(--text-dim);
}

.hp-slash {
  font-size: 22px;
  color: var(--text-faint);
}

.hp-temp-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-bottom: 14px;
  color: var(--text-dim);
  font-size: 13px;
}

.hp-temp-row .hp-num {
  width: 60px;
  font-size: 16px;
  font-weight: 600;
}

.hp-action-row {
  display: flex;
  gap: 10px;
  align-items: stretch;
}

.hp-amount-input {
  width: 76px;
  text-align: center;
  font-size: 16px;
}

/* --- Stat chips (Main tab) --- */

.stat-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 12px;
}

.stat-chip {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 8px 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  min-height: 52px;
  justify-content: center;
  min-width: 0;
}

.stat-chip-action {
  cursor: pointer;
}

.stat-chip-toggle {
  grid-column: 1 / -1;
  flex-direction: row;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 14px;
}

.stat-label {
  font-size: 11px;
  text-transform: uppercase;
  color: var(--text-dim);
  letter-spacing: 0.04em;
}

.stat-value {
  font-size: 18px;
  font-weight: 700;
}

.stat-chip-toggle input {
  width: 22px;
  height: 22px;
}

/* --- Death saves --- */

.death-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 0;
}

.death-label {
  color: var(--text-dim);
  font-size: 14px;
}

.pip-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.pip {
  width: 26px;
  height: 26px;
  padding: 0;
  border-radius: 50%;
  border: 2px solid var(--border);
  background: transparent;
  min-height: 0;
}

.pip-success.filled {
  background: var(--success);
  border-color: var(--success);
}

.pip-failure.filled {
  background: var(--danger);
  border-color: var(--danger);
}

.pip-slot.filled {
  background: var(--accent);
  border-color: var(--accent);
}

/* --- Resources / chat --- */

.resource-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 0;
}

.chat-row {
  display: flex;
  gap: 8px;
}

.chat-input {
  flex: 1;
  min-width: 0;
}

/* --- Advantage selector (Skills tab) --- */

.segmented {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}

.segmented-btn {
  flex: 1;
  border: none;
  border-radius: 0;
  background: var(--bg-elevated);
  font-size: 13px;
  padding: 10px 4px;
}

.segmented-btn + .segmented-btn {
  border-left: 1px solid var(--border);
}

.segmented-btn.active {
  background: var(--accent);
  color: #0e1220;
  font-weight: 700;
}

/* --- Abilities --- */

.ability-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px;
}

.ability-card {
  background: var(--bg-card-alt);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px;
}

.ability-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 8px;
}

.ability-label {
  font-size: 13px;
  color: var(--text-dim);
}

.ability-score {
  font-size: 18px;
  font-weight: 700;
}

.ability-buttons {
  display: flex;
  gap: 6px;
}

.ability-buttons .btn {
  font-size: 13px;
  padding: 8px 6px;
}

/* --- Skill / tool rows --- */

.skill-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.skill-row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px;
  background: var(--bg-elevated);
  text-align: left;
}

.prof-dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid var(--text-faint);
  background: transparent;
  flex-shrink: 0;
}

.prof-dot.prof-half {
  background: linear-gradient(90deg, var(--accent) 50%, transparent 50%);
  border-color: var(--accent);
}

.prof-dot.prof-full {
  background: var(--accent);
  border-color: var(--accent);
}

.prof-dot.prof-expert {
  background: var(--warning);
  border-color: var(--warning);
}

.skill-label {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.skill-ability {
  color: var(--text-faint);
  font-size: 11px;
  text-transform: uppercase;
}

.skill-mod {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  min-width: 32px;
  text-align: right;
}

/* --- Combat --- */

.attack-row {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.attack-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.attack-name {
  font-weight: 700;
}

.attack-stats {
  color: var(--text-dim);
  font-size: 13px;
}

.attack-buttons {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.attack-buttons .btn {
  flex: 1 1 80px;
}

/* --- Spells --- */

.slot-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 0;
}

.slot-label {
  color: var(--text-dim);
  font-size: 13px;
}

.spell-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.spell-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 0;
  border-top: 1px solid var(--border);
}

.spell-row:first-child {
  border-top: none;
}

.spell-name {
  flex: 1;
  min-width: 0;
  overflow-wrap: anywhere;
}

.spell-prepared {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-dim);
  white-space: nowrap;
}

.spell-prepared input {
  width: 20px;
  height: 20px;
}

/* --- Items --- */

.currency-row {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
}

.currency-field {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.currency-field label {
  font-size: 11px;
  text-transform: uppercase;
  color: var(--text-dim);
}

.currency-input {
  width: 100%;
  text-align: center;
  padding: 8px 4px;
}

.item-list {
  display: flex;
  flex-direction: column;
}

.item-row {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.item-info {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: baseline;
}

.item-name {
  font-weight: 600;
  overflow-wrap: anywhere;
}

.item-weight {
  color: var(--text-faint);
  font-size: 12px;
  white-space: nowrap;
}

.item-controls {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
}

.item-equipped {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--text-dim);
}

.item-equipped input {
  width: 20px;
  height: 20px;
}

/* --- Notes --- */

.biography-content {
  font-size: 14px;
  line-height: 1.5;
  color: var(--text);
  overflow-wrap: anywhere;
}

.biography-content p {
  margin: 0 0 8px;
}

.biography-content p:last-child {
  margin-bottom: 0;
}

.note-textarea {
  width: 100%;
  min-height: 76px;
  resize: vertical;
}

/* --- Toast --- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabbar-height) + var(--safe-bottom) + 12px);
  transform: translate(-50%, 8px);
  z-index: 60;
  max-width: calc(100vw - 32px);
  padding: 10px 16px;
  border-radius: 999px;
  background: var(--bg-card-alt);
  border: 1px solid var(--border);
  font-size: 13px;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s ease, transform 0.18s ease;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

.toast[data-kind="error"] {
  border-color: var(--danger);
  color: #ffb3ba;
}

/* --- Tab bar --- */

.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 40;
  display: flex;
  height: calc(var(--tabbar-height) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
  background: var(--bg-elevated);
  border-top: 1px solid var(--border);
}

.tab-btn {
  flex: 1;
  min-height: 44px;
  background: transparent;
  border: none;
  border-radius: 0;
  color: var(--text-dim);
  font-size: 12px;
  font-weight: 600;
}

.tab-btn.active {
  color: var(--accent);
}

/* --- Tablet layout --- */

@media (min-width: 700px) {
  .tab-panel {
    padding-top: calc(20px + var(--safe-top));
  }

  .ability-grid {
    grid-template-columns: repeat(3, 1fr);
  }

  .stat-chip {
    flex-basis: 120px;
  }

  .currency-row {
    max-width: 420px;
    margin: 0 auto;
  }
}

/* A value that the page shows but cannot change. */
.readonly-row {
  display: flex;
  align-items: baseline;
  gap: 0.75rem;
}

.readonly-note {
  color: var(--text-dim, #8b93a7);
  font-size: 0.8rem;
}
