:root { color-scheme: dark; }

html, body {
    margin: 0;
    padding: 0;
    width: 100%;
    min-height: 100svh;
    background-color: #161b22;
    background-image:
        radial-gradient(900px 420px at 12% -10%, rgba(180, 74, 74, 0.10), transparent 60%),
        radial-gradient(900px 420px at 88% -10%, rgba(42, 171, 238, 0.07), transparent 60%);
    background-repeat: no-repeat;
    color: #e6eaf0;
    font-family: "Montserrat", sans-serif;
    -webkit-font-smoothing: antialiased;
    padding-bottom: calc(24px + env(safe-area-inset-bottom, 0px));
    box-sizing: border-box;
    overflow-x: hidden;
}

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

span, button, a, p, h1, h2, h3, input {
    font-family: "Montserrat", sans-serif;
    font-optical-sizing: auto;
    font-weight: 500;
    font-style: normal;
}

a { color: inherit; text-decoration: none; }
button { font: inherit; }

/* ---- Header ---- */
.header { padding: 24px 16px 16px; }
.header-inner {
    max-width: 1080px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 14px;
}
.brand {
    display: flex;
    align-items: center;
    gap: 14px;
}
.brand img { width: 40px; height: 40px; display: block; }
.brand span { font-size: 1.4rem; font-weight: 600; letter-spacing: 0.01em; }
.header-logout { display: none; }

/* ---- Page layout ---- */
.page {
    width: 100%;
    max-width: 520px;
    margin: 0 auto;
    padding: 0 16px;
}
.dashboard {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

/* ---- Box ---- */
.box {
    width: 100%;
    margin: 0;
    padding: 16px;
    border-radius: 16px;
    border: 1px solid #2d333b;
    background: linear-gradient(135deg, #21262d 0%, #161b22 100%);
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.box.sub-info .row {
    display: flex;
    width: 100%;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
}

.box.sub-info .row .start {
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;
    gap: 12px;
    min-width: 0;
    flex: 1 1 auto;
}

.box.sub-info .row .start > span,
.box.sub-info .row .start .device-meta {
    min-width: 0;
}

.box.sub-info .row .start > span {
    font-size: 1.02rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.box.sub-info .row.secondary span { font-size: 0.88rem; }
.box.sub-info .row.secondary .start > span { font-size: 0.95rem; }

.box.sub-info .row.primary > span { font-size: 0.9rem; }

.box.sub-info .row .start .sub-icon {
    width: 36px;
    height: 36px;
    flex: 0 0 36px;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 10px;
    background-color: rgba(180, 74, 74, 0.22);
}
.box.sub-info .row .start .sub-icon svg { fill: #E6B3B3; }

.muted { color: #9aa4b2; }
.muted.small { font-size: 0.78rem; }

.device-meta {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
}
.device-meta .device-model {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.device-row {
    transition: opacity 240ms ease, transform 240ms ease, height 240ms ease;
}
.device-row.removed {
    opacity: 0;
    transform: translateX(8px);
    pointer-events: none;
}

.flag {
    font-size: 1.05rem;
    line-height: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

/* ---- Pills ---- */
.status-pill {
    padding: 6px 10px;
    border-radius: 999px;
    font-size: 0.74rem;
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    white-space: nowrap;
    flex: 0 0 auto;
}
.status-pill.green {
    color: #3DBA5B;
    background-color: rgba(61, 186, 91, 0.16);
}
.status-pill.red {
    color: #E6B3B3;
    background-color: rgba(180, 74, 74, 0.22);
}

.status-dot {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 0.8rem;
}
.status-dot::before {
    content: "";
    width: 8px; height: 8px;
    border-radius: 50%;
    background: currentColor;
    box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.04);
}
.status-dot.green { color: #3DBA5B; }
.status-dot.red   { color: #E66363; }

span.green { color: #3DBA5B; }
span.red   { color: #E6B3B3; }

/* ---- Progress ---- */
.progress {
    width: 100%;
    height: 6px;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.06);
    overflow: hidden;
}
.progress-fill {
    height: 100%;
    background: linear-gradient(90deg, #E6B3B3 0%, #c2616f 100%);
    border-radius: 999px;
    transition: width 600ms ease;
}

/* ---- Buttons ---- */
.box.sub-info .row.buttons { gap: 10px; }

.box.sub-info .row button {
    flex: 1 1 0;
    background: none;
    border: none;
    color: #e6eaf0;
    border-radius: 12px;
    padding: 12px;
    display: inline-flex;
    justify-content: center;
    align-items: center;
    gap: 6px;
    font-size: 0.92rem;
    cursor: pointer;
    transition: transform 120ms ease, background 200ms ease, opacity 200ms ease;
}
.box.sub-info .row button:active { transform: scale(0.98); }
.box.sub-info .row button:disabled { opacity: 0.5; cursor: not-allowed; }

.box.sub-info .row .primary-btn {
    background-color: rgba(180, 74, 74, 0.25);
    color: #E6B3B3;
}
.box.sub-info .row .primary-btn svg { fill: #ffa8a8; }

.box.sub-info .row .secondary-btn {
    background-color: #232A36;
    color: #E6EAF0;
}
.box.sub-info .row .secondary-btn svg { fill: #E6EAF0; }

.box.sub-info .row .secondary-btn.full,
.box.sub-info .row .primary-btn.full { width: 100%; flex: 1 1 100%; }

.icon-btn {
    flex: 0 0 auto !important;
    width: 36px;
    height: 36px;
    padding: 0 !important;
    border-radius: 10px !important;
    color: #9aa4b2 !important;
    background: transparent !important;
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 180ms ease, color 180ms ease;
}
.icon-btn:hover { color: #E6B3B3 !important; background: rgba(180, 74, 74, 0.18) !important; }
.icon-btn svg { fill: currentColor; }

/* ---- Plans (no-subscription page) ---- */
.plans-only { max-width: 520px; margin: 0 auto; }

.plan-row {
    padding: 12px 14px;
    border-radius: 12px;
    border: 1px solid #2d333b;
    background: rgba(255, 255, 255, 0.02);
}
.plan-period { font-size: 0.95rem !important; }
.plan-price {
    color: #E6B3B3;
    font-weight: 600;
    font-size: 0.95rem !important;
    flex: 0 0 auto;
}

/* ---- Sub-page iframe ---- */
.sub-frame {
    width: 100%;
    height: 70svh;
    border: none;
    border-radius: 12px;
    background: #0d1014;
}

/* ---- Auth pages ---- */
.auth-wrap {
    min-height: 100svh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
}
.auth-card {
    width: 100%;
    max-width: 360px;
    background: linear-gradient(135deg, #21262d 0%, #161b22 100%);
    border: 1px solid #2d333b;
    border-radius: 20px;
    padding: 28px 24px;
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 14px;
}
.auth-logo { width: 64px; height: 64px; }
.auth-card h1 {
    font-size: 1.35rem;
    margin: 4px 0 0 0;
    font-weight: 600;
}
.auth-sub {
    margin: 0;
    color: #9aa4b2;
    font-size: 0.95rem;
    line-height: 1.4;
}
#tg-login-slot {
    min-height: 48px;
    display: flex;
    justify-content: center;
    flex-direction: column;
    gap: 10px;
}
.auth-status {
    min-height: 1em;
    margin: 0;
    color: #E6B3B3;
    font-size: 0.85rem;
}
.auth-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 12px 18px;
    border-radius: 12px;
    background: #2AABEE;
    color: #fff;
    font-weight: 600;
    width: 100%;
}
.auth-link {
    color: #9aa4b2;
    font-size: 0.88rem;
    text-decoration: underline;
}
.auth-link.full {
    display: block;
    text-align: center;
    width: 100%;
    padding: 8px;
}

.tg-auth-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px 18px;
    border-radius: 12px;
    border: none;
    background: #2AABEE;
    color: #fff;
    font-weight: 600;
    font-size: 0.95rem;
    width: 100%;
    cursor: pointer;
    transition: background 200ms ease;
}
.tg-auth-button:hover { background: #1f97d5; }

/* ---- Toast ---- */
.toast {
    position: fixed;
    left: 50%;
    bottom: calc(24px + env(safe-area-inset-bottom, 0px));
    transform: translateX(-50%) translateY(8px);
    background: #232A36;
    color: #E6EAF0;
    padding: 10px 16px;
    border-radius: 999px;
    border: 1px solid #2d333b;
    font-size: 0.88rem;
    opacity: 0;
    transition: opacity 200ms ease, transform 200ms ease;
    z-index: 100;
    pointer-events: none;
    max-width: calc(100% - 32px);
    text-align: center;
}
.toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* ---- Traffic block ---- */
.traffic-block {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 18px;
    width: 100%;
}

.donut {
    position: relative;
    width: 120px;
    height: 120px;
    flex: 0 0 120px;
}
.donut svg {
    width: 100%;
    height: 100%;
    transform: rotate(-90deg);
}
.donut-track {
    fill: none;
    stroke: rgba(255, 255, 255, 0.06);
    stroke-width: 10;
}
.donut-fill {
    fill: none;
    stroke: url(#donut-gradient);
    stroke: #E6B3B3;
    stroke-width: 10;
    stroke-linecap: round;
    transition: stroke-dashoffset 700ms ease;
}
.donut-empty .donut-track { stroke: rgba(230, 179, 179, 0.18); }
.donut-label {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    text-align: center;
}
.donut-pct {
    font-size: 1.6rem;
    font-weight: 700;
    color: #E6B3B3;
    letter-spacing: -0.02em;
    line-height: 1;
}
.donut .donut-sub { font-size: 0.68rem; }

.traffic-stats {
    display: flex;
    flex-direction: column;
    gap: 10px;
    flex: 1 1 auto;
    min-width: 0;
}
.stat {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
}
.stat-label { text-transform: uppercase; letter-spacing: 0.06em; }
.stat-value { font-size: 1.05rem; font-weight: 600; line-height: 1.1; }

/* ---- Horizontal bars ---- */
.bars {
    display: flex;
    flex-direction: column;
    gap: 12px;
    width: 100%;
}
.bar-row { display: flex; flex-direction: column; gap: 6px; }
.bar-head {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
}
.bar-head .start { gap: 10px; }
.bar-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 0.92rem;
}
.bar-track {
    width: 100%;
    height: 6px;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.05);
    overflow: hidden;
}
.bar-fill {
    height: 100%;
    border-radius: 999px;
    background: linear-gradient(90deg, #E6B3B3 0%, #c2616f 100%);
    transition: width 700ms ease;
}

/* ---- Modal ---- */
.modal-root {
    position: fixed;
    inset: 0;
    z-index: 200;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
}
.modal-root[hidden] { display: none; }
.modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(7, 9, 13, 0.65);
    backdrop-filter: blur(4px);
    animation: fadeIn 180ms ease;
}
.modal-card {
    position: relative;
    width: 100%;
    max-width: 360px;
    background: linear-gradient(135deg, #21262d 0%, #161b22 100%);
    border: 1px solid #2d333b;
    border-radius: 16px;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    animation: popIn 200ms ease;
}
.modal-title {
    margin: 0;
    font-size: 1.1rem;
    font-weight: 600;
}
.modal-message {
    margin: 0;
    font-size: 0.9rem;
    color: #b1b8c2;
    line-height: 1.4;
}
.modal-actions {
    display: flex;
    flex-direction: row;
    gap: 10px;
    margin-top: 4px;
}
.modal-actions button {
    flex: 1 1 0;
    background: none;
    border: none;
    color: #e6eaf0;
    border-radius: 12px;
    padding: 12px;
    display: inline-flex;
    justify-content: center;
    align-items: center;
    font-size: 0.92rem;
    cursor: pointer;
    transition: transform 120ms ease, background 200ms ease;
}
.modal-actions button:active { transform: scale(0.98); }
.modal-actions .primary-btn { background-color: rgba(180, 74, 74, 0.25); color: #E6B3B3; }
.modal-actions .secondary-btn { background-color: #232A36; color: #E6EAF0; }

@keyframes fadeIn {
    from { opacity: 0; }
    to   { opacity: 1; }
}
@keyframes popIn {
    from { opacity: 0; transform: translateY(8px) scale(0.98); }
    to   { opacity: 1; transform: translateY(0) scale(1); }
}

@media (max-width: 380px) {
    .traffic-block { flex-direction: column; align-items: stretch; }
    .donut { align-self: center; }
}

/* ---- Hover (mouse / trackpad only) ---- */
@media (hover: hover) {
    .box.sub-info .row .primary-btn:hover,
    .modal-actions .primary-btn:hover { background-color: rgba(180, 74, 74, 0.38); }
    .box.sub-info .row .secondary-btn:hover,
    .modal-actions .secondary-btn:hover { background-color: #2b3340; }
    .auth-link:hover { color: #e6eaf0; }
}

/* ---- Desktop ---- */
@media (min-width: 900px) {
    .header { padding: 32px 24px 24px; }
    .header-inner { justify-content: space-between; }
    .header-logout {
        display: inline-flex;
        align-items: center;
        gap: 8px;
        color: #9aa4b2;
        font-size: 0.9rem;
        padding: 9px 16px;
        border: 1px solid #2d333b;
        border-radius: 12px;
        transition: color 180ms ease, border-color 180ms ease, background 180ms ease;
    }
    .header-logout:hover {
        color: #E6B3B3;
        border-color: rgba(180, 74, 74, 0.5);
        background: rgba(180, 74, 74, 0.12);
    }
    .header-logout svg { fill: currentColor; }

    .page { max-width: 1080px; padding: 0 24px 24px; }
    .dashboard {
        display: grid;
        grid-template-columns: minmax(0, 5fr) minmax(0, 6fr);
        /* Row 2 absorbs all remaining height, so the devices card stretches
           to the bottom edge of the (usually taller) traffic card instead of
           floating mid-column. */
        grid-template-rows: auto 1fr;
        grid-template-areas:
            "sub traffic"
            "devices traffic";
        gap: 20px;
    }
    .card-sub { grid-area: sub; }
    .card-traffic { grid-area: traffic; }
    .card-devices { grid-area: devices; }
    .card-account { display: none; }

    /* The no-subscription page has no traffic/devices grid — keep it a single
       centered column instead of inheriting the dashboard grid. */
    .dashboard.plans-only {
        display: flex;
        grid-template-columns: none;
        grid-template-areas: none;
    }

    /* Stretched cards: keep the buy buttons pinned to the bottom edge. */
    .card-traffic .row.buttons,
    .card-devices .row.buttons { margin-top: auto; }

    .box {
        padding: 24px;
        border-radius: 18px;
        gap: 16px;
        transition: border-color 200ms ease, box-shadow 200ms ease;
    }
    .box:hover {
        border-color: #39414c;
        box-shadow: 0 12px 32px rgba(0, 0, 0, 0.28);
    }

    .box.sub-info .row .start > span { font-size: 1.08rem; }
    .box.sub-info .row.secondary span { font-size: 0.92rem; }
    .muted.small { font-size: 0.82rem; }

    .traffic-block { gap: 28px; padding: 6px 0; }
    .donut { width: 156px; height: 156px; flex: 0 0 156px; }
    .donut-pct { font-size: 2rem; }
    .traffic-stats {
        flex-direction: row;
        justify-content: space-between;
        gap: 16px;
        padding-right: 8px;
    }
    .stat-value { font-size: 1.2rem; }

    .auth-card { max-width: 420px; padding: 36px 32px; }
}
