* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #8f8577;
  --bg-light: #a9a196;
  --card: #a9a196;
  --card-light: #d9d3c8;
  --red: #d81c1c;
  --red-dark: #b81414;
  --text-dark: #2b2b2b;
  --white: #f4f1ec;
}

html, body {
  height: 100%;
  font-family: 'Tahoma', 'Segoe UI', Arial, sans-serif;
  direction: rtl;
  background: var(--bg);
  color: var(--text-dark);
}

#app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100vh;
  background: var(--bg);
  position: relative;
  display: flex;
  flex-direction: column;
}

.screen { padding: 18px 16px 90px; flex: 1; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 4px 6px;
  direction: ltr; /* pins first child physically left, last child physically right,
                     regardless of the page's overall RTL direction — fixes the
                     back-arrow-on-the-wrong-side bug for good */
}

.back-btn {
  color: var(--red);
  font-size: 26px;
  background: none;
  border: none;
  cursor: pointer;
  line-height: 1;
  text-decoration: none;
  display: inline-block;
}

.title {
  font-size: 22px;
  font-weight: bold;
  flex: 1;
  text-align: center;
  cursor: default;
}

.title.clickable { cursor: pointer; text-decoration: underline; }

.icon-btn {
  background: none;
  border: none;
  font-size: 20px;
  color: var(--red);
  cursor: pointer;
}

.icon-row { display: flex; gap: 8px; }

/* ---- Cards / grid ---- */
.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
  margin-top: 16px;
}

.card {
  background: var(--card);
  border-radius: 12px;
  padding: 18px 10px;
  text-align: center;
  cursor: pointer;
  box-shadow: 0 2px 4px rgba(0,0,0,0.15);
  transition: transform .1s ease;
}
.card:active { transform: scale(0.97); }

.card .label { font-size: 17px; font-weight: 600; margin-bottom: 10px; }
.card .amount-box {
  background: var(--card-light);
  border-radius: 8px;
  padding: 10px 4px;
  color: var(--red);
  font-weight: bold;
  font-size: 15px;
}

.card.wide { grid-column: 1 / -1; }

.card.taken { background: var(--red); color: var(--white); }
.card.taken .amount-box { background: rgba(0,0,0,0.2); color: var(--white); }

/* list rows (properties / cars) */
.list-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--card);
  border-radius: 10px;
  padding: 14px 16px;
  margin-bottom: 10px;
  cursor: pointer;
  box-shadow: 0 2px 4px rgba(0,0,0,0.12);
}
.list-row.taken { background: var(--red); color: var(--white); }
.list-row .name { font-weight: 600; font-size: 16px; }
.list-row .value { color: var(--red); font-weight: bold; }
.list-row.taken .value { color: var(--white); }

.list-header {
  display: flex;
  justify-content: space-between;
  padding: 0 16px 6px;
  font-size: 13px;
  color: #444;
}

/* forms */
.field-label { font-size: 15px; margin: 18px 0 6px; }
input[type=text], input[type=password], input[type=number], select {
  width: 100%;
  padding: 12px 14px;
  border-radius: 20px;
  border: none;
  background: var(--card-light);
  font-size: 15px;
  outline: none;
}

.btn-primary {
  display: block;
  width: 100%;
  max-width: 220px;
  margin: 26px auto 0;
  background: var(--red);
  color: white;
  border: none;
  padding: 12px 20px;
  border-radius: 24px;
  font-size: 17px;
  font-weight: bold;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(0,0,0,0.25);
}
.btn-primary:active { background: var(--red-dark); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }

.login-wrap {
  display: flex;
  flex-direction: column;
  justify-content: center;
  min-height: 100vh;
  padding: 0 30px;
}
.login-wrap h1 { text-align: center; margin-bottom: 40px; color: var(--white); font-size: 22px; }
.login-wrap .field-label { color: var(--white); }
.error-msg { color: #ffd7d7; background: rgba(216,28,28,0.4); padding: 8px 12px; border-radius: 8px; margin-top: 16px; text-align: center; font-size: 14px; }

/* detail / feature box */
.feature-box {
  background: var(--card);
  border-radius: 12px;
  padding: 16px;
  margin-top: 10px;
}
.feature-box h3 { font-size: 15px; margin-bottom: 8px; }
.feature-box ul { padding-inline-start: 18px; font-size: 13px; line-height: 1.6; }
.feature-box .empty { color: #555; font-size: 13px; }

.image-box {
  background: var(--card);
  border-radius: 12px;
  height: 140px;
  margin-top: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #555;
  font-size: 13px;
}

/* transfer page */
.transfer-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-top: 20px;
}
.transfer-btn {
  background: var(--card);
  border: 3px solid transparent;
  border-radius: 12px;
  padding: 22px 10px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}
.transfer-btn.active { border-color: var(--red); background: var(--card-light); }

.transfer-detail {
  margin-top: 20px;
  background: var(--card-light);
  border-radius: 12px;
  padding: 14px;
}
.pick-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 12px;
  background: var(--card);
  border-radius: 8px;
  margin-bottom: 8px;
  cursor: pointer;
}
.pick-row.selected { outline: 2px solid var(--red); }
.pick-row.disabled { opacity: 0.4; cursor: not-allowed; }

.save-bar { position: sticky; bottom: 0; padding: 14px 0 4px; text-align: center; background: linear-gradient(to top, var(--bg) 70%, transparent); }

.home-indicator {
  position: absolute;
  bottom: 8px;
  left: 50%;
  transform: translateX(-50%);
  width: 120px;
  height: 4px;
  background: #222;
  border-radius: 3px;
}

.userbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 4px 0;
  font-size: 12px;
  color: #333;
}
.logout-link { color: var(--red); background: none; border: none; font-size: 12px; cursor: pointer; text-decoration: underline; }

.toast {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--red);
  color: white;
  padding: 10px 18px;
  border-radius: 20px;
  font-size: 14px;
  z-index: 999;
  box-shadow: 0 3px 10px rgba(0,0,0,0.3);
}

/* ---- v3 additions ---- */

.remaining-box {
  background: var(--card-light);
  border-radius: 10px;
  padding: 14px;
  margin: 14px 0;
  text-align: center;
}
.remaining-box .label { font-size: 13px; color: #333; margin-bottom: 4px; }
.remaining-box .value { color: var(--red); font-weight: bold; font-size: 24px; }

.history-section { margin-top: 16px; }
.history-section h3 { font-size: 15px; margin-bottom: 8px; }
.history-item {
  background: var(--card);
  border-radius: 8px;
  padding: 10px 12px;
  margin-bottom: 6px;
  font-size: 13px;
  display: flex;
  justify-content: space-between;
  gap: 8px;
}
.history-item .meta { color: #333; font-size: 11px; }
.history-empty { color: #444; font-size: 13px; padding: 6px 2px; }

.edit-row {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
  margin-top: 6px;
}
.edit-btn {
  background: var(--card-light);
  border: none;
  border-radius: 14px;
  padding: 4px 12px;
  font-size: 12px;
  cursor: pointer;
  color: var(--text-dark);
}
.edit-btn:active { background: var(--card); }

.edit-form {
  background: var(--card-light);
  border-radius: 10px;
  padding: 12px;
  margin-top: 8px;
}
.edit-form input { margin-bottom: 8px; }
.edit-form-actions { display: flex; gap: 8px; margin-top: 6px; }
.edit-form-actions button { flex: 1; padding: 8px; border-radius: 16px; border: none; font-size: 13px; cursor: pointer; }
.edit-form-actions .confirm { background: var(--red); color: white; }
.edit-form-actions .cancel { background: #ccc; }

.badge {
  display: inline-block;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 10px;
  background: var(--red);
  color: white;
  margin-inline-start: 6px;
}
.badge.partial { background: #c98a1a; }

.group-header {
  font-size: 16px;
  font-weight: bold;
  margin: 18px 0 8px;
  padding-bottom: 4px;
  border-bottom: 1px solid rgba(0,0,0,0.15);
  display: flex;
  justify-content: space-between;
}
.group-header .group-value { color: var(--red); font-size: 14px; }

.child-row { margin-inline-start: 10px; }

.note-card {
  background: var(--card);
  border-radius: 10px;
  padding: 12px 14px;
  margin-bottom: 10px;
}
.note-card .note-text { font-size: 14px; margin-bottom: 8px; white-space: pre-wrap; }
.note-card .note-meta { font-size: 11px; color: #333; margin-bottom: 8px; }
.note-card .note-actions { display: flex; gap: 8px; }
.note-card .note-actions button {
  background: var(--card-light);
  border: none;
  border-radius: 12px;
  padding: 4px 10px;
  font-size: 12px;
  cursor: pointer;
}
.note-card .note-actions .danger { color: var(--red); }

.new-note-box {
  background: var(--card-light);
  border-radius: 10px;
  padding: 12px;
  margin-bottom: 16px;
}
.new-note-box textarea {
  width: 100%;
  border-radius: 12px;
  border: none;
  padding: 10px;
  font-family: inherit;
  font-size: 14px;
  resize: vertical;
  min-height: 60px;
}

.share-row {
  font-size: 12px;
  color: #333;
  padding: 4px 0;
}

.percent-input-row { display: flex; align-items: center; gap: 8px; }
.percent-input-row input { flex: 1; }

.error-inline { color: var(--red); font-size: 13px; margin-top: 6px; text-align: center; }

/* ---- v4 polish ---- */

.card, .list-row {
  transition: transform .15s ease, box-shadow .15s ease;
}
.card:active, .list-row:active { transform: scale(0.96); box-shadow: 0 1px 2px rgba(0,0,0,0.1); }

.btn-primary {
  transition: transform .12s ease, box-shadow .12s ease;
}
.btn-primary:active { transform: scale(0.95); }

.toast {
  animation: toastIn .25s ease, toastOut .3s ease 2.3s forwards;
}
@keyframes toastIn {
  from { opacity: 0; transform: translateX(-50%) translateY(-12px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}
@keyframes toastOut {
  from { opacity: 1; }
  to { opacity: 0; transform: translateX(-50%) translateY(-8px); }
}

.title.clickable {
  transition: opacity .15s ease;
}
.title.clickable:active { opacity: 0.6; }

.two-btn-row {
  display: flex;
  gap: 10px;
}
.two-btn-row .btn-primary { margin: 0; }
.btn-secondary {
  display: block;
  width: 100%;
  max-width: 220px;
  background: var(--card-light);
  color: var(--text-dark);
  border: 2px solid var(--red);
  padding: 10px 20px;
  border-radius: 24px;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  transition: transform .12s ease;
}
.btn-secondary:active { transform: scale(0.95); background: var(--card); }

.icon-btn {
  transition: transform .15s ease;
}
.icon-btn:active { transform: scale(0.85) rotate(-5deg); }

.fade-in-slow {
  animation: fadeInSlow .4s ease;
}
@keyframes fadeInSlow {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* ---- v5 additions ---- */

.history-item {
  flex-direction: column;
  align-items: stretch;
  gap: 4px;
}
.history-item .history-top {
  display: flex;
  justify-content: space-between;
  gap: 8px;
}
.history-item .del-btn {
  align-self: flex-end;
  background: none;
  border: none;
  color: var(--red);
  font-size: 11px;
  text-decoration: underline;
  cursor: pointer;
  padding: 2px 4px;
}
.history-item .del-btn:active { opacity: 0.6; }

/* ---- v6 additions ---- */

.tx-actions {
  display: flex;
  gap: 12px;
  align-self: flex-end;
}
.tx-actions button {
  background: none;
  border: none;
  font-size: 11px;
  text-decoration: underline;
  cursor: pointer;
  padding: 2px 4px;
}
.tx-actions .del-btn { color: var(--red); }
.tx-actions .edit-btn-inline { color: #333; }

.history-item .tx-note {
  font-size: 12px;
  color: #333;
  font-style: italic;
  padding: 2px 2px 0;
}

.note-field-box {
  background: var(--card-light);
  border-radius: 10px;
  padding: 10px;
  margin-top: 10px;
}
.note-field-box textarea {
  width: 100%;
  border-radius: 8px;
  border: none;
  padding: 8px;
  font-family: inherit;
  min-height: 45px;
  resize: vertical;
}

/* ---- v7 additions: income/expense/exchange ---- */

.history-item.expense-item {
  background: rgba(216, 28, 28, 0.18);
  border-inline-start: 3px solid var(--red);
}
.history-item.expense-item .history-top span:first-child {
  color: var(--red);
  font-weight: bold;
}
.history-item.income-item {
  background: rgba(60, 140, 60, 0.15);
  border-inline-start: 3px solid #3c8c3c;
}
.history-item.income-item .history-top span:first-child {
  color: #2f6e2f;
  font-weight: bold;
}

.dest-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 16px;
}
.dest-btn {
  background: var(--card);
  border: 3px solid transparent;
  border-radius: 12px;
  padding: 18px 10px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.dest-btn.active { border-color: var(--red); background: var(--card-light); }
.dest-btn.expense-mode.active { border-color: #2f6e2f; }
