/* Mini App v6. */

:root {
    --bg:           #2b2546;
    --bg-elevated: #36315a;
    --bg-hover:    #423d6f;
    --bg-pressed:  #4d4783;
    --bg-card:     #322c54;
    --text:        #f4f1ff;
    --text-muted:  #b7afd9;
    --hint:        #8b85b3;
    --border:      rgba(255,255,255,0.06);
    --accent:      #a78bfa;
    --accent-2:    #c4b5fd;
    --success:     #86efac;
    --danger:      #fb7185;
    --radius:      14px;
    --radius-sm:   10px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; min-width: 0; }
html, body {
    margin: 0; padding: 0;
    background: var(--bg);
    color: var(--text);
    font: 14px/1.3 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", system-ui, sans-serif;
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    overscroll-behavior: none;
    touch-action: pan-y;
}
button { font: inherit; color: inherit; background: none; border: none; cursor: pointer; padding: 0; }
h1, h2 { font-weight: 600; margin: 0; }

#app {
    height: 100%;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 6px 10px env(safe-area-inset-bottom, 10px);
    max-width: 480px;
    margin: 0 auto;
}

/* ── Topbar ──────────────────────────────────────────────────── */
.topbar {
    display: grid;
    grid-template-columns: 36px 1fr 36px;
    align-items: center;
    gap: 6px;
    margin: 2px 0 8px;
    min-height: 36px;
}
.iconbtn {
    width: 36px; height: 36px;
    border-radius: 10px;
    background: var(--bg-elevated);
    font-size: 16px;
    display: flex; align-items: center; justify-content: center;
}
.iconbtn:active { background: var(--bg-pressed); }
.iconbtn-placeholder { width: 36px; }
.back-btn { font-size: 20px; }
.topbar h1 { text-align: center; font-size: 15px; }

/* ── Display ─────────────────────────────────────────────────── */
.display {
    text-align: center;
    display: flex; flex-direction: column; align-items: center; gap: 1px;
}
.display-amount {
    font-size: 26px;
    font-weight: 300;
    letter-spacing: -0.3px;
    line-height: 1.1;
}
.display-amount.empty { color: var(--hint); }
.display-currency {
    font-size: 11px;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.4px;
    display: flex; gap: 4px; align-items: center;
}

/* ── Numpad ──────────────────────────────────────────────────── */
.numpad {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
    margin: 6px 0;
}
.numpad button {
    aspect-ratio: 2;
    border-radius: var(--radius-sm);
    background: var(--bg-elevated);
    font-size: 20px;
    font-weight: 300;
    transition: background .08s;
}
.numpad button:active { background: var(--accent); color: var(--bg); }

/* ── Side actions ────────────────────────────────────────────── */
.side-actions {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
    margin: 4px 0 8px;
}
.sidebtn {
    background: var(--bg-elevated);
    border-radius: var(--radius-sm);
    padding: 6px 4px;
    font-size: 15px;
    display: flex; flex-direction: row; align-items: center; justify-content: center;
    gap: 6px;
    min-height: 38px;
}
.sidebtn span { font-size: 11px; color: var(--text-muted); }
.sidebtn:active { background: var(--bg-pressed); }
.sidebtn.has-note { background: var(--accent); color: var(--bg); }
.sidebtn.has-note span { color: var(--bg); opacity: 0.7; }

/* ── Categories: настоящий translate-slider ──────────────────── */
.categories { margin-top: 4px; touch-action: pan-y; }
.categories-viewport {
    overflow: hidden;
    border-radius: 4px;
}
.categories-track {
    display: flex;
    width: 100%;
    transition: transform .28s cubic-bezier(0.32, 0.72, 0, 1);
    will-change: transform;
}
.categories-track.dragging { transition: none; }
.categories-page {
    width: 100%;
    flex-shrink: 0;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 6px;
}
.cat {
    aspect-ratio: 1;
    border-radius: var(--radius);
    background: var(--bg-elevated);
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 2px;
    padding: 4px;
    transition: transform .06s, filter .1s;
    position: relative;
    overflow: hidden;
    color: var(--bg);
}
.cat::after {
    content: ""; position: absolute; inset: 0;
    background: linear-gradient(180deg, rgba(255,255,255,0.04), rgba(0,0,0,0.18));
    pointer-events: none;
}
.cat:active { transform: scale(0.94); filter: brightness(0.85); }
.cat .emoji { font-size: 24px; line-height: 1; }
.cat .name {
    font-size: 10px;
    text-align: center;
    line-height: 1.1;
    font-weight: 600;
    color: rgba(0,0,0,0.75);
}
.cat.disabled { opacity: 0.35; pointer-events: none; }
.cat-spacer { aspect-ratio: 1; visibility: hidden; }

.pager { display: flex; justify-content: center; gap: 5px; margin-top: 6px; }
.pager .dot {
    width: 5px; height: 5px; border-radius: 50%;
    background: var(--text-muted); opacity: 0.35;
    transition: opacity .2s, background .2s;
}
.pager .dot.active { opacity: 1; background: var(--accent); }

/* ── Day groups ─────────────────────────────────────────────── */
.recent-days, .history-list { margin-top: 10px; }
.day-group { margin-bottom: 14px; }
.day-head {
    display: flex; align-items: baseline; justify-content: space-between;
    padding: 6px 4px 4px;
    margin-bottom: 4px;
    border-bottom: 1px solid var(--accent);
    color: var(--accent-2);
    gap: 8px;
}
.day-head .day-title {
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.6px;
    font-weight: 600;
    white-space: nowrap;
}
.day-head .day-title small {
    color: var(--text-muted);
    font-weight: 400;
    margin-left: 6px;
    letter-spacing: 0.5px;
}
.day-head .day-total {
    font-size: 13px;
    text-align: right;
    font-weight: 500;
    color: var(--text);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    min-width: 0;
}
.day-head .day-total .num { font-weight: 600; }
.day-head .day-total .flag { font-size: 13px; margin: 0 3px; }
.day-head .day-total .ccy {
    color: var(--text-muted);
    font-weight: 400;
    font-size: 11px;
    margin-right: 4px;
}

.day-rows { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 4px; }

/* Swipe-to-delete wrapper.
   КРИТИЧНО: НИКАКОГО background на wrapper'е — red только в reveal. */
.day-row-wrap {
    position: relative;
    border-radius: var(--radius-sm);
    overflow: hidden;
}
.day-row-wrap .reveal {
    position: absolute;
    top: 0; right: 0; bottom: 0;
    width: 64px;
    display: flex; align-items: center; justify-content: center;
    color: white;
    font-size: 20px;
    font-weight: 600;
    background: var(--danger);
    z-index: 1;
}
.day-row-wrap .reveal:active { background: #e85e7c; }
.day-row-wrap .day-row {
    position: relative;
    z-index: 2;
    background: var(--bg-card);
}

.day-row {
    display: flex; align-items: center; gap: 8px;
    padding: 8px 10px;
    background: var(--bg-card);
    border-radius: var(--radius-sm);
    font-size: 13px;
    transition: transform .22s ease;
    will-change: transform;
}
.day-row:active { background: var(--bg-hover); }
.day-row .icon {
    width: 32px; height: 32px;
    border-radius: 9px;
    display: flex; align-items: center; justify-content: center;
    font-size: 18px;
    flex-shrink: 0;
    color: rgba(0,0,0,0.75);
}
.day-row .body { flex: 1; min-width: 0; }
.day-row .name {
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
    font-weight: 500;
}
.day-row .note {
    color: var(--text-muted);
    font-size: 11px;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.day-row .amount {
    display: flex; align-items: baseline; gap: 4px;
    white-space: nowrap;
    flex-shrink: 0;
}
.day-row .amount .num { color: var(--text); font-weight: 600; font-size: 14px; }
.day-row .amount .flag { font-size: 14px; }
.day-row .amount .ccy { color: var(--text-muted); font-size: 11px; font-weight: 400; }

.history-hint { color: var(--hint); text-align: center; padding: 20px; font-size: 12px; }

/* ── Modal ───────────────────────────────────────────────────── */
.modal {
    position: fixed; inset: 0; z-index: 100;
    display: flex; align-items: flex-end;
}
.modal-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,0.55); }
.modal-card {
    position: relative;
    width: 100%;
    max-width: 480px;
    margin: 0 auto;
    background: var(--bg);
    border-radius: 20px 20px 0 0;
    padding: 14px 14px calc(14px + env(safe-area-inset-bottom));
    box-shadow: 0 -10px 28px rgba(0,0,0,0.4);
    max-height: 85vh;
    overflow-y: auto;
}
.modal-card h2 {
    font-size: 15px;
    margin-bottom: 10px;
    text-align: center;
    color: var(--text);
}
.modal-card .row {
    display: flex; align-items: center; gap: 10px;
    width: 100%;
    text-align: left;
    padding: 10px 12px;
    margin: 3px 0;
    background: var(--bg-elevated);
    border-radius: var(--radius-sm);
    font-size: 14px;
}
.modal-card .row:active { background: var(--bg-pressed); }
.modal-card .row-disabled { color: var(--hint); opacity: 0.65; }
.modal-card .row-disabled small {
    margin-left: auto;
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    opacity: 0.7;
}

.currency-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
}
.currency-grid button {
    padding: 10px 6px;
    background: var(--bg-elevated);
    border-radius: var(--radius-sm);
    font-size: 13px;
    display: flex; flex-direction: column; align-items: center; gap: 2px;
    min-height: 60px;
}
.currency-grid button.active { background: var(--accent); color: var(--bg); }
.currency-grid .ccy-flag { font-size: 20px; line-height: 1; }
.currency-grid .ccy-code { font-size: 13px; font-weight: 600; }
.currency-grid .ccy-name { font-size: 10px; opacity: 0.7; }

.date-manual {
    display: flex; align-items: center; gap: 10px; padding: 10px 12px;
    background: var(--bg-elevated); border-radius: var(--radius-sm); margin: 3px 0;
}
.date-manual span { color: var(--text-muted); font-size: 13px; flex-shrink: 0; }
.date-manual input {
    flex: 1; min-width: 0;
    padding: 6px;
    border-radius: 6px;
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--text);
    font-size: 13px;
    width: 100%;
}

.modal-card textarea {
    width: 100%;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text);
    padding: 8px 10px;
    font: inherit;
    resize: vertical;
    min-height: 70px;
}
.modal-card textarea::placeholder, .edit-block textarea::placeholder { color: var(--hint); }
.row-actions { display: flex; gap: 6px; margin-top: 10px; }
.row-actions button { flex: 1; padding: 11px; border-radius: var(--radius-sm); font-size: 14px; font-weight: 500; }
.row-actions .ghost { background: var(--bg-elevated); }
.row-actions .primary { background: var(--accent); color: var(--bg); }
.row-actions .danger  { background: var(--danger); color: var(--bg); }

/* ── Edit blocks ─────────────────────────────────────────────── */
.edit-block {
    background: var(--bg-elevated);
    border-radius: var(--radius-sm);
    padding: 8px 10px;
    margin: 4px 0;
}
.edit-block .label {
    font-size: 10px;
    text-transform: uppercase;
    color: var(--text-muted);
    letter-spacing: 0.5px;
    margin-bottom: 4px;
    display: block;
}
.edit-block input, .edit-block textarea, .edit-block select {
    width: 100%;
    min-width: 0;
    max-width: 100%;
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--text);
    padding: 8px 10px;
    border-radius: 8px;
    font: inherit;
    font-size: 14px;
    box-sizing: border-box;
}
.edit-block-date input {
    min-height: 36px;
    -webkit-appearance: none;
    appearance: none;
}

/* Inline вариант: label слева, input справа */
.edit-block.inline {
    display: flex;
    align-items: center;
    gap: 12px;
}
.edit-block.inline .label {
    flex: 0 0 70px;
    margin-bottom: 0;
}
.edit-block.inline input,
.edit-block.inline select {
    flex: 1;
}

.cat-mini-grid {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 4px;
}
.cat-mini-grid button {
    aspect-ratio: 1;
    border-radius: 10px;
    background: var(--bg);
    font-size: 18px;
    color: var(--bg);
}
.cat-mini-grid button.active {
    outline: 2px solid var(--accent);
    outline-offset: -2px;
}

/* ── Focus guard ──────────────────────────────────────────────
   Когда любой input в modal-card сфокусирован, добавляется prevent-shield.
   Тап вне input — глобально blur, без активации других элементов. */
.modal-card[data-focused="1"] .focus-shield {
    position: absolute;
    inset: 0;
    z-index: 50;
    background: transparent;
}

/* ── Toast ───────────────────────────────────────────────────── */
.toast {
    position: fixed;
    bottom: calc(16px + env(safe-area-inset-bottom, 0px));
    left: 50%; transform: translateX(-50%);
    background: var(--bg-elevated);
    color: var(--text);
    padding: 8px 16px;
    border-radius: 10px;
    z-index: 200;
    box-shadow: 0 4px 16px rgba(0,0,0,0.4);
    font-size: 13px;
    max-width: 90%;
}
.toast.ok { border-left: 3px solid var(--success); }
.toast.err { border-left: 3px solid var(--danger); }
.toast.show { animation: toastIn 0.18s ease-out; }
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 10px); } to { opacity: 1; transform: translate(-50%, 0); } }

[hidden] { display: none !important; }
