:root {
  --bg: var(--tg-theme-bg-color, #14161a);
  --bg2: var(--tg-theme-secondary-bg-color, #1e2127);
  --text: var(--tg-theme-text-color, #ececec);
  --muted: var(--tg-theme-hint-color, #8a8f99);
  --accent: var(--tg-theme-button-color, #ff8a3d);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --link: var(--tg-theme-link-color, #ff8a3d);
  --border: rgba(255,255,255,.08);
  --ok: #3acc6a;
  --warn: #f3b53b;
  --err: #ef4f4f;
}
*,*::before,*::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--text); font-family: -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; }
body { padding-bottom: 80px; }

header {
  position: sticky; top: 0; z-index: 10;
  background: linear-gradient(180deg, var(--bg) 80%, transparent);
  padding: 14px 14px 10px;
}
.title-row { display: flex; align-items: center; gap: 10px; }
.logo {
  width: 36px; height: 36px; border-radius: 10px;
  background: linear-gradient(135deg, #ff8a3d, #b14d10);
  display: grid; place-items: center; font-size: 20px;
  box-shadow: 0 3px 10px -4px rgba(255,138,61,.5);
}
.title { font-weight: 700; font-size: 17px; letter-spacing: .2px; flex: 1; }
.sub { font-size: 11px; color: var(--muted); margin-top: 2px; }

.tabs { display: flex; gap: 8px; margin-top: 14px; }
.tab {
  flex: 1; padding: 10px 14px; border: 1px solid var(--border);
  border-radius: 10px; background: var(--bg2); color: var(--muted);
  font-size: 14px; font-weight: 600; cursor: pointer;
  transition: background .15s, color .15s, border-color .15s;
  text-align: center;
}
.tab.active {
  background: var(--accent); color: var(--accent-text); border-color: var(--accent);
}

main { padding: 6px 14px 14px; }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

.toolbar { display: flex; justify-content: space-between; align-items: center; margin: 6px 0 12px; }
.search-row { margin: 6px 0 10px; }
.search-row input {
  width: 100%; padding: 10px 14px;
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: 12px; color: var(--text); font-size: 14px; outline: none;
}
.search-row input:focus { border-color: var(--accent); }

.sort-select {
  background: var(--bg2); border: 1px solid var(--border);
  color: var(--text); padding: 7px 10px; border-radius: 8px;
  font-size: 12px; font-weight: 600; cursor: pointer; outline: none;
  margin-right: 6px;
}
.switch { color: var(--muted); font-size: 13px; display: inline-flex; gap: 6px; align-items: center; cursor: pointer; }

h3 { margin: 4px 0 12px; font-size: 15px; font-weight: 700; }

.empty {
  text-align: center; padding: 40px 16px; color: var(--muted);
  border: 1px dashed var(--border); border-radius: 14px;
  font-size: 14px; line-height: 1.5;
}
.empty b { color: var(--text); }

/* === Stat strip (P/L summary) === */
.stats {
  display: flex; gap: 6px; padding: 10px 14px; margin-bottom: 14px;
  background: var(--bg2); border: 1px solid var(--border); border-radius: 14px;
}
.stat { flex: 1; text-align: center; padding: 4px 4px; }
.stat .num { font-size: 18px; font-weight: 800; line-height: 1.1; }
.stat .lbl { font-size: 10px; color: var(--muted); margin-top: 3px; letter-spacing: .4px; text-transform: uppercase; }
.stat + .stat { border-left: 1px solid var(--border); }
.stat.ok .num { color: var(--ok); }
.stat.err .num { color: var(--err); }

/* === Account card === */
.acc-card {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 14px;
  margin-bottom: 10px;
  position: relative;
  overflow: hidden;
}
.acc-card::before {
  content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 3px;
  background: var(--accent);
}
.acc-card.archived::before { background: var(--muted); }
.acc-card.profit::before   { background: var(--ok); }
.acc-card.loss::before     { background: var(--err); }

.acc-head { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
.acc-avatar {
  width: 42px; height: 42px; flex: 0 0 42px; border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: 14px;
  background: rgba(255,138,61,.12); color: var(--accent);
  text-transform: uppercase;
}
.acc-name { flex: 1; min-width: 0; }
.acc-name .lbl {
  font-weight: 700; font-size: 15px; color: var(--text);
  word-break: break-all; line-height: 1.3;
}
.acc-name .ex { font-size: 12px; color: var(--muted); margin-top: 3px; }
.acc-head-side { display: flex; flex-direction: column; gap: 5px; align-items: flex-end; flex: 0 0 auto; }

.acc-badge {
  font-size: 10.5px; padding: 4px 9px; border-radius: 99px;
  font-weight: 700; letter-spacing: .2px; line-height: 1;
  flex: 0 0 auto;
}
.acc-badge.ok   { background: rgba(58,204,106,.14); color: var(--ok); }
.acc-badge.err  { background: rgba(239,79,79,.14);  color: var(--err); }
.acc-badge.muted{ background: rgba(255,255,255,.06); color: var(--muted); }
.acc-badge.warn { background: rgba(243,181,59,.14); color: var(--warn); }
.acc-badge.status-reg  { background: rgba(63,143,255,.16); color: #6daaff; }
.acc-badge.status-bush { background: rgba(243,181,59,.16); color: var(--warn); }
.acc-badge.status-done { background: rgba(58,204,106,.16); color: var(--ok); }
.acc-badge.status-ban  { background: rgba(239,79,79,.16); color: var(--err); }
.acc-badge.head {
  background: linear-gradient(135deg, #ff924a, #e07028);
  color: #fff; font-weight: 800;
}

.ref-pill {
  font-size: 11px; padding: 4px 9px;
  background: rgba(255,138,61,.12); color: var(--accent);
  border: 1px solid rgba(255,138,61,.25); border-radius: 8px;
  font-family: ui-monospace, monospace;
  white-space: nowrap; max-width: 50%; overflow: hidden; text-overflow: ellipsis;
}

.acc-info {
  display: flex; flex-direction: column; gap: 12px;
  padding: 14px; margin-bottom: 14px;
  background: rgba(0,0,0,.22); border-radius: 12px;
}
.info-row { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.info-row .info-head { display: flex; align-items: center; gap: 6px; font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; font-weight: 600; }
.info-row .ico { font-size: 12px; opacity: .85; }
.info-row .val {
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px;
  word-break: break-all; line-height: 1.35;
  display: flex; align-items: center; gap: 8px;
}
.info-row .val.muted { color: var(--muted); font-family: inherit; font-size: 12px; word-break: break-word; }
.info-row .val-text { flex: 1; min-width: 0; word-break: break-all; }
.copy {
  color: var(--accent); cursor: pointer; padding: 2px 8px;
  font-size: 10px; font-weight: 700; letter-spacing: .5px;
  border: 1px solid var(--border); border-radius: 6px; flex: 0 0 auto;
  text-transform: uppercase;
}
.copy:active { background: rgba(255,138,61,.1); }

.money-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 8px;
  margin: 4px 0;
}
.money-grid.three { grid-template-columns: 1fr 1fr 1fr; gap: 7px; }
.money-grid.three .money-cell { padding: 8px 9px; }
.money-grid.three .money-val { font-size: 13px; }
.money-grid.three .money-lbl { font-size: 9.5px; }

/* === Inline chapter chip on account card === */
.acc-chapter-row {
  display: flex; align-items: center; gap: 8px;
  margin: -4px 0 10px;
  font-size: 12px;
}
.acc-chapter-row .ico { color: var(--muted); font-size: 13px; }
.ch-inline-select {
  flex: 1;
  background: rgba(255,138,61,.08);
  border: 1px solid rgba(255,138,61,.2);
  color: var(--accent);
  padding: 6px 10px; border-radius: 8px;
  font-size: 12px; font-weight: 600;
  outline: none; cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'><path fill='%23ff8a3d' d='M0 0l5 6 5-6z'/></svg>");
  background-repeat: no-repeat;
  background-position: right 10px center;
  padding-right: 28px;
}
.ch-inline-select:focus { border-color: var(--accent); }

/* === Chapters tab === */
.inline-form {
  display: grid; grid-template-columns: 1fr 1fr auto; gap: 6px;
  margin-bottom: 10px;
}
.inline-form input { padding: 9px 10px; font-size: 13px; }
.inline-form .btn-primary { padding: 9px 14px; }

.ch-card {
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: 14px; padding: 14px; margin-bottom: 10px;
  position: relative; overflow: hidden;
}
.ch-card::before {
  content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 3px;
  background: var(--accent);
}
.ch-head { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.ch-name { flex: 1; font-weight: 700; font-size: 15px; }
.ch-actions { display: flex; gap: 4px; }
.ch-actions .acc-iconbtn { width: 32px; height: 32px; font-size: 13px; }

.ch-ref {
  display: flex; align-items: center; gap: 8px;
  background: rgba(0,0,0,.22); padding: 8px 10px; border-radius: 8px;
  font-size: 12px; margin-bottom: 10px;
  font-family: ui-monospace, monospace;
}
.ch-ref.muted { font-style: italic; color: var(--muted); font-family: inherit; }
.ch-ref-lbl {
  font-size: 9px; font-weight: 800; letter-spacing: .8px;
  background: var(--accent); color: var(--accent-text);
  padding: 2px 6px; border-radius: 4px; font-family: inherit;
}
.ch-ref-val { flex: 1; word-break: break-all; color: var(--text); }

.ch-stats {
  display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 6px; margin-bottom: 10px;
}
.ch-stats > div {
  background: rgba(255,255,255,.03); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px; text-align: center;
  display: flex; flex-direction: column; gap: 2px;
}
.ch-stat-num { font-size: 14px; font-weight: 700; font-family: ui-monospace, monospace; }
.ch-stat-num.ok { color: var(--ok); }
.ch-stat-num.err { color: var(--err); }
.ch-stat-lbl { font-size: 9.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .4px; }

.ch-accs { display: flex; flex-wrap: wrap; gap: 5px; }
.ch-acc-pill {
  font-size: 11px; padding: 4px 9px; border-radius: 99px;
  background: rgba(255,255,255,.06); color: var(--text);
  max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.ch-acc-pill.muted { color: var(--muted); }
.money-cell {
  background: rgba(255,255,255,.03);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 11px;
  display: flex; flex-direction: column; gap: 3px;
}
.money-cell.highlight { background: rgba(255,138,61,.08); border-color: rgba(255,138,61,.25); }
.money-lbl { font-size: 10.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .4px; font-weight: 600; }
.money-val { font-size: 14px; font-weight: 700; color: var(--text); font-family: ui-monospace, monospace; }
.money-val.ok { color: var(--ok); }
.money-val.err { color: var(--err); }
.money-val.muted { color: var(--muted); font-weight: 600; }

.acc-actions { display: flex; gap: 6px; }
.acc-primary {
  flex: 1; padding: 10px 12px;
  border: none; border-radius: 10px;
  background: linear-gradient(135deg, #ff924a 0%, #e07028 100%);
  color: #fff; font-size: 13.5px; font-weight: 700;
  cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 6px;
}
.acc-primary:active { filter: brightness(.92); }
.acc-iconbtn {
  flex: 0 0 40px; width: 40px; height: 40px;
  border: 1px solid var(--border); background: transparent;
  border-radius: 10px; color: var(--text); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-size: 16px;
}
.acc-iconbtn:active { background: rgba(255,255,255,.05); }
.acc-iconbtn.danger { color: var(--err); }
.acc-iconbtn.totp { color: var(--accent); }

/* === Forms === */
form { display: flex; flex-direction: column; gap: 12px; padding: 4px 0 20px; }
form label { display: flex; flex-direction: column; gap: 5px; color: var(--muted); font-size: 12px; font-weight: 500; }
form input, form select, form textarea {
  background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px;
  padding: 11px 12px; font-size: 14px; font-family: inherit;
  outline: none;
}
form input:focus, form textarea:focus, form select:focus { border-color: var(--accent); }
.btn-primary {
  background: linear-gradient(135deg, #ff924a 0%, #e07028 100%);
  color: #fff; border: none; padding: 12px 16px; border-radius: 10px;
  font-size: 14px; font-weight: 700; cursor: pointer;
}
.btn-primary:active { filter: brightness(.92); }
.btn-secondary {
  background: rgba(255,255,255,.06); color: var(--text);
  border: 1px solid var(--border); padding: 9px 12px; border-radius: 10px; cursor: pointer;
  font-size: 13px; font-weight: 600;
}
.hint { color: var(--muted); font-size: 11px; font-weight: 400; }

/* === P/L list === */
.pnl-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 14px; background: var(--bg2); border-radius: 12px;
  margin-bottom: 8px; border: 1px solid var(--border);
}
.pnl-row .m { color: var(--muted); font-size: 13px; }
.pnl-row .v { font-weight: 800; font-size: 15px; font-family: ui-monospace, monospace; }
.pnl-row .v.ok { color: var(--ok); }
.pnl-row .v.err { color: var(--err); }

/* === Modal === */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.65);
  display: none; align-items: flex-end; justify-content: center;
  z-index: 50;
}
.modal-overlay.show { display: flex; }
.modal {
  background: var(--bg2); border-top-left-radius: 18px; border-top-right-radius: 18px;
  width: 100%; max-height: 90vh; overflow: auto;
  padding: 18px 16px; border-top: 1px solid var(--border);
}
.modal h3 { margin: 4px 0 14px 0; font-size: 16px; font-weight: 700; }
.modal-handle {
  width: 36px; height: 4px; background: rgba(255,255,255,.15);
  border-radius: 99px; margin: -6px auto 12px;
}

.kind-switch {
  display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 6px;
  background: var(--bg); border: 1px solid var(--border);
  padding: 4px; border-radius: 12px; margin-bottom: 14px;
}
.kind-btn {
  background: transparent; border: none; color: var(--muted);
  padding: 10px 4px; border-radius: 9px; cursor: pointer;
  font-size: 12.5px; font-weight: 600; display: flex; flex-direction: column;
  align-items: center; gap: 3px; transition: background .15s, color .15s;
}
.kind-btn .kind-ico { font-size: 16px; line-height: 1; }
.kind-btn.active {
  background: var(--bg2); color: var(--text);
  box-shadow: 0 1px 0 rgba(255,255,255,.04);
}
.kind-btn[data-kind="deposit"].active { color: var(--ok); }
.kind-btn[data-kind="withdraw"].active { color: var(--warn); }
.kind-btn[data-kind="pnl"].active { color: var(--accent); }

.amount-input {
  display: flex; align-items: center; gap: 8px;
  background: var(--bg); border: 1.5px solid var(--border);
  border-radius: 14px; padding: 14px 18px;
  transition: border-color .15s;
}
.amount-input:focus-within { border-color: var(--accent); }
.amount-prefix {
  font-size: 26px; font-weight: 700; color: var(--muted); line-height: 1;
}
.amount-input input {
  flex: 1; background: transparent; border: none; outline: none;
  color: var(--text); font-size: 28px; font-weight: 700;
  font-family: ui-monospace, SFMono-Regular, monospace;
  padding: 0; min-width: 0;
}
.amount-input input::placeholder { color: rgba(255,255,255,.18); }

.quick-sums { display: flex; flex-wrap: wrap; gap: 6px; }
.quick-sum {
  flex: 1 1 auto; padding: 7px 10px;
  background: rgba(255,255,255,.04); border: 1px solid var(--border);
  border-radius: 8px; color: var(--muted); font-size: 12px; font-weight: 600;
  cursor: pointer;
}
.quick-sum:active { background: rgba(255,138,61,.1); color: var(--accent); border-color: rgba(255,138,61,.3); }

.note-input {
  width: 100%; background: var(--bg); border: 1px solid var(--border);
  border-radius: 10px; padding: 12px 14px; color: var(--text);
  font-size: 14px; outline: none;
}
.note-input:focus { border-color: var(--accent); }

.btn-primary.big {
  width: 100%; padding: 14px; font-size: 15px;
  letter-spacing: .3px;
}

.ops-divider {
  display: flex; align-items: center; gap: 10px;
  margin: 18px 0 10px; color: var(--muted); font-size: 11px;
  text-transform: uppercase; letter-spacing: .5px; font-weight: 600;
}
.ops-divider::before, .ops-divider::after {
  content: ""; flex: 1; height: 1px; background: var(--border);
}

.op {
  display: flex; justify-content: space-between; align-items: center;
  padding: 9px 0; border-bottom: 1px solid var(--border); font-size: 13px;
}
.op:last-child { border-bottom: none; }
.op .meta { color: var(--muted); font-size: 11px; margin-top: 2px; }
.op .v { font-weight: 700; font-family: ui-monospace, monospace; }
.op .v.ok { color: var(--ok); }
.op .v.err { color: var(--err); }

/* === TOTP popup === */
.totp-pop {
  position: fixed; left: 50%; top: 25%; transform: translateX(-50%);
  background: var(--bg2); border: 1px solid var(--border);
  padding: 22px 24px; border-radius: 18px; z-index: 60; text-align: center;
  box-shadow: 0 12px 40px rgba(0,0,0,.6); min-width: 240px;
}
.totp-pop .ttl { font-size: 11px; color: var(--muted); margin-bottom: 8px; letter-spacing: .5px; text-transform: uppercase; }
.totp-pop .code {
  font-size: 36px; font-weight: 800; letter-spacing: 6px;
  font-family: ui-monospace, SFMono-Regular, monospace;
  background: linear-gradient(135deg, #ff924a, #e07028);
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.totp-pop .ttl-bottom { font-size: 11px; color: var(--muted); margin-top: 8px; }
.totp-pop .progress {
  height: 3px; background: rgba(255,255,255,.08); border-radius: 99px; margin: 10px 0;
  overflow: hidden;
}
.totp-pop .progress .bar { height: 100%; background: linear-gradient(90deg, #ff924a, #e07028); transition: width .9s linear; }
.totp-pop .row { display: flex; gap: 8px; margin-top: 10px; }
.totp-pop .row button { flex: 1; }
