:root {
    --bg: #0f0f1a;
    --bg2: #16162a;
    --bg3: #1e1e35;
    --bg4: #252540;
    --surface: #1c1c30;
    --border: rgba(255, 255, 255, 0.08);
    --border2: rgba(255, 255, 255, 0.14);
    --text: #e8e8f4;
    --text2: #9898b8;
    --text3: #5c5c7a;
    --accent: #7c6ff7;
    --accent2: #9d98f9;
    --accent-bg: rgba(124, 111, 247, 0.12);
    --green: #4ecf8e;
    --green-bg: rgba(78, 207, 142, 0.1);
    --red: #f06b6b;
    --red-bg: rgba(240, 107, 107, 0.1);
    --amber: #f0b44e;
    --amber-bg: rgba(240, 180, 78, 0.1);
    --card-r: 16px;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-user-select: none;
    user-select: none;
}

html, body {
    height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: 'DM Sans', sans-serif;
    font-size: 15px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    overflow: hidden;
}

input, textarea, select {
    -webkit-user-select: text;
    user-select: text;
}

/* ─── APP SHELL ─── */
#app {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    max-width: 480px;
    margin: 0 auto;
    position: relative;
}

#auth-screen {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100dvh;
    padding: 40px 24px;
    gap: 12px;
    text-align: center;
}

.auth-wordmark {
    font-size: 52px;
    font-weight: 700;
    letter-spacing: -3px;
    color: var(--text);
    line-height: 1;
}

.auth-wordmark span {
    color: var(--accent2);
}

.auth-tag {
    font-size: 15px;
    color: var(--text2);
    max-width: 280px;
    line-height: 1.6;
    margin: 4px 0 16px;
}

.google-btn {
    display: flex;
    align-items: center;
    gap: 10px;
    background: white;
    color: #1f1f1f;
    border: none;
    border-radius: 12px;
    padding: 13px 28px;
    font-size: 15px;
    font-weight: 500;
    cursor: pointer;
    font-family: inherit;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.25);
    transition: transform 0.15s, box-shadow 0.15s;
}

.google-btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
}

.auth-note {
    font-size: 11px;
    color: var(--text3);
    max-width: 260px;
    line-height: 1.6;
}

/* ─── TOP BAR ─── */
#topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: calc(var(--safe-top) + 12px) 16px 12px;
    background: var(--bg);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    gap: 8px;
}

#topbar-left {
    display: flex;
    align-items: center;
    gap: 8px;
    flex: 1;
    min-width: 0;
}

.wordmark {
    font-size: 17px;
    font-weight: 700;
    letter-spacing: -0.8px;
    color: var(--text);
    flex-shrink: 0;
}

.wordmark span {
    color: var(--accent2);
}

#log-selector {
    display: flex;
    align-items: center;
    background: var(--bg3);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 3px;
    cursor: pointer;
    flex: 1;
    overflow-x: auto;
    white-space: nowrap;
}

.log_tab {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 5px 10px;
    border-radius: 7px;
    background: transparent;
    color: var(--text2);
    font-size: 12px;
    flex-shrink: 0;
    border: none;
    font-family: inherit;
    cursor: pointer;
}
.log_tab:focus {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.log_tab.active {
    background: var(--accent-bg);
    color: var(--accent2);
}

.log_tab-add {
    color: var(--text3);
    font-weight: 600;
    margin-left: auto;
    display: inline-flex;
}

.icon-btn {
    width: 34px;
    height: 34px;
    border-radius: 10px;
    background: var(--bg3);
    border: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--text2);
    transition: all 0.15s;
    flex-shrink: 0;
}

.icon-btn:hover {
    background: var(--bg4);
    color: var(--text);
}

#user-av {
    width: 34px;
    height: 34px;
    border-radius: 10px;
    border: 1px solid var(--border);
    cursor: pointer;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--accent-bg);
    font-size: 13px;
    font-weight: 600;
    color: var(--accent2);
    flex-shrink: 0;
}

#user-av img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 9px;
    display: none;
}

/* ─── QUICK STRIP ─── */
#quick-strip {
    padding: 10px 10px 8px;
    display: flex;
    gap: 8px;
    overflow-x: auto;
    flex-shrink: 0;
    scrollbar-width: none;
}

#quick-strip::-webkit-scrollbar {
    display: none;
}

.quick-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    background: var(--bg3);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 10px 14px;
    cursor: pointer;
    transition: all 0.15s;
    flex-shrink: 0;
    min-width: 58px;
    position: relative;
    -webkit-tap-highlight-color: transparent;
    font-family: inherit;
}
.quick-btn:focus {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.quick-btn:active {
    transform: scale(0.93);
}

.quick-btn:hover {
    background: var(--bg4);
    border-color: var(--border2);
}

.quick-btn .qa {
    font-size: 19px;
    font-weight: 700;
    font-family: 'DM Mono', monospace;
    line-height: 1;
}

.quick-btn .qn {
    font-size: 10px;
    color: var(--text3);
    line-height: 1;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 62px;
}

.quick-btn .rbadge {
    position: absolute;
    top: -5px;
    right: -5px;
    background: var(--red);
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    border-radius: 99px;
    padding: 2px 5px;
    min-width: 18px;
    text-align: center;
    border: 2px solid var(--bg);
    line-height: 1.2;
}

#add-stub {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    background: transparent;
    border: 1.5px dashed var(--border2);
    border-radius: 14px;
    padding: 10px 14px;
    cursor: pointer;
    color: var(--text3);
    flex-shrink: 0;
    min-width: 58px;
    transition: all 0.15s;
    font-family: inherit;
}
#add-stub:focus {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

#add-stub:hover {
    border-color: var(--accent);
    color: var(--accent2);
}

#add-stub span {
    font-size: 10px;
}

/* ─── LOG ─── */
#log-area {
    flex: 1;
    overflow-y: auto;
    padding: 4px 12px 8px;
    display: flex;
    flex-direction: column;
    gap: 1px;
    scrollbar-width: thin;
    scrollbar-color: var(--bg4) transparent;
}

.day-sep {
    text-align: center;
    font-size: 11px;
    color: var(--text3);
    padding: 14px 0 5px;
    font-weight: 500;
    letter-spacing: 0.5px;
    text-transform: uppercase;
    position: sticky;
    top: 0;
    background: linear-gradient(var(--bg) 70%, transparent);
    z-index: 1;
}

.log-row {
    display: flex;
    justify-content: flex-end;
    padding: 1px 0;
}

.bubble {
    background: var(--accent-bg);
    border: 1px solid rgba(124, 111, 247, 0.18);
    border-radius: 16px 16px 3px 16px;
    padding: 7px 13px;
    max-width: 78%;
    display: flex;
    align-items: baseline;
    gap: 8px;
    cursor: default;
    position: relative;
    transition: border-color 0.15s;
}

.bubble:hover {
    border-color: rgba(124, 111, 247, 0.38);
}

/* .ba-inline matches what renderLog emits */
.bubble .ba-inline {
    font-size: 15px;
    font-weight: 400;
    line-height: 1.4;
}

.bubble .bt {
    font-size: 11px;
    color: var(--text3);
    font-variant-numeric: tabular-nums;
}

.del-btn {
    position: absolute;
    left: -30px;
    top: 50%;
    transform: translateY(-50%);
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: var(--red-bg);
    border: 1px solid rgba(240, 107, 107, 0.2);
    display: none;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--red);
    font-size: 14px;
    line-height: 1;
    z-index: 2;
}

.bubble:hover .del-btn,
.bubble:focus-within .del-btn {
    display: flex;
}

.bubble:focus {
    outline: none;
    border-color: rgba(124, 111, 247, 0.4);
}

.empty-state {
    flex: 1;
    display: none;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 8px;
    color: var(--text3);
    padding: 40px;
    text-align: center;
}

.empty-state svg {
    opacity: 0.25;
}

.empty-state p {
    font-size: 13px;
    line-height: 1.7;
}

/* ─── BOTTOM NAV ─── */
#bottom-nav {
    background: var(--bg2);
    border-top: 1px solid var(--border);
    padding: 8px 16px calc(8px + var(--safe-bottom));
    display: flex;
    gap: 6px;
    flex-shrink: 0;
}

.nav-btn {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 3px;
    padding: 6px;
    border-radius: 10px;
    cursor: pointer;
    color: var(--text3);
    transition: all 0.15s;
    background: transparent;
    border: none;
    font-family: inherit;
    -webkit-tap-highlight-color: transparent;
}

.nav-btn.active {
    color: var(--accent2);
    background: var(--accent-bg);
}

.nav-btn span {
    font-size: 10px;
    font-weight: 500;
}

/* ─── MODALS ─── */
.overlay {
    position: fixed;
    inset: 0;
    z-index: 100;
    background: rgba(8, 8, 18, 0.82);
    backdrop-filter: blur(8px);
    display: flex;
    align-items: flex-end;
    justify-content: center;
    animation: ov-in 0.15s ease;
}

.overlay.center-v {
    align-items: center;
}

@keyframes ov-in {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

.sheet {
    background: var(--bg2);
    border-radius: 22px 22px 0 0;
    border: 1px solid var(--border2);
    border-bottom: none;
    width: 100%;
    max-width: 480px;
    padding-bottom: calc(16px + var(--safe-bottom));
    animation: sh-in 0.22s cubic-bezier(0.34, 1.4, 0.64, 1);
    max-height: 92dvh;
    overflow-y: auto;
}

.center-card {
    background: var(--bg2);
    border-radius: 20px;
    border: 1px solid var(--border2);
    width: calc(100% - 48px);
    max-width: 400px;
    padding: 22px;
    animation: card-in 0.2s cubic-bezier(0.34, 1.4, 0.64, 1);
}

@keyframes sh-in {
    from {
        transform: translateY(100%);
    }
    to {
        transform: translateY(0);
    }
}

@keyframes card-in {
    from {
        transform: scale(0.88);
        opacity: 0;
    }
    to {
        transform: scale(1);
        opacity: 1;
    }
}

.handle {
    width: 36px;
    height: 4px;
    border-radius: 2px;
    background: var(--border2);
    margin: 10px auto 0;
}

.m-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 18px 12px;
    border-bottom: 1px solid var(--border);
}

.m-title {
    font-size: 16px;
    font-weight: 600;
}

.m-close {
    width: 28px;
    height: 28px;
    border-radius: 8px;
    background: var(--bg3);
    border: none;
    color: var(--text3);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 18px;
    line-height: 1;
}

.m-body {
    padding: 14px 18px;
}

/* ─── FORMS ─── */
.field {
    display: flex;
    flex-direction: column;
    gap: 5px;
    margin-bottom: 14px;
}

.field label {
    font-size: 11px;
    font-weight: 500;
    color: var(--text3);
    letter-spacing: 0.5px;
    text-transform: uppercase;
}

.field input, .field select, .field textarea {
    background: var(--bg3);
    border: 1px solid var(--border2);
    border-radius: 10px;
    color: var(--text);
    font-family: inherit;
    font-size: 15px;
    padding: 9px 12px;
    outline: none;
    transition: border-color 0.15s;
    width: 100%;
}

.field input:focus, .field select:focus {
    border-color: var(--accent);
}

.field select option {
    background: var(--bg2);
}

.frow {
    display: flex;
    gap: 10px;
}

.frow .field {
    flex: 1;
}

.trow {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 9px 0;
    border-bottom: 1px solid var(--border);
}

.trow:last-child {
    border-bottom: none;
}

.trow-label {
    font-size: 14px;
    color: var(--text);
}

.trow-sub {
    font-size: 11px;
    color: var(--text3);
    margin-top: 1px;
}

.tog {
    width: 44px;
    height: 26px;
    border-radius: 13px;
    position: relative;
    background: var(--bg4);
    border: 1px solid var(--border2);
    cursor: pointer;
    transition: background 0.2s;
    flex-shrink: 0;
}

.tog.on {
    background: var(--accent);
    border-color: var(--accent);
}

.tog::after {
    content: '';
    position: absolute;
    left: 3px;
    top: 3px;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: #fff;
    transition: transform 0.2s;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
}

.tog.on::after {
    transform: translateX(18px);
}

.btn {
    border: none;
    border-radius: 12px;
    padding: 11px 18px;
    font-family: inherit;
    font-size: 15px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.15s;
    width: 100%;
}

.btn-p {
    background: var(--accent);
    color: #fff;
}

.btn-p:hover {
    opacity: 0.9;
}

.btn-s {
    background: var(--bg3);
    color: var(--text2);
    border: 1px solid var(--border2);
}

.btn-s:hover {
    background: var(--bg4);
    color: var(--text);
}

.btn-d {
    background: var(--red-bg);
    color: var(--red);
    border: 1px solid rgba(240, 107, 107, 0.18);
}

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

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

.brow .btn {
    flex: 1;
}

/* ─── TIME PICKER ─── */
.tp-head {
    text-align: center;
    padding: 10px 20px 4px;
}

.tp-for {
    font-size: 13px;
    color: var(--text3);
    margin-bottom: 2px;
}

.tp-time {
    font-size: 44px;
    font-weight: 300;
    color: var(--text);
    font-variant-numeric: tabular-nums;
    letter-spacing: -2px;
    font-family: 'DM Mono', monospace;
    line-height: 1.1;
}

.offsets {
    display: flex;
    gap: 6px;
    padding: 10px 18px 6px;
    flex-wrap: wrap;
    justify-content: center;
}

.off-btn {
    background: var(--bg3);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 6px 12px;
    color: var(--text2);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    font-family: inherit;
    transition: all 0.12s;
}

.off-btn:hover, .off-btn.active {
    background: var(--accent-bg);
    border-color: var(--accent);
    color: var(--accent2);
}

.tp-manual {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 6px 18px 2px;
}

.tp-manual span {
    font-size: 12px;
    color: var(--text3);
}

.tp-manual input[type="time"] {
    background: var(--bg3);
    border: 1px solid var(--border2);
    border-radius: 10px;
    color: var(--text);
    font-family: 'DM Mono', monospace;
    font-size: 14px;
    padding: 7px 11px;
    outline: none;
    color-scheme: dark;
}

.tp-manual input[type="time"]:focus {
    border-color: var(--accent);
}

/* ─── COLOR PICKER ─── */
.cpicker {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    align-items: center;
}

.cswatch {
    width: 28px;
    height: 28px;
    border-radius: 8px;
    cursor: pointer;
    border: 2px solid transparent;
    transition: transform 0.12s;
    flex-shrink: 0;
}

.cswatch.sel {
    border-color: #fff;
    transform: scale(1.18);
}

.cswatch:hover {
    transform: scale(1.1);
}

/* The custom color swatch wraps a hidden <input type="color"> */
.cswatch-custom {
    position: relative;
    width: 28px;
    height: 28px;
    border-radius: 8px;
    border: 2px solid var(--border2);
    cursor: pointer;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: transform 0.12s, border-color 0.12s;
}

.cswatch-custom:hover {
    border-color: var(--text2);
    transform: scale(1.1);
}

.cswatch-custom.sel {
    border-color: #fff;
    transform: scale(1.18);
}

.cswatch-custom svg {
    pointer-events: none;
    position: relative;
    z-index: 1;
}

.cswatch-custom input[type="color"] {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    cursor: pointer;
    border: none;
    padding: 0;
}

/* ─── SECTIONS ─── */
.scard {
    background: var(--bg3);
    border: 1px solid var(--border);
    border-radius: var(--card-r);
    padding: 14px;
    margin-bottom: 12px;
}

.scard-title {
    font-size: 11px;
    font-weight: 500;
    color: var(--text3);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 10px;
}

.urow {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 5px 0;
    border-bottom: 1px solid var(--border);
}

.urow:last-child {
    border-bottom: none;
}

.uav {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--accent-bg);
    border: 1px solid rgba(124, 111, 247, 0.18);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: 600;
    color: var(--accent2);
    flex-shrink: 0;
    overflow: hidden;
}

.uav img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

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

.uname {
    font-size: 14px;
    color: var(--text);
}

.uemail {
    font-size: 11px;
    color: var(--text3);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.role-tag {
    font-size: 11px;
    padding: 2px 8px;
    border-radius: 99px;
}

.role-owner {
    background: var(--accent-bg);
    color: var(--accent2);
}

.role-editor {
    background: var(--green-bg);
    color: var(--green);
}

/* ─── INSTALL BANNER ─── */
#install-banner {
    display: flex;
    background: var(--bg3);
    border: 0 solid var(--border2);
    border-radius: var(--card-r);
    margin: 0 12px;
    padding: 0 12px;
    align-items: center;
    gap: 10px;
    cursor: pointer;
    flex-shrink: 0;
    max-height: 0;
    opacity: 0;
    transform: translateY(-20px);
    overflow: hidden;
    pointer-events: none;
    transition: max-height 0.4s ease, margin 0.4s ease, padding 0.4s ease, border-width 0.4s ease, opacity 0.4s ease, transform 0.4s ease;
}
#install-banner.show {
    max-height: 60px;
    margin: 8px 12px;
    padding: 10px 12px;
    border-width: 1px;
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
}

#install-banner .ib-icon {
    width: 34px;
    height: 34px;
    border-radius: 10px;
    background: var(--accent-bg);
    border: 1px solid rgba(124, 111, 247, 0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

#install-banner .ib-text {
    flex: 1;
}

#install-banner .ib-title {
    font-size: 13px;
    font-weight: 600;
    color: var(--text);
}

#install-banner .ib-sub {
    font-size: 11px;
    color: var(--text3);
}

#install-banner .ib-close {
    background: none;
    border: none;
    color: var(--text3);
    font-size: 20px;
    cursor: pointer;
    line-height: 1;
    padding: 0 2px;
}

/* ─── TOAST ─── */
#toast {
    position: fixed;
    bottom: calc(72px + var(--safe-bottom));
    left: 50%;
    transform: translateX(-50%) translateY(12px);
    background: var(--bg4);
    border: 1px solid var(--border2);
    border-radius: 10px;
    padding: 8px 16px;
    font-size: 13px;
    color: var(--text);
    z-index: 200;
    pointer-events: none;
    opacity: 0;
    transition: all 0.18s;
    white-space: nowrap;
    max-width: calc(100vw - 48px);
    overflow: hidden;
    text-overflow: ellipsis;
}

#toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* ─── CONNECTING ─── */
#connecting {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    background: var(--amber);
    color: #000;
    text-align: center;
    font-size: 12px;
    font-weight: 600;
    padding: 4px;
    z-index: 300;
}