:root {
  color-scheme: light dark;
  --bg: #ffffff;
  --fg: #1e1f22;
  --card-bg: #f2f3f5;
  --border: #d4d6da;
  --accent: #2f6fed;
  --accent-text: #ffffff;
  --error: #d83c3e;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #313338;
    --fg: #f2f3f5;
    --card-bg: #2b2d31;
    --border: #1e1f22;
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 10px;
  font-family: -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 13px;
  background: var(--bg);
  color: var(--fg);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

h1 {
  font-size: 15px;
  margin: 0;
}

.reset-row {
  display: flex;
  justify-content: center;
}

.reset-btn {
  font-size: 11px;
  padding: 4px 8px;
}

h2 {
  font-size: 13px;
  margin: 0 0 6px 0;
}

.status {
  margin: 2px 0 0 0;
  min-height: 14px;
  font-size: 12px;
  color: var(--fg);
  opacity: 0.75;
}

.status.error {
  color: var(--error);
  opacity: 1;
}

details {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
}

summary {
  cursor: pointer;
  font-weight: 600;
}

.card {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px;
  background: var(--card-bg);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin: 6px 0;
}

.field label {
  font-size: 11px;
  opacity: 0.8;
}

input,
select {
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 4px 6px;
  background: var(--bg);
  color: var(--fg);
  width: 100%;
}

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

.row .field {
  flex: 1;
  min-width: 0;
}

.checkbox-field {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  margin: 6px 0;
  cursor: pointer;
}

.checkbox-field input {
  width: auto;
}

.d20-controls-row {
  align-items: flex-end;
  flex-wrap: wrap;
}

.stat-group {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.stat-group label {
  font-size: 11px;
  opacity: 0.8;
}

.modifier-control,
.tally-control {
  display: flex;
  align-items: center;
  gap: 4px;
}

.step-btn {
  width: 26px;
  height: 40px;
  padding: 0;
  font-size: 16px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

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

.stepper-stack {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.stack-btn {
  width: 26px;
  padding: 0;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.stack-btn-plus {
  height: 26px;
  font-size: 16px;
}

.stack-btn-minus {
  height: 12px;
  font-size: 9px;
}

.modifier-input {
  width: 40px;
  height: 40px;
  padding: 0;
  text-align: center;
  font-size: 15px;
  font-weight: 600;
}

.dice-input {
  width: 54px;
  font-size: 13px;
}

.tally-input {
  width: 34px;
  padding: 4px 2px;
  text-align: center;
}

.tally-btn {
  width: 40px;
  height: 40px;
  padding: 0;
  font-size: 20px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.tally-icon {
  width: 26px;
  height: 26px;
  color: var(--fg);
}

.tally-btn:hover .tally-icon {
  color: var(--accent);
}

.xdx-controls-row {
  align-items: flex-end;
  flex-wrap: wrap;
}

.keep-crit-row {
  align-items: center;
  justify-content: space-between;
  margin: 6px 0;
}

.keep-field {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.keep-field label {
  font-size: 11px;
  opacity: 0.8;
}

.keep-toggle {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 4px;
  overflow: hidden;
}

.keep-btn {
  border: none;
  border-radius: 0;
  padding: 4px 8px;
  font-size: 11px;
  background: transparent;
}

.keep-btn:not(:last-child) {
  border-right: 1px solid var(--border);
}

.keep-btn.active {
  background: var(--accent);
  color: var(--accent-text);
}

.d2-row {
  align-items: stretch;
}

.quick-die-btn {
  flex: 0 0 auto;
  width: 48px;
}

.d2-row .btn-primary {
  flex: 1;
  width: auto;
}

button {
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 5px 9px;
  background: transparent;
  color: var(--fg);
  cursor: pointer;
  font-weight: 600;
}

button:hover {
  border-color: var(--accent);
}

.btn-primary {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--accent-text);
  padding: 8px 14px;
  font-size: 14px;
  width: 100%;
}

.btn-primary:hover {
  filter: brightness(1.1);
}

button:disabled {
  opacity: 0.5;
  cursor: default;
}

.roll-history {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 240px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.roll-history:empty::before {
  content: "No rolls yet.";
  opacity: 0.6;
  font-size: 12px;
}

.roll-history li {
  font-size: 12px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 6px;
}

.roll-history li:last-child {
  border-bottom: none;
  padding-bottom: 0;
}

.roll-time {
  opacity: 0.6;
  font-size: 11px;
  font-weight: 400;
}

.roll-text {
  white-space: pre-wrap;
  margin-top: 2px;
}
