/* ---------- temas ---------- */
:root,
[data-theme="serika"] {
  --bg: #323437; --main: #e2b714; --caret: #e2b714; --sub: #646669; --sub-alt: #2c2e31;
  --text: #d1d0c5; --error: #ca4754; --error-extra: #7e2a33;
}
[data-theme="escuro"] {
  --bg: #111111; --main: #eeeeee; --caret: #eeeeee; --sub: #4a4a4a; --sub-alt: #1b1b1b;
  --text: #eeeeee; --error: #da3333; --error-extra: #791717;
}
[data-theme="claro"] {
  --bg: #f2efe6; --main: #d17b0f; --caret: #d17b0f; --sub: #a8a296; --sub-alt: #e6e2d6;
  --text: #3b3a36; --error: #c63a3a; --error-extra: #8f2a2a;
}
[data-theme="nord"] {
  --bg: #2e3440; --main: #88c0d0; --caret: #d8dee9; --sub: #5e6779; --sub-alt: #272c36;
  --text: #e5e9f0; --error: #bf616a; --error-extra: #793e44;
}
[data-theme="dracula"] {
  --bg: #282a36; --main: #bd93f9; --caret: #f8f8f2; --sub: #6272a4; --sub-alt: #20222c;
  --text: #f8f8f2; --error: #ff5555; --error-extra: #a33d3d;
}
[data-theme="matrix"] {
  --bg: #000000; --main: #15ff00; --caret: #15ff00; --sub: #006500; --sub-alt: #031a00;
  --text: #d1ffcd; --error: #da3333; --error-extra: #791717;
}
[data-theme="rosa"] {
  --bg: #f8e5e5; --main: #d96a8a; --caret: #d96a8a; --sub: #c9a3ad; --sub-alt: #f0d4d8;
  --text: #5a3d46; --error: #b8323f; --error-extra: #7d2730;
}

/* ---------- base ---------- */
* { box-sizing: border-box; margin: 0; }

html, body { min-height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Lexend Deca', system-ui, sans-serif;
  font-weight: 300;
  transition: background .2s, color .2s;
}

button {
  font: inherit;
  background: none;
  border: none;
  color: var(--sub);
  cursor: pointer;
  padding: .35rem .5rem;
  border-radius: .4rem;
  transition: color .15s, background .15s;
}
button:hover, button:focus-visible { color: var(--text); outline: none; }
button.active { color: var(--main); }

kbd {
  background: var(--sub);
  color: var(--bg);
  padding: .1rem .35rem;
  border-radius: .25rem;
  font-family: 'Roboto Mono', monospace;
  font-size: .7rem;
}

#app {
  min-height: 100vh;
  max-width: 1100px;
  margin: 0 auto;
  padding: 1.25rem 2rem 1.25rem;
  display: grid;
  grid-template-rows: auto 1fr auto;
  /* minmax(0, 1fr): conteúdo largo não estica a página no celular */
  grid-template-columns: minmax(0, 1fr);
  gap: 1rem;
}

/* ---------- topo ---------- */
header { display: flex; align-items: center; gap: 2rem; }

.logo { display: flex; align-items: center; gap: .6rem; color: var(--main); user-select: none; }
.logo div { display: flex; flex-direction: column; line-height: 1; }
.logo small { font-size: .6rem; color: var(--sub); margin-left: .1rem; }
.logo span { font-size: 1.6rem; color: var(--text); }

nav { display: flex; align-items: center; gap: .25rem; }

/* menu "sugeridos": outros sites de digitação */
#suggest { position: relative; }
#suggest-menu {
  position: absolute;
  top: calc(100% + .4rem);
  left: 0;
  z-index: 20;
  width: min(560px, calc(100vw - 2rem));
  background: var(--sub-alt);
  border: 1px solid var(--bg);
  border-radius: .5rem;
  box-shadow: 0 10px 30px rgb(0 0 0 / .3);
  padding: .75rem;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: .25rem 1rem;
}
#suggest-menu .title {
  grid-column: 1 / -1;
  color: var(--sub);
  font-size: .7rem;
  text-transform: lowercase;
  letter-spacing: .05em;
  margin-top: .4rem;
  padding: 0 .4rem;
}
#suggest-menu .title:first-child { margin-top: 0; }
#suggest-menu a {
  display: block;
  padding: .4rem;
  border-radius: .35rem;
  text-decoration: none;
  color: var(--text);
}
#suggest-menu a:hover, #suggest-menu a:focus-visible { background: var(--bg); outline: none; }
#suggest-menu .name { font-size: .9rem; }
#suggest-menu .name::after { content: ' ↗'; color: var(--sub); font-size: .75rem; }
#suggest-menu .tag {
  font-size: .6rem;
  color: var(--bg);
  background: var(--main);
  border-radius: .25rem;
  padding: 0 .3rem;
  margin-left: .35rem;
  vertical-align: middle;
}
#suggest-menu .desc { display: block; color: var(--sub); font-size: .72rem; line-height: 1.35; margin-top: .1rem; }
@media (max-width: 700px) {
  /* no celular o menu ocupa a largura da tela, com rolagem própria se precisar */
  #suggest-menu {
    position: fixed;
    top: var(--menu-top, 4.5rem);
    left: 1rem;
    right: 1rem;
    width: auto;
    max-height: calc(100vh - var(--menu-top, 4.5rem) - 1rem);
    overflow-y: auto;
    grid-template-columns: 1fr;
  }
}

/* esconde a interface enquanto digita, como no monkeytype */
nav, #config-wrap, footer, #test-actions { transition: opacity .3s; }
body.typing nav,
body.typing #config-wrap,
body.typing footer,
body.typing #test-actions { opacity: 0; }

main { display: flex; flex-direction: column; justify-content: center; }

/* ---------- barra de configuração ---------- */
/* pílula de resumo; a barra completa aparece por cima ao passar o mouse */
#config-wrap {
  position: relative;
  display: flex;
  justify-content: center;
  margin-bottom: 2rem;
  z-index: 5;
}
#config-summary {
  display: flex;
  align-items: center;
  gap: .45rem;
  font-size: .75rem;
  background: var(--sub-alt);
  border-radius: 999px;
  padding: .25rem .8rem;
}
#config-summary svg { flex: none; }
#config {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translate(-50%, -4px);
  opacity: 0;
  visibility: hidden;
  transition: opacity .15s, transform .15s, visibility 0s .15s;
  box-shadow: 0 8px 24px rgb(0 0 0 / .25);
  width: max-content;
  max-width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  background: var(--sub-alt);
  border-radius: .4rem;
  padding: .1rem .5rem;
  font-size: .75rem;
}
#config-wrap:hover #config,
#config-wrap:focus-within #config,
#config-wrap.open #config {
  opacity: 1;
  visibility: visible;
  transform: translate(-50%, 0);
  transition: opacity .15s, transform .15s;
}

/* menu suspenso de linguagens */
#lang-picker { position: relative; }
#lang-toggle { color: var(--main); }
#lang-menu {
  position: absolute;
  top: calc(100% + .3rem);
  left: 50%;
  transform: translateX(-50%);
  background: var(--sub-alt);
  border: 1px solid var(--bg);
  border-radius: .4rem;
  box-shadow: 0 8px 24px rgb(0 0 0 / .3);
  padding: .3rem;
  display: grid;
  grid-template-columns: auto auto;
  gap: 0 .6rem;
  white-space: nowrap;
  z-index: 10;
}
#lang-menu .col { display: flex; flex-direction: column; align-items: stretch; }
#lang-menu .title { color: var(--sub); opacity: .7; font-size: .65rem; padding: .2rem .4rem; }
#lang-menu button { text-align: left; }
#config button { padding: .2rem .4rem; }
#config .group { display: flex; }
#config .section { display: contents; }
#config .row { display: flex; flex-wrap: wrap; justify-content: center; align-items: center; }
/* segunda linha (modo trecho): separada por um fio fino */
#cfg-snippet { border-top: 1px solid var(--bg); width: 100%; }
#cats { flex-wrap: wrap; justify-content: center; }
#comments { align-items: center; }
#comments .label { color: var(--sub); opacity: .7; padding: 0 .3rem; }
.sep { width: 3px; height: .9rem; background: var(--bg); border-radius: 2px; margin: 0 .35rem; }

/* ---------- área de digitação ---------- */
#test { position: relative; }

#counter-row {
  display: flex;
  align-items: baseline;
  gap: 1rem;
  height: 2.2rem;
  padding-left: .5rem;
}
#counter {
  color: var(--main);
  font-family: 'Roboto Mono', monospace;
  font-size: 1.6rem;
}
#snippet-title { color: var(--sub); font-size: .9rem; }

#caps {
  position: absolute;
  top: -2.5rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--main);
  color: var(--bg);
  padding: .4rem .9rem;
  border-radius: .5rem;
  font-size: .85rem;
}

#words-wrapper {
  position: relative;
  font-family: 'Roboto Mono', monospace;
  font-size: 1.6rem;
  height: 6em; /* 3 linhas de 2em */
  overflow: hidden;
  cursor: text;
}

#words {
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  user-select: none;
  transition: filter .25s, opacity .25s;
}

.word {
  margin: .25em .3em;
  line-height: 1.5em;
  height: 1.5em;
}
/* modo trecho: fonte menor e mais linhas visíveis */
#words-wrapper.snippet { font-size: 1.3rem; height: 12em; }
#words .br { flex-basis: 100%; height: 0; }
/* comentários: itálico e mais apagados, para separar explicação de código */
.word.comment { font-style: italic; }
.word.comment .letter:not(.correct):not(.incorrect) { opacity: .55; }
.word.comment .letter.correct { opacity: .7; }
/* comentário no modo "pular": só para ler, o cursor passa direto */
#words .comment-line {
  flex-basis: 100%;
  margin: .25em .3em;
  line-height: 1.5em;
  color: var(--sub);
  font-style: italic;
  opacity: .75;
  user-select: none;
}

.word.eol::after { content: '↵'; color: var(--sub); opacity: .45; margin-left: .2em; }

.word.error {
  text-decoration: underline 2px var(--error);
  text-underline-offset: .3em;
}

.letter { color: var(--sub); }
.letter.correct { color: var(--text); }
.letter.incorrect { color: var(--error); }
.letter.extra { color: var(--error-extra); }

#caret {
  position: absolute;
  top: 0;
  left: 0;
  width: .1em;
  height: 1.2em;
  margin-left: -.05em;
  background: var(--caret);
  border-radius: 2px;
  pointer-events: none;
  transition: transform .1s ease-out;
  z-index: 1;
}
#caret.blink { animation: blink 1s infinite; }
@keyframes blink { 0%, 100% { opacity: 1; } 50% { opacity: 0; } }

#focus-warning {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Lexend Deca', sans-serif;
  font-size: 1rem;
  color: var(--text);
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s;
}
#words-wrapper.blurred #words { filter: blur(5px); opacity: .4; }
#words-wrapper.blurred #caret { opacity: 0; }
#words-wrapper.blurred #focus-warning { opacity: 1; }

#input {
  position: absolute;
  top: 0;
  left: 0;
  width: 1px;
  height: 1px;
  opacity: 0;
  border: 0;
  padding: 0;
  font-size: 16px; /* evita zoom no iOS */
  pointer-events: none;
}

#test-actions { display: flex; align-items: center; justify-content: center; gap: .5rem; margin: 2.5rem auto 0; }
#restart { padding: .6rem 1.4rem; }
#loop-toggle { padding: .6rem .8rem; border-radius: .5rem; transition: color .15s, background .15s; }
#loop-toggle.active { color: var(--main); background: var(--sub-alt); }
#loop-options { display: flex; gap: .15rem; background: var(--sub-alt); border-radius: .4rem; padding: .15rem .3rem; font-size: .8rem; }
#loop-options button { padding: .2rem .45rem; font-size: .8rem; font-family: 'Roboto Mono', monospace; }
#loop-bar { display: flex; align-items: center; justify-content: center; gap: 1rem; margin-top: 1.5rem; padding: .6rem 1.2rem; background: var(--sub-alt); border-radius: .5rem; font-size: .85rem; }
#loop-label { color: var(--main); font-family: 'Roboto Mono', monospace; }
#loop-cancel { background: none; color: var(--sub); padding: .3rem .7rem; }
#loop-cancel:hover { color: var(--error); }

/* ---------- resultado ---------- */
.result-top {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 2rem;
  align-items: center;
}
.big-stats { display: flex; flex-direction: column; gap: 1rem; }

.stat .label { color: var(--sub); font-size: 1rem; }
.stat .value { color: var(--main); font-family: 'Roboto Mono', monospace; font-size: 2rem; line-height: 1.2; }
.stat .value.big { font-size: 4rem; line-height: 1; }
.stat .value.small { font-size: 1rem; line-height: 1.4; }

.chart-box { position: relative; height: 220px; min-width: 0; }
#chart { width: 100%; height: 100%; display: block; cursor: crosshair; }

#tooltip {
  position: absolute;
  top: 0;
  background: var(--sub-alt);
  color: var(--text);
  padding: .45rem .65rem;
  border-radius: .4rem;
  font-family: 'Roboto Mono', monospace;
  font-size: .75rem;
  line-height: 1.5;
  pointer-events: none;
  white-space: nowrap;
}

.more-stats { display: flex; flex-wrap: wrap; gap: 1.5rem 3rem; margin-top: 2rem; }

#r-pb { margin-top: 1.5rem; color: var(--main); text-align: center; font-size: 1.1rem; }

.result-actions { display: flex; justify-content: center; gap: 1rem; margin-top: 2rem; }
.result-actions button,
#clear-history { background: var(--sub-alt); padding: .6rem 1.2rem; }

/* ---------- histórico ---------- */
#view-stats { align-self: stretch; }
#view-stats h2 { font-weight: 400; font-size: 1.1rem; color: var(--sub); margin: 2.5rem 0 1rem; }
#view-stats h2 small { font-size: .8rem; margin-left: .5rem; opacity: .8; }

.summary { display: flex; flex-wrap: wrap; gap: 1.5rem 3rem; }

#pb-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: .75rem; }
.pb-card { background: var(--sub-alt); border-radius: .5rem; padding: .8rem 1rem; text-align: center; }
.pb-card .label { color: var(--sub); font-size: .8rem; }
.pb-card .value { color: var(--main); font-family: 'Roboto Mono', monospace; font-size: 1.8rem; }
.pb-card .detail { color: var(--sub); font-size: .75rem; }

.table-wrap { overflow-x: auto; }
#history { width: 100%; border-collapse: collapse; font-family: 'Roboto Mono', monospace; font-size: .85rem; }
#history th { color: var(--sub); font-weight: 400; text-align: left; padding: .5rem .75rem; }
#history td { padding: .5rem .75rem; white-space: nowrap; }
#history tbody tr:nth-child(odd) { background: var(--sub-alt); }
#history td:first-child { color: var(--main); }

#history-empty { color: var(--sub); margin: 1rem 0; }
#clear-history { margin-top: 1.5rem; }

/* ---------- rodapé ---------- */
footer {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  color: var(--sub);
  font-size: .75rem;
}
select {
  background: var(--sub-alt);
  color: var(--text);
  border: none;
  border-radius: .3rem;
  padding: .3rem .5rem;
  margin-left: .4rem;
  font: inherit;
  cursor: pointer;
}

/* ---------- janela de valor personalizado ---------- */
#custom-modal,
#import-modal {
  position: fixed;
  inset: 0;
  background: rgb(0 0 0 / .5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 10;
}
.modal {
  background: var(--bg);
  border-radius: .75rem;
  padding: 1.5rem;
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  gap: .8rem;
  box-shadow: 0 10px 40px rgb(0 0 0 / .4);
}
.modal h3 { font-weight: 400; color: var(--sub); font-size: 1.1rem; }
.modal input {
  background: var(--sub-alt);
  color: var(--text);
  border: 2px solid transparent;
  border-radius: .5rem;
  padding: .6rem .8rem;
  font: 1.2rem 'Roboto Mono', monospace;
  outline: none;
}
.modal input:focus { border-color: var(--main); }
.modal .hint { color: var(--sub); font-size: .8rem; line-height: 1.5; }
.modal-actions { display: flex; justify-content: flex-end; gap: .5rem; }
.modal-actions button { background: var(--sub-alt); padding: .5rem 1.1rem; }
.modal-actions .primary { background: var(--main); color: var(--bg); }

/* ---------- janela de importar texto ---------- */
.modal.wide { max-width: 560px; max-height: calc(100vh - 2rem); overflow-y: auto; }
.modal #import-title { font-size: .95rem; }
.modal textarea {
  background: var(--sub-alt);
  color: var(--text);
  border: 2px solid transparent;
  border-radius: .5rem;
  padding: .6rem .8rem;
  font: .85rem/1.5 'Roboto Mono', monospace;
  min-height: 11rem;
  resize: vertical;
  outline: none;
}
.modal textarea:focus { border-color: var(--main); }
.import-row { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: .5rem; color: var(--sub); font-size: .8rem; }
.import-row button { background: var(--sub-alt); padding: .4rem .9rem; }
.modal .hint.error { color: var(--error); }
#import-list { border-top: 1px solid var(--sub-alt); padding-top: .8rem; font-size: .85rem; }
#import-list .title { color: var(--sub); font-size: .75rem; margin-bottom: .3rem; }
#import-list .item { display: flex; align-items: center; gap: .5rem; }
#import-list .name { flex: 1; min-width: 0; text-align: left; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#import-list .name:hover, #import-list .name:focus-visible { color: var(--main); }
#import-list .info { color: var(--sub); font-size: .72rem; white-space: nowrap; }
#import-list .remove:hover, #import-list .remove:focus-visible { color: var(--error); }

[hidden] { display: none !important; }

/* ---------- celular ---------- */
@media (max-width: 700px) {
  #app { padding: 1rem; }
  header { gap: .5rem 1rem; flex-wrap: wrap; }
  nav button { padding: .3rem .4rem; }
  .logo span { font-size: 1.5rem; }
  #config-wrap { margin-bottom: 1.5rem; }
  .sep { display: none; }
  #words-wrapper { font-size: 1.25rem; }
  .result-top { grid-template-columns: 1fr; }
  .big-stats { flex-direction: row; gap: 3rem; }
  .stat .value.big { font-size: 3rem; }
}
