/* ========================================================================
   Editor HTML Inteligente · Estilos especificos da ferramenta
   Herda 100% do design system de /styles.css (cores, tipografia, scrollbar).
   Nenhum componente recriado: o textarea usa a scrollbar nativa do
   navegador, que ja e tematizada globalmente pelas regras de
   ::-webkit-scrollbar* em styles.css.
   ======================================================================== */

/* Wrapper que agrupa toolbar + split. Recebe a classe .expanded para
   ocupar quase toda a viewport (a largura/margem vem inline via JS). */
.tool-frame {
  margin-top: clamp(32px, 4vw, 48px);
  margin-bottom: clamp(40px, 5vw, 64px);
  display: block;
}

/* Toolbar da ferramenta */
.tool-toolbar {
  background: var(--cream-warm);
  border: 1px solid var(--gray-10);
  border-bottom: none;
  padding: 12px 18px;
  display: flex;
  align-items: center;
  gap: 12px;
  font-family: var(--sans);
  font-size: 13px;
  color: var(--gray-60);
  flex-wrap: wrap;
  border-radius: 2px 2px 0 0;
}
.tool-toolbar button {
  background: var(--green);
  color: var(--cream);
  border: 1px solid var(--green);
  padding: 7px 14px;
  border-radius: 3px;
  font-family: var(--sans);
  font-size: 13px;
  font-weight: 500;
  letter-spacing: 0.02em;
  cursor: pointer;
  transition: all 0.2s var(--ease);
}
.tool-toolbar button:hover { background: var(--green-dark); border-color: var(--green-dark); }
.tool-toolbar button.btn-secondary {
  background: transparent;
  color: var(--green);
  border-color: var(--green);
}
.tool-toolbar button.btn-secondary:hover { background: var(--green); color: var(--cream); }
.tool-toolbar button.btn-icon {
  background: transparent;
  color: var(--green);
  border: 1px solid var(--green);
  padding: 6px 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
}
.tool-toolbar button.btn-icon:hover {
  background: var(--green);
  color: var(--cream);
}
.tool-toolbar button.btn-icon svg { display: block; }
.tool-frame.expanded .tool-toolbar button.btn-icon .icon-expand,
.tool-frame:not(.expanded) .tool-toolbar button.btn-icon .icon-collapse {
  display: none;
}
.tool-toolbar label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  user-select: none;
  color: var(--gray-60);
  font-size: 13px;
}
.tool-toolbar label input[type=checkbox] {
  width: 14px; height: 14px;
  accent-color: var(--green);
  cursor: pointer;
}
.tool-toolbar .spacer { flex: 1; }
.tool-toolbar .status {
  font-size: 12px;
  color: var(--gray-60);
  font-style: italic;
  min-width: 200px;
  text-align: right;
}

/* Split principal: editor (textarea) a esquerda, preview (iframe) a direita */
.tool-split {
  display: flex;
  height: clamp(540px, 72vh, 820px);
  background: var(--cream);
  border: 1px solid var(--gray-10);
  border-radius: 0 0 2px 2px;
  overflow: hidden;
}
.tool-pane {
  flex: 1 1 0;
  min-width: 100px;
  position: relative;
  background: var(--cream);
}

/* Editor com numeros de linha + syntax highlighting.
   Layout: shell (flex) > gutter (numeros) + canvas (highlight pre + textarea).
   O textarea fica TRANSPARENTE por cima do <pre> highlighted, que mostra
   o codigo colorido. As 3 partes (gutter, highlight, textarea) sao
   rolados em sincronia. */

.editor-shell {
  position: absolute;
  inset: 0;
  display: flex;
  background: var(--cream);
  font-family: "JetBrains Mono", "Fira Code", Consolas, "Courier New", monospace;
  font-size: 13px;
  line-height: 1.55;
}

.editor-gutter {
  flex: 0 0 auto;
  min-width: 44px;
  padding: 14px 10px 14px 14px;
  background: var(--cream-warm);
  border-right: 1px solid var(--gray-10);
  color: var(--gray-40);
  text-align: right;
  user-select: none;
  overflow: hidden;
  position: relative;
}
.gutter-track {
  will-change: transform;
}
.gutter-track .lineno {
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum";
  font-size: 12px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: flex-end;
}

.editor-canvas {
  flex: 1 1 0;
  min-width: 0;
  position: relative;
  overflow: hidden;
}

/* Pre por tras do textarea: mesmo padding, mesma font, mesma quebra.
   Recebe o codigo com tags coloridas (spans .hl-*).
   Altura NATURAL (sem bottom: 0) pra acompanhar o conteudo. O canvas
   que clipa via overflow: hidden. O scroll vem do textarea via JS
   (sincroniza translateY). */
.editor-highlight {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  margin: 0;
  /* padding-right = 16 (padrao) + 10 (largura da scrollbar fina do
     textarea). Sem isso, a area util do textarea fica 10px menor e
     suas linhas quebram em pontos diferentes do highlight, causando
     desalinhamento progressivo em arquivos grandes. */
  padding: 14px 26px 14px 16px;
  font-family: inherit;
  font-size: inherit;
  line-height: inherit;
  color: var(--off-black);
  background: transparent;
  white-space: pre-wrap;
  word-break: break-word;
  overflow: visible;
  pointer-events: none;
  tab-size: 2;
  will-change: transform;
}
.editor-highlight .hl-line { display: block; min-height: 1em; }
.editor-highlight .hl-tag { color: var(--green); font-weight: 600; }
.editor-highlight .hl-attr { color: var(--gold-dark); }
.editor-highlight .hl-str { color: #6B5A3A; }
.editor-highlight .hl-comment { color: var(--gray-40); font-style: italic; }
.editor-highlight .hl-doctype { color: var(--gray-60); }

/* Textarea: ocupa toda a area do canvas. Texto TRANSPARENTE pra deixar
   o pre highlighted aparecer atras; cursor (caret) visivel em verde.
   A scrollbar nativa herda os tokens do design system. */
textarea#editor {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 14px 16px;
  border: 0;
  outline: 0;
  resize: none;
  background: transparent;
  color: transparent;
  caret-color: var(--green);
  font-family: inherit;
  font-size: inherit;
  line-height: inherit;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-x: hidden;
  overflow-y: auto;
  tab-size: 2;
  scrollbar-width: thin;
  scrollbar-color: var(--gold-light) var(--cream-warm);
}
/* Selecao do textarea visivel mesmo com texto transparente. */
textarea#editor::selection { background: rgba(176, 141, 87, 0.35); color: transparent; }
textarea#editor::-moz-selection { background: rgba(176, 141, 87, 0.35); color: transparent; }

#preview-frame {
  width: 100%;
  height: 100%;
  border: 0;
  background: var(--cream);
  display: block;
}

/* Gutter (divisor arrastavel) */
.tool-gutter {
  width: 8px;
  background: var(--cream-warm);
  border-left: 1px solid var(--gray-10);
  border-right: 1px solid var(--gray-10);
  cursor: col-resize;
  flex: 0 0 8px;
  position: relative;
  transition: background 0.2s;
}
.tool-gutter:hover { background: var(--gold-light); }
.tool-gutter::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 2px;
  height: 36px;
  background: var(--gold);
  border-radius: 2px;
  transform: translate(-50%, -50%);
}

/* Estado expandido: width/margin definidos inline pelo JS */
.tool-frame.expanded {
  margin-top: clamp(32px, 4vw, 48px);
  margin-bottom: clamp(40px, 5vw, 64px);
  padding: 0 clamp(12px, 2vw, 24px);
}
.tool-frame.expanded .tool-split {
  height: clamp(640px, 88vh, 1200px);
}
.container:has(> .tool-frame.expanded) {
  overflow: visible;
}

/* Responsivo: em mobile, empilha vertical.
   Ordem invertida: preview (visualizacao final) em cima, editor (codigo)
   embaixo. column-reverse reverte a ordem visual sem mexer no DOM
   (paneL = editor vem primeiro no HTML; paneR = preview vem por ultimo).
   Botao de expandir/contrair fica oculto em mobile (full-bleed nao faz
   sentido em tela pequena, ja ocupa toda a largura util).

   ALINHAMENTO CRITICO: em mobile, desativamos word-wrap no textarea E no
   highlight (white-space: pre). Cada linha logica vira UMA linha visual
   unica. Sem wrap, nao ha como textarea e <pre> discordarem sobre onde
   quebrar uma linha — eliminamos a fonte de divergencia entre o que o
   usuario VE no highlight e onde o cursor cai no textarea. Linhas longas
   ganham scroll horizontal nos dois (overflow-x: auto), com scrollbar
   tematizada pelo design system. */
@media (max-width: 720px) {
  .tool-split { flex-direction: column-reverse; height: clamp(720px, 110vh, 1100px); }
  .tool-pane { min-height: 100px; }
  #pane-left { flex: 1 1 0; }
  #pane-right { flex: 0 0 50%; }
  .tool-gutter { width: 100%; height: 10px; cursor: row-resize; flex: 0 0 10px; touch-action: none; }
  .tool-gutter::after { width: 36px; height: 2px; }
  .tool-toolbar .status { display: none; }
  .tool-toolbar { padding: 10px 14px; }
  .tool-toolbar #btn-expand { display: none; }
  /* Sem wrap em mobile — cada linha logica = 1 linha visual */
  .editor-highlight {
    white-space: pre;
    word-break: normal;
    overflow-x: hidden;
    padding-right: 16px;
  }
  .editor-highlight .hl-line {
    white-space: pre;
  }
  textarea#editor {
    white-space: pre;
    word-break: normal;
    overflow-x: auto;
  }
}
