:root {
    color-scheme: light;
    --ink: #14211e;
    --muted: #66746f;
    --emerald-900: #0c2e29;
    --emerald-700: #124b42;
    --emerald-500: #1a7a68;
    --brass: #c8a45d;
    --brass-soft: #f4ecd9;
    --brass-deep: #7d5f26;
    --line: #e2e7e3;
    --paper: #ffffff;
    --bg: #eef1ee;
    --radius: 14px;
    --font: "IBM Plex Sans Arabic", "Segoe UI", Tahoma, Arial, sans-serif;
}

* {
    box-sizing: border-box;
}

html {
    -webkit-text-size-adjust: 100%;
}

body {
    margin: 0;
    min-height: 100dvh;
    color: var(--ink);
    font-family: var(--font);
    font-size: 16px;
    line-height: 1.6;
    background:
        radial-gradient(900px 380px at 100% -80px, #d9e8e1 0%, transparent 70%),
        var(--bg);
}

.app {
    max-width: 720px;
    margin: 0 auto;
    padding: max(16px, env(safe-area-inset-top)) 16px 96px;
}

/* ---------- Header ---------- */
.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
    padding: 22px 24px;
    margin-bottom: 12px;
    border-radius: 20px;
    color: #f7f5ee;
    background:
        linear-gradient(135deg, var(--emerald-900) 0%, var(--emerald-700) 100%);
    box-shadow: 0 18px 40px -18px #0c2e29, inset 0 -3px 0 var(--brass);
}

.topbar h1 {
    margin: 0;
    font-size: 24px;
    line-height: 1.3;
    font-weight: 700;
}

.topbar p {
    margin: 4px 0 0;
    font-size: 13px;
    color: #b9d3ca;
}

.ghost {
    min-height: 44px;
    padding: 0 18px;
    border: 1px solid #ffffff40;
    border-radius: 999px;
    color: #fff;
    background: #ffffff14;
    font: inherit;
    font-weight: 600;
    cursor: pointer;
    transition: background .15s;
}

.ghost:hover {
    background: #ffffff26;
}

.header-actions {
    display: flex;
    gap: 8px;
}

.header-actions .ghost {
    min-height: 36px;
    padding-inline: 12px;
    font-size: 13px;
}

.header-actions {
    display: flex;
    gap: 8px;
}

/* ---------- Stats (sticky) ---------- */
.stats {
    position: sticky;
    top: max(8px, env(safe-area-inset-top));
    z-index: 5;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    padding: 8px 4px;
    margin-bottom: 12px;
    background: #ffffffee;
    backdrop-filter: blur(10px);
}

.stats div {
    padding: 4px 4px;
    text-align: center;
    border-inline-start: 1px solid var(--line);
}

.stats div:first-child {
    border-inline-start: 0;
}

.stats strong {
    display: block;
    font-size: 20px;
    line-height: 1.3;
    color: var(--emerald-700);
    font-variant-numeric: tabular-nums;
}

.stats span {
    font-size: 12px;
    color: var(--muted);
}

.stats div:nth-child(4) strong {
    color: #a4442f;
}

/* ---------- Cards ---------- */
.card {
    padding: 22px;
    margin-bottom: 14px;
    border: 1px solid var(--line);
    border-radius: 18px;
    background: var(--paper);
    box-shadow: 0 1px 2px #14211e0d, 0 14px 30px -20px #14211e33;
}

h2 {
    margin: 0 0 18px;
    font-size: 19px;
    font-weight: 700;
    color: var(--emerald-900);
}

.section-head {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 12px;
    margin-bottom: 18px;
}

.section-head h2 {
    margin: 0 0 2px;
}

#recordNo {
    color: var(--muted);
    font-size: 13px;
}

.badge {
    padding: 6px 12px;
    border-radius: 999px;
    color: var(--brass-deep);
    background: var(--brass-soft);
    font-size: 12px;
    font-weight: 600;
    white-space: nowrap;
}

/* ---------- Fields ---------- */
label,
.field {
    display: block;
    margin: 0 0 16px;
}

label,
.lbl {
    font-size: 15px;
    font-weight: 600;
    color: #2b3b36;
}

.lbl {
    display: block;
    margin-bottom: 8px;
}

input,
select,
textarea {
    display: block;
    width: 100%;
    min-height: 50px;
    margin-top: 7px;
    padding: 12px 14px;
    border: 1.5px solid #d3dcd6;
    border-radius: 12px;
    color: var(--ink);
    background: #fbfcfb;
    font: inherit;
    font-size: 16px;
    font-weight: 500;
    outline: none;
    transition: border-color .15s, box-shadow .15s, background .15s;
}

input:focus,
select:focus,
textarea:focus {
    border-color: var(--emerald-500);
    background: #fff;
    box-shadow: 0 0 0 4px #1a7a682a;
}

textarea {
    resize: vertical;
    min-height: 88px;
}

input[dir="ltr"] {
    text-align: right;
}

.row {
    display: grid;
    grid-template-columns: 1fr 1.4fr;
    gap: 12px;
    align-items: start;
}

.input-with-pin {
    display: flex;
    align-items: center;
    gap: 8px;
}

.input-with-pin input {
    flex: 1;
    min-width: 0;
}

.pin-btn {
    flex: none;
    min-height: 50px;
    margin-top: 7px;
    padding: 0 16px;
    border: 1.5px solid #e4d3ad;
    border-radius: 12px;
    color: var(--brass-deep);
    background: var(--brass-soft);
    font: inherit;
    font-size: 14px;
    font-weight: 700;
    cursor: pointer;
}

.pin-btn.is-pinned {
    border-color: var(--emerald-500);
    color: #fff;
    background: var(--emerald-500);
}

.input-with-history {
    position: relative;
    display: flex;
    align-items: center;
    gap: 8px;
}

.input-with-history input,
.input-with-history textarea {
    flex: 1;
    min-width: 0;
}

.history-btn {
    flex: none;
    align-self: flex-start;
    min-height: 50px;
    margin-top: 7px;
    padding: 0 14px;
    border: 1.5px solid #e4d3ad;
    border-radius: 12px;
    color: var(--brass-deep);
    background: var(--brass-soft);
    font-size: 16px;
    cursor: pointer;
}

.history-list {
    position: absolute;
    inset-inline: 0;
    top: 100%;
    margin-top: 4px;
    z-index: 5;
    max-height: 220px;
    overflow-y: auto;
    background: #fff;
    border: 1.5px solid #e4d3ad;
    border-radius: 12px;
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.12);
}

.history-item {
    display: block;
    width: 100%;
    padding: 10px 14px;
    border: none;
    background: none;
    font: inherit;
    text-align: start;
    cursor: pointer;
}

.history-item:hover,
.history-item:focus-visible {
    background: var(--brass-soft);
}

.history-empty {
    padding: 10px 14px;
    color: #8a8a8a;
    font-size: 14px;
}

/* ---------- Chips ---------- */
.sr {
    position: absolute !important;
    width: 1px;
    height: 1px;
    min-height: 0;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0 0 0 0);
    border: 0;
    opacity: 0;
}

.chips {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.chip {
    flex: 1 1 auto;
    min-height: 50px;
    padding: 10px 16px;
    border: 1.5px solid #d3dcd6;
    border-radius: 12px;
    color: #2b3b36;
    background: #fbfcfb;
    font: inherit;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    transition: background .15s, border-color .15s, color .15s, transform .1s;
}

.chip:active {
    transform: scale(.97);
}

.chip.on {
    color: #fff;
    border-color: var(--emerald-700);
    background: linear-gradient(135deg, var(--emerald-700), var(--emerald-500));
    box-shadow: inset 0 -2px 0 var(--brass);
}

.extra-fields {
    padding-top: 14px;
    margin: 2px 0 16px;
    border-top: 1px solid var(--line);
}

.extra-fields h3 {
    margin: 0 0 12px;
    color: var(--emerald-700);
    font-size: 15px;
    font-weight: 700;
}

/* ---------- GPS ---------- */
.gps-box {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding: 14px;
    margin: 0 0 16px;
    border-radius: 12px;
    background: #f3f7f5;
}

.gps-box small {
    display: block;
    margin-top: 2px;
    color: var(--muted);
    direction: ltr;
    text-align: right;
    font-size: 12px;
}

/* ---------- Buttons ---------- */
.primary,
.secondary {
    min-height: 52px;
    padding: 0 20px;
    border: 0;
    border-radius: 14px;
    font: inherit;
    font-weight: 700;
    cursor: pointer;
    transition: transform .1s, box-shadow .15s, background .15s;
}

.primary {
    width: 100%;
    color: #fff;
    background: linear-gradient(135deg, var(--emerald-700), var(--emerald-500));
    box-shadow: 0 10px 20px -10px var(--emerald-700), inset 0 -3px 0 var(--brass);
}

.primary:hover {
    box-shadow: 0 14px 26px -10px var(--emerald-700), inset 0 -3px 0 var(--brass);
}

.secondary {
    color: var(--emerald-700);
    background: #e3efe9;
    white-space: nowrap;
}

.secondary:hover {
    background: #d4e6dd;
}

.primary:active,
.secondary:active {
    transform: translateY(1px);
}

.savebar {
    display: flex;
    gap: 8px;
    position: sticky;
    bottom: 0;
    z-index: 4;
    margin: 0 -22px -22px;
    padding: 12px 22px calc(12px + env(safe-area-inset-bottom));
    border-radius: 0 0 18px 18px;
    background: linear-gradient(to top, #fff 70%, #fff0);
}

.savebar .primary {
    width: auto;
    flex: 1;
}

.savebar .secondary {
    flex: 0 0 auto;
}

/* ---------- Toast ---------- */
.message {
    position: fixed;
    z-index: 20;
    inset-inline: 0;
    bottom: calc(92px + env(safe-area-inset-bottom));
    width: min(520px, calc(100% - 28px));
    margin-inline: auto;
    padding: 14px 18px;
    border-radius: 14px;
    color: #fff;
    background: var(--emerald-900);
    border-bottom: 3px solid var(--brass);
    text-align: center;
    font-weight: 600;
    box-shadow: 0 18px 40px -12px #0c2e29aa;
}

/* ---------- Drafts and submission queue ---------- */
.records-dialog {
    width: min(620px, calc(100% - 24px));
    max-width: none;
    max-height: min(82dvh, 760px);
    padding: 0;
    border: 1px solid var(--line);
    border-radius: 20px;
    color: var(--ink);
    background: var(--paper);
    box-shadow: 0 24px 80px #071b1766;
}

.records-dialog::backdrop {
    background: #071b17a8;
    backdrop-filter: blur(3px);
}

.records-panel {
    padding: 22px;
}

.records-head {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 16px;
    margin-bottom: 18px;
}

.records-head h2 {
    margin: 0;
}

.records-head p {
    margin: 3px 0 0;
    color: var(--muted);
    font-size: 13px;
}

.dialog-close {
    flex: none;
    width: 42px;
    height: 42px;
    border: 1px solid var(--line);
    border-radius: 50%;
    color: var(--emerald-900);
    background: #f5f7f5;
    font: inherit;
    font-size: 25px;
    line-height: 1;
    cursor: pointer;
}

.record-filters {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding-bottom: 16px;
    border-bottom: 1px solid var(--line);
}

.record-filter {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    min-height: 40px;
    padding: 0 12px;
    border: 1px solid var(--line);
    border-radius: 999px;
    color: var(--muted);
    background: #fff;
    font: inherit;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
}

.record-filter.is-active {
    border-color: var(--emerald-700);
    color: #fff;
    background: var(--emerald-700);
}

.filter-count {
    min-width: 20px;
    padding: 0 5px;
    border-radius: 999px;
    color: var(--emerald-900);
    background: #e8eee9;
    font-size: 11px;
    text-align: center;
}

.record-filter.is-active .filter-count {
    color: var(--emerald-900);
    background: #fff;
}

.records-list {
    max-height: min(54dvh, 480px);
    overflow-y: auto;
    overscroll-behavior: contain;
}

.record-item {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: 8px 12px;
    padding: 14px 2px;
    border-bottom: 1px solid var(--line);
}

.record-detail {
    display: grid;
    min-width: 0;
    gap: 2px;
}

.record-detail strong {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.record-detail small {
    color: var(--muted);
    font-size: 12px;
}

.record-status {
    padding: 5px 9px;
    border-radius: 999px;
    color: #795a23;
    background: var(--brass-soft);
    font-size: 11px;
    font-weight: 700;
    white-space: nowrap;
}

.status-sent .record-status {
    color: #145c43;
    background: #e3f2e9;
}

.status-failed .record-status {
    color: #963b2c;
    background: #f9e9e5;
}

.status-sending .record-status {
    color: #315e82;
    background: #e7f0f7;
}

.record-error {
    grid-column: 1 / -1;
    overflow-wrap: anywhere;
    color: #963b2c;
    font-size: 12px;
}

.draft-edit {
    justify-self: start;
    min-height: 36px;
    padding: 0 12px;
    border: 1px solid #c7d9ce;
    border-radius: 9px;
    color: var(--emerald-700);
    background: #edf5f0;
    font: inherit;
    font-size: 12px;
    font-weight: 700;
    cursor: pointer;
}

.records-empty {
    margin: 0;
    padding: 30px 12px;
    color: var(--muted);
    text-align: center;
}

.intro-dialog {
    width: min(680px, calc(100% - 24px));
    max-width: none;
    max-height: min(90dvh, 820px);
    padding: 0;
    border: 1px solid var(--line);
    border-radius: 20px;
    color: var(--ink);
    background: var(--paper);
    box-shadow: 0 24px 80px #071b1766;
}

.intro-dialog::backdrop {
    background: #071b17b8;
    backdrop-filter: blur(3px);
}

.intro-panel {
    max-height: min(90dvh, 820px);
    overflow-y: auto;
    padding: 24px;
}

.intro-head {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 14px;
    margin-bottom: 18px;
}

.intro-head h2 {
    margin: 0;
}

.intro-kicker {
    margin: 0 0 3px;
    color: var(--brass-deep);
    font-size: 12px;
    font-weight: 700;
}

.intro-section {
    padding: 14px 0;
    border-top: 1px solid var(--line);
}

.intro-section h3 {
    margin: 0 0 8px;
    color: var(--emerald-700);
    font-size: 16px;
}

.intro-section ol {
    margin: 0;
    padding-inline-start: 22px;
}

.intro-section li {
    padding: 3px 0;
}

.install-section {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px 22px;
}

.install-section h3 {
    grid-column: 1 / -1;
    margin-bottom: 0;
}

.install-platform h4 {
    margin: 0 0 4px;
    color: var(--ink);
    font-size: 14px;
}

.install-platform p {
    margin: 0;
    color: var(--muted);
    font-size: 13px;
}

.intro-note {
    margin: 8px 0 16px;
    padding: 10px 12px;
    border-radius: 10px;
    color: var(--brass-deep);
    background: var(--brass-soft);
    font-size: 12px;
}

/* ---------- Sent and pending records ---------- */
.records-dialog {
    width: min(620px, calc(100% - 24px));
    max-width: none;
    max-height: min(82dvh, 760px);
    padding: 0;
    border: 1px solid var(--line);
    border-radius: 20px;
    color: var(--ink);
    background: var(--paper);
    box-shadow: 0 24px 80px #071b1766;
}

.records-dialog::backdrop {
    background: #071b17a8;
    backdrop-filter: blur(3px);
}

.records-panel {
    padding: 22px;
}

.records-head {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 16px;
    margin-bottom: 18px;
}

.records-head h2 {
    margin: 0;
}

.records-head p {
    margin: 3px 0 0;
    color: var(--muted);
    font-size: 13px;
}

.dialog-close {
    flex: none;
    width: 42px;
    height: 42px;
    border: 1px solid var(--line);
    border-radius: 50%;
    color: var(--emerald-900);
    background: #f5f7f5;
    font: inherit;
    font-size: 25px;
    line-height: 1;
    cursor: pointer;
}

.record-filters {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding-bottom: 16px;
    border-bottom: 1px solid var(--line);
}

.record-filter {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    min-height: 40px;
    padding: 0 12px;
    border: 1px solid var(--line);
    border-radius: 999px;
    color: var(--muted);
    background: #fff;
    font: inherit;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
}

.record-filter.is-active {
    border-color: var(--emerald-700);
    color: #fff;
    background: var(--emerald-700);
}

.filter-count {
    min-width: 20px;
    padding: 0 5px;
    border-radius: 999px;
    color: var(--emerald-900);
    background: #e8eee9;
    font-size: 11px;
    text-align: center;
}

.record-filter.is-active .filter-count {
    color: var(--emerald-900);
    background: #fff;
}

.records-list {
    max-height: min(54dvh, 480px);
    overflow-y: auto;
    overscroll-behavior: contain;
}

.record-item {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: 8px 12px;
    padding: 14px 2px;
    border-bottom: 1px solid var(--line);
}

.record-detail {
    display: grid;
    min-width: 0;
    gap: 2px;
}

.record-detail strong {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.record-detail small {
    color: var(--muted);
    font-size: 12px;
}

.record-status {
    padding: 5px 9px;
    border-radius: 999px;
    color: #795a23;
    background: var(--brass-soft);
    font-size: 11px;
    font-weight: 700;
    white-space: nowrap;
}

.status-sent .record-status {
    color: #145c43;
    background: #e3f2e9;
}

.status-failed .record-status {
    color: #963b2c;
    background: #f9e9e5;
}

.status-sending .record-status {
    color: #315e82;
    background: #e7f0f7;
}

.record-error {
    grid-column: 1 / -1;
    overflow-wrap: anywhere;
    color: #963b2c;
    font-size: 12px;
}

.records-empty {
    margin: 0;
    padding: 30px 12px;
    color: var(--muted);
    text-align: center;
}

.hidden {
    display: none !important;
}

.signature {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 7px;
    padding: 14px 0 20px;
    color: var(--muted);
    font-size: 12px;
}

.signature::before,
.signature::after {
    width: 18px;
    height: 1px;
    background: var(--brass);
    content: "";
}

.signature strong {
    color: var(--emerald-700);
    font-weight: 700;
}

:focus-visible {
    outline: 3px solid var(--brass);
    outline-offset: 2px;
}

@media (max-width: 520px) {
    .app {
        padding-inline: 12px;
    }

    .topbar {
        padding: 18px;
        border-radius: 18px;
    }

    .topbar h1 {
        font-size: 21px;
    }

    .header-actions {
        flex-direction: column;
        gap: 6px;
    }

    .ghost {
        min-height: 40px;
        padding-inline: 14px;
    }

    .topbar {
        flex-wrap: wrap;
    }

    .header-actions {
        display: grid;
        width: 100%;
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: 6px;
    }

    .header-actions .ghost {
        min-height: 34px;
        padding-inline: 6px;
        font-size: 12px;
        white-space: nowrap;
    }

    .header-actions {
        flex-direction: column;
        gap: 6px;
    }

    .ghost {
        min-height: 40px;
        padding-inline: 14px;
    }

    .card {
        padding: 16px;
    }

    .savebar {
        margin: 0 -16px -16px;
        padding-inline: 16px;
    }

    .gps-box {
        flex-direction: column;
        align-items: stretch;
    }

    .records-panel {
        padding: 17px;
    }

    .intro-panel {
        padding: 17px;
    }

    .install-section {
        grid-template-columns: 1fr;
        gap: 12px;
    }

    .record-filters {
        gap: 6px;
    }

    .record-filter {
        min-height: 38px;
        padding-inline: 10px;
        font-size: 12px;
    }

    .row {
        grid-template-columns: 1fr;
        gap: 0;
    }

    .records-panel {
        padding: 17px;
    }

    .record-filters {
        gap: 6px;
    }

    .record-filter {
        min-height: 38px;
        padding-inline: 10px;
        font-size: 12px;
    }
}

@media (prefers-reduced-motion: reduce) {
    * {
        transition: none !important;
    }
}