@import url('https://fonts.googleapis.com/css2?family=Fraunces:opsz,wght@9..144,400;9..144,500;9..144,600;9..144,700&family=Karla:wght@400;500;600;700&display=swap');

:root {
    --ink: #2b2118;
    --ink-soft: #5a4d3f;
    --parchment: #faf5ea;
    --parchment-deep: #f1e8d6;
    --card: #fffdf8;
    --line: #d9c9a8;
    --trunk: #7a5a3a;
    --forest: #33513f;
    --forest-deep: #22392b;
    --moss: #6f8c6a;
    --terracotta: #c1622d;
    --terracotta-deep: #a34f22;
    --dove: #8b8677;
    --gold: #b48a3f;
    --shadow: 0 1px 2px rgba(43, 33, 24, 0.06), 0 6px 18px rgba(43, 33, 24, 0.08);
    --shadow-lift: 0 4px 10px rgba(43, 33, 24, 0.1), 0 14px 30px rgba(43, 33, 24, 0.12);
    --radius: 16px 18px 16px 20px;
    --radius-sm: 10px 12px 10px 12px;
    --font-display: 'Fraunces', 'Iowan Old Style', Georgia, serif;
    --font-body: 'Karla', -apple-system, BlinkMacSystemFont, sans-serif;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }
html, body { overflow-x: hidden; max-width: 100%; }

body {
    margin: 0;
    font-family: var(--font-body);
    color: var(--ink);
    background-color: var(--parchment);
    background-image:
        radial-gradient(ellipse 900px 500px at 15% -10%, rgba(180, 138, 63, 0.12), transparent 60%),
        radial-gradient(ellipse 700px 500px at 100% 0%, rgba(51, 81, 63, 0.08), transparent 55%),
        url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='120' height='120'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix type='saturate' values='0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.035'/%3E%3C/svg%3E");
    min-height: 100vh;
    line-height: 1.5;
}

h1, h2, h3 { font-family: var(--font-display); font-weight: 600; color: var(--forest-deep); margin: 0; }

.app { max-width: 1100px; margin: 0 auto; padding: 18px 16px 60px; }

/* ---------- Topbar ---------- */
.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    flex-wrap: wrap;
    padding: 18px 20px;
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    margin-bottom: 16px;
}

.topbar-title { display: flex; align-items: center; gap: 14px; }
.topbar-title .logo { font-size: 2.1rem; filter: drop-shadow(0 2px 2px rgba(43,33,24,0.15)); }
.topbar-title h1 { font-size: 1.6rem; letter-spacing: 0.2px; }
.member-count { margin: 2px 0 0; font-size: 0.85rem; color: var(--ink-soft); }
.member-count span { font-weight: 700; color: var(--forest); }

.topbar-actions { display: flex; gap: 10px; flex-wrap: wrap; }

/* ---------- Buttons ---------- */
.btn {
    font-family: var(--font-body);
    font-weight: 600;
    font-size: 0.92rem;
    border: none;
    border-radius: var(--radius-sm);
    padding: 11px 18px;
    cursor: pointer;
    transition: transform 0.15s ease, box-shadow 0.15s ease, background-color 0.15s ease;
    white-space: nowrap;
}
.btn:active { transform: translateY(1px) scale(0.98); }

.btn-primary { background: var(--forest); color: #fdf9ef; box-shadow: 0 2px 0 var(--forest-deep); }
.btn-primary:hover { background: var(--forest-deep); }

.btn-ghost { background: transparent; color: var(--forest-deep); border: 1.5px solid var(--line); }
.btn-ghost:hover { background: var(--parchment-deep); }

.btn-quick {
    background: rgba(51, 81, 63, 0.08);
    color: var(--forest-deep);
    border: 1px dashed var(--moss);
    font-size: 0.78rem;
    padding: 6px 11px;
    border-radius: 999px;
}
.btn-quick:hover { background: rgba(51, 81, 63, 0.16); }

.btn-icon {
    background: transparent;
    border: none;
    color: var(--ink-soft);
    font-size: 0.95rem;
    padding: 5px 8px;
    border-radius: 8px;
    cursor: pointer;
}
.btn-icon:hover { background: var(--parchment-deep); color: var(--ink); }
.btn-icon.danger:hover { color: var(--terracotta-deep); background: rgba(193, 98, 45, 0.12); }
.btn-icon.restore { color: var(--forest); }

/* ---------- Toolbar ---------- */
.toolbar {
    display: flex;
    align-items: center;
    gap: 14px;
    flex-wrap: wrap;
    margin-bottom: 22px;
}

.search-input {
    flex: 1 1 220px;
    font-family: var(--font-body);
    font-size: 0.95rem;
    padding: 10px 14px;
    border: 1.5px solid var(--line);
    border-radius: 999px;
    background: var(--card);
    color: var(--ink);
}
.search-input:focus { outline: none; border-color: var(--moss); box-shadow: 0 0 0 3px rgba(111, 140, 106, 0.2); }

.toggle-deleted {
    display: flex;
    align-items: center;
    gap: 7px;
    font-size: 0.85rem;
    color: var(--ink-soft);
    cursor: pointer;
    user-select: none;
}
.toggle-deleted input { accent-color: var(--terracotta); width: 16px; height: 16px; }

/* ---------- Tree ---------- */
.tree-container { padding: 10px 0 20px; }

.tree-node-wrap {
    position: relative;
    animation: rise 0.4s ease backwards;
}

@keyframes rise {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

.member-card {
    position: relative;
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 14px 16px;
    width: 100%;
    max-width: 320px;
    transition: box-shadow 0.2s ease, transform 0.2s ease;
}
.member-card:hover { box-shadow: var(--shadow-lift); transform: translateY(-2px); }

.member-card.deceased { background: linear-gradient(180deg, var(--card), #f5f1e6); }
.member-card.deceased::before {
    content: "🕊";
    position: absolute;
    top: 12px;
    right: 14px;
    font-size: 0.85rem;
    opacity: 0.6;
}

.member-card.marked-deleted {
    opacity: 0.55;
    border-style: dashed;
    border-color: var(--terracotta);
}

.member-card.highlight-new {
    animation: highlightPulse 2.2s ease;
}
@keyframes highlightPulse {
    0%, 100% { box-shadow: var(--shadow); border-color: var(--line); }
    15%, 60% { box-shadow: 0 0 0 4px rgba(111, 140, 106, 0.35), var(--shadow-lift); border-color: var(--moss); }
}

.member-card .avatar {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    border-radius: 50%;
    font-family: var(--font-display);
    font-weight: 600;
    font-size: 0.9rem;
    color: #fff;
    margin-bottom: 8px;
}
.avatar.g-vyras { background: linear-gradient(135deg, var(--forest), var(--moss)); }
.avatar.g-moteris { background: linear-gradient(135deg, var(--terracotta), var(--gold)); }
.avatar.g-nenurodyta { background: linear-gradient(135deg, var(--dove), #a8a293); }

.member-card h3 {
    font-size: 1.08rem;
    margin: 0 0 4px;
    line-height: 1.25;
}

.member-meta { font-size: 0.82rem; color: var(--ink-soft); margin: 2px 0; display: flex; gap: 5px; align-items: baseline; }
.member-meta .icon { opacity: 0.75; }

.member-notes {
    font-size: 0.8rem;
    color: var(--ink-soft);
    font-style: italic;
    margin-top: 6px;
    border-left: 2px solid var(--line);
    padding-left: 8px;
}

.deleted-badge {
    display: inline-block;
    font-size: 0.68rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    color: var(--terracotta-deep);
    background: rgba(193, 98, 45, 0.14);
    border-radius: 999px;
    padding: 3px 9px;
    margin-bottom: 6px;
}

.co-parent-note {
    font-size: 0.76rem;
    color: var(--moss);
    margin-top: 6px;
}

.spouse-note {
    font-size: 0.76rem;
    color: var(--terracotta-deep);
    margin-top: 4px;
}

.card-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; align-items: center; }
.card-actions-secondary { margin-left: auto; display: flex; gap: 2px; }

/* ---------- Generational tree grid ---------- */
.tree-section { position: relative; }

.tree-scroll {
    overflow: auto;
    padding: 10px 4px 34px;
    -webkit-overflow-scrolling: touch;
    cursor: grab;
    touch-action: none;
    user-select: none;
}
.tree-scroll.grabbing { cursor: grabbing; }

.tree-grid {
    position: relative;
    display: inline-grid;
    grid-auto-rows: min-content;
    row-gap: 96px;
    column-gap: 34px;
    padding: 6px 20px 20px;
}

.tree-grid .member-card { width: 226px; max-width: 226px; }
.tree-grid .member-card,
.tree-grid .member-card * { user-select: text; }

/* ---------- Fullscreen tree mode ---------- */
/* .fake-fullscreen is a JS-driven fallback for browsers without element Fullscreen API (e.g. iOS Safari) */
.tree-section:fullscreen,
.tree-section:-webkit-full-screen,
.tree-section.fake-fullscreen {
    background: var(--parchment);
    display: flex;
    flex-direction: column;
    padding: 16px 20px;
    height: 100vh;
    width: 100vw;
}
.tree-section.fake-fullscreen {
    position: fixed;
    inset: 0;
    z-index: 500;
    height: 100dvh;
}
.tree-section:fullscreen .toolbar,
.tree-section:-webkit-full-screen .toolbar,
.tree-section.fake-fullscreen .toolbar { margin-bottom: 14px; flex-shrink: 0; }

.tree-section:fullscreen main,
.tree-section:-webkit-full-screen main,
.tree-section.fake-fullscreen main {
    flex: 1;
    min-height: 0;
    min-width: 0;
    display: flex;
    flex-direction: column;
}
.tree-section:fullscreen .view,
.tree-section:-webkit-full-screen .view,
.tree-section.fake-fullscreen .view {
    flex: 1;
    min-height: 0;
    min-width: 0;
    display: flex;
    flex-direction: column;
}
.tree-section:fullscreen .view[hidden],
.tree-section:-webkit-full-screen .view[hidden],
.tree-section.fake-fullscreen .view[hidden] { display: none; }

.tree-section:fullscreen .tree-container,
.tree-section:-webkit-full-screen .tree-container,
.tree-section.fake-fullscreen .tree-container { flex: 1; min-height: 0; min-width: 0; padding: 0; }
.tree-section:fullscreen .tree-scroll,
.tree-section:-webkit-full-screen .tree-scroll,
.tree-section.fake-fullscreen .tree-scroll { height: 100%; width: 100%; }
.tree-section:fullscreen .table-wrap,
.tree-section:-webkit-full-screen .table-wrap,
.tree-section.fake-fullscreen .table-wrap { flex: 1; min-height: 0; overflow: auto; }

.tree-connectors {
    position: absolute;
    top: 0;
    left: 0;
    overflow: visible;
    pointer-events: none;
}
.tree-connectors path {
    fill: none;
    stroke: var(--trunk);
    stroke-width: 2;
    opacity: 0.5;
    stroke-linecap: round;
}
.tree-connectors path.spouse-link {
    stroke: var(--terracotta);
    stroke-width: 3;
    stroke-dasharray: 1 7;
    opacity: 0.65;
}

.empty-state {
    text-align: center;
    padding: 60px 20px;
    color: var(--ink-soft);
    font-family: var(--font-display);
    font-size: 1.1rem;
}

.loading-state { text-align: center; padding: 50px; color: var(--ink-soft); }

/* ---------- Table view ---------- */
.table-wrap {
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow-x: auto;
}
table { width: 100%; border-collapse: collapse; font-size: 0.9rem; min-width: 480px; }
thead th {
    text-align: left;
    font-family: var(--font-display);
    font-weight: 600;
    color: var(--forest-deep);
    padding: 12px 14px;
    border-bottom: 2px solid var(--line);
    white-space: nowrap;
}
tbody td { padding: 11px 14px; border-bottom: 1px solid var(--parchment-deep); vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
tbody tr.deceased td:first-child { color: var(--ink-soft); }
tbody tr.marked-deleted { opacity: 0.5; }
tbody tr:hover td { background: var(--parchment-deep); }

/* ---------- Modal ---------- */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(43, 33, 24, 0.45);
    backdrop-filter: blur(2px);
    display: flex;
    align-items: flex-end;
    justify-content: center;
    z-index: 100;
    animation: fadeIn 0.2s ease;
}
.modal-overlay[hidden] { display: none; }
@media (min-width: 640px) {
    .modal-overlay { align-items: center; }
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.modal {
    background: var(--parchment);
    width: 100%;
    max-width: 480px;
    max-height: 92vh;
    overflow-y: auto;
    border-radius: 22px 22px 0 0;
    padding: 26px 22px 24px;
    position: relative;
    box-shadow: 0 -10px 40px rgba(43,33,24,0.25);
    animation: slideUp 0.25s ease;
    border: 1px solid var(--line);
}
@media (min-width: 640px) {
    .modal { border-radius: var(--radius); animation: popIn 0.2s ease; }
}
@keyframes slideUp { from { transform: translateY(30px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
@keyframes popIn { from { transform: scale(0.96); opacity: 0; } to { transform: scale(1); opacity: 1; } }

.modal h2 { font-size: 1.35rem; margin-bottom: 4px; }
.modal-context { font-size: 0.85rem; color: var(--forest); margin: 0 0 16px; font-style: italic; }

.modal-close {
    position: absolute;
    top: 16px;
    right: 16px;
    background: var(--parchment-deep);
    border: none;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    font-size: 1.2rem;
    line-height: 1;
    color: var(--ink-soft);
    cursor: pointer;
}
.modal-close:hover { background: var(--line); }

.form-row { margin-bottom: 14px; display: flex; flex-direction: column; gap: 6px; }
.form-row-inline { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-row-inline > div { display: flex; flex-direction: column; gap: 6px; }

label { font-size: 0.8rem; font-weight: 600; color: var(--ink-soft); }

input[type="text"], input[type="date"], select, textarea {
    font-family: var(--font-body);
    font-size: 0.95rem;
    padding: 10px 12px;
    border: 1.5px solid var(--line);
    border-radius: 10px;
    background: var(--card);
    color: var(--ink);
    width: 100%;
}
input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--moss);
    box-shadow: 0 0 0 3px rgba(111, 140, 106, 0.2);
}
textarea { resize: vertical; font-family: var(--font-body); }

.parents-advanced {
    margin: 4px 0 18px;
    border-top: 1px solid var(--parchment-deep);
    padding-top: 12px;
}
.parents-advanced summary { cursor: pointer; font-weight: 600; font-size: 0.85rem; color: var(--forest); }
.parents-advanced .hint { font-size: 0.76rem; color: var(--ink-soft); margin: 8px 0; }
.parent-checkboxes { display: flex; flex-direction: column; gap: 6px; max-height: 160px; overflow-y: auto; }
.parent-checkboxes label { display: flex; align-items: center; gap: 8px; font-weight: 400; font-size: 0.88rem; color: var(--ink); }

.form-actions { display: flex; gap: 10px; margin-top: 6px; }
.form-actions .btn { flex: 1; }

/* ---------- Toasts ---------- */
.toast-container {
    position: fixed;
    bottom: 18px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 200;
    display: flex;
    flex-direction: column;
    gap: 8px;
    width: min(92vw, 360px);
}
.toast {
    font-size: 0.88rem;
    font-weight: 600;
    padding: 12px 16px;
    border-radius: 12px;
    box-shadow: var(--shadow-lift);
    animation: toastIn 0.25s ease;
    color: #fdf9ef;
}
.toast.success { background: var(--forest); }
.toast.error { background: var(--terracotta-deep); }
@keyframes toastIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

footer { text-align: center; margin-top: 40px; color: var(--dove); font-size: 0.8rem; }

@media (max-width: 600px) {
    .toolbar { flex-direction: column; align-items: stretch; }
    .search-input { flex: none; width: 100%; }
    .toggle-deleted { justify-content: flex-start; }
}

@media (max-width: 480px) {
    .topbar { padding: 14px 16px; }
    .topbar-title h1 { font-size: 1.3rem; }
    .form-row-inline { grid-template-columns: 1fr; }
}
