/* =========================
   TAPD — THEME
========================= */
:root {
  --bg-1: #0b1220;
  --bg-2: #111827;
  --bg-3: #1a2335;

  --border: rgba(255, 255, 255, 0.1);
  --text: #f8fafc;
  --muted: rgba(248, 250, 252, 0.7);
  --muted-2: rgba(248, 250, 252, 0.52);

  --accent: #67e8f9;
  --danger: #f87171;

  --shadow:
    0 10px 30px rgba(0, 0, 0, 0.28),
    0 2px 10px rgba(0, 0, 0, 0.18);

  --shadow-soft:
    0 8px 24px rgba(0, 0, 0, 0.22),
    inset 0 1px 0 rgba(255, 255, 255, 0.03);

  --radius: 24px;
  --gap: 12px;
}

/* =========================
   TAPD — RESET / BASE
========================= */
* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  font-family:
    Inter,
    system-ui,
    -apple-system,
    BlinkMacSystemFont,
    "Segoe UI",
    sans-serif;
  background:
    radial-gradient(circle at top, rgba(103, 232, 249, 0.12), transparent 28%),
    radial-gradient(circle at 20% 80%, rgba(168, 85, 247, 0.08), transparent 30%),
    linear-gradient(180deg, var(--bg-3) 0%, var(--bg-2) 38%, var(--bg-1) 100%);
  color: var(--text);
  overflow: hidden;
}

body {
  display: flex;
  flex-direction: column;
}

button,
input,
textarea {
  font: inherit;
}

.hidden,
[hidden] {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  white-space: nowrap;
  border: 0;
  clip: rect(0, 0, 0, 0);
}

/* =========================
   TAPD — SHARED SCROLLBAR
========================= */
.grid,
.modal-panel,
#presetEditor,
.advanced-guide pre {
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.14) transparent;
}

.grid::-webkit-scrollbar,
.modal-panel::-webkit-scrollbar,
#presetEditor::-webkit-scrollbar,
.advanced-guide pre::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

.grid::-webkit-scrollbar-thumb,
.modal-panel::-webkit-scrollbar-thumb,
#presetEditor::-webkit-scrollbar-thumb,
.advanced-guide pre::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.14);
  border-radius: 999px;
}

.grid::-webkit-scrollbar-track,
.modal-panel::-webkit-scrollbar-track,
#presetEditor::-webkit-scrollbar-track,
.advanced-guide pre::-webkit-scrollbar-track {
  background: transparent;
}

/* =========================
   TAPD — APP LAYOUT
========================= */
.app {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  padding: 14px;
  gap: var(--gap);
}

/* =========================
   TAPD — QUICK DICE
========================= */
.quick-dice {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 8px;
  flex-shrink: 0;
}

.quick-die {
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 11px 0;
  font-size: 0.94rem;
  font-weight: 800;
  letter-spacing: 0.01em;
  color: var(--text);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.08), rgba(255, 255, 255, 0.04));
  box-shadow: var(--shadow-soft);
  backdrop-filter: blur(10px);
  transition:
    transform 0.14s ease,
    border-color 0.14s ease,
    background 0.14s ease,
    box-shadow 0.14s ease,
    color 0.14s ease;
}

.quick-die:hover {
  border-color: rgba(103, 232, 249, 0.28);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.11), rgba(255, 255, 255, 0.06));
}

.quick-die:active {
  transform: scale(0.97);
}

/* =========================
   TAPD — GRID
========================= */
.grid {
  flex: 1;
  display: grid;
  width: 100%;
  gap: var(--gap);
  min-width: 0;
  min-height: 0;
  align-content: start;
  overflow-y: auto;
  overflow-x: hidden;
  padding-bottom: 96px;
  overscroll-behavior: contain;
  scroll-behavior: smooth;
}

.empty {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  text-align: center;
  color: var(--muted);
  border-radius: var(--radius);
  border: 1px dashed rgba(255, 255, 255, 0.16);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.06), rgba(255, 255, 255, 0.03));
  backdrop-filter: blur(10px);
}

/* =========================
   TAPD — CATEGORY SECTIONS
========================= */
.category-section {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-width: 0;
}

.category-section + .category-section {
  margin-top: 4px;
}

.category-header {
  display: flex;
  align-items: center;
  min-width: 0;
  padding: 2px 2px 0;
}

.category-header::after {
  content: "";
  flex: 1;
  height: 1px;
  margin-left: 10px;
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.18), rgba(255, 255, 255, 0.04));
}

.category-chip {
  display: inline-flex;
  align-items: center;
  max-width: 100%;
  min-height: 28px;
  padding: 0 10px;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: rgba(255, 255, 255, 0.08);
  color: rgba(248, 250, 252, 0.88);
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  backdrop-filter: blur(8px);
}

.category-items {
  display: grid;
  width: 100%;
  gap: var(--gap);
  min-width: 0;
  min-height: 0;
  overflow: visible;
}

.category-items.grid::-webkit-scrollbar {
  display: none;
}

/* =========================
   TAPD — PRESET CARDS
========================= */
.preset {
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  column-gap: 18px;
  min-height: 124px;
  padding: 16px 18px;
  text-align: left;
  color: #0f172a;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  cursor: pointer;
  overflow: hidden;
  transition:
    transform 0.14s ease,
    filter 0.14s ease,
    box-shadow 0.14s ease;
}

.preset::before {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.2), transparent 35%);
  pointer-events: none;
  opacity: 0.9;
}

.preset > * {
  position: relative;
  z-index: 1;
}

.preset:active {
  transform: scale(0.987);
  filter: brightness(0.98);
}

.preset-head {
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 6px;
}

.preset-top {
  display: block;
  min-height: 0;
}

.preset-name {
  display: -webkit-box;
  overflow: hidden;
  word-break: break-word;
  overflow-wrap: anywhere;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  font-size: clamp(1rem, 3vw, 1.16rem);
  font-weight: 900;
  line-height: 1.08;
  letter-spacing: -0.02em;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.16);
}

.formula {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: clamp(0.88rem, 2.5vw, 0.98rem);
  font-weight: 700;
  line-height: 1.12;
  opacity: 0.82;
}

.result {
  min-width: 110px;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: center;
  text-align: right;
  gap: 4px;
}

.total {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: clamp(2.2rem, 5vw, 3rem);
  font-weight: 950;
  line-height: 0.92;
  letter-spacing: -0.04em;
}

.details {
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: clamp(0.78rem, 1.5vw, 0.92rem);
  line-height: 1.12;
  opacity: 0.76;
}

/* =========================
   TAPD — DENSITY MODES
========================= */
.grid[data-density="compact"] .preset {
  min-height: 108px;
  padding: 12px 14px;
  column-gap: 14px;
  border-radius: 20px;
}

.grid[data-density="compact"] .preset-name {
  font-size: clamp(0.92rem, 2.15vw, 1rem);
  line-height: 1.04;
}

.grid[data-density="compact"] .formula {
  font-size: clamp(0.78rem, 1.85vw, 0.88rem);
}

.grid[data-density="compact"] .result {
  min-width: 88px;
  gap: 2px;
}

.grid[data-density="compact"] .total {
  font-size: clamp(1.4rem, 4.8vw, 2rem);
}

.grid[data-density="compact"] .details {
  max-width: 140px;
  font-size: clamp(0.7rem, 1.7vw, 0.82rem);
}

.grid[data-density="ultra"] .preset {
  min-height: 94px;
  padding: 10px 12px;
  column-gap: 12px;
  border-radius: 17px;
}

.grid[data-density="ultra"] .preset-name {
  font-size: clamp(0.8rem, 1.8vw, 0.9rem);
  line-height: 1.02;
}

.grid[data-density="ultra"] .formula {
  font-size: clamp(0.66rem, 1.4vw, 0.76rem);
}

.grid[data-density="ultra"] .result {
  min-width: 72px;
  gap: 1px;
}

.grid[data-density="ultra"] .total {
  font-size: clamp(1.02rem, 3.4vw, 1.42rem);
}

/* =========================
   TAPD — FLOATING ACTION BUTTON
========================= */
.add-btn {
  position: fixed;
  right: 22px;
  bottom: 22px;
  z-index: 10;
  width: 62px;
  height: 62px;
  border: none;
  border-radius: 20px;
  font-size: 2rem;
  font-weight: 300;
  color: #06121c;
  background: linear-gradient(180deg, #a5f3fc 0%, #67e8f9 100%);
  box-shadow:
    0 16px 30px rgba(0, 0, 0, 0.35),
    0 4px 14px rgba(34, 211, 238, 0.25);
  transition:
    transform 0.14s ease,
    box-shadow 0.14s ease,
    filter 0.14s ease;
}

.add-btn:hover {
  filter: brightness(1.03);
}

.add-btn:active {
  transform: scale(0.95);
}

/* =========================
   TAPD — MODAL
========================= */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: none;
  align-items: stretch;
  justify-content: stretch;
  background: rgba(3, 7, 18, 0.82);
  backdrop-filter: blur(10px);
}

.modal-backdrop.open {
  display: flex;
}

.modal {
  width: 100%;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  gap: 14px;
  overflow: hidden;
  padding:
    max(14px, env(safe-area-inset-top))
    14px
    calc(14px + env(safe-area-inset-bottom))
    14px;
  color: var(--text);
  background:
    radial-gradient(circle at top, rgba(103, 232, 249, 0.08), transparent 32%),
    linear-gradient(180deg, rgba(31, 41, 55, 0.99), rgba(11, 18, 32, 0.99));
}

.modal-head-compact {
  display: none;
}

.modal-tabs,
.modal-panel,
.modal-actions {
  width: min(100%, 980px);
  margin-inline: auto;
}

.modal-tabs {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px;
  border-radius: 18px;
  border: 1px solid rgba(255, 255, 255, 0.07);
  background: rgba(255, 255, 255, 0.04);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.03);
}

.modal-tab {
  appearance: none;
  border: 1px solid transparent;
  border-radius: 14px;
  padding: 12px 16px;
  min-height: 48px;
  font-size: 0.96rem;
  font-weight: 850;
  color: var(--text);
  background: transparent;
  transition:
    background 0.12s ease,
    border-color 0.12s ease,
    transform 0.12s ease,
    color 0.12s ease,
    box-shadow 0.12s ease;
}

.modal-tab.active {
  color: #effcff;
  background: rgba(103, 232, 249, 0.14);
  border-color: rgba(103, 232, 249, 0.3);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.06);
}

.modal-tab:active {
  transform: scale(0.985);
}

.modal-tabs .icon-btn {
  margin-left: auto;
}

.icon-btn {
  width: 42px;
  height: 42px;
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 12px;
  color: var(--text);
  background: rgba(255, 255, 255, 0.06);
  font-size: 1.05rem;
  transition:
    transform 0.12s ease,
    background 0.12s ease,
    border-color 0.12s ease;
}

.icon-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

.icon-btn:active {
  transform: scale(0.97);
}

.modal-panel {
  display: none;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding-bottom: 4px;
}

.modal-panel.active {
  display: block;
}

#editorPanel.active {
  display: flex;
  flex-direction: column;
}

.field-editor {
  flex: 1;
  min-height: 0;
  margin-bottom: 0;
}

/* =========================
   TAPD — GUIDE
========================= */
.advanced-guide {
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-height: 100%;
}

.guide-section {
  padding: 16px;
  border-radius: 20px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.06), rgba(255, 255, 255, 0.03));
  box-shadow: var(--shadow-soft);
}

.guide-section h3 {
  margin: 0 0 8px;
  font-size: 1.05rem;
  font-weight: 850;
  letter-spacing: -0.02em;
}

.guide-section p {
  margin: 0;
  color: var(--muted);
  font-size: 0.94rem;
  line-height: 1.5;
}

.guide-section p + pre,
.guide-section h3 + p + pre {
  margin-top: 12px;
}

.guide-eyebrow {
  margin-bottom: 8px !important;
  color: var(--accent);
  font-size: 0.76rem !important;
  font-weight: 850 !important;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.advanced-guide pre {
  margin: 12px 0 0;
  padding: 13px 14px;
  border-radius: 16px;
  overflow-x: auto;
  background: rgba(5, 10, 20, 0.32);
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.advanced-guide code,
#presetEditor {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.advanced-guide code {
  font-size: 0.9rem;
  line-height: 1.55;
  color: var(--accent);
}

/* =========================
   TAPD — FORM FIELDS
========================= */
.field {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 14px;
}

.field input,
.field textarea {
  width: 100%;
  padding: 14px 15px;
  font-size: 1rem;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.05);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.03);
  transition:
    border-color 0.14s ease,
    background 0.14s ease,
    box-shadow 0.14s ease;
}

.field input::placeholder,
.field textarea::placeholder {
  color: var(--muted-2);
}

.field input:focus,
.field textarea:focus {
  outline: none;
  border-color: rgba(103, 232, 249, 0.45);
  background: rgba(255, 255, 255, 0.07);
  box-shadow:
    0 0 0 4px rgba(103, 232, 249, 0.12),
    inset 0 1px 0 rgba(255, 255, 255, 0.04);
}

.field textarea {
  min-height: 140px;
  resize: none;
}

#presetEditor {
  flex: 1;
  min-height: 300px;
  padding: 16px 17px;
  border-radius: 20px;
  font-size: 0.98rem;
  line-height: 1.55;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.055), rgba(255, 255, 255, 0.035));
}

/* =========================
   TAPD — EDITOR ERRORS
========================= */
.field.has-errors #presetEditor {
  border-color: rgba(248, 113, 113, 0.65);
  background:
    linear-gradient(180deg, rgba(127, 29, 29, 0.18), rgba(255, 255, 255, 0.035));
  box-shadow:
    0 0 0 4px rgba(248, 113, 113, 0.14),
    inset 0 1px 0 rgba(255, 255, 255, 0.03);
}

.field.has-errors #presetEditor:focus {
  border-color: rgba(248, 113, 113, 0.78);
  box-shadow:
    0 0 0 4px rgba(248, 113, 113, 0.18),
    inset 0 1px 0 rgba(255, 255, 255, 0.04);
}

.editor-errors {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 2px;
  padding: 10px 12px;
  border-radius: 16px;
  border: 1px solid rgba(248, 113, 113, 0.3);
  background:
    linear-gradient(180deg, rgba(127, 29, 29, 0.22), rgba(127, 29, 29, 0.12));
}

.editor-errors-head {
  font-size: 0.82rem;
  font-weight: 850;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: #fca5a5;
}

.editor-errors-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.editor-error-item {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  padding: 10px 12px;
  text-align: left;
  color: var(--text);
  border: 1px solid rgba(248, 113, 113, 0.18);
  border-radius: 12px;
  background: rgba(127, 29, 29, 0.18);
}

.editor-error-item:active {
  transform: scale(0.985);
}

.editor-error-line {
  font-size: 0.78rem;
  font-weight: 800;
  color: #fca5a5;
}

.editor-error-message {
  font-size: 0.9rem;
  line-height: 1.4;
  color: #fee2e2;
}

/* =========================
   TAPD — MODAL ACTIONS
========================= */
.modal-actions {
  flex-shrink: 0;
  display: flex;
  gap: 10px;
  padding-top: 12px;
  margin-top: auto;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  background: linear-gradient(180deg, rgba(17, 24, 39, 0.12), rgba(17, 24, 39, 0));
}

.modal-actions .btn {
  flex: 1;
}

.btn {
  appearance: none;
  min-height: 52px;
  padding: 13px 18px;
  font-weight: 850;
  border: 1px solid transparent;
  border-radius: 16px;
  transition:
    transform 0.12s ease,
    background 0.12s ease,
    border-color 0.12s ease,
    filter 0.12s ease,
    box-shadow 0.12s ease;
}

.btn:active {
  transform: scale(0.97);
}

.btn-secondary {
  color: var(--text);
  background: rgba(255, 255, 255, 0.07);
  border-color: rgba(255, 255, 255, 0.08);
}

.btn-secondary:hover {
  background: rgba(255, 255, 255, 0.1);
}

.btn-primary {
  color: #07121b;
  background: linear-gradient(180deg, #8ff3ff 0%, #67e8f9 100%);
  box-shadow: 0 10px 24px rgba(34, 211, 238, 0.18);
}

.btn-primary:hover {
  filter: brightness(1.03);
}

/* =========================
   TAPD — RESPONSIVE
========================= */
@media (max-width: 900px) {
  .app {
    padding: 12px;
    gap: 10px;
  }

  .quick-dice {
    gap: 6px;
  }

  .quick-die {
    padding: 9px 0;
    font-size: 0.86rem;
    border-radius: 14px;
  }

  .add-btn {
    right: 18px;
    bottom: 18px;
    width: 58px;
    height: 58px;
    border-radius: 18px;
  }

  .modal {
    gap: 12px;
    padding:
      max(12px, env(safe-area-inset-top))
      12px
      calc(12px + env(safe-area-inset-bottom))
      12px;
  }

  .guide-section {
    padding: 14px;
    border-radius: 18px;
  }
}

@media (max-width: 640px) {
  .preset {
    grid-template-columns: 1fr;
    row-gap: 8px;
    min-height: 112px;
  }

  .result {
    min-width: 0;
    align-items: flex-start;
    text-align: left;
  }

  .details {
    max-width: none;
  }

  .category-section {
    gap: 8px;
  }

  .category-chip {
    min-height: 26px;
    padding: 0 9px;
    font-size: 0.72rem;
  }

  .modal-tab {
    flex: 0 0 auto;
    min-width: 0;
  }

  #presetEditor {
    min-height: 240px;
    font-size: 0.95rem;
  }

  .guide-section h3 {
    font-size: 1rem;
  }
}