:root {
  --bg: #f7f7f5;
  --fg: #1a1a1a;
  --muted: #6b6b6b;
  --border: #e0e0dc;
  --accent: #2b6cb0;
  --del-bg: #ffe3e3;
  --del-fg: #9b2c2c;
  --ins-bg: #d7f5dd;
  --ins-fg: #22543d;
  --radius: 10px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  line-height: 1.5;
}

.wrap {
  max-width: 760px;
  margin: 6vh auto;
  padding: 0 24px;
}
.wrap.wide { max-width: 1180px; }
/* Duża rozdzielczość: edytor dokumentu na (prawie) całą szerokość zamiast wąskiej kolumny. */
@media (min-width: 1400px) { .wrap.wide { max-width: 94vw; } }

.masthead h1 {
  font-size: 2.4rem;
  margin: 0 0 0.3rem;
  letter-spacing: -0.02em;
}
.masthead p { color: var(--muted); margin: 0 0 1.5rem; }

/* --- Formularz --- */
.field-label { display: block; font-weight: 600; margin-bottom: 0.4rem; }

.text-input {
  width: 100%;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 0.95rem;
  background: #fff;
  resize: vertical;
}

.tools {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin: 20px 0;
  background: #fff;
}
.tools legend { font-weight: 600; padding: 0 6px; }

.tool-option {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 2px 10px;
  align-items: baseline;
  padding: 10px 6px;
  border-top: 1px solid var(--border);
}
.tool-option:first-of-type { border-top: none; }
.tool-option input { grid-row: span 2; }
.tool-name { font-weight: 600; }
.tool-desc { color: var(--muted); font-size: 0.9rem; }

.btn-primary {
  appearance: none;
  border: none;
  background: var(--accent);
  color: #fff;
  font-size: 1rem;
  font-weight: 600;
  padding: 12px 22px;
  border-radius: var(--radius);
  cursor: pointer;
}
.btn-primary:hover { background: #245a93; }

.actions {
  display: flex;
  gap: 16px;
  align-items: center;
  margin-bottom: 1.2rem;
}
.btn-secondary {
  display: inline-block;
  color: var(--accent);
  text-decoration: none;
  font-weight: 600;
}
.actions form { margin: 0; }
.actions .btn-secondary {
  appearance: none;
  border: 1px solid var(--accent);
  background: #fff;
  border-radius: var(--radius);
  padding: 6px 14px;
  cursor: pointer;
  font-size: 0.9rem;
}
.checked-note { color: var(--muted); font-size: 0.9rem; display: inline-flex; align-items: center; gap: 6px; }

/* Podświetlenie oflagowanych zmian + inline popover (styl Google Docs) */
.flagged-mark {
  background: #fff3cd;
  border-bottom: 2px solid #d4a017;
  border-radius: 2px;
  padding: 0 1px;
  position: relative;
  cursor: pointer;
}
.mark-tools {
  display: none;
  position: absolute;
  left: 0;
  top: 100%;
  z-index: 20;
  margin-top: 4px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
  padding: 8px;
  width: max-content;
  max-width: 360px;
  white-space: normal;
  font-family: ui-sans-serif, system-ui, sans-serif;
  font-size: 0.82rem;
}
.flagged-mark:hover .mark-tools,
.flagged-mark:focus-within .mark-tools { display: block; }
.mark-reason { color: #8a6d3b; margin-bottom: 6px; }
.mark-tools form { display: inline-block; margin: 0 4px 0 0; }
.mark-btn {
  appearance: none;
  border: 1px solid var(--border);
  background: #fff;
  border-radius: 6px;
  padding: 3px 9px;
  font-size: 0.8rem;
  cursor: pointer;
}
.mark-btn.ok { border-color: #bfe6c8; color: var(--ins-fg); }
.mark-btn.suggest { border-color: #bfe6c8; background: var(--ins-bg); color: var(--ins-fg); font-weight: 600; }
.mark-btn.reject { border-color: #f0d0d0; color: var(--del-fg); }

/* Sekcja "Do przeglądu" */
.review {
  margin-top: 28px;
  padding: 16px 18px;
  background: #fffbf0;
  border: 1px solid #f0d8a0;
  border-radius: var(--radius);
}
.review h2 { margin: 0 0 6px; font-size: 1.1rem; color: #8a6d3b; }
.review .edit-list { max-height: 420px; overflow-y: auto; }
.edit-suggestion { font-size: 0.85rem; margin-top: 4px; }
.edit-suggestion ins { background: var(--ins-bg); color: var(--ins-fg); text-decoration: none; border-radius: 3px; padding: 0 3px; }
.edit-actions { display: flex; gap: 8px; margin-top: 6px; }
.edit-toggle.suggest { color: var(--ins-fg); border-color: #bfe6c8; font-weight: 600; }

/* --- Panele wynikowe --- */
.panels {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}
@media (max-width: 860px) { .panels { grid-template-columns: 1fr; } }

.panel {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.panel-title {
  margin: 0;
  padding: 12px 16px;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
}

.text-pane {
  margin: 0;
  padding: 16px;
  white-space: pre-wrap;
  word-wrap: break-word;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 0.92rem;
  line-height: 1.7;
}

/* --- Edytor "popraw ręcznie": split edytor | podgląd na żywo --- */
.md-split {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  align-items: stretch;
}
.md-split .text-input { min-height: 420px; }
.md-preview {
  margin: 0;
  padding: 16px;
  overflow-y: auto;
  max-height: 600px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #fff;
  font-size: 0.95rem;
  line-height: 1.7;
}
.md-preview > :first-child { margin-top: 0; }
.md-preview h1, .md-preview h2, .md-preview h3 { line-height: 1.3; margin: 1.2em 0 0.5em; }
.md-preview pre {
  background: var(--code-bg, #f4f4f5);
  padding: 12px;
  border-radius: var(--radius);
  overflow-x: auto;
}
.md-preview code { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 0.9em; }
.md-preview blockquote {
  margin: 1em 0;
  padding-left: 14px;
  border-left: 3px solid var(--border);
  color: var(--muted);
}
.md-preview .anchor { display: none; }
@media (max-width: 760px) {
  .md-split { grid-template-columns: 1fr; }
}

.diff del, .edit del {
  background: var(--del-bg);
  color: var(--del-fg);
  text-decoration: line-through;
  border-radius: 3px;
}
.diff ins, .edit ins {
  background: var(--ins-bg);
  color: var(--ins-fg);
  text-decoration: none;
  border-radius: 3px;
}

/* --- Koszt LLM (telemetria per przebieg) --- */
.ai-cost { margin-top: 28px; }
.ai-cost-note { font-weight: 400; font-size: 0.8rem; color: var(--muted); }
.ai-cost-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
  margin-top: 10px;
}
.ai-cost-table th, .ai-cost-table td {
  text-align: left;
  padding: 6px 12px;
  border-bottom: 1px solid var(--border);
}
.ai-cost-table td:nth-child(n+3), .ai-cost-table th:nth-child(n+3) { text-align: right; }
.ai-cost-table .ai-cost-total td { font-weight: 600; border-bottom: 2px solid var(--border); }

/* --- Licznik znalezione/zastosowane/pominięte --- */
.stats {
  display: flex;
  gap: 10px;
  margin-top: 28px;
  flex-wrap: wrap;
}
.stat {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 16px;
  font-size: 0.9rem;
  color: var(--muted);
}
.stat strong { color: var(--fg); font-size: 1.05rem; }
.stat.applied { border-color: #bfe6c8; background: var(--ins-bg); }
.stat.applied strong { color: var(--ins-fg); }
.stat.skipped { border-color: #f0d0d0; background: var(--del-bg); }
.stat.skipped strong { color: var(--del-fg); }
.stat.flagged { border-color: #f0d8a0; background: #fff7e6; }
.stat.flagged strong { color: #8a6d3b; }

/* --- Lista poprawek --- */
.revisions { margin-top: 20px; }
.revision {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 12px;
  padding: 4px 16px;
}
.revision summary {
  cursor: pointer;
  padding: 10px 0;
  display: flex;
  gap: 12px;
  align-items: baseline;
  list-style: none;
}
.revision summary::-webkit-details-marker { display: none; }
.rev-caret { color: var(--muted); transition: transform 0.15s; display: inline-block; }
.revision[open] .rev-caret { transform: rotate(90deg); }
.rev-step { font-weight: 700; }
.rev-tool { color: var(--accent); font-weight: 600; }
.rev-count { color: var(--muted); font-size: 0.85rem; margin-left: auto; }

.edit-list { list-style: none; margin: 0; padding: 0 0 10px; }
.edit {
  padding: 10px 0;
  border-top: 1px solid var(--border);
  font-size: 0.9rem;
}
.edit-change { font-family: ui-monospace, Menlo, monospace; }
.edit-reason { color: var(--muted); font-size: 0.85rem; margin-top: 4px; }
.edit-note { color: #9b2c2c; font-size: 0.8rem; margin-top: 4px; }
.edit.skipped { opacity: 0.6; }
.edit.flagged { border-left: 3px solid #d4a017; padding-left: 10px; }
.edit-flag { color: #8a6d3b; font-size: 0.8rem; margin-top: 4px; font-weight: 600; }
.edit.rejected { opacity: 0.55; border-left: 3px solid #9b2c2c; padding-left: 10px; }
.edit.rejected .edit-change ins { background: none; color: var(--muted); text-decoration: line-through; }
.edit-toggle {
  appearance: none;
  margin-top: 6px;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--del-fg);
  border-radius: 6px;
  padding: 3px 10px;
  font-size: 0.78rem;
  cursor: pointer;
}
.edit-toggle.is-rejected { color: var(--ins-fg); border-color: #bfe6c8; }
.stat.rejected-stat { border-color: #f0d0d0; background: var(--del-bg); }
.stat.rejected-stat strong { color: var(--del-fg); }

/* --- Przetwarzanie w tle --- */
.processing {
  display: flex;
  align-items: center;
  gap: 12px;
  background: #fff7e6;
  border: 1px solid #f0d8a0;
  color: #8a6d3b;
  border-radius: var(--radius);
  padding: 14px 18px;
  margin-bottom: 20px;
}
.spinner {
  width: 18px;
  height: 18px;
  border: 3px solid #e0c48a;
  border-top-color: #8a6d3b;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  flex: none;
}
@keyframes spin { to { transform: rotate(360deg); } }

.processing-head { display: flex; align-items: center; gap: 10px; }
.processing-head .muted { color: var(--muted); font-weight: 400; font-size: 0.85rem; }
.progress-bar {
  height: 8px;
  background: #f0e6cc;
  border-radius: 6px;
  overflow: hidden;
  margin: 10px 0 8px;
}
.progress-fill {
  height: 100%;
  background: #d4a017;
  border-radius: 6px;
  transition: width 0.4s ease;
}
.progress-counts { font-size: 0.85rem; color: #8a6d3b; }
.progress-counts .pc { margin-right: 14px; }

/* --- Ponowny przebieg --- */
.rerun {
  margin-top: 28px;
  padding: 16px 18px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.rerun-hint { color: var(--muted); font-size: 0.9rem; margin: 0 0 12px; }

/* --- Flash --- */
.flash {
  max-width: 1180px;
  margin: 16px auto 0;
  padding: 12px 24px;
}
.flash-notice { color: var(--ins-fg); }
.flash-alert { color: var(--del-fg); }

/* --- Inline suggest (ADR-0010): popover/marker na zaznaczeniu w podglądzie --- */
#md_preview [data-sourcepos]::selection { background: #cfe3f7; }
.inline-suggest-popover {
  position: fixed;
  z-index: 50;
  width: 320px;
  max-width: calc(100vw - 24px);
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.14);
  padding: 12px;
  font-size: 0.9rem;
}
.inline-suggest-popover .is-row { display: flex; gap: 8px; align-items: flex-start; }
.inline-suggest-popover .is-comment {
  flex: 1; resize: vertical; padding: 8px; border: 1px solid var(--border);
  border-radius: 8px; font: inherit; color: var(--fg);
}
.inline-suggest-popover .is-mic {
  flex: 0 0 auto; width: 36px; height: 36px; border: 1px solid var(--border);
  border-radius: 8px; background: var(--bg); cursor: pointer; font-size: 1rem;
}
.inline-suggest-popover .is-mic.recording { background: var(--del-bg); border-color: var(--del-fg); animation: is-pulse 1s infinite; }
@keyframes is-pulse { 50% { opacity: 0.55; } }
.inline-suggest-popover .is-actions { display: flex; gap: 8px; margin-top: 10px; }
.inline-suggest-popover .is-cancel {
  background: none; border: 1px solid var(--border); border-radius: 8px;
  padding: 6px 12px; cursor: pointer; color: var(--muted);
}
.inline-suggest-popover .btn-primary { padding: 6px 14px; font-size: 0.88rem; }
.inline-suggest-popover .is-note { color: var(--muted); margin-bottom: 8px; font-style: italic; }
.inline-suggest-popover .is-suggestion {
  background: var(--ins-bg); color: var(--ins-fg); border-radius: 8px;
  padding: 8px; white-space: pre-wrap; max-height: 220px; overflow: auto;
}
.inline-suggest-popover .is-error { color: var(--del-fg); }
.inline-suggest-popover .is-pending { color: var(--muted); display: flex; align-items: center; gap: 8px; }
.inline-suggest-popover .is-spinner {
  width: 14px; height: 14px; border: 2px solid var(--border);
  border-top-color: var(--accent); border-radius: 50%; animation: is-spin 0.7s linear infinite;
}
@keyframes is-spin { to { transform: rotate(360deg); } }

/* --- Zwijane sekcje (details/summary) na stronie dokumentu --- */
details.panel > summary,
details.review > summary,
details.revisions > summary {
  cursor: pointer;
  list-style: none;
  user-select: none;
}
details.panel > summary::-webkit-details-marker,
details.review > summary::-webkit-details-marker,
details.revisions > summary::-webkit-details-marker { display: none; }
details.panel > summary::before,
details.review > summary::before,
details.revisions > summary::before {
  content: "▸ ";
  color: var(--muted);
  display: inline-block;
  transition: transform 0.12s;
}
details.panel[open] > summary::before,
details.review[open] > summary::before,
details.revisions[open] > summary::before { content: "▾ "; }
.section-summary { font-size: 1.05rem; font-weight: 600; margin: 16px 0 8px; }
.editor-actions { display: flex; gap: 10px; align-items: center; margin-top: 12px; }
.save-status { font-size: 0.85rem; }
.save-status.ok { color: var(--ins-fg); }
.save-status.err { color: var(--del-fg); }
details.review, details.revisions { margin: 12px 0; }
/* Panele zwinięte: nie rozciągaj się na 2 kolumny gdy details zamknięty */
.panels details.panel { align-self: flex-start; }
