/* === Reset & Basis === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: #f5f5f7;
    color: #1d1d1f;
    line-height: 1.6;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* === Navigation === */
nav {
    background: #1d1d1f;
    color: white;
    padding: 0 1.5rem;
    position: sticky;
    top: 0;
    z-index: 100;
}

.nav-inner {
    max-width: 960px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 56px;
}

.logo {
    color: white;
    text-decoration: none;
    font-size: 1.1rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.nav-logo {
    width: 28px;
    height: 28px;
}

.nav-links { display: flex; gap: 1.5rem; }
.nav-links a {
    color: #a1a1a6;
    text-decoration: none;
    font-size: 0.9rem;
    transition: color 0.2s;
}
.nav-links a:hover { color: white; }

/* === Main === */
main {
    max-width: 960px;
    margin: 0 auto;
    padding: 2rem 1.5rem;
    width: 100%;
    flex: 1;
}

/* === Flash Messages === */
.flash {
    padding: 0.75rem 1rem;
    border-radius: 8px;
    margin-bottom: 1rem;
    font-size: 0.9rem;
}
.flash-success { background: #d4edda; color: #155724; }
.flash-error { background: #f8d7da; color: #721c24; }
.flash-info { background: #d1ecf1; color: #0c5460; }

/* === Header Row === */
.header-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
    flex-wrap: wrap;
    gap: 0.75rem;
}
.header-left { display: flex; align-items: baseline; gap: 0.75rem; }
.header-stats {
    font-size: 0.85rem;
    color: #86868b;
    font-weight: 400;
}
.header-actions { display: flex; gap: 0.4rem; align-items: center; }

h1 { font-size: 1.5rem; font-weight: 700; }

.section-title {
    font-size: 1.15rem;
    font-weight: 600;
    margin: 1.5rem 0 0.75rem;
    display: flex;
    align-items: baseline;
    gap: 0.5rem;
}

/* === Buttons === */
.btn {
    display: inline-block;
    padding: 0.5rem 1rem;
    border: none;
    border-radius: 8px;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    text-decoration: none;
    transition: opacity 0.2s;
    text-align: center;
}
.btn:hover { opacity: 0.85; }
.btn-primary { background: #0071e3; color: white; }
.btn-secondary { background: #e8e8ed; color: #1d1d1f; }
.btn-danger { background: #ff3b30; color: white; }
.btn-small { padding: 0.35rem 0.7rem; font-size: 0.8rem; }
.btn-notaus { background: #ff3b30; color: white; }
.btn-activate-all { background: #34c759; color: white; }

.inline-form { display: inline; }

/* === Toolbar === */
.toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.75rem;
    gap: 0.75rem;
    flex-wrap: wrap;
}

.filter-pills { display: flex; gap: 0.35rem; }
.pill {
    padding: 0.3rem 0.75rem;
    border: 1px solid #d2d2d7;
    border-radius: 999px;
    background: white;
    font-size: 0.8rem;
    color: #6e6e73;
    cursor: pointer;
    transition: all 0.15s;
    font-weight: 500;
}
.pill:hover { border-color: #0071e3; color: #0071e3; }
.pill.active {
    background: #0071e3;
    color: white;
    border-color: #0071e3;
}

.sort-select {
    padding: 0.3rem 0.6rem;
    border: 1px solid #d2d2d7;
    border-radius: 8px;
    font-size: 0.8rem;
    color: #6e6e73;
    background: white;
    cursor: pointer;
}

/* === Compact Search List === */
.search-list {
    display: flex;
    flex-direction: column;
    gap: 2px;
    background: #e8e8ed;
    border-radius: 12px;
    overflow: hidden;
}

.search-row {
    background: white;
    transition: background 0.15s;
}
.search-row.inactive { opacity: 0.55; }
.search-row.inactive:hover { opacity: 0.8; }

.row-main {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.65rem 1rem;
    cursor: pointer;
    gap: 0.75rem;
    user-select: none;
}
.row-main:hover { background: #f9f9fb; }

.row-left {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    min-width: 0;
    flex: 1;
}

.row-status {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
}
.status-on { background: #34c759; box-shadow: 0 0 4px rgba(52, 199, 89, 0.4); }
.status-off { background: #d2d2d7; }

.row-name {
    font-weight: 600;
    font-size: 0.9rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.row-keyword {
    font-size: 0.78rem;
    color: #86868b;
    background: #f5f5f7;
    padding: 0.1rem 0.45rem;
    border-radius: 4px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 180px;
}

.row-right {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-shrink: 0;
}

.row-hits {
    font-size: 0.78rem;
    color: #86868b;
    white-space: nowrap;
}

.row-badge {
    font-size: 0.7rem;
    padding: 0.15rem 0.4rem;
    border-radius: 4px;
    white-space: nowrap;
}
.badge-ai {
    background: #f0e6ff;
    color: #7c3aed;
}

.row-chevron {
    font-size: 0.75rem;
    color: #c7c7cc;
    transition: transform 0.2s;
    width: 12px;
    text-align: center;
}
.search-row.expanded .row-chevron { transform: rotate(90deg); }

/* Row details (collapsed by default) */
.row-details {
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.25s ease;
}
.search-row.expanded .row-details {
    max-height: 300px;
}

.details-grid {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    padding: 0 1rem 0.75rem 2rem;
    gap: 1rem;
    flex-wrap: wrap;
}

.details-info {
    display: flex;
    flex-wrap: wrap;
    gap: 0.3rem 1.2rem;
}

.details-actions {
    display: flex;
    gap: 0.35rem;
    flex-wrap: wrap;
    align-items: center;
}

/* === Empty State === */
.empty-state {
    text-align: center;
    padding: 3rem 1rem;
    color: #86868b;
}
.empty-state p { margin-bottom: 1rem; font-size: 1rem; }

/* === Forms === */
.search-form {
    max-width: 640px;
}

fieldset {
    border: 1px solid #e8e8ed;
    border-radius: 12px;
    padding: 1.25rem;
    margin-bottom: 1.25rem;
    background: white;
}

legend {
    font-weight: 600;
    font-size: 0.95rem;
    padding: 0 0.5rem;
    color: #1d1d1f;
}

.form-group {
    margin-bottom: 1rem;
}
.form-group:last-child { margin-bottom: 0; }

.form-group label {
    display: block;
    font-size: 0.85rem;
    font-weight: 500;
    margin-bottom: 0.3rem;
    color: #1d1d1f;
}

.form-group input[type="text"],
.form-group input[type="number"],
.form-group select {
    width: 100%;
    padding: 0.55rem 0.75rem;
    border: 1px solid #d2d2d7;
    border-radius: 8px;
    font-size: 0.9rem;
    background: #fafafa;
    transition: border-color 0.2s;
}
.form-group input:focus,
.form-group select:focus {
    outline: none;
    border-color: #0071e3;
    background: white;
}

.form-group small {
    display: block;
    font-size: 0.75rem;
    color: #86868b;
    margin-top: 0.25rem;
}

.form-row {
    display: flex;
    gap: 1rem;
}
.form-group.half { flex: 1; }

.checkbox-label {
    display: flex !important;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
}
.checkbox-label input { width: auto; }

.form-actions {
    display: flex;
    gap: 0.75rem;
    margin-top: 1.5rem;
}

/* === Logs === */
.log-container {
    background: #1d1d1f;
    color: #a1ffa1;
    border-radius: 12px;
    padding: 1.25rem;
    font-family: "SF Mono", "Fira Code", monospace;
    font-size: 0.8rem;
    max-height: 70vh;
    overflow-y: auto;
}

.log-line {
    padding: 0.15rem 0;
    border-bottom: 1px solid #333;
    word-break: break-all;
}

/* === Footer === */
footer {
    text-align: center;
    padding: 1.5rem;
    color: #86868b;
    font-size: 0.8rem;
}

/* === Responsive === */
@media (max-width: 768px) {
    main { padding: 1.25rem 1rem; }

    /* Nav: hamburger-style stacking */
    .nav-inner {
        flex-wrap: wrap;
        height: auto;
        padding: 0.5rem 0;
        gap: 0.25rem;
    }

    .nav-links {
        width: 100%;
        flex-wrap: wrap;
        gap: 0.5rem 1rem;
        padding-top: 0.25rem;
        font-size: 0.85rem;
    }

    /* Header */
    .header-row {
        flex-direction: column;
        align-items: flex-start;
    }

    .header-actions {
        width: 100%;
        flex-wrap: wrap;
    }

    h1 { font-size: 1.3rem; }

    /* Toolbar */
    .toolbar {
        flex-direction: column;
        align-items: stretch;
    }

    .filter-pills {
        flex-wrap: wrap;
    }

    .sort-select { width: 100%; }

    /* Search rows */
    .row-main {
        padding: 0.5rem 0.75rem;
        gap: 0.4rem;
    }

    .row-left {
        min-width: 0;
        flex: 1;
    }

    .row-name {
        font-size: 0.85rem;
        max-width: 140px;
    }

    .row-keyword { display: none; }

    .row-right {
        flex-wrap: wrap;
        justify-content: flex-end;
        gap: 0.3rem;
    }

    .row-hits { font-size: 0.72rem; }
    .row-badge { font-size: 0.65rem; padding: 0.1rem 0.3rem; }

    .badge {
        font-size: 0.7rem;
        padding: 0.15rem 0.4rem;
    }

    /* Row details */
    .details-grid {
        flex-direction: column;
        padding: 0.5rem 0.75rem 0.75rem 0.75rem;
        gap: 0.5rem;
    }

    .details-info {
        flex-direction: column;
        gap: 0.25rem;
    }

    .detail { font-size: 0.82rem; }

    .details-actions {
        width: 100%;
        flex-wrap: wrap;
    }

    .details-actions .btn {
        flex: 1;
        min-width: calc(50% - 0.2rem);
        text-align: center;
        font-size: 0.75rem;
        padding: 0.4rem 0.5rem;
    }

    /* Search row expanded height */
    .search-row.expanded .row-details {
        max-height: 500px;
    }

    /* Forms */
    .form-container { max-width: 100%; }
    .form-row { flex-direction: column; gap: 0; }
    fieldset { padding: 1rem; }

    .form-actions {
        flex-direction: column;
    }

    .form-actions .btn { width: 100%; }

    /* Login */
    .login-container { min-height: 50vh; padding: 1rem; }
    .login-card {
        margin: 0;
        padding: 1.5rem;
        max-width: 100%;
    }

    /* Telegram status */
    .telegram-status {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.5rem;
    }

    /* Profile password inputs */
    .form-group input[type="password"] {
        width: 100%;
    }

    /* Logs */
    .log-container {
        font-size: 0.7rem;
        padding: 0.75rem;
        border-radius: 8px;
    }

    .log-line { word-break: break-word; }

    /* Flash */
    .flash { font-size: 0.82rem; }
    .flash a { word-break: break-all; }

    /* Footer */
    footer { padding: 1rem; font-size: 0.75rem; }
}

/* Extra small (iPhone SE etc.) */
@media (max-width: 380px) {
    nav { padding: 0 0.75rem; }
    main { padding: 1rem 0.75rem; }

    .nav-links {
        gap: 0.4rem 0.75rem;
        font-size: 0.8rem;
    }

    .row-name { max-width: 110px; font-size: 0.8rem; }

    .details-actions .btn {
        min-width: 100%;
    }

    .btn-small { padding: 0.3rem 0.5rem; font-size: 0.75rem; }

    .header-actions .btn-small { font-size: 0.72rem; }
}

/* === Login === */
.login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 60vh;
}

.login-card {
    background: white;
    border-radius: 12px;
    padding: 2.5rem;
    width: 100%;
    max-width: 380px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}

.login-card h1 {
    text-align: center;
    margin-bottom: 1.5rem;
    font-size: 1.5rem;
}

.btn-full {
    width: 100%;
    margin-top: 0.5rem;
}

/* === Nav Logout === */
.nav-logout {
    display: inline;
}

.btn-nav-logout {
    background: none;
    border: none;
    color: #86868b;
    cursor: pointer;
    font-size: 0.85rem;
    padding: 0.25rem 0.5rem;
    border-radius: 6px;
    transition: color 0.2s, background 0.2s;
}

.btn-nav-logout:hover {
    color: white;
    background: rgba(255,255,255,0.1);
}

/* === Telegram Status === */
.telegram-status {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0.75rem 1rem;
    border-radius: 8px;
    margin-bottom: 0.5rem;
}

.telegram-connected {
    background: #e8f5e9;
    color: #2e7d32;
}

.telegram-disconnected {
    background: #fff3e0;
    color: #e65100;
}

.form-hint {
    color: #86868b;
    font-size: 0.85rem;
    margin-top: 0.5rem;
}

/* === Form Container === */
.form-container {
    max-width: 640px;
}

/* === Badges === */
.badge {
    display: inline-block;
    font-size: 0.75rem;
    padding: 0.15rem 0.5rem;
    border-radius: 999px;
    white-space: nowrap;
    font-weight: 500;
}

.badge-active {
    background: #d4edda;
    color: #155724;
}

.badge-paused {
    background: #e8e8ed;
    color: #6e6e73;
}

/* === Password/API Key inputs === */
.form-group input[type="password"] {
    width: 100%;
    padding: 0.55rem 0.75rem;
    border: 1px solid #d2d2d7;
    border-radius: 8px;
    font-size: 0.9rem;
    background: #fafafa;
    transition: border-color 0.2s;
}

.form-group input[type="password"]:focus {
    outline: none;
    border-color: #0071e3;
    background: white;
}
