/* Theme variables.
   - Default (no data-theme): light.
   - data-theme="auto": follow OS via prefers-color-scheme.
   - data-theme="light": force light.
   - data-theme="dark": force dark.
   The JS sets data-theme on <html> based on the user's saved choice (default: "auto"). */

:root, :root[data-theme="light"] {
  --bg: #f6f3ec;
  --surface: #fffdf7;
  --ink: #1a1a18;
  --ink-soft: #5a5853;
  --ink-faint: #8a877f;
  --rule: #e4dfd2;
  --rule-soft: #efeadd;
  --accent: #1f4068;
  --accent-soft: #e8eef6;
  --warn: #b46523;
  --good: #2d6a3e;
  --bad: #993b2c;
  --serif: "Fraunces", Georgia, serif;
  --sans: "Inter", system-ui, sans-serif;
  --radius: 10px;
}

:root[data-theme="dark"] {
  --bg: #131312;
  --surface: #1d1d1b;
  --ink: #ece9e1;
  --ink-soft: #aba89e;
  --ink-faint: #7a7770;
  --rule: #2c2c29;
  --rule-soft: #232321;
  --accent: #9ec2ee;
  --accent-soft: #1d2a3c;
  --warn: #d99a5b;
  --good: #7fc28f;
  --bad: #e69789;
}

@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] {
    --bg: #131312;
    --surface: #1d1d1b;
    --ink: #ece9e1;
    --ink-soft: #aba89e;
    --ink-faint: #7a7770;
    --rule: #2c2c29;
    --rule-soft: #232321;
    --accent: #9ec2ee;
    --accent-soft: #1d2a3c;
    --warn: #d99a5b;
    --good: #7fc28f;
    --bad: #e69789;
  }
}

* { box-sizing: border-box; }

/* The [hidden] attribute must beat any later `display: grid/flex/block` rules.
   Without this, .modal[hidden] still renders because `display: grid` from the
   .modal rule wins over the user-agent default of `[hidden] { display: none }`. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

button, input, select { font-family: inherit; font-size: inherit; color: inherit; }

/* ------- Gate ------- */
.gate { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.gate__inner { max-width: 380px; width: 100%; text-align: center; }
.gate__mark {
  font-family: var(--serif); font-style: italic; font-size: 18px;
  color: var(--ink-faint); letter-spacing: 0.02em; margin-bottom: 36px;
}
.gate__title { font-family: var(--serif); font-weight: 500; font-size: 36px; line-height: 1.1; margin: 0 0 8px; }
.gate__sub { color: var(--ink-soft); margin: 0 0 28px; font-size: 14px; }
.gate__form { display: flex; gap: 8px; }
.gate__form input {
  flex: 1; padding: 12px 14px; border: 1px solid var(--rule);
  background: var(--surface); border-radius: var(--radius);
  outline: none; letter-spacing: 0.05em;
}
.gate__form input:focus { border-color: var(--accent); }
.gate__form button {
  padding: 12px 18px; background: var(--ink); color: var(--bg);
  border: none; border-radius: var(--radius); cursor: pointer; font-weight: 500;
}
.gate__form button:hover { opacity: 0.88; }
.gate__error { color: var(--bad); font-size: 13px; margin-top: 14px; }

/* ------- App ------- */
.app { max-width: 820px; margin: 0 auto; padding: 32px 24px 80px; }
.app__header {
  display: flex; justify-content: space-between; align-items: flex-start;
  gap: 16px; padding-bottom: 18px; border-bottom: 1px solid var(--rule); margin-bottom: 14px;
}
.app__brand { font-family: var(--serif); font-weight: 500; font-size: 26px; letter-spacing: -0.01em; }
.app__sub { font-size: 13px; color: var(--ink-soft); margin-top: 4px; }
.app__actions { display: flex; gap: 8px; flex-wrap: wrap; }
.app__provider-badge {
  font-size: 12px;
  color: var(--ink-faint);
  margin-bottom: 18px;
  display: flex;
  gap: 6px;
  align-items: center;
}
.app__provider-badge .dot {
  display: inline-block; width: 6px; height: 6px; border-radius: 50%;
  background: var(--good);
}
.app__provider-badge .name {
  color: var(--ink-soft);
  font-weight: 500;
}

.btn, .select, .input {
  padding: 7px 12px; background: var(--surface); border: 1px solid var(--rule);
  border-radius: 7px; cursor: pointer; font-size: 13px;
  transition: border-color 120ms, background 120ms;
}
.btn:hover, .select:hover, .input:hover { border-color: var(--ink-faint); }
.input { cursor: text; width: 100%; }
.input--num { width: 90px; }
.btn--ghost { background: transparent; }
.btn--primary { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.btn--primary:hover { opacity: 0.88; border-color: var(--ink); }
.btn--small { padding: 5px 10px; font-size: 12px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.select {
  appearance: none; padding-right: 28px;
  background-image: linear-gradient(45deg, transparent 45%, var(--ink-soft) 45%, var(--ink-soft) 55%, transparent 55%);
  background-position: right 10px center; background-size: 8px; background-repeat: no-repeat;
}

.filters { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 20px; }
.chip {
  font-size: 12px; padding: 4px 10px; border: 1px solid var(--rule);
  border-radius: 999px; cursor: pointer; background: var(--surface);
  color: var(--ink-soft); transition: all 120ms;
}
.chip:hover { border-color: var(--ink-faint); }
.chip[aria-pressed="true"] {
  background: var(--accent-soft); color: var(--accent); border-color: var(--accent);
}

/* Scope toggle: segmented control sitting between the provider badge and filters */
.scope {
  display: inline-flex;
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 3px;
  gap: 2px;
  margin-bottom: 14px;
}
.scope__btn {
  font-size: 13px;
  padding: 6px 14px;
  border: none;
  border-radius: 7px;
  background: transparent;
  color: var(--ink-soft);
  cursor: pointer;
  font-family: inherit;
  transition: background 120ms, color 120ms;
  white-space: nowrap;
}
.scope__btn:hover { color: var(--ink); }
.scope__btn[aria-pressed="true"] {
  background: var(--ink);
  color: var(--bg);
  font-weight: 500;
}

.sort-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 14px;
  font-size: 13px;
}
.sort-row__label {
  color: var(--ink-faint);
  font-size: 12px;
}
.sort-row .select { padding: 5px 28px 5px 10px; font-size: 12px; }

/* ------- Post card ------- */
.posts { display: flex; flex-direction: column; gap: 12px; }
.card {
  background: var(--surface); border: 1px solid var(--rule);
  border-radius: var(--radius); padding: 18px 20px;
}
.card__head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; margin-bottom: 10px; }

/* Meta line: engagement numbers on the left, subreddit+age right-aligned and muted */
.card__meta {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  margin-bottom: 6px;
  font-size: 13px;
}
.card__meta-primary {
  display: flex;
  align-items: baseline;
  gap: 12px;
  flex-wrap: wrap;
}
.card__engagement {
  font-weight: 500;
  color: var(--ink);
}
.card__engagement-sec {
  color: var(--ink-soft);
}
.card__meta-secondary {
  font-size: 12px;
  color: var(--ink-faint);
  white-space: nowrap;
  flex-shrink: 0;
}

.card__title { font-family: var(--serif); font-weight: 500; font-size: 19px; line-height: 1.25; margin: 0; }
.card__title a {
  color: inherit; text-decoration: none;
  background-image: linear-gradient(currentColor, currentColor);
  background-size: 0% 1px; background-position: 0 95%; background-repeat: no-repeat;
  transition: background-size 200ms;
}
.card__title a:hover { background-size: 100% 1px; }

.rising-badge {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  padding: 2px 7px;
  border-radius: 4px;
  color: var(--warn);
  background: transparent;
  border: 1px solid var(--warn);
  white-space: nowrap;
  cursor: help;
}

.delta {
  font-weight: 500;
  font-size: 12px;
  margin-left: 2px;
  cursor: help;
  white-space: nowrap;
}
.delta--up   { color: var(--good); }
.delta--down { color: var(--bad); }
.delta--flat { color: var(--ink-faint); }

.ratings { display: flex; gap: 10px; flex-shrink: 0; }
.rating { text-align: center; min-width: 48px; }
.rating__val { font-family: var(--serif); font-weight: 500; font-size: 22px; line-height: 1; }
.rating__lbl { font-size: 10px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--ink-faint); margin-top: 2px; }
.rating--high .rating__val { color: var(--good); }
.rating--mid .rating__val { color: var(--warn); }
.rating--low .rating__val { color: var(--ink-faint); }

.summary { font-size: 14px; line-height: 1.6; color: var(--ink-soft); margin: 10px 0; }
.summary strong { color: var(--ink); font-weight: 500; }

.take {
  background: var(--rule-soft); border-left: 2px solid var(--accent);
  padding: 10px 14px; margin: 12px 0; font-size: 13.5px; line-height: 1.6;
}
.take__label { font-size: 10px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--accent); font-weight: 500; margin-bottom: 4px; }
.take__edit { font-size: 11px; color: var(--ink-faint); float: right; cursor: pointer; text-decoration: underline dotted; }
.take__edit:hover { color: var(--ink); }
.take__textarea {
  width: 100%;
  min-height: 90px;
  resize: vertical;
  font-family: var(--sans);
  font-size: 13.5px;
  line-height: 1.6;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: 7px;
  padding: 8px 10px;
  margin-top: 2px;
  box-sizing: border-box;
}
.take__textarea:focus { outline: none; border-color: var(--accent); }
.take__edit-actions {
  display: flex;
  justify-content: flex-end;
  gap: 6px;
  margin-top: 8px;
}

.tags { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; margin-top: 12px; font-size: 11px; }
.tag { background: var(--rule-soft); color: var(--ink-soft); padding: 3px 8px; border-radius: 4px; }
.card__icons { margin-left: auto; display: flex; gap: 10px; color: var(--ink-faint); }
.card__icons button {
  background: none; border: none; cursor: pointer; padding: 2px;
  color: inherit; font-size: 15px; line-height: 1;
}
.card__icons button:hover { color: var(--ink); }
.card__icons .on { color: var(--warn); }

.empty { text-align: center; color: var(--ink-soft); padding: 60px 20px; }
.app__footer { margin-top: 28px; font-size: 12px; color: var(--ink-faint); text-align: center; }

/* ------- Modal ------- */
.modal {
  position: fixed; inset: 0; z-index: 50;
  display: grid; place-items: center;
  padding: 20px;
}
.modal__backdrop {
  position: absolute; inset: 0;
  background: rgba(0,0,0,0.4);
  backdrop-filter: blur(2px);
}
.modal__card {
  position: relative;
  background: var(--bg);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  width: 100%;
  max-width: 640px;
  max-height: calc(100vh - 40px);
  display: flex;
  flex-direction: column;
  box-shadow: 0 10px 40px rgba(0,0,0,0.2);
  overflow: hidden;
}
.modal__card--narrow { max-width: 480px; }
.modal__head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 16px 20px; border-bottom: 1px solid var(--rule);
}
.modal__title { font-family: var(--serif); font-weight: 500; font-size: 20px; margin: 0; }
.modal__close {
  background: none; border: none; cursor: pointer;
  color: var(--ink-soft); font-size: 18px; line-height: 1; padding: 4px 8px;
}
.modal__close:hover { color: var(--ink); }
.modal__body { padding: 20px; overflow-y: auto; flex: 1; }
.modal__foot {
  display: flex; gap: 8px; align-items: center; justify-content: flex-end;
  padding: 14px 20px; border-top: 1px solid var(--rule);
  font-size: 12px; color: var(--ink-soft);
}

.section { margin-bottom: 28px; }
.section:last-child { margin-bottom: 0; }
.section__title { font-family: var(--serif); font-weight: 500; font-size: 16px; margin: 0 0 4px; }
.section__hint { font-size: 12px; color: var(--ink-faint); margin: 0 0 12px; }
.section__hint code {
  background: var(--rule-soft); padding: 1px 5px; border-radius: 3px;
  font-family: ui-monospace, monospace; font-size: 11px;
}
.row { display: flex; gap: 12px; flex-wrap: wrap; }
.row label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--ink-soft); }

.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 14px; }
.field__label { font-size: 12px; color: var(--ink-soft); font-weight: 500; }
.field__hint { font-size: 11px; color: var(--ink-faint); }
.field--inline { flex-direction: row; align-items: center; gap: 8px; font-size: 13px; }

/* Providers list */
.providers { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.provider {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  position: relative;
}
.provider--active {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.provider--disabled { opacity: 0.55; }
.provider__main { flex: 1; min-width: 0; }
.provider__name { font-weight: 500; font-size: 14px; display: flex; align-items: center; gap: 8px; }
.provider__name .pill {
  font-size: 10px; padding: 1px 7px; border-radius: 999px;
  background: var(--rule-soft); color: var(--ink-soft);
  text-transform: uppercase; letter-spacing: 0.05em; font-weight: 500;
}
.provider__name .pill--active { background: var(--accent); color: var(--bg); }
.provider__sub { font-size: 12px; color: var(--ink-faint); margin-top: 2px; }
.provider__actions { display: flex; gap: 6px; flex-shrink: 0; }

.provider__name .pill--warn {
  background: transparent;
  border: 1px solid var(--bad);
  color: var(--bad);
}
.provider__name .pill--ok {
  background: transparent;
  border: 1px solid var(--good);
  color: var(--good);
}

.readonly {
  font-size: 13px;
  font-family: ui-monospace, "JetBrains Mono", monospace;
  color: var(--ink-soft);
  background: var(--rule-soft);
  padding: 7px 12px;
  border-radius: 7px;
  border: 1px solid var(--rule);
}

.callout {
  background: var(--rule-soft);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-top: 12px;
}
.callout__title {
  font-size: 12px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--ink-soft);
  margin-bottom: 8px;
}
.callout__body {
  font-size: 13px;
  color: var(--ink-soft);
  margin: 0 0 8px;
  line-height: 1.5;
}
.callout__code {
  font-family: ui-monospace, "JetBrains Mono", monospace;
  font-size: 12px;
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: 6px;
  padding: 10px 12px;
  margin: 0 0 10px;
  white-space: pre;
  overflow-x: auto;
  line-height: 1.7;
  color: var(--ink);
}
.callout__hint {
  font-size: 12px;
  color: var(--ink-faint);
  margin: 0 0 8px;
}
.callout code {
  font-family: ui-monospace, "JetBrains Mono", monospace;
  font-size: 11px;
  background: var(--surface);
  padding: 1px 5px;
  border-radius: 3px;
}

#pe-secret-cmd {
  font-family: ui-monospace, "JetBrains Mono", monospace;
  font-size: 11px;
  background: var(--rule-soft);
  padding: 2px 6px;
  border-radius: 4px;
  word-break: break-all;
}

.status-good { color: var(--good); }
.status-bad  { color: var(--bad); }
.status-pending { color: var(--ink-faint); }

@media (max-width: 600px) {
  .app { padding: 20px 16px 60px; }
  .app__header { flex-direction: column; }
  .app__brand { font-size: 22px; }
  .card { padding: 14px 16px; }
  .card__title { font-size: 17px; }
  .ratings { gap: 6px; }
  .rating { min-width: 42px; }
  .modal { padding: 10px; }
  .modal__card { max-height: calc(100vh - 20px); }
  .modal__body { padding: 16px; }
  .row { flex-direction: column; }
  .input--num { width: 100%; }
  .provider { flex-direction: column; align-items: flex-start; }
  .provider__actions { width: 100%; justify-content: flex-end; }
  .scope { width: 100%; }
  .scope__btn { flex: 1; padding: 6px 8px; font-size: 12px; }
  /* Stack engagement and context on narrow screens instead of squishing them */
  .card__meta { flex-direction: column; align-items: flex-start; gap: 2px; }
  .card__meta-secondary { white-space: normal; }
}
