/* ==========================================================================
   WritePadPro — embeddable widgets
   1. The widget page inside the iframe (/widget/{tool}, pages/widget.php):
      everything is scoped to html.wpp-widget. System fonts only, no images,
      no external resources. The text box grows to fill whatever height the
      embedding site gives the iframe.
   2. The gallery on /widgets (pages/widgets.php): .wgal-* (uses the site's
      style.css tokens).
   ========================================================================== */

/* ── 1. Widget page: colours ── */
html.wpp-widget {
    --w-bg: #ffffff;
    --w-surface: #f8fafc;
    --w-line: #e2e8f0;
    --w-field: #64748b;
    --w-text: #0f172a;
    --w-muted: #475569;
    --w-accent: #4338ca;
    --w-ok: #047857;
    --w-warn: #b45309;
    --w-bad: #b91c1c;
    --w-track: #e2e8f0;
    color-scheme: light;
}
html.wpp-widget[data-theme="dark"] {
    --w-bg: #0f172a;
    --w-surface: #1e293b;
    --w-line: #334155;
    --w-field: #64748b;
    --w-text: #f1f5f9;
    --w-muted: #a3b1c6;
    --w-accent: #a5b4fc;
    --w-ok: #34d399;
    --w-warn: #fbbf24;
    --w-bad: #f87171;
    --w-track: #334155;
    color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
    html.wpp-widget[data-theme="auto"] {
        --w-bg: #0f172a;
        --w-surface: #1e293b;
        --w-line: #334155;
        --w-field: #64748b;
        --w-text: #f1f5f9;
        --w-muted: #a3b1c6;
        --w-accent: #a5b4fc;
        --w-ok: #34d399;
        --w-warn: #fbbf24;
        --w-bad: #f87171;
        --w-track: #334155;
        color-scheme: dark;
    }
}

/* ── Widget page: layout ── */
html.wpp-widget,
html.wpp-widget *,
html.wpp-widget *::before,
html.wpp-widget *::after {
    box-sizing: border-box;
}
html.wpp-widget {
    height: 100%;
    background: var(--w-bg);
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}
.wpp-widget body {
    margin: 0;
    min-height: 100%;
    display: flex;
    flex-direction: column;
    padding: 12px;
    background: var(--w-bg);
    color: var(--w-text);
    border: 1px solid var(--w-line);
    border-radius: 12px;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    font-size: 14px;
    line-height: 1.45;
    overflow-wrap: break-word;
}
.wpp-widget [hidden] {
    display: none !important;
}
.wpw {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    gap: 10px;
    min-width: 0;
}
.wpw-field {
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-width: 0;
}
.wpw-field--grow {
    flex: 1 1 auto;
}
.wpw-label {
    margin: 0;
    font-size: 13px;
    font-weight: 600;
    line-height: 1.35;
    color: var(--w-text);
}

/* Inputs: 16px text so phones do not zoom in on focus */
.wpw-text,
.wpw-input,
.wpw-select {
    width: 100%;
    min-width: 0;
    margin: 0;
    font-family: inherit;
    font-size: 16px;
    color: var(--w-text);
    background: var(--w-bg);
    border: 1px solid var(--w-field);
    border-radius: 8px;
}
.wpw-text {
    flex: 1 1 96px;
    min-height: 96px;
    padding: 10px 12px;
    line-height: 1.5;
    resize: none;
}
.wpw-input,
.wpw-select {
    height: 40px;
    padding: 0 10px;
    line-height: 1.2;
}
.wpw-text::placeholder,
.wpw-input::placeholder {
    color: var(--w-muted);
    opacity: 1;
}
.wpw-text:focus-visible,
.wpw-input:focus-visible,
.wpw-select:focus-visible,
.wpw-foot a:focus-visible {
    outline: 2px solid var(--w-accent);
    outline-offset: 2px;
}
.wpw-input[aria-invalid="true"] {
    border-color: var(--w-bad);
}

/* Two fields side by side (words + paper) */
.wpw-row2 {
    display: flex;
    flex-wrap: wrap;
    gap: 8px 10px;
}
.wpw-row2 > .wpw-field--grow {
    flex: 1 1 140px;
}
.wpw-row2 > .wpw-field:not(.wpw-field--grow) {
    flex: 0 1 130px;
}

/* Label + select on one line */
.wpw-inline {
    display: flex;
    align-items: center;
    gap: 8px;
}
.wpw-inline > .wpw-label {
    flex: none;
}
.wpw-inline > .wpw-select {
    flex: 1 1 auto;
    width: auto;
}

/* Result cards: value above the label (DOM order is label, value) */
.wpw-stats {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    margin: 0;
}
@media (min-width: 520px) {
    .wpw-stats--4 {
        grid-template-columns: repeat(4, minmax(0, 1fr));
    }
}
.wpw-stat {
    display: flex;
    flex-direction: column-reverse;
    justify-content: flex-end;
    gap: 2px;
    min-width: 0;
    padding: 8px 10px;
    background: var(--w-surface);
    border: 1px solid var(--w-line);
    border-radius: 8px;
}
.wpw-stat dt {
    font-size: 12px;
    line-height: 1.3;
    color: var(--w-muted);
}
.wpw-stat dd {
    margin: 0;
    font-size: 18px;
    line-height: 1.2;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    overflow-wrap: anywhere;
}
.wpw-unit {
    font-size: 14px;
    font-weight: 600;
}

/* Result rows (reading time) */
.wpw-rows {
    margin: 0;
    background: var(--w-surface);
    border: 1px solid var(--w-line);
    border-radius: 8px;
}
.wpw-row {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    justify-content: space-between;
    gap: 0 12px;
    padding: 8px 12px;
}
.wpw-row + .wpw-row {
    border-top: 1px solid var(--w-line);
}
.wpw-row dt {
    font-size: 13px;
    color: var(--w-muted);
}
.wpw-row dd {
    margin: 0;
    font-size: 16px;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

/* Limit meter (character counter) */
.wpw-meter {
    display: grid;
    gap: 4px;
}
.wpw-meter__head {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    gap: 0 10px;
    font-size: 13px;
}
.wpw-meter__name {
    font-weight: 600;
}
.wpw-meter__count {
    color: var(--w-muted);
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
}
.wpw-meter__bar {
    height: 8px;
    border-radius: 4px;
    background: var(--w-track);
    overflow: hidden;
}
.wpw-meter__fill {
    display: block;
    width: 0;
    height: 100%;
    background: var(--w-ok);
    transition: width 0.15s ease;
}
.wpw-meter.is-near .wpw-meter__fill {
    background: var(--w-warn);
}
.wpw-meter.is-over .wpw-meter__fill {
    background: var(--w-bad);
}
.wpw-meter__state {
    margin: 0;
    font-size: 12px;
    color: var(--w-muted);
    font-variant-numeric: tabular-nums;
}
.wpw-meter.is-over .wpw-meter__count,
.wpw-meter.is-over .wpw-meter__state {
    color: var(--w-bad);
    font-weight: 700;
}

/* Notes, errors, footer */
.wpw-note {
    margin: 0;
    font-size: 12px;
    line-height: 1.4;
    color: var(--w-muted);
}
.wpw-note a {
    color: var(--w-accent);
}
.wpw-err {
    margin: -4px 0 0;
    font-size: 13px;
    font-weight: 600;
    color: var(--w-bad);
}
.wpw-foot {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    gap: 2px 12px;
    margin-top: 10px;
    font-size: 12px;
    line-height: 1.4;
    color: var(--w-muted);
}
.wpw-foot a {
    color: var(--w-accent);
    font-weight: 600;
    text-decoration: underline;
    text-underline-offset: 2px;
    border-radius: 2px;
}
.wpw-sr {
    position: absolute !important;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    border: 0;
    overflow: hidden;
    clip: rect(0 0 0 0);
    clip-path: inset(50%);
    white-space: nowrap;
}
@media (prefers-reduced-motion: reduce) {
    .wpw-meter__fill {
        transition: none;
    }
}

/* ── 2. Gallery on /widgets ── */
.wgal-list {
    display: grid;
    gap: 28px;
    margin: 28px 0 8px;
}
.wgal-card {
    min-width: 0;
    padding: 22px;
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    scroll-margin-top: calc(var(--nav-h) + 16px);
}
.wgal-card > h2 {
    font-size: 1.35rem;
    font-weight: 800;
    color: var(--text);
    margin: 0 0 6px;
}
.wgal-card > p {
    color: var(--text2);
    margin-bottom: 16px;
}
.wgal-grid {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 20px;
}
@media (min-width: 980px) {
    .wgal-grid {
        grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr);
        align-items: start;
    }
}
.wgal-preview {
    min-width: 0;
    padding: 14px;
    background: var(--bg2);
    border: 1px dashed var(--border-strong);
    border-radius: var(--radius-sm);
    overflow: hidden;
}
.wgal-preview__label,
.wgal-code-label {
    display: block;
    font-size: 0.75rem;
    font-weight: 700;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--text2);
    margin-bottom: 8px;
}
.wgal-preview iframe {
    display: block;
    width: 100%;
    border: 0;
    border-radius: 12px;
    background: transparent;
}
.wgal-options {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
    gap: 10px;
    min-width: 0;
    margin: 0 0 14px;
    padding: 0;
    border: 0;
}
.wgal-options legend {
    padding: 0;
    margin-bottom: 8px;
    font-size: 0.75rem;
    font-weight: 700;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--text2);
}
.wgal-field {
    min-width: 0;
}
.wgal-field label {
    display: block;
    margin-bottom: 4px;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text);
}
.wgal-field select {
    width: 100%;
    min-width: 0;
    padding: 8px 10px;
    font: inherit;
    font-size: 0.95rem;
    color: var(--text);
    background: var(--input-bg);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
}
.wgal-code {
    display: block;
    width: 100%;
    min-height: 168px;
    padding: 10px 12px;
    font-family: var(--font-mono);
    font-size: 0.8rem;
    line-height: 1.55;
    color: var(--text);
    background: var(--input-bg);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    resize: vertical;
    white-space: pre-wrap;
    word-break: break-all;
}
.wgal-field select:focus-visible,
.wgal-code:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 1px;
}
.wgal-actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px 16px;
    margin-top: 12px;
}
.wgal-actions .btn {
    cursor: pointer;
    border: 0;
}
.wgal-meta {
    margin: 10px 0 0;
    font-size: 0.875rem;
    color: var(--text2);
}
.wgal-meta a {
    color: var(--primary);
    font-weight: 600;
}
@media (max-width: 640px) {
    .wgal-card {
        padding: 16px;
    }
    .wgal-preview {
        padding: 10px;
    }
}
@media print {
    .wgal-preview,
    .wgal-options,
    .wgal-actions {
        display: none !important;
    }
}
