* {
  box-sizing: border-box;
}

:root {
  --card-bg: rgba(255, 255, 255, 0.82);
  --card-border: rgba(255, 255, 255, 0.35);
  --text: #0b1221;
  --ok: #12b886;
  --err: #ff4d4f;
  --muted: #6b7280;
}

html,
body {
  height: 100%;
}

body {
  display: grid;
  place-items: center;
  min-height: 100vh;
  margin: 0;
  color: var(--text);
  background: #0f172a;
  overflow-x: hidden;
}

/* iOS Liquid Retina–inspired animated gradient */
.bg {
  position: fixed;
  inset: 0;
  z-index: -1;
  background: radial-gradient(40% 60% at 20% 20%, #ff6b6b88, transparent 60%),
    radial-gradient(45% 65% at 80% 20%, #ffd93d88, transparent 60%),
    radial-gradient(60% 70% at 30% 80%, #4dabf788, transparent 60%),
    radial-gradient(50% 60% at 75% 75%, #845ef788, transparent 60%),
    radial-gradient(100% 100% at 50% 50%, #0ea5e980, #0f172a 60%);
  filter: saturate(120%) blur(12px);
  animation: float 18s ease-in-out infinite alternate;
}

@keyframes float {
  0% {
    transform: translate3d(0, 0, 0) scale(1);
  }

  50% {
    transform: translate3d(-2%, 1%, 0) scale(1.03);
  }

  100% {
    transform: translate3d(2%, -1%, 0) scale(1.05);
  }
}

@media (prefers-reduced-motion: reduce) {
  .bg {
    animation: none;
  }
}

body,
.text-input {
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Apple Color Emoji", "Segoe UI Emoji";
}

.container {
  position: fixed;
  background: var(--card-bg);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  border: 1px solid var(--card-border);
  padding: 1rem 1.25rem 1.25rem;
  border-radius: 0.75rem;
  width: 800px;
  max-width: 92vw;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}

.score-container {
  position: fixed;
  top: 1rem;
  left: 50%;
  transform: translateX(-50%);
  display: grid;
  grid-auto-flow: column;
  gap: .75rem 1rem;
  align-items: center;
  padding: .35rem .75rem;
  border-radius: .75rem;
  background: rgba(255, 255, 255, 0.5);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  border: 1px solid var(--card-border);
}

.metric {
  display: flex;
  gap: .4rem;
  align-items: baseline;
  font-weight: 600;
}

.metric .label {
  color: var(--muted);
  font-size: .8rem;
  font-weight: 500;
}

.metric .value {
  min-width: 4ch;
  text-align: right;
}

.controls {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: .5rem;
  margin-bottom: .75rem;
}

.toggle {
  display: flex;
  align-items: center;
  gap: .5rem;
  -webkit-user-select: none;
  user-select: none;
}

.toggle input {
  width: 1.1rem;
  height: 1.1rem;
}

.toggle-label {
  font-size: .95rem;
  color: var(--muted);
}

.control-buttons {
  display: flex;
  gap: .5rem;
}

.btn {
  border: 1px solid #11182720;
  background: #11182710;
  color: #111827;
  padding: .45rem .8rem;
  border-radius: .5rem;
  font-weight: 600;
  cursor: pointer;
}

.btn:hover {
  background: #11182715;
}

.btn.secondary {
  background: transparent;
}

.text-display {
  margin: 0 0 .75rem 0;
  padding: .75rem 1rem;
  border-radius: .5rem;
  min-height: 5.5rem;
  line-height: 1.6;
  border: 1px dashed #11182725;
  background: rgba(255, 255, 255, 0.35);
}

.text-display.loading {
  position: relative;
  overflow: hidden;
}

.text-display.loading::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, .6), transparent);
  animation: shimmer 1.2s infinite;
}

@keyframes shimmer {
  0% {
    transform: translateX(-100%);
  }

  100% {
    transform: translateX(100%);
  }
}

.text-display span {
  transition: color .15s ease, background-color .15s ease, text-shadow .15s ease;
}

.text-display .correct {
  color: var(--ok);
}

.text-display .incorrect {
  color: var(--err);
  text-decoration: underline;
  text-decoration-thickness: 2px;
  text-underline-offset: 2px;
}

.text-display .current {
  background: #1118271a;
  border-radius: .2rem;
  box-shadow: 0 0 0 2px #11182715 inset;
}

/* Blinking caret overlay */
.caret {
  position: absolute;
  width: 2px;
  background: #111827cc;
  animation: blink 1s steps(2, start) infinite;
  height: 1.2em;
  transform: translateY(3px);
  pointer-events: none;
}

@keyframes blink {
  50% {
    opacity: 0;
  }
}

.text-display {
  position: relative;
}

.text-display.shake {
  animation: shake .12s linear;
}

@keyframes shake {

  0%,
  100% {
    transform: translateX(0)
  }

  50% {
    transform: translateX(-1px)
  }
}

.text-input {
  background-color: transparent;
  border: 2px solid #11182744;
  outline: none;
  width: 100%;
  height: 8rem;
  margin: auto;
  resize: none;
  padding: .75rem 1rem;
  font-size: 1.05rem;
  border-radius: .5rem;
}

.text-input:focus {
  border-color: #111827aa;
}

.correct {
  color: var(--ok);
}

.incorrect {
  color: var(--err);
  text-decoration: underline;
}

/* Subcontrols and history */
.subcontrols {
  display: flex;
  gap: 1rem;
  align-items: flex-end;
  flex-wrap: wrap;
  margin: .25rem 0 .75rem;
}

.subcontrols .group {
  display: flex;
  flex-direction: column;
  gap: .25rem;
}

label {
  font-size: .85rem;
  color: var(--muted);
}

.select {
  appearance: none;
  border: 1px solid #11182729;
  background: #fff;
  border-radius: .5rem;
  padding: .4rem .6rem;
}

.segmented {
  display: flex;
  border: 1px solid #11182729;
  border-radius: .5rem;
  overflow: hidden;
  width: max-content;
}

.seg-btn {
  background: #fff;
  border: 0;
  padding: .4rem .75rem;
  cursor: pointer;
  color: #111827;
  font-weight: 600;
}

.seg-btn+.seg-btn {
  border-left: 1px solid #11182729;
}

.seg-btn.active {
  background: #111827;
  color: #fff;
}

.seg-btn:focus-visible {
  outline: 2px solid #0ea5e9;
  outline-offset: 2px;
}

.progress {
  padding: .45rem .6rem;
  background: #11182712;
  border: 1px solid #11182722;
  border-radius: .5rem;
  color: #111827;
  min-width: 8ch;
}

.history {
  margin-top: 1rem;
  background: rgba(255, 255, 255, .5);
  border: 1px solid var(--card-border);
  border-radius: .75rem;
  overflow: hidden;
}

.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: .6rem .8rem;
}

.history h2 {
  margin: 0;
  font-size: 1rem;
  color: #111827;
}

.history table {
  width: 100%;
  border-collapse: collapse;
  font-size: .95rem;
}

.history thead th {
  text-align: left;
  padding: .5rem .8rem;
  color: var(--muted);
  font-weight: 600;
  background: rgba(17, 24, 39, 0.05);
}

.history tbody td {
  padding: .55rem .8rem;
  border-top: 1px solid #11182714;
}

/* Error Heatmap */
.heatmap-section {
  margin-top: 1rem;
  background: rgba(255, 255, 255, .5);
  border: 1px solid var(--card-border);
  border-radius: .75rem;
  overflow: hidden;
}

.heatmap {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(28px, 1fr));
  gap: 6px;
  padding: .75rem;
}

.heatmap .cell {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: .8rem;
  border-radius: .4rem;
  padding: .4rem 0;
  border: 1px solid #11182710;
  background: #fff;
  color: #111827;
}

.heatmap .cell[data-val="0"] {
  background: #f8fafc;
  color: #94a3b8;
}

.heatmap .cell[data-val="1"] {
  background: #fee2e2;
}

.heatmap .cell[data-val="2"] {
  background: #fecaca;
}

.heatmap .cell[data-val="3"] {
  background: #fca5a5;
}

.heatmap .cell[data-val="4"] {
  background: #f87171;
  color: #fff;
}

.heatmap .cell[data-val="5"] {
  background: #ef4444;
  color: #fff;
}

.heatmap .cell[data-val="6"] {
  background: #dc2626;
  color: #fff;
}

.heatmap .cell[data-val="7"] {
  background: #b91c1c;
  color: #fff;
}

.heatmap .cell[data-val="8"] {
  background: #991b1b;
  color: #fff;
}

.heatmap .cell[data-val="9"] {
  background: #7f1d1d;
  color: #fff;
}

@media (max-width: 760px) {
  .score-container {
    grid-auto-flow: row;
    gap: .4rem .75rem;
  }

  .container {
    width: 96vw;
  }

  .history table {
    font-size: .85rem;
  }
}

/* Words & Custom groups visibility */
#wordsCountGroup,
#customTextGroup {
  display: none;
}

.show-inline {
  display: inline-flex !important;
}

.show-block {
  display: block !important;
}

/* Custom text area and summary chips */
.custom-text {
  display: flex;
  flex-direction: column;
  gap: .5rem;
}

.custom-area {
  width: 100%;
  border: 1px solid #11182729;
  border-radius: .5rem;
  padding: .6rem .7rem;
  background: #fff;
}

.custom-actions {
  display: flex;
  justify-content: flex-end;
}

.summary {

  /* Debug */
  .debug {
    margin-top: .5rem;
  }

  .debug-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: .5rem;
    padding: .5rem;
    border: 1px dashed #11182725;
    border-radius: .5rem;
    background: #ffffff80;
  }

  display: flex;
  gap: .5rem;
  flex-wrap: wrap;
}

.chip {
  background: #11182710;
  border: 1px solid #11182725;
  padding: .25rem .5rem;
  border-radius: .5rem;
  font-size: .8rem;
}

/* Themes */
/* System default already uses dark glass. */
[data-theme="light"] body {
  background: #eef2ff;
  color: #0b1221;
}

[data-theme="light"] .container {
  background: rgba(255, 255, 255, 0.9);
}

[data-theme="light"] .score-container {
  background: rgba(255, 255, 255, 0.7);
}

[data-theme="dark"] body {
  background: #0f172a;
}

[data-theme="amoled"] body {
  background: #000;
}

[data-theme="amoled"] .bg {
  filter: saturate(140%) blur(14px) brightness(0.9);
}
