/* ---- tokens ------------------------------------------------------------ */

:root {
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --primary-soft: #eff6ff;
    --on-primary: #ffffff;
    --text: #0f172a;
    --text-muted: #64748b;
    --bg: #f8fafc;
    --card: #ffffff;
    --border: #e2e8f0;
    --border-strong: #cbd5e1;
    --dup-bg: #fef3c7;
    --dup-border: #f59e0b;
    --dup-text: #78350f;
    --success: #16a34a;
    --error: #dc2626;
    --error-soft: #fef2f2;
    --warn-text: #92400e;
    --shadow: 0 1px 2px rgb(15 23 42 / 0.04), 0 8px 24px rgb(15 23 42 / 0.06);
    --shadow-pop: 0 4px 12px rgb(15 23 42 / 0.08), 0 16px 40px rgb(15 23 42 / 0.14);
    --radius: 14px;
    --radius-sm: 9px;
    --font: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
    --font-numbers: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, "Noto Naskh Arabic", sans-serif;
    color-scheme: light;
}

:root[data-theme="dark"] {
    --primary: #3b82f6;
    --primary-hover: #60a5fa;
    --primary-soft: #172554;
    --text: #e2e8f0;
    --text-muted: #94a3b8;
    --bg: #0b1120;
    --card: #131c2e;
    --border: #243049;
    --border-strong: #334155;
    --dup-bg: #453209;
    --dup-border: #f59e0b;
    --dup-text: #fde68a;
    --success: #22c55e;
    --error: #f87171;
    --error-soft: #3b1219;
    --warn-text: #fcd34d;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.3), 0 8px 24px rgb(0 0 0 / 0.25);
    --shadow-pop: 0 4px 12px rgb(0 0 0 / 0.4), 0 16px 40px rgb(0 0 0 / 0.5);
    color-scheme: dark;
}

/* ---- base -------------------------------------------------------------- */

*,
*::before,
*::after {
    box-sizing: border-box;
}

[hidden] {
    display: none !important;
}

html {
    -webkit-text-size-adjust: 100%;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 15px/1.5 var(--font);
    transition: background-color 0.2s ease, color 0.2s ease;
}

h1,
h2,
h3,
p {
    margin: 0;
}

button,
input {
    font: inherit;
    color: inherit;
}

code,
kbd {
    font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
    font-size: 0.92em;
    padding: 1px 6px;
    border-radius: 5px;
    background: var(--bg);
    border: 1px solid var(--border);
}

:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

.icon {
    width: 18px;
    height: 18px;
    flex: none;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.sprite {
    position: absolute;
    width: 0;
    height: 0;
    overflow: hidden;
}

.sr-only {
    position: fixed;
    top: 0;
    left: 0;
    width: 1px;
    height: 1px;
    opacity: 0;
}

.muted {
    color: var(--text-muted);
}

.skip-link {
    position: absolute;
    left: 16px;
    top: -60px;
    z-index: 50;
    padding: 8px 14px;
    border-radius: var(--radius-sm);
    background: var(--primary);
    color: var(--on-primary);
    text-decoration: none;
}

.skip-link:focus {
    top: 12px;
}

/* ---- layout ------------------------------------------------------------ */

.app {
    max-width: 1080px;
    margin: 0 auto;
    padding: 20px 24px 32px;
}

.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
}

.brand {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
}

.brand-mark {
    display: grid;
    place-items: center;
    width: 36px;
    height: 36px;
    border-radius: 10px;
    background: var(--primary);
    color: var(--on-primary);
}

.brand h1 {
    font-size: 1.2rem;
    font-weight: 650;
    letter-spacing: -0.01em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 4px;
}

.tagline {
    margin: 18px 0 16px;
    color: var(--text-muted);
    font-size: 1rem;
}

.app-footer {
    margin-top: 28px;
    text-align: center;
    color: var(--text-muted);
    font-size: 0.85rem;
}

/* ---- buttons ----------------------------------------------------------- */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 38px;
    padding: 0 14px;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    background: var(--card);
    font-weight: 550;
    cursor: pointer;
    white-space: nowrap;
    transition: background-color 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease, opacity 0.15s ease;
}

.btn:hover:not(:disabled) {
    border-color: var(--primary);
    background: var(--primary-soft);
}

.btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.btn-icon {
    width: 38px;
    padding: 0;
}

.btn-ghost {
    border-color: transparent;
    background: transparent;
}

.btn-soft {
    border-color: transparent;
    background: var(--primary-soft);
    color: var(--primary);
}

.btn-small {
    min-height: 30px;
    padding: 0 10px;
    font-size: 0.87rem;
}

.btn-primary {
    border-color: var(--primary);
    background: var(--primary);
    color: var(--on-primary);
    box-shadow: 0 2px 8px rgb(37 99 235 / 0.3);
}

.btn-primary:hover:not(:disabled) {
    border-color: var(--primary-hover);
    background: var(--primary-hover);
}

.btn-danger {
    border-color: var(--error);
    background: var(--error);
    color: #ffffff;
}

.btn-danger:hover:not(:disabled) {
    border-color: var(--error);
    background: var(--error);
    filter: brightness(0.92);
}

.btn-danger-soft {
    color: var(--error);
}

.btn-danger-soft:hover:not(:disabled) {
    border-color: var(--error);
    background: var(--error-soft);
}

.btn.is-busy {
    opacity: 0.7;
    cursor: progress;
}

/* ---- toolbar ----------------------------------------------------------- */

.toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 10px;
    margin-bottom: 12px;
}

.toolbar-group {
    display: flex;
    gap: 6px;
}

.search {
    display: flex;
    align-items: center;
    gap: 4px;
    flex: 1 1 320px;
    min-width: 0;
    min-height: 38px;
    padding: 0 4px 0 12px;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    background: var(--card);
    color: var(--text-muted);
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.search:focus-within {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgb(37 99 235 / 0.18);
}

.search input {
    flex: 1;
    min-width: 60px;
    height: 36px;
    padding: 0 6px;
    border: 0;
    outline: 0;
    background: transparent;
    color: var(--text);
}

.search input::-webkit-search-cancel-button {
    display: none;
}

.search-count {
    font-size: 0.85rem;
    white-space: nowrap;
}

.search .btn-icon {
    width: 30px;
    min-height: 30px;
}

.chip {
    padding: 3px 9px;
    border: 1px solid var(--border-strong);
    border-radius: 999px;
    background: transparent;
    color: var(--text-muted);
    font-size: 0.8rem;
    cursor: pointer;
}

.chip[aria-pressed="true"] {
    border-color: var(--primary);
    background: var(--primary);
    color: var(--on-primary);
}

.switch {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    margin-left: auto;
    padding: 6px 4px;
    border: 0;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-muted);
    font-size: 0.9rem;
    cursor: pointer;
}

.switch-track {
    position: relative;
    width: 34px;
    height: 20px;
    border-radius: 999px;
    background: var(--border-strong);
    transition: background-color 0.15s ease;
}

.switch-thumb {
    position: absolute;
    top: 2px;
    left: 2px;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: #ffffff;
    transition: transform 0.15s ease;
}

.switch[aria-checked="true"] .switch-track {
    background: var(--success);
}

.switch[aria-checked="true"] .switch-thumb {
    transform: translateX(14px);
}

.theme-sun,
:root[data-theme="dark"] .theme-moon {
    display: none;
}

:root[data-theme="dark"] .theme-sun,
.theme-moon {
    display: inline-flex;
}

/* ---- editor ------------------------------------------------------------ */

.card {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--card);
    box-shadow: var(--shadow);
    transition: background-color 0.2s ease, border-color 0.2s ease;
}

.editor-head {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 8px 16px;
    padding: 16px 20px 12px;
    border-bottom: 1px solid var(--border);
}

.editor-head h2 {
    font-size: 1rem;
    font-weight: 650;
}

.editor-head p {
    color: var(--text-muted);
    font-size: 0.9rem;
}

.legend {
    display: flex;
    gap: 16px;
    margin: 0;
    padding: 0;
    list-style: none;
    color: var(--text-muted);
    font-size: 0.85rem;
}

.swatch {
    padding: 1px 6px;
    border-radius: 4px;
    font-size: 0.8rem;
}

.swatch-dup {
    background: var(--dup-bg);
    color: var(--dup-text);
    text-decoration: underline 2px var(--dup-border);
    text-underline-offset: 3px;
}

.swatch-invalid {
    background: var(--error-soft);
    color: var(--error);
    text-decoration: underline wavy var(--error);
    text-underline-offset: 3px;
}

.editor-wrap {
    position: relative;
}

.editor {
    /* Fill the window so the whole workspace fits without page scrolling. */
    height: clamp(260px, calc(100vh - 470px), 620px);
    padding: 18px 20px;
    overflow-y: auto;
    outline: 0;
    font-family: var(--font-numbers);
    font-size: 1.05rem;
    font-variant-numeric: tabular-nums;
    line-height: 2;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    caret-color: var(--primary);
    /* Cheaper text shaping and an isolated layout: it matters at 50,000 numbers. */
    font-kerning: none;
    text-rendering: optimizeSpeed;
    contain: content;
    /* Mixed Latin/Urdu digits and commas should read left to right like the source list. */
    unicode-bidi: plaintext;
}

.editor:focus-visible {
    box-shadow: inset 0 0 0 2px var(--primary);
}

/* Custom highlights only accept literal colours, so both themes are spelled out. */
::highlight(ndf-dup) {
    background-color: #fef3c7;
    color: #78350f;
    text-decoration: underline;
    text-decoration-color: #f59e0b;
    text-decoration-thickness: 2px;
}

::highlight(ndf-invalid) {
    background-color: #fee2e2;
    color: #b91c1c;
    text-decoration: underline wavy;
    text-decoration-color: #dc2626;
}

::highlight(ndf-search) {
    background-color: #bfdbfe;
    color: #0f172a;
}

::highlight(ndf-search-current) {
    background-color: #2563eb;
    color: #ffffff;
}

:root[data-theme="dark"] ::highlight(ndf-dup) {
    background-color: #5c4208;
    color: #fde68a;
    text-decoration-color: #f59e0b;
}

:root[data-theme="dark"] ::highlight(ndf-invalid) {
    background-color: #4c1519;
    color: #fca5a5;
    text-decoration-color: #f87171;
}

:root[data-theme="dark"] ::highlight(ndf-search) {
    background-color: #1e3a8a;
    color: #e2e8f0;
}

:root[data-theme="dark"] ::highlight(ndf-search-current) {
    background-color: #60a5fa;
    color: #0b1120;
}

.empty-state {
    position: absolute;
    inset: 0;
    display: none;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 10px;
    padding: 24px;
    text-align: center;
    color: var(--text-muted);
    pointer-events: none;
}

.is-empty .empty-state {
    display: flex;
}

.empty-state .btn {
    margin-top: 6px;
    pointer-events: auto;
}

.empty-title {
    color: var(--text);
    font-size: 1.05rem;
    font-weight: 600;
}

.drop-overlay {
    position: absolute;
    inset: 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 10px;
    border: 2px dashed var(--primary);
    border-radius: var(--radius-sm);
    background: color-mix(in srgb, var(--primary-soft) 92%, transparent);
    color: var(--primary);
    font-weight: 600;
    pointer-events: none;
}

.drop-overlay .icon {
    width: 28px;
    height: 28px;
}

.notice {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 10px;
    padding: 10px 20px;
    border-top: 1px solid var(--border);
    color: var(--text-muted);
    font-size: 0.9rem;
}

.notice-warn {
    color: var(--warn-text);
    font-weight: 550;
}

.notice .btn {
    margin-left: auto;
}

/* ---- statistics -------------------------------------------------------- */

.stats {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 12px;
    margin: 16px 0;
}

.stat {
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 14px 18px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--card);
    box-shadow: var(--shadow);
}

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

.stat-value {
    font-size: 1.6rem;
    font-weight: 650;
    font-variant-numeric: tabular-nums;
    letter-spacing: -0.01em;
}

.stat-dup {
    border-left: 3px solid var(--dup-border);
}

/* ---- actions ----------------------------------------------------------- */

.actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 10px;
}

.actions .btn {
    min-height: 42px;
    padding: 0 16px;
}

.menu-wrap {
    position: relative;
    margin-left: auto;
}

.menu {
    position: absolute;
    right: 0;
    bottom: calc(100% + 6px);
    z-index: 20;
    display: flex;
    flex-direction: column;
    min-width: 230px;
    padding: 6px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--card);
    box-shadow: var(--shadow-pop);
}

.menu button {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 9px 10px;
    border: 0;
    border-radius: 6px;
    background: transparent;
    text-align: left;
    cursor: pointer;
}

.menu button:hover,
.menu button:focus-visible {
    background: var(--primary-soft);
}

/* ---- dialogs ----------------------------------------------------------- */

.dialog {
    width: min(460px, calc(100vw - 32px));
    padding: 0;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--card);
    color: var(--text);
    box-shadow: var(--shadow-pop);
}

.dialog-wide {
    width: min(600px, calc(100vw - 32px));
}

.dialog::backdrop {
    background: rgb(15 23 42 / 0.5);
    backdrop-filter: blur(2px);
}

.dialog form {
    display: flex;
    flex-direction: column;
    gap: 14px;
    padding: 24px;
}

.dialog h2 {
    font-size: 1.15rem;
}

.dialog h3 {
    font-size: 0.95rem;
}

.dialog-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    margin-top: 6px;
}

.choices {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin: 0;
    padding: 0;
    border: 0;
}

.choices legend {
    margin-bottom: 8px;
    padding: 0;
    font-weight: 600;
}

.choices-inline {
    gap: 6px;
}

.choice {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: border-color 0.15s ease, background-color 0.15s ease;
}

.choice:has(input:checked) {
    border-color: var(--primary);
    background: var(--primary-soft);
}

.choice-danger:has(input:checked) {
    border-color: var(--error);
    background: var(--error-soft);
}

.choice input {
    margin-top: 4px;
}

.choice small {
    display: block;
    color: var(--text-muted);
    font-size: 0.85rem;
}

.checks {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.check {
    display: flex;
    align-items: center;
    gap: 10px;
    cursor: pointer;
}

.choice input,
.check input {
    width: 16px;
    height: 16px;
    flex: none;
    accent-color: var(--primary);
}

.dropzone {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    padding: 28px 16px;
    border: 2px dashed var(--border-strong);
    border-radius: var(--radius-sm);
    text-align: center;
    transition: border-color 0.15s ease, background-color 0.15s ease;
}

.dropzone .icon {
    width: 28px;
    height: 28px;
    color: var(--primary);
}

.dropzone.is-over {
    border-color: var(--primary);
    background: var(--primary-soft);
}

.help-list {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin: 0;
    padding-left: 20px;
}

.shortcuts {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 8px 24px;
    margin: 0;
}

.shortcuts div {
    display: flex;
    justify-content: space-between;
    gap: 12px;
}

.shortcuts dd {
    margin: 0;
    color: var(--text-muted);
}

/* ---- floating pieces --------------------------------------------------- */

.tooltip,
.popover {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 30;
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-pop);
}

.tooltip {
    display: flex;
    flex-direction: column;
    padding: 7px 11px;
    background: #0f172a;
    color: #f8fafc;
    font-size: 0.85rem;
    pointer-events: none;
}

:root[data-theme="dark"] .tooltip {
    background: #e2e8f0;
    color: #0f172a;
}

.popover {
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 12px 14px;
    border: 1px solid var(--border);
    background: var(--card);
}

.popover-value {
    font-size: 1.05rem;
    font-weight: 650;
    font-variant-numeric: tabular-nums;
    overflow-wrap: anywhere;
    max-width: 280px;
}

.popover-count {
    color: var(--text-muted);
    font-size: 0.87rem;
}

.popover-actions {
    display: flex;
    gap: 6px;
    margin-top: 10px;
}

/* Top centre: the bottom of the page is where the action buttons live. */
.toasts {
    position: fixed;
    top: 16px;
    left: 50%;
    z-index: 60;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    width: max-content;
    max-width: min(420px, calc(100vw - 24px));
    transform: translateX(-50%);
    pointer-events: none;
}

.toast {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    padding: 11px 14px;
    border: 1px solid var(--border);
    border-left-width: 4px;
    border-radius: var(--radius-sm);
    background: var(--card);
    box-shadow: var(--shadow-pop);
    white-space: pre-line;
    animation: toast-in 0.2s ease;
    transition: opacity 0.25s ease, transform 0.25s ease;
}

.toast .icon {
    margin-top: 2px;
}

.toast-success {
    border-left-color: var(--success);
}

.toast-success .icon {
    color: var(--success);
}

.toast-warn {
    border-left-color: var(--dup-border);
}

.toast-warn .icon {
    color: var(--dup-border);
}

.toast-error {
    border-left-color: var(--error);
}

.toast-error .icon {
    color: var(--error);
}

.toast-out {
    opacity: 0;
    transform: translateY(-6px);
}

@keyframes toast-in {
    from {
        opacity: 0;
        transform: translateY(-8px);
    }
}

/* ---- responsive -------------------------------------------------------- */

@media (max-width: 760px) {
    .app {
        padding: 14px 14px 24px;
    }

    .stats {
        grid-template-columns: repeat(2, 1fr);
    }

    .search {
        order: 5;
        flex-basis: 100%;
    }

    .switch {
        margin-left: auto;
    }

    .editor {
        height: clamp(220px, 40vh, 420px);
        padding: 14px 16px;
    }

    .editor-head,
    .notice {
        padding-left: 16px;
        padding-right: 16px;
    }

    .actions .btn-primary {
        flex: 1 1 100%;
    }

    .actions .btn {
        flex: 1 1 auto;
    }

    .menu-wrap {
        flex: 1 1 auto;
        margin-left: 0;
    }

    .menu-wrap .btn {
        width: 100%;
    }

    .shortcuts {
        grid-template-columns: 1fr;
    }

    .brand h1 {
        font-size: 1.05rem;
    }
}

@media (max-width: 420px) {
    #helpBtn span {
        display: none;
    }

    .stat-value {
        font-size: 1.35rem;
    }

    .popover-actions {
        flex-wrap: wrap;
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation: none !important;
        transition: none !important;
    }
}
