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

/* ===== TOKENS ===== */
:root {
    --bg: #141414;
    --surface: #1C1C1C;
    --surface-h: #242424;
    --border: #2A2A2A;
    --text: #E8E8E8;
    --text-2: #888;
    --accent: #4F8EF7;
    --accent-h: #3A7AE8;
    --accent-dim: rgba(79, 142, 247, .12);
    --danger: #E05252;
    --warn: #E09252;
    --radius: 12px;
    --header-h: 54px;
    --font: 'Inter', system-ui, sans-serif;
    --font-mono: 'JetBrains Mono', monospace;
    --fs: 14px;
}

:root.light {
    --bg: #F0F0F0;
    --surface: #FFFFFF;
    --surface-h: #EBEBEB;
    --border: #DEDEDE;
    --text: #1A1A1A;
    --text-2: #777;
    --accent: #1A73E8;
    --accent-h: #1558B0;
    --accent-dim: rgba(26, 115, 232, .10);
}

:root.black {
    --bg: #000000;
    --surface: #0A0A0A;
    --surface-h: #111111;
    --border: #1E1E1E;
    --text: #EDEEEF;
    --text-2: #555;
    --accent: #7BAAF7;
    --accent-h: #5E96F5;
    --accent-dim: rgba(123, 170, 247, .10);
}

html,
body {
    height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: var(--font);
    font-size: var(--fs);
    -webkit-font-smoothing: antialiased;
    overflow: hidden;
}

/* ===== APP ===== */
#app {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    max-width: 750px;
    margin: 0 auto;
}

/* ===== HEADER ===== */
#header {
    flex-shrink: 0;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: relative;
    z-index: 10;
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
    border-bottom-left-radius: var(--radius);
    border-bottom-right-radius: var(--radius);
}

#header-inner {
    display: flex;
    align-items: center;
    gap: 8px;
    height: var(--header-h);
    padding: 0 12px;
}

.logo {
    font-family: var(--font-mono);
    font-size: 16px;
    font-weight: 700;
    letter-spacing: -0.5px;
    color: var(--text);
    flex-shrink: 0;
    user-select: none;
}

.logo span {
    color: var(--accent);
}

/* ===== HEADER MIDDLE (search toggle + search wrap) ===== */
.header-middle {
    flex: 1;
    display: flex;
    align-items: center;
    min-width: 0;
    position: relative;
}

/* ===== SEARCH ===== */
.search-wrap {
    flex: 1;
    display: flex;
    align-items: center;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0 10px;
    gap: 6px;
    height: 38px;
    transition: border-color .15s;
    min-width: 0;
}

.search-wrap:focus-within {
    border-color: var(--accent);
}

.search-icon {
    color: var(--text-2);
    flex-shrink: 0;
}

#search-input {
    flex: 1;
    border: none;
    background: transparent;
    color: var(--text);
    font-family: var(--font);
    font-size: 13px;
    outline: none;
    min-width: 0;
    height: 100%;
}

#search-input::placeholder {
    color: var(--text-2);
}

#search-input::-webkit-search-cancel-button {
    display: none;
}

.search-clear-btn {
    color: var(--text-2);
    width: 24px;
    height: 24px;
    flex-shrink: 0;
}

.search-clear-btn:hover {
    color: var(--text);
    background: transparent;
}

/* Переопределяем .search-wrap для анимации раскрытия/скрытия */
.header-middle .search-wrap {
    flex: 0 0 auto;
    max-width: 0;
    opacity: 0;
    padding: 0;
    border-color: transparent;
    overflow: hidden;
    transition: max-width 0.25s ease, opacity 0.2s ease, padding 0.25s ease, border-color 0.25s ease;
}

.header-middle .search-wrap.open {
    flex: 1 1 0%;
    max-width: 100%;
    opacity: 1;
    padding: 0 10px;
    border-color: var(--border);
}

/* При фокусе внутри открытого поиска — акцентная рамка */
.header-middle .search-wrap.open:focus-within {
    border-color: var(--accent);
}

#btn-search-toggle {
    flex-shrink: 0;
}

/* ===== ICON BUTTONS ===== */
.btn-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    border: none;
    background: transparent;
    color: var(--text-2);
    border-radius: 8px;
    cursor: pointer;
    transition: background .15s, color .15s;
    flex-shrink: 0;
}

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

.btn-icon:active {
    opacity: .7;
}

.btn-add-main {
    background: var(--accent);
    color: #fff;
    width: 34px;
    height: 34px;
}

.btn-add-main:hover {
    background: var(--accent-h);
    color: #fff;
}

/* ===== LIST ===== */
#list-wrap {
    flex: 1;
    overflow-y: auto;
    padding: 10px 12px 20px;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

#list-wrap::-webkit-scrollbar {
    width: 3px;
}

#list-wrap::-webkit-scrollbar-track {
    background: transparent;
}

#list-wrap::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 2px;
}

#bm-list {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

/* ===== BOOKMARK CARD ===== */
.bm-card {
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0 8px 0 14px;
    height: 48px;
    cursor: pointer;
    transition: background .15s, border-color .15s;
    text-decoration: none;
    color: inherit;
    user-select: none;
}

.bm-card:hover {
    background: var(--surface-h);
    border-color: var(--accent-dim);
}

.bm-card:active {
    opacity: .8;
}

.bm-favicon {
    width: 18px;
    height: 18px;
    flex-shrink: 0;
    border-radius: 3px;
    object-fit: contain;
}

.bm-favicon-fallback {
    width: 18px;
    height: 18px;
    flex-shrink: 0;
    border-radius: 3px;
    background: var(--accent-dim);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 10px;
    color: var(--accent);
    font-weight: 700;
    font-family: var(--font-mono);
}

.bm-favicon-wrap {
    position: relative;
    width: 18px;
    height: 18px;
    flex-shrink: 0;
}

.bm-favicon-wrap .bm-favicon {
    position: absolute;
    inset: 0;
    transition: opacity .2s ease;
}

.bm-favicon-hover {
    opacity: 0;
}

.bm-card:hover .bm-favicon-hover {
    opacity: 1;
}

.bm-card:hover .bm-favicon:not(.bm-favicon-hover) {
    opacity: 0;
}

.bm-name {
    flex: 1;
    font-size: var(--fs);
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    color: var(--text);
}

.bm-menu-btn {
    color: var(--text-2);
    opacity: 0;
    transition: opacity .15s, color .15s;
    flex-shrink: 0;
}

.bm-card:hover .bm-menu-btn {
    opacity: 1;
}

.bm-menu-btn:hover {
    color: var(--text);
    background: var(--surface-h);
}

/* always show on touch */
@media (hover: none) {
    .bm-menu-btn {
        opacity: 1;
    }
}

/* ===== EMPTY STATE ===== */
.empty-state {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 8px;
    color: var(--text-2);
    font-size: 13px;
    text-align: center;
    opacity: .5;
    padding: 40px;
}

.empty-icon {
    font-size: 30px;
    margin-bottom: 4px;
}

.empty-hint {
    font-size: 11px;
    margin-top: 2px;
}

/* ===== OVERLAY ===== */
.overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .55);
    backdrop-filter: blur(4px);
    z-index: 100;
    align-items: flex-start;
    justify-content: center;
}

.overlay.open {
    display: flex;
}

/* ===== POPUP ===== */
.popup {
    background: var(--surface);
    border: 1px solid var(--border);
    border-top: none;
    border-radius: 0 0 16px 16px;
    width: 100%;
    max-width: 750px;
    overflow: hidden;
    box-shadow: 0 8px 32px rgba(0, 0, 0, .45);
}

/* Settings popup stays at bottom */
#popup-settings .popup {
    border-radius: 16px 16px 0 0;
    border-top: 1px solid var(--border);
    border-bottom: none;
    margin-top: 0;
    margin-bottom: 0;
    padding-bottom: env(safe-area-inset-bottom, 8px);
    align-self: flex-end;
}

#popup-settings {
    align-items: flex-end;
}

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

.popup-title {
    font-size: 11px;
    font-weight: 700;
    letter-spacing: .1em;
    text-transform: uppercase;
    color: var(--text-2);
    padding: 12px 16px 8px;
}

/* ===== POPUP FIELDS ===== */
.popup-field-group {
    padding: 6px 16px 10px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.field-row {
    display: flex;
    flex-direction: column;
    gap: 5px;
}

.field-label {
    font-size: 10px;
    font-weight: 600;
    letter-spacing: .06em;
    text-transform: uppercase;
    color: var(--text-2);
}

.field-optional {
    font-weight: 400;
    text-transform: none;
    letter-spacing: 0;
    opacity: .7;
}

.loading-wrap {
    position: relative;
}

.popup-input {
    width: 100%;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 10px 12px;
    font-family: var(--font);
    font-size: 13px;
    color: var(--text);
    outline: none;
    transition: border-color .15s;
}

.popup-input:focus {
    border-color: var(--accent);
}

.popup-input[readonly] {
    color: var(--text-2);
    font-style: italic;
}

.popup-input[type="url"] {
    font-family: var(--font-mono);
    font-size: 12px;
}

/* Input with clear button inside */
.input-clear-wrap {
    position: relative;
    display: flex;
    align-items: center;
}

.input-clear-wrap .popup-input {
    padding-right: 36px;
}

.input-clear-btn {
    position: absolute;
    right: 6px;
    top: 50%;
    transform: translateY(-50%);
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    background: transparent;
    color: var(--text-2);
    border-radius: 5px;
    cursor: pointer;
    opacity: 0;
    transition: opacity .15s, color .15s, background .15s;
    flex-shrink: 0;
    padding: 0;
}

.input-clear-btn.visible {
    opacity: 1;
}

.input-clear-btn:hover {
    color: var(--text);
    background: var(--surface-h);
}

/* spinner */
.spinner {
    position: absolute;
    right: 34px;
    top: 50%;
    transform: translateY(-50%);
    width: 14px;
    height: 14px;
    border: 2px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin .7s linear infinite;
}

@keyframes spin {
    to {
        transform: translateY(-50%) rotate(360deg);
    }
}

.popup-divider {
    height: 1px;
    background: var(--border);
    margin: 0 16px;
}

/* ===== POPUP ACTIONS ===== */
.popup-actions {
    display: flex;
    border-top: 1px solid var(--border);
    margin-top: 12px;
}

.popup-btn {
    flex: 1;
    height: 48px;
    border: none;
    background: transparent;
    color: var(--text-2);
    font-family: var(--font);
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: background .15s, color .15s;
}

.popup-btn:not(:last-child) {
    border-right: 1px solid var(--border);
}

.popup-btn:hover:not(:disabled) {
    background: var(--surface-h);
    color: var(--text);
}

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

.popup-btn.primary {
    color: var(--accent);
}

.popup-btn.primary:hover:not(:disabled) {
    color: var(--accent-h);
}

.popup-btn.danger {
    color: var(--danger);
}

.popup-btn.danger-active {
    background: var(--danger) !important;
    color: #fff !important;
}

.popup-btn.warn-text {
    color: var(--warn) !important;
}

/* ===== SETTINGS ===== */
.settings-section {
    padding: 10px 16px 14px;
    border-bottom: 1px solid var(--border);
}

.settings-section:last-of-type {
    border-bottom: none;
}

.settings-label {
    font-size: 10px;
    font-weight: 700;
    letter-spacing: .1em;
    text-transform: uppercase;
    color: var(--text-2);
    margin-bottom: 10px;
}

.theme-switcher {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    display: flex;
    padding: 3px;
    position: relative;
    overflow: hidden;
}

.theme-switcher input {
    display: none;
}

.t-slider {
    position: absolute;
    z-index: 1;
    width: calc((100% - 6px) / 3);
    top: 3px;
    bottom: 3px;
    left: 3px;
    border-radius: 5px;
    background: var(--surface-h);
    border: 1px solid var(--border);
    transition: transform .2s cubic-bezier(.4, 0, .2, 1);
    pointer-events: none;
}

.theme-switcher input:nth-of-type(1):checked~.t-slider {
    transform: translateX(0);
}

.theme-switcher input:nth-of-type(2):checked~.t-slider {
    transform: translateX(100%);
}

.theme-switcher input:nth-of-type(3):checked~.t-slider {
    transform: translateX(200%);
}

.theme-switcher label {
    position: relative;
    z-index: 2;
    width: calc(100% / 3);
    cursor: pointer;
}

.theme-switcher label span {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
    padding: 7px 0;
    font-size: 11px;
    font-weight: 700;
    color: var(--text-2);
    letter-spacing: .04em;
    transition: color .15s;
}

.theme-switcher input:checked+label span {
    color: var(--text);
}

.slider-row {
    display: flex;
    align-items: center;
    gap: 10px;
}

.slider-val {
    font-size: 11px;
    color: var(--accent);
    width: 32px;
    text-align: right;
    flex-shrink: 0;
}

input[type=range] {
    flex: 1;
    -webkit-appearance: none;
    height: 3px;
    border-radius: 2px;
    background: var(--border);
    outline: none;
    cursor: pointer;
}

input[type=range]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-dim);
    transition: box-shadow .15s;
}

input[type=range]::-webkit-slider-thumb:hover {
    box-shadow: 0 0 0 5px var(--accent-dim);
}

.slider-ends {
    display: flex;
    justify-content: space-between;
    font-size: 10px;
    color: var(--text-2);
    margin-top: 4px;
}

/* ===== TOAST ===== */
#toast {
    position: fixed;
    bottom: 28px;
    left: 50%;
    transform: translateX(-50%) translateY(10px);
    background: var(--surface);
    border: 1px solid var(--border);
    color: var(--text);
    padding: 9px 18px;
    border-radius: 8px;
    font-size: 12px;
    font-weight: 600;
    box-shadow: 0 4px 24px rgba(0, 0, 0, .4);
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s, transform .2s;
    z-index: 300;
    white-space: nowrap;
}

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

#toast.ok {
    border-color: var(--accent);
    color: var(--accent);
}

#toast.err {
    border-color: var(--danger);
    color: var(--danger);
}