:root {
  color-scheme: light dark;
  --bg: #1a1612;
  --surface: #2a231c;
  --text: #f4ead8;
  --muted: #b8a88e;
  --accent: #c9a227;
  --link: #e8c547;
  --border: #4a3f32;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
}

body {
  line-height: 1.65;
  font-size: 1.05rem;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: color-mix(in srgb, var(--bg) 94%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
  padding: 0.45rem calc(0.65rem + env(safe-area-inset-right))
    calc(0.45rem + env(safe-area-inset-bottom))
    calc(0.65rem + env(safe-area-inset-left));
  padding-top: calc(0.45rem + env(safe-area-inset-top));
}

.header-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem 0.5rem;
  max-width: 48rem;
  margin: 0 auto;
}

.book-select {
  flex: 1 1 10rem;
  min-width: 0;
  margin: 0;
  padding: 0.4rem 0.5rem;
  border-radius: 0.45rem;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.88rem;
  line-height: 1.2;
}

.section-nav {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  flex: 0 0 auto;
}

.section-nav-label {
  font-weight: 800;
  color: var(--accent);
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
}

.section-nav input {
  width: 3.6rem;
  padding: 0.4rem 0.35rem;
  border-radius: 0.45rem;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.95rem;
  font-weight: 700;
  text-align: center;
}

.btn-compact {
  padding: 0.4rem 0.55rem;
  min-width: 2.1rem;
  border-radius: 0.45rem;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #1a1612;
  font-weight: 700;
  font-size: 0.95rem;
  line-height: 1;
  cursor: pointer;
}

.btn-compact.secondary {
  background: transparent;
  color: var(--accent);
  border-color: var(--border);
}

.btn-compact:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.tool-group {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  flex: 0 0 auto;
  margin-left: auto;
}

.tool-roll-btn {
  font-size: 1.05rem;
}

.tool-mode-btn {
  min-width: 2.6rem;
}

.header-logout {
  text-decoration: none;
  color: var(--muted);
  border-color: var(--border);
  background: transparent;
}

.tool-menu {
  position: fixed;
  z-index: 20;
  padding: 0.25rem;
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  background: var(--surface);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
}

.tool-menu-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 0.4rem 0.55rem;
  border-radius: 0.4rem;
  border: none;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  font-weight: 600;
  font-size: 0.9rem;
  white-space: nowrap;
}

.tool-menu-item:hover,
.tool-menu-item:focus-visible {
  outline: none;
  background: color-mix(in srgb, var(--link) 18%, transparent);
}

.tool-result {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  min-height: 1.75rem;
}

.tool-box {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.75rem;
  height: 1.75rem;
  padding: 0 0.35rem;
  border: 1px solid var(--border);
  border-radius: 0.45rem;
  background: var(--surface);
  color: var(--accent);
  font-weight: 800;
  font-size: 1rem;
}

.tool-die {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.75rem;
  height: 1.75rem;
  font-size: 1.55rem;
  line-height: 1;
  color: var(--accent);
}

.muted {
  color: var(--muted);
}

main {
  max-width: 48rem;
  margin: 0 auto;
  padding: 0.65rem calc(0.85rem + env(safe-area-inset-right))
    calc(1.25rem + env(safe-area-inset-bottom))
    calc(0.85rem + env(safe-area-inset-left));
}

.section-text p {
  margin: 0 0 0.85rem;
}

.section-link {
  display: inline;
  padding: 0.1rem 0.35rem;
  margin: 0 0.1rem;
  border-radius: 0.35rem;
  background: color-mix(in srgb, var(--link) 18%, transparent);
  color: var(--link);
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  border: none;
  font: inherit;
  line-height: inherit;
}

.section-link:hover,
.section-link:focus-visible {
  background: color-mix(in srgb, var(--link) 30%, transparent);
  outline: 2px solid var(--link);
  outline-offset: 2px;
}

.error {
  color: #f08080;
  font-weight: 600;
}

@media (max-width: 560px) {
  .header-bar {
    gap: 0.35rem;
  }

  .book-select {
    flex: 1 1 100%;
  }

  .tool-group {
    margin-left: 0;
  }
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f7f2e8;
    --surface: #fff;
    --text: #2c2419;
    --muted: #6b5d4a;
    --accent: #8b6914;
    --link: #6b5200;
    --border: #d4c4a8;
  }

  .btn-compact {
    color: #fff;
  }

  .btn-compact.secondary,
  .header-logout {
    color: var(--accent);
  }
}
