@font-face {
  font-family: 'Black Ops One';
  src: url('fonts/black-ops-one-400.woff2') format('woff2');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: 'IBM Plex Mono';
  src: url('fonts/ibm-plex-mono-400.woff2') format('woff2');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: 'IBM Plex Mono';
  src: url('fonts/ibm-plex-mono-600.woff2') format('woff2');
  font-weight: 600;
  font-style: normal;
  font-display: swap;
}

:root {
  --ground: #2E3420;
  --ground-deep: #1F2416;
  --khaki: #CDBE8E;
  --olive: #4B5530;
  --sand: #E3D6A8;
  --dim: #59613F;
  --muted: #A29C78; /* subtle text, still ~4.5:1 on --ground */

  --display: 'Black Ops One', Impact, sans-serif;
  --mono: 'IBM Plex Mono', ui-monospace, Menlo, monospace;

  /* Card width: 330px max, 84vw on small phones, and never taller than the space left under the top bar. */
  --cw: min(330px, 84vw, calc((100dvh - 150px - env(safe-area-inset-top) - env(safe-area-inset-bottom)) / 1.4));
  --ease: cubic-bezier(.2, .7, .2, 1);
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}

body {
  background: var(--ground);
  color: var(--sand);
  font-family: var(--mono);
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  margin: 0;
  cursor: pointer;
  touch-action: manipulation;
}
button:focus { outline: none; }
button:focus-visible { outline: 2px solid var(--khaki); outline-offset: 3px; }

/* ---------- Main layout ---------- */

.app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  padding:
    calc(env(safe-area-inset-top) + 14px)
    calc(env(safe-area-inset-right) + 20px)
    calc(env(safe-area-inset-bottom) + 16px)
    calc(env(safe-area-inset-left) + 20px);
}

.topbar {
  display: flex;
  align-items: flex-end;
  gap: 24px;
  width: 100%;
  max-width: 420px;
  margin: 0 auto;
}
.top-left, .top-right {
  flex: 1 1 0;
  min-width: 0;
}
.top-right {
  display: block;
  text-align: right;
  padding-top: 18px; /* tall enough tap target */
}

.label {
  display: block;
  font: 600 11px/1 var(--mono);
  letter-spacing: 2px;
  color: var(--sand);
  white-space: nowrap;
  margin-bottom: 8px;
}

.segs { display: flex; gap: 4px; }
.seg {
  flex: 1 1 0;
  height: 8px;
  border-radius: 1px;
  background: var(--ground-deep);
  box-shadow: inset 0 0 0 1px var(--dim);
  transition: background-color 200ms, box-shadow 200ms;
}
.seg.on { background: var(--khaki); box-shadow: none; }

.bar {
  display: block;
  height: 8px;
  border-radius: 1px;
  background: var(--ground-deep);
  box-shadow: inset 0 0 0 1px var(--dim);
  overflow: hidden;
}
.bar-fill {
  display: block;
  height: 100%;
  background: var(--khaki);
  transform-origin: left center;
  transform: scaleX(0);
  transition: transform 300ms var(--ease);
}

.stage {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  place-items: center;
}

/* Practice word: pinned to the bottom, out of the layout flow so the card never moves.
   Comes before .stage in the DOM so flying cards pass over it. */
.practice {
  position: absolute;
  left: 0;
  right: 0;
  bottom: calc(env(safe-area-inset-bottom) + 18px);
  text-align: center;
  font: 600 15px/1 var(--mono);
  letter-spacing: 4px;
  color: var(--muted);
  white-space: nowrap;
  pointer-events: none;
}
.practice b { font-weight: 600; color: var(--khaki); }

/* ---------- Card ---------- */

.card {
  grid-area: 1 / 1;
  position: relative;
  width: var(--cw);
  height: calc(var(--cw) * 1.4);
  perspective: 1200px;
  touch-action: none;
  will-change: transform;
}
.card:focus-visible { outline: none; }
.card:focus-visible .face { outline: 2px solid var(--sand); outline-offset: 4px; }

.card-inner {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  transition: transform 450ms var(--ease);
}
.card.flipped .card-inner { transform: rotateY(180deg); }

.face {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  padding: 16px 18px 14px;
  border-radius: 14px;
  border: 3px solid var(--ground-deep);
  box-shadow: 0 30px 50px rgba(0, 0, 0, .55), 0 8px 14px rgba(0, 0, 0, .35);
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  overflow: hidden;
  text-align: left;
}
/* Very soft light-to-shade gradients, centered on the base colors. */
.front {
  background: var(--khaki) linear-gradient(160deg, #D6C89B 0%, var(--khaki) 50%, #C3B383 100%);
  color: var(--ground-deep);
}
.back {
  background: var(--olive) linear-gradient(160deg, #545E37 0%, var(--olive) 50%, #434C2A 100%);
  color: var(--sand);
  transform: rotateY(180deg);
}

.face-row {
  display: flex;
  justify-content: space-between;
  font: 600 12px/1 var(--mono);
  letter-spacing: 2px;
}

.big {
  flex: 1 1 auto;
  display: grid;
  place-items: center;
  font: 400 calc(var(--cw) * .79)/1 var(--display);
  padding-top: calc(var(--cw) * .04);
}

.mid {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: center;
  text-align: center;
}
.word {
  display: block;
  font: 400 calc(var(--cw) * .218)/1 var(--display);
  white-space: nowrap;
  text-transform: uppercase;
}
.pron {
  margin-top: 16px;
  font: 400 18px/1.2 var(--mono);
  letter-spacing: 1px;
}

.face-foot {
  border-top: 2px dashed currentColor;
  padding-top: 12px;
  text-align: center;
  font: 600 12px/1 var(--mono);
  letter-spacing: 2px;
}
.front .face-foot { border-top-color: rgba(31, 36, 22, .45); }
.back  .face-foot { border-top-color: rgba(227, 214, 168, .4); }

/* ---------- Stats sheet ---------- */

.sheet {
  position: fixed;
  inset: 0;
  z-index: 10;
  visibility: hidden;
  transition: visibility 0s linear 320ms;
}
.sheet.open { visibility: visible; transition-delay: 0s; }

.sheet-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, .5);
  opacity: 0;
  transition: opacity 320ms var(--ease);
}
.sheet.open .sheet-backdrop { opacity: 1; }

.sheet-panel {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  max-width: 560px;
  margin: 0 auto;
  max-height: calc(100dvh - env(safe-area-inset-top) - 20px);
  display: flex;
  flex-direction: column;
  background: var(--ground-deep);
  border-radius: 18px 18px 0 0;
  box-shadow: 0 -10px 40px rgba(0, 0, 0, .5);
  transform: translateY(100%);
  transition: transform 320ms var(--ease);
  will-change: transform;
}
.sheet.open .sheet-panel { transform: translateY(0); }

.sheet-head {
  flex: none;
  padding: 8px 20px 16px;
  touch-action: none;
  cursor: grab;
}
.grabber {
  width: 40px;
  height: 4px;
  margin: 0 auto 6px;
  border-radius: 2px;
  background: var(--dim);
}
.sheet-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.sheet-title-row h2 {
  margin: 0;
  font: 400 30px/1 var(--display);
  color: var(--khaki);
  letter-spacing: 1px;
}
.close {
  width: 44px;
  height: 44px;
  margin-right: -10px;
  font-size: 20px;
  line-height: 44px;
  text-align: center;
  color: var(--sand);
}
.sheet-count {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  font: 600 12px/1 var(--mono);
  letter-spacing: 2px;
  margin: 6px 0 10px;
}

.sheet-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
  padding: 4px 20px calc(env(safe-area-inset-bottom) + 20px);
}

.grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
}
.tile {
  height: 62px;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  border-radius: 6px;
  background: var(--ground-deep);
  color: var(--sand);
  box-shadow: inset 0 0 0 1px var(--dim);
}
.tile.mastered { background: var(--khaki); color: var(--ground-deep); box-shadow: none; }
.tile-l { font: 400 22px/1 var(--display); }
.tile-w { font: 400 10px/1 var(--mono); letter-spacing: .3px; white-space: nowrap; }
.mini { display: flex; gap: 2px; width: 70%; }
.mini span {
  flex: 1 1 0;
  height: 4px;
  border-radius: 1px;
  box-shadow: inset 0 0 0 1px var(--dim);
}
.mini span.on { background: var(--khaki); box-shadow: none; }
.tile.mastered .mini span.on { background: var(--ground-deep); }

.actions {
  display: flex;
  gap: 8px;
  margin-top: 18px;
}
.btn {
  flex: 1 1 0;
  min-height: 44px;
  padding: 0 8px;
  border-radius: 6px;
  border: 1px solid var(--dim);
  font: 600 11px/1.2 var(--mono);
  letter-spacing: 1.5px;
  color: var(--sand);
}
.btn:active { background: rgba(205, 190, 142, .12); }

.status {
  margin: 16px 0;
  min-height: 14px;
  text-align: center;
  font: 600 11px/1.3 var(--mono);
  letter-spacing: 2px;
  color: var(--khaki);
}
.status button { letter-spacing: 2px; text-decoration: underline; text-underline-offset: 3px; padding: 10px 0; }

.reset { display: block; width: 100%; }
.reset.armed { background: var(--khaki); color: var(--ground-deep); border-color: var(--khaki); }

/* ---------- Reduced motion: flips become cross-fades ---------- */

@media (prefers-reduced-motion: reduce) {
  .card-inner, .card.flipped .card-inner { transform: none; transition: none; }
  .face { transition: opacity 150ms linear; }
  .back { transform: none; opacity: 0; }
  .card.flipped .front { opacity: 0; }
  .card.flipped .back { opacity: 1; }
  .sheet-panel, .sheet-backdrop, .bar-fill { transition-duration: 150ms; }
}
