/* -- Reset & Custom Properties ------------------------------- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:             #110a04;
  --parchment:      #f8edd6;
  --parchment-mid:  #f0ddb4;
  --parchment-edge: #c9a468;
  --card-shadow:    0 4px 20px rgba(0,0,0,0.6);
  --accent:         #7a1e0e;
  --gold:           #a8832a;
  --gold-light:     #c9a825;
  --text:           #2e1a0c;
  --text-muted:     #7a5c3a;
  --title-bg:       #5c2a10;
  --title-fg:       #f8e5b4;
  --radius:         6px;
  --builder-width:  430px;
}

html { font-size: 15px; }

body {
  background-color: var(--bg);
  background-image:
    radial-gradient(ellipse at 15% 15%, #2a1208 0%, transparent 55%),
    radial-gradient(ellipse at 85% 85%, #1a0c02 0%, transparent 55%);
  color: var(--text);
  font-family: 'Segoe UI', system-ui, sans-serif;
  min-height: 100vh;
}

/* -- Spinner -------------------------------------------------- */
#spinner {
  position: fixed;
  inset: 0;
  display: none;
  z-index: 9999;
  background: rgba(0, 0, 0, 0.55);
}

.loadingspinner {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 2.8em;
  height: 2.8em;
  border: 0.35em solid rgba(255,255,255,0.15);
  border-top-color: var(--gold-light);
  border-radius: 50%;
  animation: spin 0.85s linear infinite;
}

@keyframes spin { to { transform: translate(-50%, -50%) rotate(360deg); } }

/* -- Header --------------------------------------------------- */
.site-header {
  background: linear-gradient(135deg, #1c0902 0%, #3d1608 50%, #1c0902 100%);
  border-bottom: 3px solid var(--gold);
  padding: 1.25rem 4rem;
  text-align: center;
  position: relative;
}

.site-header::before,
.site-header::after {
  content: '⚔';
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  font-size: 1.5rem;
  color: var(--gold);
  opacity: 0.65;
}
.site-header::before { left: 1.5rem; }
.site-header::after  { right: 1.5rem; }

.site-header h1 {
  font-family: Georgia, 'Times New Roman', serif;
  font-size: 1.75rem;
  font-weight: 700;
  color: var(--gold-light);
  text-shadow: 0 2px 10px rgba(0,0,0,0.9), 0 0 40px rgba(200,160,40,0.25);
  letter-spacing: 0.04em;
  line-height: 1.4;
}

.site-header h1 sup {
  font-size: 0.6em;
}

.site-header h1 span {
  font-size: 0.95rem;
  font-weight: 400;
  color: #ddc890;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

/* -- Page Layout ---------------------------------------------- */
.layout {
  display: flex;
  gap: 1.5rem;
  padding: 1.5rem;
  align-items: flex-start;
  max-width: 1500px;
  margin: 0 auto;
}

/* -- Builder (left column) ------------------------------------ */
.builder {
  flex: 0 0 var(--builder-width);
  display: flex;
  flex-direction: column;
  gap: 1.2rem;
}

/* -- Card ----------------------------------------------------- */
.card {
  background: var(--parchment);
  border: 1px solid var(--parchment-edge);
  border-radius: var(--radius);
  box-shadow: var(--card-shadow), inset 0 0 0 1px rgba(255,255,255,0.35);
  overflow: hidden;
}

.card-title {
  font-family: Georgia, 'Times New Roman', serif;
  font-size: 0.85rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--title-fg);
  background: linear-gradient(135deg, var(--title-bg), #884018);
  padding: 0.5rem 1rem;
  border-bottom: 2px solid var(--gold);
}

/* -- Radio Group ---------------------------------------------- */
.radio-group {
  padding: 0.8rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.radio-option {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
  font-size: 0.92rem;
  color: var(--text);
  padding: 0.2rem 0;
}

.radio-option input[type=radio] {
  accent-color: var(--accent);
  width: 15px;
  height: 15px;
  cursor: pointer;
  flex-shrink: 0;
}

/* -- Score Table ---------------------------------------------- */
.score-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}

.score-table thead th {
  background: var(--parchment-mid);
  color: var(--text-muted);
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  padding: 0.4rem 0.65rem;
  border-bottom: 1px solid var(--parchment-edge);
  text-align: left;
}

.score-table tbody tr {
  border-bottom: 1px solid #e8d8b8;
}

.score-table tbody tr:last-child {
  border-bottom: none;
}

.score-table tbody tr:nth-child(even) {
  background: rgba(0,0,0,0.025);
}

.score-table td {
  padding: 0.35rem 0.65rem;
  vertical-align: middle;
}

.roll-num,
.stat-num {
  width: 40px;
  padding: 0.22rem 0.3rem;
  border: 1px solid var(--parchment-edge);
  border-radius: 3px;
  background: #fff9ef;
  color: var(--text);
  font-size: 0.92rem;
  font-weight: 700;
  text-align: center;
  -moz-appearance: textfield;
}

.roll-num::-webkit-inner-spin-button,
.stat-num::-webkit-inner-spin-button { display: none; }

.score-table select {
  width: 100%;
  font-size: 0.8rem;
  padding: 0.22rem 0.3rem;
  border: 1px solid var(--parchment-edge);
  border-radius: 3px;
  background: #fff9ef;
  color: var(--text);
  cursor: pointer;
}

.stat-name {
  font-size: 0.87rem;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
}

.stat-final-cell {
  display: flex;
  align-items: center;
  gap: 4px;
}

.pct-label {
  font-size: 0.62rem;
  font-weight: 700;
  color: var(--accent);
  min-width: 26px;
  line-height: 1;
}

/* -- Roll Button ---------------------------------------------- */
.roll-btn-wrap {
  padding: 0.75rem 1rem;
  border-top: 1px solid var(--parchment-edge);
  background: var(--parchment-mid);
}

.roll-btn {
  width: 100%;
  padding: 0.6rem 1.2rem;
  font-family: Georgia, 'Times New Roman', serif;
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  color: var(--title-fg);
  background: linear-gradient(180deg, #a8361c 0%, #7a1e0e 100%);
  border: 2px solid #c0561c;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.12s ease;
  text-shadow: 0 1px 3px rgba(0,0,0,0.55);
  box-shadow: 0 3px 10px rgba(0,0,0,0.45), inset 0 1px 0 rgba(255,255,255,0.15);
}

.roll-btn:hover {
  background: linear-gradient(180deg, #c04020 0%, #8b2412 100%);
  box-shadow: 0 5px 14px rgba(0,0,0,0.55), inset 0 1px 0 rgba(255,255,255,0.2);
  transform: translateY(-1px);
}

.roll-btn:active {
  transform: translateY(1px);
  box-shadow: 0 1px 5px rgba(0,0,0,0.4);
}

.roll-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

/* -- Character Fields ----------------------------------------- */
.char-fields {
  padding: 0.8rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}

.field-row {
  display: flex;
  align-items: center;
  gap: 0.7rem;
}

.field-label {
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--text-muted);
  width: 60px;
  flex-shrink: 0;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.field-row select {
  flex: 1;
  padding: 0.28rem 0.4rem;
  border: 1px solid var(--parchment-edge);
  border-radius: 3px;
  background: #fff9ef;
  color: var(--text);
  font-size: 0.9rem;
  cursor: pointer;
}

#multiclass {
  flex: 1;
  border: 1px solid var(--parchment-edge);
  border-radius: 3px;
  background: #fff9ef;
  color: var(--text);
  font-size: 0.88rem;
}

/* -- Derived Stats bar ---------------------------------------- */
.derived-stats {
  display: flex;
  border-top: 1px solid var(--parchment-edge);
  background: var(--parchment-mid);
}

.derived-stat {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.6rem 0.5rem 0.7rem;
  gap: 0.15rem;
}

.derived-stat + .derived-stat {
  border-left: 1px solid var(--parchment-edge);
}

.derived-label {
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-muted);
}

.derived-value {
  font-size: 1.7rem;
  font-weight: 700;
  font-family: Georgia, serif;
  color: var(--accent);
  min-height: 2rem;
  line-height: 1;
}

/* -- Stats Area (right grid) ---------------------------------- */
.stats-area {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1.2rem;
  align-content: start;
}

/* -- Stat Card ------------------------------------------------ */
.stat-card {
  background: var(--parchment);
  border: 1px solid var(--parchment-edge);
  border-radius: var(--radius);
  box-shadow: var(--card-shadow), inset 0 0 0 1px rgba(255,255,255,0.35);
  overflow: hidden;
}

.saves-card {
  grid-column: 1 / -1;
}

.stat-card-title {
  font-family: Georgia, 'Times New Roman', serif;
  font-size: 0.82rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--title-fg);
  background: linear-gradient(135deg, var(--title-bg), #884018);
  padding: 0.45rem 0.9rem;
  border-bottom: 2px solid var(--gold);
}

/* -- Definition List ------------------------------------------ */
.stat-dl {
  display: grid;
  grid-template-columns: auto 1fr;
}

.stat-dl dt {
  font-size: 0.78rem;
  color: var(--text-muted);
  padding: 0.3rem 0.75rem 0.3rem 0.9rem;
  border-right: 1px solid #e0ccaa;
  border-bottom: 1px solid #eeddb8;
  white-space: nowrap;
  display: flex;
  align-items: center;
}

.stat-dl dd {
  font-size: 0.88rem;
  font-weight: 700;
  color: var(--text);
  padding: 0.3rem 0.9rem;
  border-bottom: 1px solid #eeddb8;
  display: flex;
  align-items: center;
  min-height: 1.75rem;
}

.stat-dl dt:last-of-type,
.stat-dl dd:last-child {
  border-bottom: none;
}

/* Saving throws: two-column pairs */
.saves-dl {
  grid-template-columns: repeat(2, auto 1fr);
}

/* -- Export Button -------------------------------------------- */
.export-wrap {
  padding: 0.6rem 1rem;
  background: var(--parchment-mid);
  border-top: 1px solid var(--parchment-edge);
}

.export-btn {
  width: 100%;
  padding: 0.5rem 1.2rem;
  font-family: Georgia, 'Times New Roman', serif;
  font-size: 0.9rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  color: var(--gold);
  background: transparent;
  border: 2px solid var(--gold);
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.12s ease;
}

.export-btn:hover {
  background: var(--gold);
  color: #1c0902;
  box-shadow: 0 3px 10px rgba(0,0,0,0.35);
  transform: translateY(-1px);
}

.export-btn:active {
  transform: translateY(1px);
  box-shadow: none;
}

/* -- Responsive ----------------------------------------------- */
@media (max-width: 1150px) {
  .stats-area {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 860px) {
  .layout {
    flex-direction: column;
  }
  .builder {
    flex: none;
    width: 100%;
    max-width: 100%;
  }
  .stats-area {
    grid-template-columns: repeat(2, 1fr);
    width: 100%;
  }
}

@media (max-width: 520px) {
  .stats-area {
    grid-template-columns: 1fr;
  }
  .saves-card {
    grid-column: 1;
  }
  .saves-dl {
    grid-template-columns: auto 1fr;
  }
}