/* ==========================================================
   CTV Tor
   System colors, system type, one action per gate.
   ========================================================== */

:root,
:root[data-theme="light"] {
    color-scheme: light;
    --bg: #f2f2f7;
    --surface: #ffffff;
    --fill: rgba(120, 120, 128, 0.12);
    --fill-strong: rgba(120, 120, 128, 0.2);
    --label: #000000;
    --label-2: rgba(60, 60, 67, 0.6);
    --label-3: rgba(60, 60, 67, 0.3);
    --separator: rgba(60, 60, 67, 0.18);
    --accent: #f27a00;          /* CTV orange, tuned for contrast on white */
    --green: #248a3d;
    --green-fill: rgba(52, 199, 89, 0.16);
    --red: #d70015;
    --sheet: rgba(242, 242, 247, 0.86);
    --scrim: rgba(0, 0, 0, 0.28);
    --thumb: #ffffff;
    --sheet-row: #ffffff;
    --tile-shadow: 0 0.5px 0 rgba(0, 0, 0, 0.04);
}

:root[data-theme="dark"] {
    color-scheme: dark;
    --bg: #000000;
    --surface: #1c1c1e;
    --fill: rgba(118, 118, 128, 0.24);
    --fill-strong: rgba(118, 118, 128, 0.36);
    --label: #ffffff;
    --label-2: rgba(235, 235, 245, 0.6);
    --label-3: rgba(235, 235, 245, 0.3);
    --separator: rgba(84, 84, 88, 0.6);
    --accent: #ff9500;
    --green: #30d158;
    --green-fill: rgba(48, 209, 88, 0.18);
    --red: #ff453a;
    --sheet: rgba(28, 28, 30, 0.86);
    --scrim: rgba(0, 0, 0, 0.5);
    --thumb: #636366;
    --sheet-row: #2c2c2e;
    --tile-shadow: none;
}

/* iOS sheet curve – close to a critically damped spring */
:root { --ease: cubic-bezier(0.32, 0.72, 0, 1); }

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

html {
    font: 100%/1.4 -apple-system, system-ui, 'Segoe UI', Roboto, sans-serif;
    -webkit-text-size-adjust: 100%;
    -webkit-font-smoothing: antialiased;
}

body {
    margin: 0;
    min-height: 100dvh;
    background: var(--bg);
    color: var(--label);
    -webkit-tap-highlight-color: transparent;
    /* ease theme changes instead of jumping brightness */
    transition: background-color 0.35s ease, color 0.35s ease;
}

button {
    font: inherit;
    color: inherit;
    background: none;
    border: 0;
    padding: 0;
    cursor: pointer;
    touch-action: manipulation;
}
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }

[hidden] { display: none !important; }

p, h1, h2 { margin: 0; }

.view {
    max-width: 34rem;
    margin: 0 auto;
    padding:
        calc(env(safe-area-inset-top) + 1rem)
        calc(env(safe-area-inset-right) + 1rem)
        calc(env(safe-area-inset-bottom) + 1.5rem)
        calc(env(safe-area-inset-left) + 1rem);
}

/* ---------- loading ---------- */

.view--loading { min-height: 100dvh; display: grid; place-items: center; }

.activity {
    width: 1.5rem;
    height: 1.5rem;
    border-radius: 50%;
    border: 2.5px solid var(--fill-strong);
    border-top-color: var(--label-2);
    animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- sign in ---------- */

.view--login {
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
}

.login__body {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding-bottom: 4rem;
}

.login__icon {
    width: 5.5rem;
    height: 5.5rem;
    border-radius: 22.5%;
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.12);
}

.login__title {
    margin-top: 1.25rem;
    font-size: 2.125rem;
    line-height: 1.12;
    font-weight: 700;
    letter-spacing: -0.022em;
}

.login__text {
    margin-top: 0.5rem;
    max-width: 17rem;
    color: var(--label-2);
    font-size: 1.0625rem;
    line-height: 1.35;
    letter-spacing: -0.01em;
}

.pill-button {
    width: 100%;
    min-height: 3.25rem;
    border-radius: 0.875rem;
    background: var(--label);
    color: var(--bg);
    font-size: 1.0625rem;
    font-weight: 600;
    letter-spacing: -0.01em;
    transition: transform 0.12s ease-out, opacity 0.12s ease-out;
}
.pill-button:active { transform: scale(0.97); opacity: 0.85; transition-duration: 0s; }

/* ---------- large title header ---------- */

.large-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding: 1.5rem 0.25rem 1.25rem;
}

.large-header__brand {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    min-width: 0;
}

/* CTV shield logo (portrait, 329×441) */
.brand-logo {
    flex: none;
    width: auto;
    height: 2.5rem;
    display: block;
}

.large-header__title {
    font-size: 2rem;
    line-height: 1.1;
    font-weight: 700;
    letter-spacing: -0.022em;
    white-space: nowrap;
}

.avatar {
    flex: none;
    width: 2.25rem;
    height: 2.25rem;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: var(--fill-strong);
    color: var(--label);
    font-size: 0.875rem;
    font-weight: 600;
    letter-spacing: 0.01em;
    transition: transform 0.12s ease-out;
}
.avatar:active { transform: scale(0.92); transition-duration: 0s; }
.avatar { position: relative; overflow: hidden; }
.avatar img {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 50%;
    animation: photo-in 0.3s ease both;
}
/* hairline keeps light photos from melting into the background */
.avatar--photo::after {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: 50%;
    box-shadow: inset 0 0 0 0.5px rgba(0, 0, 0, 0.12);
    pointer-events: none;
}
@keyframes photo-in { from { opacity: 0; } }
.avatar--large { width: 3.5rem; height: 3.5rem; margin: 0; font-size: 1.25rem; }

/* ---------- connection notice (only when needed) ---------- */

.connection {
    margin: 0 0.25rem 0.75rem;
    color: var(--label-2);
    font-size: 0.8125rem;
}

/* ---------- gates ---------- */

.gates { display: grid; gap: 0.75rem; }

.gate {
    display: flex;
    align-items: center;
    gap: 1rem;
    min-height: 5.5rem;
    padding: 1rem 1rem 1rem 1.25rem;
    background: var(--surface);
    border-radius: 1.25rem;
    box-shadow: var(--tile-shadow);
    --p: 0;                       /* hold progress 0…1, driven from JS */
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    touch-action: pan-y;          /* vertical swipe scrolls and cancels the hold */
    transform: scale(calc(1 - var(--p) * 0.02));
    transition: background-color 0.35s ease;
}
.gate.is-pressed { background: color-mix(in srgb, var(--surface), var(--label) 4%); }
.gate:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }

.gate__text { flex: 1; min-width: 0; }

.gate__name {
    font-size: 1.25rem;
    line-height: 1.2;
    font-weight: 600;
    letter-spacing: -0.017em;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.gate__status {
    margin-top: 0.1875rem;
    display: flex;
    align-items: center;
    gap: 0.375rem;
    color: var(--label-2);
    font-size: 0.9375rem;
    letter-spacing: -0.01em;
    font-variant-numeric: tabular-nums;
}

.gate__status::before {
    content: '';
    width: 0.4375rem;
    height: 0.4375rem;
    border-radius: 50%;
    background: var(--label-3);
    transition: background-color 0.3s ease;
}

.gate--open .gate__status { color: var(--green); }
.gate--open .gate__status::before { background: var(--green); }

.gate--error .gate__status { color: var(--red); }
.gate--error .gate__status::before { background: var(--red); }

/* --- hold-to-open control --- */

.hold {
    position: relative;
    flex: none;
    width: 3.75rem;
    height: 3.75rem;
    border-radius: 50%;
    display: grid;
    place-items: center;
    background: var(--fill);
    color: var(--accent);
    user-select: none;
    pointer-events: none;
    transform: scale(calc(1 - var(--p) * 0.06));
}

.gate.is-pressed .hold { background: var(--fill-strong); }

.hold__ring {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    transform: rotate(-90deg);
    pointer-events: none;
}
.hold__ring circle {
    fill: none;
    stroke: var(--accent);
    stroke-width: 3;
    stroke-linecap: round;
    stroke-dasharray: 100;
    stroke-dashoffset: calc(100 - var(--p) * 100);
    opacity: min(1, calc(var(--p) * 6));
}

.hold__glyph { width: 1.625rem; height: 1.625rem; display: block; }
.hold__glyph--check, .hold__glyph--busy { display: none; }

.hold[data-phase="sending"] .hold__glyph--gate,
.hold[data-phase="done"] .hold__glyph--gate { display: none; }
.hold[data-phase="sending"] .hold__glyph--busy { display: block; }
.hold[data-phase="done"] .hold__glyph--check { display: block; }

.hold[data-phase="sending"] .hold__ring circle { opacity: 0; }
.hold[data-phase="done"] { background: var(--green-fill); color: var(--green); }
.hold[data-phase="done"] .hold__ring circle { opacity: 0; }

.hold__glyph--busy {
    width: 1.375rem;
    height: 1.375rem;
    border-radius: 50%;
    border: 2.5px solid currentColor;
    border-right-color: transparent;
    animation: spin 0.8s linear infinite;
}

.gate.is-shaking { animation: shake 0.42s cubic-bezier(0.36, 0.07, 0.19, 0.97); }
@keyframes shake {
    15%, 85% { transform: translateX(-2px); }
    30%, 70% { transform: translateX(5px); }
    45%, 55% { transform: translateX(-7px); }
    50% { transform: translateX(7px); }
}

.empty {
    padding: 3rem 1rem;
    text-align: center;
    color: var(--label-2);
}

/* ---------- account sheet ---------- */

.scrim {
    position: fixed;
    inset: 0;
    z-index: 10;
    background: var(--scrim);
    opacity: 0;
    transition: opacity 0.4s var(--ease);
}
.scrim.is-visible { opacity: 1; }

.sheet {
    position: fixed;
    z-index: 11;
    left: 0;
    right: 0;
    bottom: 0;
    max-width: 34rem;
    margin: 0 auto;
    padding: 0.5rem 1rem calc(env(safe-area-inset-bottom) + 1.25rem);
    background: var(--sheet);
    -webkit-backdrop-filter: blur(30px) saturate(180%);
    backdrop-filter: blur(30px) saturate(180%);
    border-radius: 1.5rem 1.5rem 0 0;
    box-shadow: 0 -10px 40px rgba(0, 0, 0, 0.18);
    transform: translateY(100%);
    transition: transform 0.45s var(--ease);
    touch-action: none;
}
.sheet.is-open { transform: translateY(0); }
.sheet.is-dragging { transition: none; }

.sheet__grabber {
    width: 2.25rem;
    height: 0.3125rem;
    margin: 0 auto 1rem;
    border-radius: 3px;
    background: var(--label-3);
}

.sheet__account {
    display: flex;
    align-items: center;
    gap: 0.875rem;
    padding: 0 0.25rem 1.5rem;
}

.sheet__name {
    font-size: 1.25rem;
    font-weight: 600;
    letter-spacing: -0.017em;
}

.sheet__email {
    margin-top: 0.125rem;
    color: var(--label-2);
    font-size: 0.9375rem;
    word-break: break-all;
}

.sheet__label {
    margin: 0 0.25rem 0.5rem;
    color: var(--label-2);
    font-size: 0.8125rem;
    font-weight: 600;
    letter-spacing: 0.02em;
    text-transform: uppercase;
}

/* segmented control */
.segmented {
    --index: 0;
    position: relative;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    padding: 2px;
    border-radius: 0.6875rem;
    background: var(--fill);
}

.segmented button {
    position: relative;
    z-index: 1;
    min-height: 2.25rem;
    font-size: 0.9375rem;
    font-weight: 500;
    letter-spacing: -0.01em;
}
.segmented button[aria-checked="true"] { font-weight: 600; }

.segmented__thumb {
    position: absolute;
    top: 2px;
    bottom: 2px;
    left: 2px;
    width: calc((100% - 4px) / 3);
    border-radius: 0.5625rem;
    background: var(--thumb);
    box-shadow: 0 3px 8px rgba(0, 0, 0, 0.12), 0 3px 1px rgba(0, 0, 0, 0.04);
    transform: translateX(calc(var(--index) * 100%));
    transition: transform 0.35s var(--ease);
}

.group {
    margin-top: 1.5rem;
    border-radius: 0.75rem;
    overflow: hidden;
    background: var(--sheet-row);
}

.group__row {
    width: 100%;
    min-height: 2.875rem;
    padding: 0 1rem;
    text-align: left;
    font-size: 1.0625rem;
    letter-spacing: -0.01em;
    transition: background-color 0.1s ease;
}
.group__row:active { background: var(--fill); transition-duration: 0s; }
.group__row--destructive { color: var(--red); }

/* ---------- accessibility preferences ---------- */

@media (prefers-reduced-motion: reduce) {
    .sheet { transition: opacity 0.2s ease; transform: none; opacity: 0; }
    .sheet.is-open { opacity: 1; }
    .segmented__thumb, body { transition: none; }
    .gate.is-shaking { animation: none; }
    .hold, .gate { transform: none; }
}

@media (prefers-reduced-transparency: reduce) {
    .sheet { background: var(--bg); -webkit-backdrop-filter: none; backdrop-filter: none; }
}

@media (prefers-contrast: more) {
    .gate { box-shadow: inset 0 0 0 1px var(--separator); }
    .hold { box-shadow: inset 0 0 0 1px var(--accent); }
}
