/*
 * Tema aplikasi. Pengguna memilih lewat menu tema (ikon palet);
 * pilihan disimpan dan dipasang sebagai <html data-app-theme="a|b|d|e|f">.
 *   a = Ungu Bersih (default)   b = Lavender Lembut   d = Minimal Garis
 *   e = Hijau Segar             f = Kuning Merah
 * Setiap tema juga punya versi gelap (mengikuti mode gelap HP).
 */
:root, :root[data-app-theme="a"] {
  --bg: #f6f7fc;
  --paper: #ffffff;
  --entry: #ffffff;
  --pad: #f6f7fc;
  --ink: #1c1f33;
  --muted: #8a90ad;
  --line: #eceef7;
  --bar: #ffffff;
  --bar-ink: #1c1f33;
  --bar-title: var(--bar-ink);
  --bar-line: #eceef7;
  --accent: #4d6bfe;
  --row-op: #4d6bfe;
  --total-line: #4d6bfe;
  --total-bg: #f4f6ff;
  --total-ink: #2c45d8;
  --key: #ffffff;
  --key-ink: #1c1f33;
  --key-fn: #eef0f8;
  --fn-ink: #4a4f6e;
  --key-op: #e8edff;
  --op-ink: #4d6bfe;
  --key-eq: #4d6bfe;
  --key-eq-ink: #ffffff;
  --key-ppn: #059669;
  --radius: 12px;
  --key-shadow: 0 1px 2px rgba(40, 50, 120, .08);
  --key-border: 0;
  --neg: #dc2626;
}
:root[data-app-theme="b"] {
  --bg: #f3f1fd; --paper: #fbfaff; --entry: #ffffff; --pad: #f3f1fd;
  --ink: #241f3d; --muted: #9c95c0; --line: #ece8fb;
  --bar: #6c5cff; --bar-ink: #ffffff; --bar-line: #6c5cff;
  --accent: #6c5cff; --row-op: #6c5cff;
  --total-line: #c9c0ff; --total-bg: #f3f0ff; --total-ink: #4b3ae0;
  --key: #ffffff; --key-ink: #241f3d; --key-fn: #e9e5fc; --fn-ink: #4b3ae0;
  --key-op: #dcd6ff; --op-ink: #4b3ae0; --key-eq: #6c5cff;
  --radius: 16px; --key-shadow: 0 1px 0 rgba(80, 60, 200, .10); --key-border: 0;
}
:root[data-app-theme="d"] {
  --bg: #ffffff; --paper: #ffffff; --entry: #ffffff; --pad: #ffffff;
  --ink: #111427; --muted: #9a9eb4; --line: #eef0f5;
  --bar: #ffffff; --bar-ink: #111427; --bar-line: #eef0f5;
  --accent: #4d6bfe; --row-op: #4d6bfe;
  --total-line: #111427; --total-bg: transparent; --total-ink: #111427;
  --key: #ffffff; --key-ink: #111427; --key-fn: #ffffff; --fn-ink: #6b6f86;
  --key-op: #ffffff; --op-ink: #4d6bfe; --key-eq: #4d6bfe;
  --radius: 10px; --key-shadow: none; --key-border: 1px solid #e6e8f0;
}
:root[data-app-theme="e"] {
  --bg: #ffffff; --paper: #ffffff; --entry: #ffffff; --pad: #ffffff;
  --ink: #1f2a24; --muted: #98a59e; --line: #eef2ef;
  --bar: #ffffff; --bar-ink: #1f2a24; --bar-title: #00a65a; --bar-line: #ffffff;
  --accent: #00a65a; --row-op: #00a65a;
  --total-line: transparent; --total-bg: #f2faf5; --total-ink: #008a4a;
  --key: #f5f7f6; --key-ink: #1f2a24; --key-fn: #f5f7f6; --fn-ink: #66736b;
  --key-op: #e6f6ed; --op-ink: #00a65a; --key-eq: #00a65a;
  --radius: 999px; --key-shadow: none; --key-border: 0;
}
:root[data-app-theme="f"] {
  --bg: #fff8d6; --paper: #fffdf3; --entry: #ffffff; --pad: #fff8d6;
  --ink: #1a1a1a; --muted: #9a916c; --line: #f3ecc9;
  --bar: #ffd100; --bar-ink: #1a1a1a; --bar-line: #e6bc00;
  --accent: #e3001b; --row-op: #e3001b;
  --total-line: #1a1a1a; --total-bg: #fff6c7; --total-ink: #1a1a1a;
  --key: #ffffff; --key-ink: #1a1a1a; --key-fn: #1a1a1a; --fn-ink: #ffd100;
  --key-op: #ffd100; --op-ink: #1a1a1a; --key-eq: #e3001b;
  --radius: 8px; --key-shadow: 0 2px 0 rgba(0, 0, 0, .12); --key-border: 0;
}

/* ---- Mode gelap: dasar bersama, lalu aksen tiap tema ---- */
@media (prefers-color-scheme: dark) {
  :root, :root[data-app-theme] {
    color-scheme: dark;
    --bg: #11131c; --paper: #171a26; --entry: #171a26; --pad: #11131c;
    --ink: #e8eaf4; --muted: #8f95b2; --line: #2a2e42;
    --bar: #171a26; --bar-ink: #e8eaf4; --bar-title: var(--bar-ink); --bar-line: #2a2e42;
    --key: #232737; --key-ink: #e8eaf4; --key-fn: #2c3145; --fn-ink: #b9bfdc;
    --key-shadow: none; --key-border: 0;
    --neg: #f87171;
  }
  :root, :root[data-app-theme="a"] {
    --accent: #7d93ff; --row-op: #7d93ff;
    --total-line: #7d93ff; --total-bg: #1d2340; --total-ink: #aebcff;
    --key-op: #2b3466; --op-ink: #aebcff; --key-eq: #4d6bfe;
  }
  :root[data-app-theme="b"] {
    --bar: #3b2fa8; --bar-ink: #ffffff; --bar-line: #3b2fa8;
    --accent: #9d91ff; --row-op: #9d91ff;
    --total-line: #6c5cff; --total-bg: #221d3d; --total-ink: #cfc8ff;
    --key-op: #3a3270; --op-ink: #cfc8ff; --key-eq: #6c5cff;
  }
  :root[data-app-theme="d"] {
    --pad: #171a26; --key: #171a26; --key-fn: #171a26; --key-border: 1px solid #2a2e42;
    --accent: #7d93ff; --row-op: #7d93ff;
    --total-line: #e8eaf4; --total-bg: transparent; --total-ink: #e8eaf4;
    --key-op: #171a26; --op-ink: #7d93ff; --key-eq: #4d6bfe;
  }
  :root[data-app-theme="e"] {
    --bar-title: #2ecc85; --bar-line: #171a26;
    --accent: #2ecc85; --row-op: #2ecc85;
    --total-line: transparent; --total-bg: #16291f; --total-ink: #4fe0a0;
    --key-op: #173b2a; --op-ink: #4fe0a0; --key-eq: #00a65a;
  }
  :root[data-app-theme="f"] {
    --bar: #1a1a1a; --bar-ink: #ffd100; --bar-line: #ffd100;
    --accent: #ff4d5e; --row-op: #ff4d5e;
    --total-line: #ffd100; --total-bg: #2a2610; --total-ink: #ffd100;
    --key-fn: #ffd100; --fn-ink: #1a1a1a;
    --key-op: #ffd100; --op-ink: #1a1a1a; --key-eq: #e3001b;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

body {
  display: flex;
  flex-direction: column;
  max-width: 520px;
  margin: 0 auto;
  height: 100dvh;
}

button { font: inherit; color: inherit; cursor: pointer; }

.topbar {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 8px 6px;
  padding-top: max(8px, env(safe-area-inset-top));
  background: var(--bar);
  color: var(--bar-ink);
  border-bottom: 1px solid var(--bar-line);
}
.title-btn {
  flex: 1;
  min-width: 0;
  margin: 0 4px;
  padding: 2px 6px;
  border: 0;
  border-radius: 8px;
  background: none;
  color: var(--bar-title);
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1.2;
}
.title-btn:active { background: rgba(127, 127, 127, .18); }
.title-line { display: flex; align-items: center; gap: 5px; max-width: 100%; }
.topbar h1 {
  margin: 0;
  min-width: 0;
  font-size: 1rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.title-btn .pen { flex: none; opacity: .6; }
.title-btn small { font-size: .68rem; font-weight: 400; opacity: .65; }
.icon-btn {
  background: none;
  border: 0;
  color: inherit;
  font-size: 1.3rem;
  width: 35px;
  height: 40px;
  flex: none;
  border-radius: 8px;
}
.icon-btn:active { background: rgba(127, 127, 127, .18); }

/* ---- Tape ---- */
.tape {
  flex: 1;
  /* Ukuran teks tape bisa diatur pengguna (menu Aa) */
  font-size: calc(16px * var(--tape-scale, 1));
  overflow-y: auto;
  background: var(--paper);
  padding: 8px 0;
  font-variant-numeric: tabular-nums;
}
.tape .empty {
  color: var(--muted);
  text-align: center;
  padding: 32px 16px;
  line-height: 1.5;
}
.tape-row {
  display: grid;
  /* angka | operasi | garis pemisah + nama transaksi */
  grid-template-columns: minmax(0, 1fr) 24px minmax(0, 1.1fr);
  align-items: start;
  column-gap: 4px;
  padding: 1px 12px 1px 8px;
  border-bottom: 1px dashed var(--line);
}
.tape-row input, .tape-row textarea {
  width: 100%;
  min-width: 0;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  color: var(--ink);
  font: inherit;
  padding: 3px 6px;
}
.tape-row input:hover, .tape-row textarea:hover { border-color: var(--line); }
.tape-row input:focus, .tape-row textarea:focus { outline: none; border-color: var(--accent); background: var(--bg); }
.tape-row .num { font-size: .85em; text-align: right; font-variant-numeric: tabular-nums; }
.tape-row span.num { padding: 3px 6px; }
.tape-row .op {
  border: 0;
  background: none;
  padding: 4px 0;
  color: var(--row-op);
  font-weight: 700;
  font-size: .8em;
  border-radius: 6px;
}
.tape-row .op:hover { background: rgba(37,99,235,.08); color: var(--accent); }
.tape-row .note {
  border-left: 2px solid var(--line);
  border-radius: 0 6px 6px 0;
  padding-left: 10px;
  font-size: .78em;
  /* Keterangan panjang turun ke baris berikutnya */
  resize: none;
  overflow: hidden;
  display: block;
  line-height: 1.35;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.tape-row .note::placeholder { color: var(--muted); opacity: .6; }
.tape-row .run { grid-column: 1 / 3; text-align: right; font-size: .68em; color: var(--muted); padding: 0 30px 2px 0; }
.tape-row.total {
  border-bottom: 2px solid var(--total-line);
  border-top: 1px solid var(--total-line);
  background: var(--total-bg);
  font-weight: 700;
}
.tape-row.total .num { font-size: .95em; color: var(--total-ink); }
/* Hijau Segar: baris total berupa blok membulat tanpa garis */
:root[data-app-theme="e"] .tape-row.total { border: 0; border-radius: 10px; margin: 3px 6px; }
.tape-row.total .note { font-weight: 700; }
.tape-row.total .note::placeholder { color: var(--ink); opacity: .7; }
.neg { color: var(--neg); }

/* ---- Entry ---- */
.entry {
  background: var(--entry);
  border-top: 2px solid var(--line);
  padding: 8px 16px;
  font-variant-numeric: tabular-nums;
}
.entry-line {
  display: grid;
  grid-template-columns: 1fr 24px;
  align-items: baseline;
}
.entry-line .value {
  grid-column: 1;
  grid-row: 1;
  text-align: right;
  font-size: 2rem;
  font-weight: 500;
  overflow-x: auto;
  white-space: nowrap;
}
.entry-line .op {
  grid-column: 2;
  grid-row: 1;
  text-align: center;
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--accent);
}
.total-line {
  display: flex;
  justify-content: space-between;
  color: var(--muted);
  font-size: .95rem;
}
.total-line #grandTotal { color: var(--ink); font-weight: 700; }

/* ---- Keypad ---- */
.keypad {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  padding: 8px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  background: var(--pad);
  /* Keypad ±40% tinggi layar; 5 baris tombol membagi rata */
  height: 40dvh;
  min-height: 260px;
  grid-template-rows: repeat(5, 1fr);
}
.k {
  min-height: 0;
  border: var(--key-border);
  border-radius: var(--radius);
  background: var(--key);
  color: var(--key-ink);
  font-size: clamp(1.1rem, 3dvh, 1.6rem);
  box-shadow: var(--key-shadow);
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}
.k:active { filter: brightness(.9); transform: translateY(1px); }
.k.fn { background: var(--key-fn); color: var(--fn-ink); }
.k.op { background: var(--key-op); color: var(--op-ink); font-weight: 700; }
.k.eq { background: var(--key-eq); color: var(--key-eq-ink); font-weight: 700; border: 0; }
.k.ppn { background: var(--key-ppn); color: var(--key-eq-ink); font-size: clamp(.85rem, 2.1dvh, 1.05rem); font-weight: 700; line-height: 1.1; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.k.ppn small { font-size: .62em; font-weight: 500; opacity: .85; }
.k.eq { font-size: clamp(1.4rem, 3.6dvh, 2rem); }

.text-size-btn { font-weight: 700; font-size: 1.15rem; letter-spacing: -1px; }
.text-size-btn small { font-size: .7em; }
.icon-btn svg { display: block; margin: auto; }
.size-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.size-row button { font-size: 1.2rem; font-weight: 700; min-width: 64px; padding: 10px 14px !important; }
.size-row span { font-size: 1.1rem; font-variant-numeric: tabular-nums; }
dialog.popover { margin: 64px 8px auto auto; width: min(92vw, 300px); }
dialog.popover::backdrop { background: rgba(0,0,0,.12); }

/* ---- Menu bagikan ---- */
.menu-list { display: grid; gap: 8px; }
.menu-list button { text-align: left; padding: 10px 12px !important; border-radius: 12px !important; }
.menu-list b { display: block; font-size: .95rem; }
.menu-list small { display: block; color: var(--muted); font-size: .78rem; }

/* ---- Pilihan tema ---- */
.theme-list { display: grid; gap: 8px; }
.theme-opt {
  display: grid;
  grid-template-columns: 54px 1fr;
  align-items: center;
  gap: 12px;
  text-align: left;
  padding: 8px 10px !important;
  border-radius: 12px !important;
}
.theme-opt[aria-checked="true"] { border: 2px solid var(--accent) !important; }
.theme-opt b { display: block; font-size: .95rem; }
.theme-opt small { display: block; color: var(--muted); font-size: .78rem; line-height: 1.3; }
.theme-chip {
  display: grid;
  grid-template-rows: 10px 1fr;
  height: 40px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid rgba(127, 127, 127, .25);
}
.theme-chip i { display: block; }
.theme-chip span { display: grid; grid-template-columns: 1fr 1fr; gap: 3px; padding: 4px; }
.theme-chip span i { border-radius: 3px; }
.k.ppn small { white-space: nowrap; }
.k.active { outline: 3px solid var(--accent); outline-offset: -3px; }

/* ---- Dialogs ---- */
dialog {
  border: 0;
  padding: 0;
  border-radius: 14px;
  width: min(92vw, 420px);
  background: var(--paper);
  color: var(--ink);
}
dialog::backdrop { background: rgba(0,0,0,.45); }
.sheet { padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.sheet h2 { margin: 0; font-size: 1.1rem; }
.sheet label { display: flex; flex-direction: column; gap: 4px; font-size: .9rem; color: var(--muted); }
.sheet label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--ink); }
.sheet input[type=text], .sheet select {
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--ink);
  font: inherit;
  font-size: 1rem;
}
.row { display: flex; gap: 8px; align-items: center; }
.spacer { flex: 1; }
.sheet button {
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--key);
}
.sheet button.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.sheet button.danger { color: var(--neg); }

.tape-list { list-style: none; margin: 0; padding: 0; max-height: 50vh; overflow-y: auto; }
.tape-list li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 4px;
  border-bottom: 1px solid var(--line);
}
.tape-list li.current { font-weight: 700; }
.tape-list .t-open { flex: 1; text-align: left; border: 0 !important; background: none !important; padding: 0 !important; }
.tape-list .t-meta { display: block; font-size: .8rem; color: var(--muted); font-weight: 400; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 40%;
  transform: translateX(-50%);
  background: var(--bar);
  color: var(--bar-ink);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: .9rem;
  box-shadow: 0 4px 16px rgba(0,0,0,.3);
}
