:root {
    --paper: #f4ead3;
    --paper-dark: #e7d7b5;
    --ink: #1c140c;
    --muted: #6b5344;
    --rule: #c45c4a;
    --rule-soft: rgba(196, 92, 74, 0.28);
    --line: rgba(46, 74, 122, 0.18);
    --green: #1f5c3a;
    --maroon: #7a1f24;
    --navy: #1d3557;
    --gold: #c9a227;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
    font-family: "Hind Siliguri", sans-serif;
    color: var(--ink);
    background:
        radial-gradient(1200px 500px at 10% -10%, #3d2a1c 0%, transparent 50%),
        radial-gradient(900px 400px at 110% 0%, #1d3557 0%, transparent 46%),
        #2a2118;
    min-height: 100vh;
}

a { color: inherit; text-decoration: none; }
button, input, select, textarea { font-family: inherit; }
a, button { touch-action: manipulation; }
.table-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.app {
    max-width: 1180px;
    margin: 0 auto;
    padding: 18px 16px 48px;
    padding-left: max(16px, env(safe-area-inset-left));
    padding-right: max(16px, env(safe-area-inset-right));
    padding-bottom: max(48px, env(safe-area-inset-bottom));
}

.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
    margin-bottom: 18px;
    flex-wrap: wrap;
}
.brand {
    display: flex;
    align-items: center;
    gap: 10px;
    color: #f4ead3;
}
.brand-mark {
    width: 42px; height: 42px;
    border-radius: 8px;
    background: linear-gradient(160deg, #c9a227, #8a6a12);
    color: #1c140c;
    display: grid; place-items: center;
    font-weight: 700;
    font-size: 22px;
    box-shadow: 0 2px 0 #5c4708;
}
.brand small { display: block; opacity: .75; font-size: 12px; }
.nav { display: flex; gap: 8px; flex-wrap: wrap; }
.nav a {
    background: rgba(244, 234, 211, .08);
    color: #f4ead3;
    padding: 7px 12px;
    border-radius: 999px;
    border: 1px solid rgba(244, 234, 211, .18);
    font-weight: 500;
    font-size: 14px;
}
.nav a.is-on {
    background: #f4ead3;
    color: #1c140c;
}
.nav a.nav-out {
    background: rgba(196, 92, 74, .2);
    border-color: rgba(196, 92, 74, .45);
}

.login-body {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 20px;
}
.login-wrap { width: 100%; max-width: 420px; }
.login-card {
    background: var(--paper);
    border: 1px solid #d9c7a3;
    border-radius: 14px;
    padding: 32px 26px 28px;
    text-align: center;
    box-shadow: 0 18px 40px rgba(0,0,0,.28);
}
.login-mark {
    width: 56px;
    height: 56px;
    margin: 0 auto 12px;
    border-radius: 12px;
    background: linear-gradient(160deg, #c9a227, #8a6a12);
    color: #1c140c;
    display: grid;
    place-items: center;
    font-weight: 700;
    font-size: 28px;
}
.login-card h1 { margin: 0; font-size: 26px; }
.login-card > p { margin: 6px 0 18px; color: var(--muted); }
.login-form { text-align: left; }
.login-form > div { margin-bottom: 12px; }
.login-form .btn { width: 100%; min-height: 46px; margin-top: 6px; }

.flash {
    padding: 10px 14px;
    border-radius: 8px;
    margin-bottom: 14px;
    font-weight: 500;
}
.flash-ok { background: #d7f0df; color: #14532d; }
.flash-err { background: #fde2e1; color: #7f1d1d; }

/* Desk / three books */
.desk-title {
    color: #f4ead3;
    text-align: center;
    margin: 10px 0 28px;
}
.desk-title h1 { margin: 0; font-size: 30px; font-weight: 700; }
.desk-title p { margin: 6px 0 0; opacity: .78; }

.books {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 22px;
    max-width: 920px;
    margin: 0 auto;
}
.book {
    display: block;
    border-radius: 6px 14px 14px 6px;
    min-height: 340px;
    padding: 28px 22px 22px 36px;
    position: relative;
    color: #f8f1e3;
    box-shadow:
        8px 12px 24px rgba(0,0,0,.35),
        inset -8px 0 0 rgba(0,0,0,.18);
    transition: transform .15s ease;
}
.book:hover { transform: none; }
@media (hover: hover) {
    .book:hover { transform: translateY(-4px); }
}
.book::before {
    content: "";
    position: absolute;
    left: 12px; top: 10px; bottom: 10px;
    width: 8px;
    background: repeating-linear-gradient(
        to bottom,
        rgba(255,255,255,.18) 0 8px,
        transparent 8px 16px
    );
    border-radius: 4px;
}
.book-stock { background: linear-gradient(145deg, #1f5c3a, #0f3a24); }
.book-balance { background: linear-gradient(145deg, #7a1f24, #4a1014); }
.book-company { background: linear-gradient(145deg, #1d3557, #0f1e33); }
.book-capital { background: linear-gradient(145deg, #8a6a12, #4a3808); }
.book h2 { margin: 40px 0 8px; font-size: 28px; }
.book p { margin: 0; opacity: .86; line-height: 1.5; }
.book .cols { margin-top: 18px; font-size: 13px; opacity: .8; }
.book em { position: absolute; bottom: 18px; right: 18px; font-style: italic; opacity: .7; }

/* Index cards */
.page-head {
    display: flex;
    justify-content: space-between;
    align-items: end;
    gap: 12px;
    color: #f4ead3;
    margin-bottom: 14px;
    flex-wrap: wrap;
}
.page-head h1 { margin: 0; font-size: 26px; }
.page-head p { margin: 4px 0 0; opacity: .75; }

.panel {
    background: var(--paper);
    border: 1px solid #d9c7a3;
    border-radius: 10px;
    padding: 16px;
    margin-bottom: 16px;
    box-shadow: 0 8px 20px rgba(0,0,0,.18);
}
.panel h3 { margin: 0 0 10px; font-size: 16px; }

.form-grid {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    gap: 10px;
    align-items: end;
}
.form-grid.tight {
    grid-template-columns: 150px minmax(180px, 1.5fr) 110px 130px 130px 140px;
}
.form-grid > input[type="hidden"] { display: none; }
.span-2 { grid-column: span 2; }
label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; font-weight: 500; }
input, select {
    width: 100%;
    padding: 8px 10px;
    border: 1px solid #cbb896;
    background: #fffdf6;
    border-radius: 6px;
    color: var(--ink);
    font-size: 16px;
}
.btn {
    border: 0;
    background: var(--ink);
    color: #f4ead3;
    padding: 9px 16px;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 600;
}
.form-grid .btn { width: 100%; }
.btn:hover { filter: brightness(1.08); }
.btn-ghost {
    background: transparent;
    color: var(--maroon);
    border: 1px solid var(--rule-soft);
    padding: 2px 7px;
    font-size: 11px;
    margin-left: 6px;
    opacity: 0;
}
tr:hover .btn-ghost { opacity: 1; }
.btn-print { background: var(--navy); }

.index-table, .khata-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 15px;
}
.index-table th, .index-table td {
    padding: 10px 8px;
    border-bottom: 1px solid #e0d0b0;
    text-align: left;
}
.index-table th { font-size: 13px; color: var(--muted); font-weight: 600; }
.index-table tr:hover td { background: rgba(255,255,255,.35); }
.num { text-align: right; font-variant-numeric: tabular-nums; font-family: "Libre Baskerville", serif; }
.due { color: var(--maroon); font-weight: 700; }
.ok { color: var(--green); font-weight: 700; }
.link { font-weight: 600; color: var(--navy); }
.link:hover { text-decoration: underline; }

/* Traditional khata page */
.khata-wrap {
    background: #c4a574;
    padding: 14px;
    border-radius: 8px;
    box-shadow: 0 18px 40px rgba(0,0,0,.28);
    overflow-x: auto;
}
.khata {
    background:
        linear-gradient(90deg, rgba(196,92,74,.18) 0 42px, transparent 42px),
        repeating-linear-gradient(
            to bottom,
            transparent 0 31px,
            var(--line) 31px 32px
        ),
        var(--paper);
    border: 1px solid #b08950;
    min-height: 720px;
    position: relative;
    padding: 18px 18px 18px 18px;
}
.khata::before {
    content: "";
    position: absolute;
    left: 42px; top: 0; bottom: 0;
    width: 2px;
    background: var(--rule);
}
.khata-head {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 8px;
    padding: 6px 8px 14px 52px;
    border-bottom: 3px double var(--ink);
    margin-bottom: 8px;
}
.khata-head h2 {
    margin: 0;
    font-family: "Libre Baskerville", "Hind Siliguri", serif;
    font-size: 22px;
}
.khata-head .meta { font-size: 14px; color: var(--muted); }
.khata-head .folio {
    text-align: right;
    font-size: 13px;
    line-height: 1.6;
}
.khata-head .folio strong { display: block; font-size: 18px; color: var(--maroon); }
.khata-head .folio strong.now { color: var(--green); }

.khata-table {
    margin-left: 46px;
    width: calc(100% - 46px);
    background: transparent;
}
.khata-table th {
    font-size: 13px;
    font-weight: 700;
    border-bottom: 2px solid var(--ink);
    padding: 6px 8px;
    text-align: center;
}
.khata-table td {
    padding: 6px 8px;
    height: 32px;
    border-bottom: 1px solid transparent;
    vertical-align: middle;
}
.khata-table tbody tr:not(.blank-row):not(.total-row):hover td {
    background: rgba(255,255,255,.28);
}
.khata-table .blank-row td { border-bottom: none; }
.khata-table .c { text-align: center; }
.khata-table .r { text-align: right; font-variant-numeric: tabular-nums; font-family: "Libre Baskerville", serif; font-size: 14px; }
.opening { font-style: italic; color: var(--muted); }
.total-row td {
    border-top: 3px double var(--ink);
    font-weight: 700;
    padding-top: 10px;
}
.balance-cell { font-weight: 700; }

.khata-actions {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 0 0 10px;
    color: #f4ead3;
    flex-wrap: wrap;
    gap: 8px;
}
.khata-actions a { color: #f4ead3; opacity: .85; }
.khata-actions a:hover { opacity: 1; text-decoration: underline; }

.summary-pills, .facts {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    align-items: stretch;
}
.facts-row {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
    margin: 0 0 14px;
}
.pill {
    background: #fffdf6;
    border: 1px solid #d9c7a3;
    padding: 8px 14px;
    border-radius: 8px;
    font-size: 14px;
}
.pill b { margin-left: 4px; }
.fact {
    background: #fffdf6;
    color: var(--ink);
    border: 1px solid #d9c7a3;
    border-radius: 8px;
    padding: 12px 16px;
    min-width: 140px;
    line-height: 1.3;
}
.fact span {
    display: block;
    font-size: 13px;
    color: var(--muted);
    font-weight: 500;
}
.fact strong {
    display: block;
    font-size: 22px;
    font-weight: 700;
    margin-top: 4px;
}
.fact-now strong { color: var(--green); }
.fact-due strong { color: var(--maroon); }
.facts .btn-print {
    align-self: stretch;
    min-width: 88px;
}

.del-form { display: inline; }
.empty-note {
    color: #f4ead3;
    text-align: center;
    padding: 40px 16px;
    opacity: .85;
}

@media (max-width: 900px) {
    .form-grid, .form-grid.tight { grid-template-columns: 1fr 1fr; }
    .form-grid.tight > div:nth-of-type(2) { grid-column: 1 / -1; }
    .facts-row { grid-template-columns: 1fr; }
    .khata { min-width: 640px; }
    .khata-table { margin-left: 28px; width: calc(100% - 28px); }
    .khata::before { left: 26px; }
    .khata-head { padding-left: 36px; }
    tr .btn-ghost { opacity: 1; }
    .index-table { min-width: 520px; }
}

@media (max-width: 720px) {
    .app { padding-top: 12px; }
    .topbar {
        flex-direction: column;
        align-items: stretch;
        gap: 12px;
    }
    .nav {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 8px;
        width: 100%;
    }
    .nav a.nav-out { grid-column: 1 / -1; }
    .nav a {
        text-align: center;
        padding: 12px 8px;
        font-size: 15px;
        border-radius: 10px;
    }
    .desk-title { margin: 4px 0 16px; }
    .desk-title h1 { font-size: 22px; }
    .desk-title p { font-size: 14px; padding: 0 8px; }
    .books {
        grid-template-columns: 1fr;
        gap: 12px;
        max-width: none;
    }
    .book {
        min-height: 0;
        padding: 18px 16px 16px 28px;
    }
    .book h2 { margin: 6px 0 6px; font-size: 22px; }
    .book p { font-size: 14px; }
    .book .cols { margin-top: 10px; font-size: 12px; }
    .book em {
        position: static;
        display: block;
        margin-top: 10px;
        text-align: right;
    }
    .page-head h1 { font-size: 22px; }
    .facts-row { gap: 8px; }
    .fact { padding: 10px 12px; min-width: 0; }
    .fact strong { font-size: 18px; }
    .form-grid, .form-grid.tight {
        grid-template-columns: 1fr;
    }
    .span-2,
    .form-grid.tight > div:nth-of-type(2) {
        grid-column: auto;
    }
    .form-grid .btn,
    .btn-print {
        width: 100%;
        min-height: 46px;
        font-size: 16px;
    }
    input, select {
        min-height: 46px;
        padding: 10px 12px;
    }
    .panel { padding: 14px; }
    .khata-actions {
        flex-direction: column;
        align-items: stretch;
    }
    .khata-actions .btn-print { width: 100%; }
    .blank-row { display: none; }
    .khata-wrap::before {
        content: "খাতার পাতা দেখতে পাশে সরান →";
        display: block;
        color: #3d2a1c;
        font-size: 13px;
        font-weight: 600;
        margin: 0 0 8px;
    }
    .index-table a.link {
        display: inline-block;
        padding: 6px 0;
        min-height: 44px;
    }
}

@media (max-width: 420px) {
    .brand strong { font-size: 14px; }
    .book::before { left: 8px; width: 6px; }
}

@media print {
    body { background: #fff; }
    .app { max-width: none; padding: 0; }
    .no-print, .topbar, .flash, .panel, .khata-actions { display: none !important; }
    .khata-wrap { box-shadow: none; background: none; padding: 0; }
    .khata-wrap::before { display: none !important; }
    .khata { min-height: auto; min-width: 0; }
}
