/* exWriter — minimal, iA-Writer-inspired writing surface. */

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

/* Author display rules (flex on .sidebar, .editor-wrap, …) would
   otherwise override the hidden attribute's UA style. */
[hidden] { display: none !important; }

body {
  font-family: var(--font-ui);
  color: var(--color-text);
  background: var(--color-bg);
  display: flex;
  flex-direction: column;
  height: 100%;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
}

/* ── Topbar ─────────────────────────────────────────────────── */

.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.5rem 1rem;
  border-bottom: 1px solid var(--color-border);
  font-size: 0.85rem;
  transition: opacity 0.6s ease;
}

/* Chrome fades while the user types; any mouse movement restores it. */
.topbar--faded { opacity: 0.12; }
.topbar:hover { opacity: 1; }

.topbar__group {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  min-width: 0;
}

.topbar__group--file { position: relative; }

/* Recent-files dropdown, anchored under the file group. */
.recents {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  min-width: 18rem;
  max-width: 26rem;
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: 8px;
  box-shadow: 0 10px 32px rgba(0, 0, 0, 0.14);
  padding: 0.3rem;
  z-index: 9;
}

.recents__item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 0.45rem 0.6rem;
  border-radius: 6px;
}
.recents__item:hover { background: var(--color-surface); }

.recents__name { display: block; }

.recents__meta {
  display: block;
  color: var(--color-text-subtle);
  font-size: 0.72rem;
  margin-top: 0.1rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.recents__empty {
  padding: 0.5rem 0.6rem;
  color: var(--color-text-muted);
}

.topbar__filename {
  color: var(--color-text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 14rem;
}

.topbar__dirty {
  color: var(--color-accent);
  margin-left: -0.25rem;
}
/* Draft is safely in localStorage — still unsaved to a file, but a
   refresh loses nothing. */
.topbar__dirty--backed { color: var(--color-text-subtle); }

.topbar__btn {
  color: var(--color-text-muted);
  padding: 0.2rem 0.45rem;
  border-radius: 4px;
}
.topbar__btn:hover:not(:disabled) {
  color: var(--color-text);
  background: var(--color-surface);
}
.topbar__btn:disabled {
  color: var(--color-text-subtle);
  cursor: default;
}
.topbar__btn--ai:not(:disabled) { color: var(--color-accent); }
.topbar__btn--active {
  color: var(--color-text);
  background: var(--color-surface);
}
.topbar__btn--gear { font-size: 1rem; }

/* ── Workspace: editor + sidebar ────────────────────────────── */

.workspace {
  flex: 1;
  min-height: 0;
  display: flex;
}

body.show-settings .workspace { display: none; }
body.show-settings .topbar { display: none; }

.editor-col {
  flex: 1;
  min-width: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.editor,
.preview {
  width: 100%;
  max-width: var(--measure);
  margin: 0 auto;
  padding: 3.5rem 1.5rem 6rem;
  box-sizing: border-box;
  font-family: var(--font-editor);
  font-size: 1.05rem;
  line-height: 1.7;
}

/* The textarea sits on a transparent background over a metrics-identical
   backdrop div; AI findings are rendered as <mark>s in the backdrop so
   they show through under the (editable) text.

   The textarea auto-grows with its content (app.js) instead of
   scrolling internally, so the scroll container is the full-width
   .editor-col — putting the scrollbar at the far right of the window
   rather than at the edge of the centered column. */
.editor-wrap {
  flex: 1 0 auto;
  position: relative;
}

.editor {
  white-space: pre-wrap;
  overflow-wrap: break-word;
}

.editor--input {
  display: block;
  border: none;
  outline: none;
  resize: none;
  overflow: hidden; /* never scrolls itself — it grows */
  background: transparent;
  color: var(--color-text);
  caret-color: var(--color-accent);
  position: relative;
  z-index: 1;
}
.editor--input::placeholder { color: var(--color-text-subtle); }

.editor--backdrop {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
  pointer-events: none;
  color: transparent;
  z-index: 0;
}

.hl {
  color: transparent;
  border-radius: 3px;
}
.hl--issue { background: rgba(255, 178, 56, 0.32); }
.hl--issue.hl--focus { background: rgba(255, 148, 30, 0.55); }
.hl--review { background: rgba(31, 163, 255, 0.14); }

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .hl--issue { background: rgba(255, 178, 56, 0.26); }
  :root:not([data-theme="light"]) .hl--issue.hl--focus { background: rgba(255, 158, 40, 0.45); }
  :root:not([data-theme="light"]) .hl--review { background: rgba(74, 179, 255, 0.16); }
}

:root[data-theme="dark"] .hl--issue { background: rgba(255, 178, 56, 0.26); }
:root[data-theme="dark"] .hl--issue.hl--focus { background: rgba(255, 158, 40, 0.45); }
:root[data-theme="dark"] .hl--review { background: rgba(74, 179, 255, 0.16); }

.preview :is(h1, h2, h3) { line-height: 1.3; }
.preview code {
  background: var(--color-surface);
  padding: 0.1em 0.3em;
  border-radius: 3px;
  font-size: 0.9em;
}
.preview pre {
  background: var(--color-surface);
  padding: 0.8rem 1rem;
  border-radius: 6px;
  overflow-x: auto;
}
.preview pre code { background: none; padding: 0; }
.preview blockquote {
  margin: 0;
  padding-left: 1rem;
  border-left: 3px solid var(--color-border);
  color: var(--color-text-muted);
}
.preview a { color: var(--color-accent); }

/* ── Sidebar (AI results) ───────────────────────────────────── */

.sidebar {
  flex: 0 0 var(--sidebar-width);
  border-right: 1px solid var(--color-border);
  display: flex;
  flex-direction: column;
  min-height: 0;
  font-size: 0.85rem;
}

.sidebar__head {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.55rem 0.9rem;
  border-bottom: 1px solid var(--color-border);
}

.sidebar__title {
  font-weight: 600;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-size: 0.72rem;
}

.sidebar__close {
  color: var(--color-text-muted);
  font-size: 1.1rem;
  line-height: 1;
}
.sidebar__close:hover { color: var(--color-text); }

.sidebar__body {
  flex: 1;
  overflow-y: auto;
  padding: 0.9rem;
}

.sidebar__status {
  color: var(--color-text-muted);
  padding: 0.4rem 0;
}

.sidebar__notice {
  background: var(--color-warn-bg);
  border: 1px solid var(--color-warn-border);
  color: var(--color-warn-text);
  border-radius: 6px;
  padding: 0.5rem 0.7rem;
  margin-bottom: 0.8rem;
}

.sidebar__error {
  color: var(--color-error);
  padding: 0.4rem 0;
  white-space: pre-wrap;
  overflow-wrap: break-word;
}

.sidebar__actionlink {
  color: var(--color-accent);
  text-decoration: underline;
}

.sidebar__model {
  color: var(--color-text-subtle);
  font-size: 0.72rem;
  margin-bottom: 0.8rem;
}

/* Grammar issue cards */

.issue {
  border: 1px solid var(--color-border);
  border-radius: 8px;
  padding: 0.65rem 0.75rem;
  margin-bottom: 0.7rem;
  cursor: pointer;
}
.issue:hover { border-color: var(--color-accent); }
.issue--applied { opacity: 0.55; }

.issue__original {
  font-family: var(--font-editor);
  background: var(--color-del-bg);
  text-decoration: line-through;
  border-radius: 3px;
  padding: 0.05em 0.25em;
  overflow-wrap: break-word;
}

.issue__suggestion {
  font-family: var(--font-editor);
  background: var(--color-add-bg);
  border-radius: 3px;
  padding: 0.05em 0.25em;
  margin-top: 0.3rem;
  display: inline-block;
  overflow-wrap: break-word;
}

.issue__explanation {
  color: var(--color-text-muted);
  margin-top: 0.35rem;
  font-size: 0.78rem;
}

.issue__actions {
  margin-top: 0.5rem;
  display: flex;
  gap: 0.6rem;
  align-items: center;
}

.issue__apply {
  color: var(--color-accent);
  border: 1px solid var(--color-border);
  border-radius: 5px;
  padding: 0.15rem 0.6rem;
}
.issue__apply:hover:not(:disabled) { background: var(--color-surface); }
.issue__apply:disabled { color: var(--color-text-subtle); cursor: default; }

.issue__learn {
  color: var(--color-text-muted);
  border: 1px solid var(--color-border);
  border-radius: 5px;
  padding: 0.15rem 0.6rem;
}
.issue__learn:hover:not(:disabled) { background: var(--color-surface); color: var(--color-text); }
.issue__learn:disabled { color: var(--color-text-subtle); cursor: default; }

.issue__state { color: var(--color-text-muted); font-size: 0.75rem; }
.issue__state--ok { color: var(--color-ok); }
.issue__state--gone { color: var(--color-error); }

/* Review report */

.report {
  line-height: 1.55;
  overflow-wrap: break-word;
}
.report :is(h1, h2, h3) { font-size: 0.95rem; margin: 0.9rem 0 0.3rem; }
.report ul { padding-left: 1.1rem; }
.report a { color: var(--color-accent); }
.report code {
  background: var(--color-surface);
  padding: 0.05em 0.25em;
  border-radius: 3px;
}

.sidebar__stop {
  margin-top: 0.6rem;
  color: var(--color-error);
  border: 1px solid var(--color-border);
  border-radius: 5px;
  padding: 0.15rem 0.6rem;
}

/* Spinner */

.spinner {
  display: inline-block;
  width: 0.85em;
  height: 0.85em;
  border: 2px solid var(--color-border);
  border-top-color: var(--color-accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  vertical-align: -0.1em;
  margin-right: 0.4rem;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Save-as modal ──────────────────────────────────────────── */

.modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.25);
  z-index: 10;
}

.modal__box {
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: 10px;
  padding: 1.2rem 1.4rem;
  min-width: 20rem;
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.18);
}

.modal__label {
  font-size: 0.78rem;
  color: var(--color-text-muted);
}

.modal__input {
  font: inherit;
  font-family: var(--font-editor);
  color: var(--color-text);
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: 6px;
  padding: 0.5rem 0.65rem;
}
.modal__input:focus {
  outline: none;
  border-color: var(--color-accent);
}

.modal__buttons {
  display: flex;
  justify-content: flex-end;
  gap: 0.6rem;
}

/* ── Settings ───────────────────────────────────────────────── */

body.show-settings .settings { display: block; }

.settings {
  flex: 1;
  overflow-y: auto;
  padding: 2.5rem 1.5rem 4rem;
}

.settings__inner {
  max-width: 44rem;
  margin: 0 auto;
}

.settings__header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 1.5rem;
}

.settings__title { font-size: 1.25rem; font-weight: 600; margin: 0; }

.settings__back { color: var(--color-accent); font-size: 0.9rem; }

.settings__hint {
  color: var(--color-text-muted);
  font-size: 0.85rem;
  margin: 0.5rem 0 1.5rem;
}

.settings__lock {
  background: var(--color-warn-bg);
  border: 1px solid var(--color-warn-border);
  color: var(--color-warn-text);
  border-radius: 6px;
  padding: 0.6rem 0.9rem;
  margin-bottom: 1.2rem;
  font-size: 0.85rem;
}

.settings__empty { color: var(--color-text-muted); margin-bottom: 1rem; }

.settings__error { color: var(--color-error); font-size: 0.85rem; margin: 0.5rem 0; }

.themes { gap: 0.5rem; }

.theme-btn { color: var(--color-text-muted); }
.theme-btn[aria-pressed="true"] {
  color: var(--color-accent);
  border-color: var(--color-accent);
}

.settings__footer {
  margin-top: 2.5rem;
  padding-top: 1rem;
  border-top: 1px solid var(--color-border);
  color: var(--color-text-subtle);
  font-size: 0.78rem;
}
.settings__footer a { color: var(--color-text-muted); }
.settings__footer a:hover { color: var(--color-accent); }

.provider {
  border: 1px solid var(--color-border);
  border-radius: 8px;
  padding: 0.8rem 1rem;
  margin-bottom: 0.7rem;
}

.provider__head {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
}

.provider__name { font-weight: 600; }

.provider__model { color: var(--color-text-muted); font-size: 0.8rem; }

.badge {
  font-size: 0.68rem;
  border-radius: 999px;
  padding: 0.1rem 0.55rem;
  border: 1px solid var(--color-border);
  color: var(--color-text-muted);
}
.badge--active { color: var(--color-ok); border-color: var(--color-ok); }
.badge--web { color: var(--color-accent); border-color: var(--color-accent); }
.badge--env { color: var(--color-warn-text); border-color: var(--color-warn-border); }

.provider__actions {
  display: flex;
  gap: 0.7rem;
  margin-top: 0.55rem;
  font-size: 0.82rem;
}

.provider__action { color: var(--color-accent); }
.provider__action--danger { color: var(--color-error); }
.provider__action:hover { text-decoration: underline; }

.addform {
  border: 1px dashed var(--color-border);
  border-radius: 8px;
  padding: 1rem;
  margin-top: 1rem;
}

.addform__row {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  margin-bottom: 0.8rem;
}

.addform__row label {
  font-size: 0.78rem;
  color: var(--color-text-muted);
}

.addform__row :is(input, select) {
  font: inherit;
  color: var(--color-text);
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: 6px;
  padding: 0.45rem 0.6rem;
}
.addform__row :is(input, select):focus {
  outline: none;
  border-color: var(--color-accent);
}

.addform__buttons { display: flex; gap: 0.7rem; }

.btn {
  font-size: 0.85rem;
  border: 1px solid var(--color-border);
  border-radius: 6px;
  padding: 0.4rem 0.9rem;
}
.btn--primary {
  background: var(--color-accent);
  border-color: var(--color-accent);
  color: #fff;
}
.btn:hover { filter: brightness(0.96); }
