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

html {
    overflow-x: hidden;
}

body {
    font-family: 'Segoe UI', Helvetica, Arial, sans-serif;
    max-width: 820px;
    margin: 0 auto;
    padding: 24px 20px 40px;
    background: radial-gradient(120% 90% at 50% 0%, #eef6fd 0%, #dbe7f2 100%);
    background-attachment: fixed;
    color: #14171a;
    min-height: 100vh;
}

header {
    background: linear-gradient(180deg, #37b0f7 0%, #1187dd 100%);
    border-radius: 16px 16px 0 0;
    padding: 16px 20px;
    text-align: center;
    box-shadow: 0 10px 30px rgba(10, 37, 64, 0.18);
}

header h1 {
    margin: 0;
    color: #fff;
    font-size: 1.6em;
    letter-spacing: 0.3px;
}

.container {
    background-color: white;
    border-radius: 0 0 16px 16px;
    box-shadow: 0 10px 30px rgba(10, 37, 64, 0.18);
    padding: 22px;
}

#setup-container,
#login-container,
#app-container {
    display: none;
}

.tweet {
    background: #fff;
    border: 1px solid #eef2f5;
    border-radius: 14px;
    padding: 14px;
    margin-bottom: 12px;
    box-shadow: 0 1px 3px rgba(10, 37, 64, 0.06);
    transition: box-shadow 0.2s ease;
}

.tweet:hover {
    box-shadow: 0 4px 14px rgba(10, 37, 64, 0.10);
}

.tweet-main {
    display: flex;
    gap: 12px;
    align-items: flex-start;
}

.tweet-body {
    flex: 1;
    min-width: 0;
}

.tweet-header {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-bottom: 3px;
}

.tweet-user {
    font-weight: bold;
}

.tweet-time {
    color: #8295a6;
    font-size: 0.85em;
    margin-left: auto;
}

.tweet-content {
    margin: 5px 0;
    overflow-wrap: anywhere;
}

.tweet-actions {
    display: flex;
    justify-content: flex-end;
    margin-top: 5px;
}

.delete-tweet-button {
    background: rgba(224, 36, 94, 0.1);
    color: #e0245e;
    border: 1px solid rgba(224, 36, 94, 0.3);
    border-radius: 6px;
    padding: 3px 10px;
    font-size: 0.8em;
    cursor: pointer;
    box-shadow: none;
}

.delete-tweet-button:hover {
    background: rgba(224, 36, 94, 0.2);
    filter: none;
    box-shadow: none;
}

/* ---- Feed pagination / windowing controls ---- */
.feed-footer {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    padding: 14px 0 4px;
}

.feed-more-button {
    background: rgba(29, 161, 242, 0.12);
    color: #1da1f2;
    border: 1px solid rgba(29, 161, 242, 0.35);
    border-radius: 999px;
    padding: 8px 18px;
    font-size: 0.9em;
    cursor: pointer;
    box-shadow: none;
}

.feed-more-button:hover {
    background: rgba(29, 161, 242, 0.2);
    filter: none;
    box-shadow: none;
}

.feed-more-button:disabled {
    opacity: 0.6;
    cursor: default;
}

/* "Show newest" sits above the list and spans its width */
.feed-newer-button {
    display: block;
    width: 100%;
    margin-bottom: 10px;
}

.feed-peer-button {
    background: rgba(121, 75, 196, 0.12);
    color: #794bc4;
    border-color: rgba(121, 75, 196, 0.35);
}

.feed-peer-button:hover {
    background: rgba(121, 75, 196, 0.2);
}

.feed-end-note {
    color: #8899a6;
    font-size: 0.85em;
    margin: 6px 0;
}

/* Zero-height tripwire the IntersectionObserver watches to auto-load */
.feed-sentinel {
    width: 100%;
    height: 1px;
}

/* ---- Identity / trust indicators ---- */
.tweet-fingerprint {
    color: #8899a6;
    font-weight: 400;
    font-size: 0.85em;
    margin-left: 2px;
}

.trust-badge {
    font-size: 0.72em;
    padding: 1px 7px;
    border-radius: 999px;
    margin-left: 6px;
    white-space: nowrap;
    cursor: help;
}

.trust-verified {
    background: rgba(23, 191, 99, 0.12);
    color: #17bf63;
    border: 1px solid rgba(23, 191, 99, 0.35);
}

.trust-unverified {
    background: rgba(136, 153, 166, 0.12);
    color: #8899a6;
    border: 1px solid rgba(136, 153, 166, 0.3);
}

.trust-conflict {
    background: rgba(224, 36, 94, 0.14);
    color: #e0245e;
    border: 1px solid rgba(224, 36, 94, 0.4);
    font-weight: 600;
}

/* ---- Connect / invite / QR scanner ---- */
.connect-actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    margin-top: 8px;
}

#scan-qr-button {
    background: rgba(29, 161, 242, 0.12);
    color: #1da1f2;
    border: 1px solid rgba(29, 161, 242, 0.35);
}

.invite-actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    margin-top: 8px;
}

.qr-scanner-overlay {
    position: fixed;
    inset: 0;
    z-index: 1000;
    background: rgba(0, 0, 0, 0.9);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
    padding: 20px;
}

.qr-scanner-video {
    width: min(92vw, 420px);
    height: min(92vw, 420px);
    object-fit: cover;
    border-radius: 16px;
    background: #000;
    box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.15), 0 0 0 9999px rgba(0, 0, 0, 0.4);
}

.qr-scanner-status {
    color: #fff;
    font-size: 0.95em;
    text-align: center;
    max-width: 420px;
}

.qr-scanner-cancel {
    background: rgba(255, 255, 255, 0.15);
    color: #fff;
    border: 1px solid rgba(255, 255, 255, 0.4);
    border-radius: 999px;
    padding: 10px 28px;
    font-size: 1em;
    cursor: pointer;
}

/* ---- ✨ Spark reaction ---- */
.spark-button {
    background: rgba(121, 75, 196, 0.10);
    color: #794bc4;
    border: 1px solid rgba(121, 75, 196, 0.30);
    border-radius: 999px;
    padding: 3px 12px;
    font-size: 0.85em;
    cursor: pointer;
    box-shadow: none;
    transition: transform 0.08s ease, background 0.15s ease;
}

.spark-button:hover {
    background: rgba(121, 75, 196, 0.2);
    filter: none;
    box-shadow: none;
}

.spark-button:active {
    transform: scale(1.15);
}

.spark-button.sparked {
    background: rgba(232, 164, 0, 0.18);
    color: #b07d00;
    border-color: rgba(232, 164, 0, 0.5);
    font-weight: 600;
}

.spark-button.spark-readonly {
    cursor: default;
    opacity: 0.85;
}

.spark-button.spark-readonly:hover {
    background: rgba(121, 75, 196, 0.10);
}

/* ---- Identity backup (import on login) ---- */
.login-import {
    margin-top: 18px;
    padding-top: 14px;
    border-top: 1px solid rgba(136, 153, 166, 0.25);
}

.login-import input[type="file"] {
    display: block;
    margin: 8px 0;
    width: 100%;
}

/* Media-preview buttons set their own background inline — clear the gradient image */
.media-upload-button,
.clear-media-button {
    background-image: none;
    box-shadow: none;
}

textarea {
    width: 100%;
    padding: 12px;
    border: 1px solid #cfd9de;
    border-radius: 12px;
    resize: none;
    margin-bottom: 10px;
    font-family: inherit;
    font-size: 14px;
    box-sizing: border-box;
}

textarea:focus,
input[type="text"]:focus {
    outline: none;
    border-color: #1da1f2;
    box-shadow: 0 0 0 3px rgba(29, 161, 242, 0.15);
}

button {
    background: linear-gradient(180deg, #34a8f5 0%, #1a8fe0 100%);
    color: white;
    border: none;
    border-radius: 30px;
    padding: 10px 20px;
    cursor: pointer;
    font-weight: bold;
    box-shadow: 0 2px 6px rgba(26, 143, 224, 0.35);
    transition: transform 0.08s ease, box-shadow 0.2s ease, filter 0.2s ease;
}

button:hover {
    filter: brightness(1.06);
    box-shadow: 0 4px 12px rgba(26, 143, 224, 0.42);
}

button:active {
    transform: translateY(1px);
}

.danger-button {
    background: linear-gradient(180deg, #f0436f 0%, #e0245e 100%);
    box-shadow: 0 2px 6px rgba(224, 36, 94, 0.3);
}

.peer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    padding: 12px;
    margin-bottom: 10px;
    background-color: #fff;
    border: 1px solid #eef2f5;
    border-radius: 12px;
    box-shadow: 0 1px 3px rgba(10, 37, 64, 0.05);
    cursor: default;
}

.peer-left {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

#peers-list {
    margin-top: 15px;
}

.peer:hover {
    background-color: #fbfdff;
    box-shadow: 0 3px 10px rgba(10, 37, 64, 0.08);
}

#qrcode,
.profile-qrcode {
    text-align: center;
    margin: 20px 0;
}

.profile-info {
    background-color: #f7f9fa;
    border-radius: 10px;
    padding: 15px;
    margin-bottom: 20px;
}

.profile-actions {
    margin-top: 30px;
}

.profile-actions button {
    margin-right: 10px;
    margin-bottom: 10px;
}

.profile-actions button:disabled {
    opacity: 0.6;
    cursor: default;
}

#tweet-form {
    margin-bottom: 20px;
}

.tab-container {
    display: flex;
    margin-bottom: 15px;
    border-bottom: 1px solid #e6ecf0;
}

.tab {
    padding: 10px 15px;
    cursor: pointer;
}

.tab.active {
    border-bottom: 2px solid #1da1f2;
    color: #1da1f2;
    font-weight: bold;
}

#connect-form {
    margin: 20px 0;
}

#status {
    color: #657786;
    margin: 10px 0;
}

.timeout {
    background-color: #95a5a6;
}

/* Gray color for timeout status */

.connection-quality {
    display: inline-block;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    margin-right: 10px;
}

#connection-quality.unknown {
    background-color: #ccc;
}

#connection-quality.good {
    background-color: #2ecc71;
}

#connection-quality.medium {
    background-color: #f1c40f;
}

#connection-quality.poor {
    background-color: #f39c12;
}

#connection-quality.offline {
    background-color: #e74c3c;
}

#connection-quality.error {
    background-color: #9b59b6;
}

.small-button {
    background-color: #1da1f2;
    color: white;
    border: none;
    border-radius: 4px;
    padding: 2px 8px;
    font-size: 0.8em;
    cursor: pointer;
    display: inline-block;
    margin-left: 8px;
}

.peer-name {
    font-weight: bold;
}

.peer-status {
    margin-top: 3px;
}

.peer-last-seen {
    margin-top: 2px;
}

.peer-item {
    display: flex;
    align-items: center;
    margin: 5px 0;
    padding: 5px;
    border-bottom: 1px solid #eee;
  }

/* ===== Circles: layout + sidebar ===== */
.app-body {
    display: flex;
    gap: 16px;
    align-items: flex-start;
}

#circles-sidebar {
    width: 180px;
    flex-shrink: 0;
    border-right: 1px solid #e6ecf0;
    padding-right: 12px;
}

#circles-sidebar h3 {
    margin: 0 0 10px;
    font-size: 0.8em;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #657786;
}

.app-main {
    flex: 1;
    min-width: 0;
}

.circle-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 10px;
    margin-bottom: 4px;
    border-radius: 8px;
    cursor: pointer;
}

.circle-item:hover {
    background-color: #f0f3f5;
}

.circle-item.active {
    background-color: #e8f5fd;
    color: #1da1f2;
    font-weight: bold;
}

.circle-item-count {
    background-color: #e1e8ed;
    color: #657786;
    border-radius: 10px;
    padding: 0 8px;
    font-size: 0.75em;
    font-weight: normal;
}

.sidebar-empty {
    color: #657786;
    font-size: 0.9em;
}

.sidebar-new-circle {
    display: flex;
    gap: 4px;
    margin-top: 12px;
}

.sidebar-new-circle input {
    flex: 1;
    min-width: 0;
    padding: 6px 8px;
    border: 1px solid #cfd9de;
    border-radius: 8px;
}

#sidebar-create-circle {
    padding: 6px 12px;
    border-radius: 8px;
}

/* ===== Circles: compose audience + badges ===== */
.tweet-form-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.cast-target {
    color: #657786;
    font-size: 0.9em;
    font-style: italic;
}

.circle-add-row select {
    padding: 6px 8px;
    border: 1px solid #cfd9de;
    border-radius: 8px;
    background-color: white;
}

/* Push the image button + Cast button to the right, image on the left of Cast */
.tweet-form-actions .media-upload-button {
    margin-left: auto;
    flex-shrink: 0;
}

.tweet-form-actions #tweet-button {
    margin-left: 0;
}

#feed-heading {
    font-weight: bold;
    color: #14171a;
    margin-bottom: 12px;
}

.tweet-circle-badge {
    background-color: #e8f5fd;
    color: #1da1f2;
    border-radius: 10px;
    padding: 1px 8px;
    font-size: 0.72em;
    margin-left: 8px;
}

/* ===== Circles: management tab ===== */
#create-circle-form {
    margin-bottom: 20px;
}

#create-circle-form input {
    padding: 8px;
    border: 1px solid #cfd9de;
    border-radius: 8px;
    margin-right: 8px;
}

.hint {
    color: #657786;
    font-size: 0.85em;
}

.circle-card {
    border: 1px solid #e6ecf0;
    border-radius: 10px;
    padding: 12px 14px;
    margin-bottom: 12px;
}

.circle-card-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
}

.circle-card-head h4 {
    margin: 0;
}

.circle-member-row,
.circle-add-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    margin: 6px 0;
}

.circle-member-row span {
    overflow-wrap: anywhere;
    min-width: 0;
}

/* Long peer IDs should wrap, never force the layout wider */
.peer-id {
    overflow-wrap: anywhere;
}

.circle-add-row select {
    flex: 1;
}

/* ===== Link previews + links ===== */
.tweet-link {
    color: #1da1f2;
    text-decoration: none;
    word-break: break-word;
}

.tweet-link:hover {
    text-decoration: underline;
}

.link-preview {
    display: flex;
    border: 1px solid #e1e8ed;
    border-radius: 12px;
    overflow: hidden;
    text-decoration: none;
    color: inherit;
    margin-top: 8px;
    max-width: 100%;
}

.link-preview:hover {
    background-color: #f7f9fa;
}

.link-preview-image {
    border: none;
}

.link-preview-image img {
    display: block;
    max-width: 100%;
    max-height: 300px;
    object-fit: cover;
}

.link-preview-generic {
    align-items: center;
    padding: 10px;
    gap: 10px;
}

.link-preview-favicon {
    width: 24px;
    height: 24px;
    flex-shrink: 0;
}

.link-preview-meta {
    min-width: 0;
}

.link-preview-site {
    font-weight: bold;
    font-size: 0.9em;
}

.link-preview-url {
    color: #657786;
    font-size: 0.8em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.link-preview-youtube {
    flex-direction: column;
}

.link-preview-thumb {
    position: relative;
}

.link-preview-thumb img {
    display: block;
    width: 100%;
    max-height: 300px;
    object-fit: cover;
}

.link-preview-play {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background-color: rgba(0, 0, 0, 0.7);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
}

.link-preview-youtube .link-preview-meta {
    padding: 10px;
}

/* ===== Form inputs ===== */
input[type="text"] {
    padding: 10px 12px;
    border: 1px solid #cfd9de;
    border-radius: 10px;
    font-family: inherit;
    font-size: 14px;
    box-sizing: border-box;
}

/* ===== Avatars ===== */
.avatar {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-weight: bold;
    font-size: 17px;
    flex-shrink: 0;
    box-shadow: 0 2px 5px rgba(10, 37, 64, 0.18);
    user-select: none;
}

/* ===== Top toolbar: greeting + connection pill ===== */
.user-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
    margin-bottom: 16px;
    padding-bottom: 14px;
    border-bottom: 1px solid #eef2f5;
}

.user-greeting {
    margin: 0;
    color: #3a5168;
}

.user-greeting #current-user {
    font-weight: bold;
    color: #14171a;
}

.status-pill {
    display: flex;
    align-items: center;
    gap: 8px;
    background: #f0f3f5;
    border-radius: 999px;
    padding: 5px 12px;
    font-size: 0.82em;
    color: #5b6b7a;
}

#status {
    color: inherit;
    margin: 0;
}

/* The connection-quality dot in the toolbar (color set by its quality class) */
#connection-quality {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    flex-shrink: 0;
}

/* ===== Compose box as a card ===== */
#tweet-form {
    margin-bottom: 20px;
    background: #fff;
    border: 1px solid #e7eef3;
    border-radius: 16px;
    padding: 14px;
    box-shadow: 0 1px 3px rgba(10, 37, 64, 0.05);
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

#tweet-form:focus-within {
    border-color: #9fd4f7;
    box-shadow: 0 0 0 3px rgba(29, 161, 242, 0.12);
}

#tweet-form textarea {
    border: none;
    padding: 6px;
    margin-bottom: 4px;
    background: transparent;
}

#tweet-form textarea:focus {
    box-shadow: none;
}

/* Hide the per-tweet media container when there's nothing in it */
.tweet-media-container:empty {
    display: none;
}

.tweet-media-container {
    margin-top: 8px;
}

/* Any image inside a message never exceeds the message width */
.tweet-media-container img,
.tweet-media,
.link-preview img {
    max-width: 100%;
}

/* =====================================================================
   Responsive layout
   ===================================================================== */

/* ----- Tablet: slimmer sidebar, tighter spacing ----- */
@media (max-width: 820px) {
    body {
        padding: 16px 14px 32px;
    }

    .container {
        padding: 18px;
    }

    .app-body {
        gap: 12px;
    }

    #circles-sidebar {
        width: 150px;
        padding-right: 10px;
    }
}

/* ----- Phones: stack everything, circles become a chip row ----- */
@media (max-width: 600px) {
    body {
        padding: 10px 8px 28px;
    }

    header {
        border-radius: 12px 12px 0 0;
        padding: 14px;
    }

    header h1 {
        font-size: 1.35em;
    }

    .container {
        padding: 14px;
        border-radius: 0 0 12px 12px;
    }

    .user-info {
        margin-bottom: 12px;
        padding-bottom: 12px;
    }

    /* Sidebar moves above the feed as a horizontal, scrollable chip row */
    .app-body {
        flex-direction: column;
        gap: 14px;
    }

    #circles-sidebar {
        width: auto;
        border-right: none;
        border-bottom: 1px solid #e6ecf0;
        padding-right: 0;
        padding-bottom: 14px;
    }

    #circles-sidebar h3 {
        margin-bottom: 8px;
    }

    #circles-sidebar-list {
        display: flex;
        flex-direction: row;
        gap: 8px;
        overflow-x: auto;
        padding-bottom: 6px;
        -webkit-overflow-scrolling: touch;
    }

    #circles-sidebar-list .circle-item {
        flex: 0 0 auto;
        margin-bottom: 0;
        white-space: nowrap;
    }

    .sidebar-empty {
        white-space: nowrap;
        margin: 0;
    }

    .sidebar-new-circle {
        margin-top: 10px;
    }

    /* Tabs scroll horizontally instead of overflowing */
    .tab-container {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
    }

    .tab-container::-webkit-scrollbar {
        display: none;
    }

    .tab {
        padding: 10px 12px;
        white-space: nowrap;
    }

    /* Smaller avatars and tighter cards so messages use the screen well */
    .avatar {
        width: 34px;
        height: 34px;
        font-size: 14px;
    }

    .tweet {
        padding: 11px;
    }

    .tweet-main {
        gap: 9px;
    }

    .tweet-header {
        flex-wrap: wrap;
    }

    .tweet-content {
        font-size: 0.95em;
    }

    /* Cap media height so a tall image doesn't dominate a small screen */
    .tweet-media-container img,
    .tweet-media,
    .link-preview-image img,
    .link-preview-thumb img {
        max-height: 240px;
    }

    /* Peer rows: details on top, actions wrap below full-width */
    .peer {
        flex-wrap: wrap;
    }

    .peer-left {
        flex: 1 1 100%;
    }

    .peer-actions {
        width: 100%;
        display: flex;
        gap: 8px;
        margin-top: 10px;
    }

    .peer-actions button {
        flex: 1;
        margin: 0 !important;
    }

    /* Circle management: stack controls */
    .circle-member-row,
    .circle-add-row {
        flex-wrap: wrap;
    }

    .circle-add-row select {
        width: 100%;
    }

    #create-circle-form input {
        width: 100%;
        margin: 0 0 8px 0;
    }

    #create-circle-form button {
        width: 100%;
    }

    /* Compose actions can wrap on very narrow screens */
    .tweet-form-actions {
        flex-wrap: wrap;
        gap: 10px;
    }
}

/* ----- Small phones ----- */
@media (max-width: 380px) {
    header h1 {
        font-size: 1.2em;
    }

    .tab {
        padding: 9px 10px;
        font-size: 0.95em;
    }

    .avatar {
        width: 30px;
        height: 30px;
        font-size: 13px;
    }

    .tweet {
        padding: 10px;
    }

    .tweet-circle-badge {
        display: none;
    }
}
