:root {
  color-scheme: dark;
  --ink: #f8f3df;
  --muted: #9eb8ae;
  --felt: #07533f;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
}

body {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  background:
    radial-gradient(circle at 50% -12%, rgba(52, 164, 120, 0.46), transparent 35%),
    linear-gradient(150deg, #0a644c, #06382e 60%, #03231d);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Hiragino Sans", sans-serif;
}

body::before {
  position: fixed;
  inset: 0;
  z-index: -1;
  background-image:
    linear-gradient(rgba(255, 255, 255, 0.018) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.018) 1px, transparent 1px);
  background-size: 22px 22px;
  content: "";
}

button,
a {
  color: inherit;
  font: inherit;
}

.achievement-header {
  z-index: 10;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  min-height: calc(52px + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) max(12px, env(safe-area-inset-right)) 0 max(12px, env(safe-area-inset-left));
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(2, 27, 21, 0.92);
  backdrop-filter: blur(16px);
}

.back-link {
  justify-self: start;
  padding: 7px 10px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.07);
  color: #dcebe4;
  font-size: 11px;
  font-weight: 850;
  text-decoration: none;
}

.header-title {
  display: grid;
  gap: 0;
  text-align: center;
}

.header-title span {
  color: #f2ce75;
  font-family: Georgia, serif;
  font-size: 7px;
  font-weight: 850;
  letter-spacing: 0.18em;
}

.header-title strong {
  font-size: 15px;
}

.collection-count {
  display: flex;
  justify-self: end;
  align-items: baseline;
  gap: 4px;
}

.collection-count > span {
  color: var(--muted);
  font-size: 8px;
  font-weight: 800;
}

.collection-count strong {
  color: #f2ce75;
  font-family: Georgia, serif;
  font-size: 20px;
}

.collection-count b,
.collection-count i {
  font: inherit;
}

.collection-count i {
  color: #8ca299;
  font-size: 10px;
}

.achievement-main {
  display: grid;
  min-height: 0;
  grid-template-rows: 26px minmax(0, 1fr);
  gap: 5px;
  width: min(540px, 100%);
  margin: 0 auto;
  padding: 6px max(7px, env(safe-area-inset-right)) max(7px, env(safe-area-inset-bottom)) max(7px, env(safe-area-inset-left));
}

.rarity-guide {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  align-items: center;
  gap: 3px;
  padding: 3px 5px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 9px;
  background: rgba(1, 31, 24, 0.62);
}

.rarity-guide span {
  display: inline-flex;
  min-width: 0;
  align-items: center;
  justify-content: center;
  gap: 3px;
  overflow: hidden;
  color: #a8beb6;
  font-family: Georgia, serif;
  font-size: clamp(5px, 1.55vw, 7px);
  font-weight: 850;
  letter-spacing: 0.02em;
  white-space: nowrap;
}

.rarity-dot {
  width: 6px;
  height: 6px;
  flex: 0 0 auto;
  border-radius: 50%;
  background: #b8733e;
  box-shadow: 0 0 5px currentColor;
}

.rarity-dot.uncommon { background: #c7d0d3; }
.rarity-dot.rare { background: #e2b84f; }
.rarity-dot.epic { background: #83e4ff; }
.rarity-dot.legendary { background: conic-gradient(#ff5b74, #ffd65d, #62e39e, #55baff, #b178ff, #ff5b74); }

.badge-grid {
  display: grid;
  min-height: 0;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(8, minmax(0, 1fr));
  gap: 4px;
}

.badge-card,
.detail-dialog {
  --metal-dark: #6b351d;
  --metal-mid: #b8733e;
  --metal-light: #efb277;
  --enamel: #4b281d;
  --glow: rgba(213, 126, 63, 0.3);
  --tier-bg: rgba(134, 69, 32, 0.14);
}

.badge-card[data-tier="uncommon"],
.detail-dialog[data-tier="uncommon"] {
  --metal-dark: #586064;
  --metal-mid: #b8c2c6;
  --metal-light: #f3f6f7;
  --enamel: #29423e;
  --glow: rgba(220, 238, 240, 0.28);
  --tier-bg: rgba(190, 207, 212, 0.1);
}

.badge-card[data-tier="rare"],
.detail-dialog[data-tier="rare"] {
  --metal-dark: #775316;
  --metal-mid: #dfb34e;
  --metal-light: #fff1a8;
  --enamel: #49330f;
  --glow: rgba(255, 206, 90, 0.34);
  --tier-bg: rgba(206, 149, 40, 0.13);
}

.badge-card[data-tier="epic"],
.detail-dialog[data-tier="epic"] {
  --metal-dark: #24627a;
  --metal-mid: #75d9fa;
  --metal-light: #ecfdff;
  --enamel: #173f66;
  --glow: rgba(107, 221, 255, 0.42);
  --tier-bg: rgba(72, 184, 226, 0.13);
}

.badge-card[data-tier="legendary"],
.detail-dialog[data-tier="legendary"] {
  --metal-dark: #74469c;
  --metal-mid: #f1d96f;
  --metal-light: #ffffff;
  --enamel: #313478;
  --glow: rgba(188, 115, 255, 0.45);
  --tier-bg: rgba(184, 100, 235, 0.14);
}

.badge-card {
  position: relative;
  display: grid;
  min-width: 0;
  min-height: 0;
  grid-template-rows: minmax(0, 1fr);
  align-items: center;
  justify-items: center;
  padding: 1px 2px 2px;
  overflow: hidden;
  border: 1px solid color-mix(in srgb, var(--metal-mid) 42%, transparent);
  border-radius: 9px;
  background:
    radial-gradient(circle at 50% 38%, var(--glow), transparent 41%),
    linear-gradient(150deg, var(--tier-bg), rgba(1, 28, 22, 0.82));
  appearance: none;
}

.badge-card:active {
  transform: scale(0.96);
}

.badge-card.is-unlocked {
  border-color: color-mix(in srgb, var(--metal-mid) 70%, transparent);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--metal-light) 10%, transparent);
}

.badge-emblem {
  /* Keep iPhone iframe badges deterministic across repeated rotations. */
  --badge-size: min(60px, 15vw);
  position: relative;
  display: grid;
  width: var(--badge-size);
  height: var(--badge-size);
  place-items: center;
  border: max(2px, calc(var(--badge-size) * 0.045)) solid var(--metal-mid);
  border-radius: 50%;
  background:
    radial-gradient(circle at 35% 25%, rgba(255, 255, 255, 0.25), transparent 20%),
    repeating-conic-gradient(from 0deg, rgba(255, 255, 255, 0.05) 0 5deg, transparent 5deg 18deg),
    radial-gradient(circle, var(--enamel), #061d18 74%);
  box-shadow:
    0 0 0 2px var(--metal-dark),
    0 0 0 3px var(--metal-light),
    0 3px 7px rgba(0, 0, 0, 0.35),
    inset 0 0 0 2px rgba(255, 255, 255, 0.09);
}

.badge-emblem::before,
.badge-emblem::after {
  position: absolute;
  top: 50%;
  width: calc(var(--badge-size) * 0.18);
  height: calc(var(--badge-size) * 0.28);
  border: 1px solid var(--metal-light);
  border-radius: 14%;
  background: linear-gradient(135deg, var(--metal-light), var(--metal-dark));
  content: "";
  transform: translateY(-50%) rotate(45deg);
}

.badge-emblem::before { left: calc(var(--badge-size) * -0.13); }
.badge-emblem::after { right: calc(var(--badge-size) * -0.13); }

[data-tier="epic"] .badge-emblem {
  background:
    linear-gradient(145deg, transparent 35%, rgba(255, 255, 255, 0.16) 36% 48%, transparent 49%),
    repeating-conic-gradient(from 12deg, rgba(121, 224, 255, 0.16) 0 12deg, transparent 12deg 30deg),
    radial-gradient(circle, var(--enamel), #071b30 75%);
}

[data-tier="legendary"] .badge-emblem {
  border-color: transparent;
  background:
    radial-gradient(circle at 34% 24%, rgba(255, 255, 255, 0.34), transparent 18%) padding-box,
    radial-gradient(circle, #303477, #101b3e 73%) padding-box,
    conic-gradient(#ff5b72, #ffd95f, #59e49a, #56c7ff, #a876ff, #ff5b72) border-box;
  box-shadow:
    0 0 0 2px #70439b,
    0 0 9px rgba(118, 214, 255, 0.55),
    0 3px 7px rgba(0, 0, 0, 0.36),
    inset 0 0 0 2px rgba(255, 255, 255, 0.1);
}

.badge-icon {
  position: relative;
  z-index: 2;
  display: grid;
  width: 70%;
  height: 70%;
  place-items: center;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 50%;
  color: var(--metal-light);
}

.badge-icon::before,
.badge-icon::after {
  position: absolute;
  width: calc(var(--badge-size) * 0.11);
  height: calc(var(--badge-size) * 0.11);
  border-radius: 24%;
  background: currentColor;
  color: var(--metal-mid);
  content: "";
  transform: rotate(45deg);
}

.badge-icon::before { top: -8%; }
.badge-icon::after { bottom: -8%; }

.badge-glyph {
  display: block;
  width: 78%;
  height: 78%;
  object-fit: contain;
  filter: drop-shadow(0 1px 0 var(--metal-dark)) drop-shadow(0 0 4px var(--glow));
  pointer-events: none;
  transform: translateY(-2px);
  user-select: none;
}

.badge-card.is-locked .badge-emblem {
  filter: saturate(0.48) brightness(0.62);
}

.badge-lock {
  position: absolute;
  top: calc(50% - 1px);
  left: calc(50% + min(15px, 4vw));
  z-index: 4;
  width: 13px;
  height: 11px;
  border: 1px solid #d5e0dc;
  border-radius: 3px;
  background: #17392f;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.35);
}

.badge-lock::before {
  position: absolute;
  top: -6px;
  left: 2px;
  width: 7px;
  height: 6px;
  border: 1px solid #d5e0dc;
  border-bottom: 0;
  border-radius: 5px 5px 0 0;
  content: "";
}

.badge-card.is-unlocked .badge-lock { display: none; }

.badge-card-name {
  position: absolute;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 3;
  display: grid;
  width: auto;
  min-height: 0;
  margin: 0;
  padding: 2px 3px;
  place-items: center;
  overflow: hidden;
  border-radius: 0 0 7px 7px;
  background: rgba(0, 14, 11, 0.62);
  color: #eef4e9;
  font-family: Georgia, "Yu Mincho", serif;
  font-size: clamp(9px, 2.45vw, 11px);
  font-weight: 900;
  line-height: 1.1;
  text-align: center;
  text-shadow: 0 1px 2px #000;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.detail-sheet {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 14px max(12px, env(safe-area-inset-right)) max(14px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
}

.detail-sheet[hidden] { display: none; }

.detail-backdrop {
  position: absolute;
  inset: 0;
  border: 0;
  background: rgba(0, 10, 8, 0.72);
  backdrop-filter: blur(7px);
}

.detail-dialog {
  position: relative;
  z-index: 1;
  display: grid;
  width: min(520px, 100%);
  grid-template-columns: 132px minmax(0, 1fr);
  gap: 16px;
  align-items: center;
  padding: 20px 18px 18px;
  border: 1px solid color-mix(in srgb, var(--metal-mid) 62%, transparent);
  border-radius: 22px;
  background:
    radial-gradient(circle at 14% 44%, var(--glow), transparent 29%),
    linear-gradient(150deg, #0a4637, #031f19 72%);
  box-shadow: 0 24px 65px rgba(0, 0, 0, 0.5);
}

.detail-close {
  position: absolute;
  top: 9px;
  right: 10px;
  z-index: 2;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.07);
  font-size: 22px;
  line-height: 1;
}

.detail-visual {
  display: grid;
  gap: 13px;
  justify-items: center;
}

.detail-badge-wrap {
  position: relative;
}

.detail-emblem {
  --badge-size: 108px;
}

.detail-dialog.is-locked .detail-emblem {
  filter: saturate(0.48) brightness(0.62);
}

.detail-badge-lock {
  top: auto;
  right: -2px;
  bottom: 3px;
  left: auto;
  width: 20px;
  height: 16px;
  border-width: 2px;
  border-radius: 5px;
  box-shadow: 0 3px 7px rgba(0, 0, 0, 0.48), 0 0 0 1px rgba(0, 0, 0, 0.18);
}

.detail-badge-lock::before {
  top: -10px;
  left: 3px;
  width: 10px;
  height: 10px;
  border-width: 2px;
}

.detail-dialog.is-unlocked .detail-badge-lock {
  display: none;
}

.detail-state {
  padding: 5px 10px;
  border: 1px solid color-mix(in srgb, var(--metal-mid) 55%, transparent);
  border-radius: 999px;
  background: var(--tier-bg);
  color: var(--metal-light);
  font-size: 10px;
  font-weight: 850;
}

.detail-copy {
  display: grid;
  min-width: 0;
  gap: 6px;
}

.detail-tier {
  color: var(--metal-mid);
  font-family: Georgia, serif;
  font-size: 9px;
  font-weight: 900;
  letter-spacing: 0.15em;
}

.detail-copy h1 {
  margin: 0;
  padding-right: 30px;
  font-family: Georgia, "Yu Mincho", serif;
  font-size: 25px;
  line-height: 1.15;
}

.detail-copy > p {
  min-height: 2.8em;
  margin: 1px 0 4px;
  color: #b7ccc4;
  font-size: 12px;
  line-height: 1.45;
}

.detail-progress-copy {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}

.detail-progress-copy span {
  color: #849d94;
  font-family: Georgia, serif;
  font-size: 7px;
  font-weight: 800;
  letter-spacing: 0.08em;
}

.detail-progress-copy strong {
  color: var(--metal-light);
  font-family: Georgia, serif;
  font-size: 11px;
}

.detail-progress {
  height: 6px;
  overflow: hidden;
  border-radius: 99px;
  background: rgba(255, 255, 255, 0.1);
}

.detail-progress i {
  display: block;
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--metal-mid), var(--metal-light));
  box-shadow: 0 0 9px var(--glow);
}

.detail-done {
  justify-self: end;
  min-width: 84px;
  margin-top: 7px;
  padding: 8px 14px;
  border: 1px solid color-mix(in srgb, var(--metal-mid) 60%, transparent);
  border-radius: 10px;
  background: var(--tier-bg);
  color: var(--metal-light);
  font-size: 11px;
  font-weight: 850;
}

@media (max-width: 520px) {
  .detail-dialog {
    grid-template-columns: 112px minmax(0, 1fr);
    gap: 13px;
    padding: 18px 15px 15px;
  }

  .detail-emblem { --badge-size: 92px; }
  .detail-copy h1 { font-size: 22px; }
}

@media (max-height: 660px) {
  .achievement-header {
    min-height: calc(46px + env(safe-area-inset-top));
  }

  .header-title span,
  .collection-count > span { display: none; }

  .achievement-main {
    grid-template-rows: 22px minmax(0, 1fr);
    gap: 3px;
    padding-top: 4px;
  }

  .rarity-guide { padding: 1px 4px; }
  .badge-grid { gap: 3px; }
  .badge-card { border-radius: 7px; }
  .badge-card-name { font-size: 8px; }
}

@media (orientation: landscape) {
  .achievement-main { width: min(1120px, 100%); }

  .badge-grid {
    grid-template-columns: repeat(8, minmax(0, 1fr));
    grid-template-rows: repeat(4, minmax(0, 1fr));
  }

  .badge-emblem { --badge-size: clamp(56px, 8vw, 72px); }
  .detail-dialog .detail-emblem { --badge-size: clamp(92px, 11vw, 108px); }
  .badge-card-name { font-size: 7px; }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after { transition: none !important; }
}
