:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --fg: #0c0d10;
  --muted: #6b7280;
  --line: #e7e9ee;
  --soft: #f2f4f7;
  --accent: #0c0d10;
  --green: #0f9d58;
  --green-soft: #e7f5ec;
  --red: #d93025;
  --red-soft: #fdeceb;
  --danger: #c5221f;
  --radius: 18px;
  --radius-sm: 13px;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.05), 0 1px 3px rgba(16, 24, 40, 0.08);
  --shadow-md: 0 6px 18px rgba(16, 24, 40, 0.09);
  --shadow-lg: 0 18px 44px rgba(16, 24, 40, 0.16);
  --nav-h: 76px;
  --card-h: 140px;
  --content-max: 640px;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html {
  -webkit-text-size-adjust: 100%;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--fg);
  font-family: "Cairo", "Segoe UI", "Tahoma", "Arial", sans-serif;
  font-size: clamp(17px, 2.6vw, 19px);
  line-height: 1.55;
  direction: rtl;
  height: 100%;
  overflow: hidden;
}

:focus-visible {
  outline: 3px solid #111;
  outline-offset: 2px;
  border-radius: 6px;
}

/* App shell */
.app {
  width: 100%;
  max-width: var(--content-max);
  margin: 0 auto;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  position: relative;
}

/* Top bar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px clamp(16px, 3vw, 28px);
  background: rgba(255, 255, 255, 0.88);
  backdrop-filter: saturate(180%) blur(14px);
  -webkit-backdrop-filter: saturate(180%) blur(14px);
  border-bottom: 1px solid var(--line);
  box-shadow: var(--shadow-sm);
}

.logo {
  width: clamp(40px, 8vw, 48px);
  height: clamp(40px, 8vw, 48px);
  object-fit: cover;
  border-radius: 12px;
  box-shadow: var(--shadow-sm);
  flex: none;
}

.brand {
  font-size: clamp(21px, 4.5vw, 25px);
  font-weight: 800;
  margin: 0;
  letter-spacing: -0.01em;
}

.topbar .sync-badge {
  margin-inline-start: auto;
  padding: 6px 12px;
  font-size: clamp(12px, 2.4vw, 14px);
  font-weight: 700;
  color: var(--muted);
  background: var(--soft);
  border: 1px solid var(--line);
  border-radius: 999px;
  box-shadow: none;
  white-space: nowrap;
  max-width: 46%;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: center;
}

/* View */
.view {
  flex: 1;
  min-height: 0;
  width: 100%;
  max-width: var(--content-max);
  margin: 0 auto;
  padding: clamp(14px, 3vw, 24px);
  padding-bottom: clamp(18px, 4vw, 32px);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}

.screen {
  display: flex;
  flex-direction: column;
  gap: clamp(12px, 2.4vw, 18px);
  flex: 0 0 auto;
}

/* Fixed screen: fills the viewport; scrolling is only a fallback */
.screen--fixed {
  flex: 1 0 auto;
}

.screen--fixed > :not(.accounts-box) {
  flex: none;
}

.page-title {
  font-size: clamp(22px, 4.6vw, 29px);
  font-weight: 800;
  margin: 0;
  letter-spacing: -0.02em;
  display: flex;
  align-items: center;
  gap: 10px;
}

.page-head {
  display: flex;
  align-items: center;
  gap: 10px;
}

.section-title {
  font-size: clamp(17px, 3.4vw, 20px);
  font-weight: 800;
  margin: 6px 0 0;
  padding-inline-start: 12px;
  position: relative;
}

.section-title::before {
  content: "";
  position: absolute;
  inset-inline-start: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 4px;
  height: 1.1em;
  border-radius: 999px;
  background: var(--fg);
}

.muted {
  color: var(--muted);
}

.small {
  font-size: clamp(14px, 2.4vw, 16px);
}

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: clamp(13px, 2.4vw, 18px);
  box-shadow: var(--shadow-sm);
}

a.card,
.card.linkable {
  color: var(--fg);
  text-decoration: none;
  display: flex;
}

a.card {
  transition: box-shadow 0.18s ease, transform 0.12s ease, border-color 0.18s ease;
}

a.card:hover {
  box-shadow: var(--shadow-md);
  transform: translateY(-2px);
  border-color: #dfe2e8;
}

a.card:active {
  transform: translateY(0) scale(0.995);
}

/* Summary */
.summary {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: clamp(10px, 2vw, 16px);
}

.summary-card {
  position: relative;
  overflow: hidden;
  padding-inline-start: clamp(16px, 3vw, 20px);
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.summary-card::before {
  content: "";
  position: absolute;
  inset-inline-start: 0;
  top: 0;
  bottom: 0;
  width: 6px;
  background: var(--fg);
}

.summary-card.for_me::before {
  background: linear-gradient(180deg, #16b364, #0f9d58);
}

.summary-card.on_me::before {
  background: linear-gradient(180deg, #f0615a, #d93025);
}

.summary .label {
  font-size: clamp(16px, 3vw, 18px);
  font-weight: 700;
  color: var(--muted);
}

.summary .value {
  font-size: clamp(26px, 7vw, 40px);
  font-weight: 800;
  margin-top: 2px;
  direction: ltr;
  text-align: right;
  overflow-wrap: anywhere;
  letter-spacing: -0.02em;
}

.summary .dir-chip {
  align-self: flex-start;
  margin-top: 6px;
}

/* Account cards */
.list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.account {
  flex-direction: column;
  gap: 9px;
}

.account .acc-name {
  font-size: clamp(19px, 3.6vw, 22px);
  font-weight: 800;
  overflow-wrap: anywhere;
  letter-spacing: -0.01em;
  line-height: 1.25;
}

.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.account-mid {
  border-top: 1px dashed var(--line);
  padding-top: 9px;
}

.account .value {
  font-size: clamp(22px, 4vw, 26px);
  font-weight: 800;
  direction: ltr;
  letter-spacing: -0.01em;
  line-height: 1.15;
}

.account-foot {
  opacity: 0.9;
  line-height: 1.2;
}

/* Scrolling box: fills remaining space (up to two cards); scrolling is a fallback */
.accounts-box {
  flex: 1 1 auto;
  min-height: 170px;
  max-height: calc(2 * var(--card-h) + 12px);
  overflow-y: auto;
  overscroll-behavior: contain;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 2px 4px 2px 2px;
  border-radius: var(--radius);
  scrollbar-width: thin;
  scrollbar-color: #d0d5dd transparent;
}

.accounts-box::-webkit-scrollbar {
  width: 8px;
}

.accounts-box::-webkit-scrollbar-thumb {
  background: #d0d5dd;
  border-radius: 999px;
}

.accounts-box .account {
  height: var(--card-h);
  min-height: var(--card-h);
  overflow: hidden;
  justify-content: space-between;
  flex: none;
}

.accounts-box .account .acc-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.accounts-box .note-chip {
  max-width: 62%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Direction chip */
.dir-chip {
  font-size: clamp(13px, 2.4vw, 14px);
  font-weight: 800;
  padding: 3px 12px;
  border-radius: 999px;
  border: 1px solid transparent;
  white-space: nowrap;
  flex: none;
}

.dir-chip.for_me {
  background: var(--green-soft);
  color: #0b7a44;
  border-color: #c7e9d5;
}

.dir-chip.on_me {
  background: var(--red-soft);
  color: #b3261e;
  border-color: #f5cbc7;
}

/* Note / category chip */
.note-chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  align-self: flex-start;
  max-width: 100%;
  font-size: clamp(13px, 2.4vw, 14px);
  font-weight: 700;
  color: #475467;
  background: var(--soft);
  border: 1px solid var(--line);
  padding: 4px 12px 4px 10px;
  border-radius: 999px;
  overflow-wrap: anywhere;
}

.note-chip::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 3px;
  background: linear-gradient(135deg, #98a2b3, #667085);
  flex: none;
}

/* Buttons */
.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--fg);
  font-family: inherit;
  font-size: clamp(17px, 2.8vw, 19px);
  font-weight: 800;
  min-height: 52px;
  padding: 10px 18px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: transform 0.08s ease, box-shadow 0.18s ease, background 0.18s ease,
    color 0.18s ease, border-color 0.18s ease;
}

.btn.primary {
  background: var(--fg);
  color: #fff;
  border-color: var(--fg);
  box-shadow: 0 6px 16px rgba(12, 13, 16, 0.22);
}

.btn.primary:hover {
  box-shadow: 0 10px 22px rgba(12, 13, 16, 0.28);
}

.btn.outline:hover {
  background: var(--soft);
}

.btn.danger {
  border-color: #f2c9c7;
  color: var(--danger);
  background: var(--red-soft);
}

.btn.block {
  display: block;
  width: 100%;
  text-align: center;
  text-decoration: none;
}

.btn-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  flex: none;
}

.btn:active {
  transform: scale(0.99);
}

.btn:disabled {
  opacity: 0.5;
  cursor: default;
  box-shadow: none;
}

.icon-btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--fg);
  font-size: 22px;
  font-weight: 800;
  width: 44px;
  height: 44px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  flex: none;
  box-shadow: var(--shadow-sm);
}

/* Inputs */
.field {
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.field-label {
  font-size: clamp(15px, 2.6vw, 17px);
  font-weight: 700;
  color: #344054;
}

.input {
  width: 100%;
  font-family: inherit;
  font-size: clamp(17px, 2.8vw, 19px);
  font-weight: 600;
  color: var(--fg);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  min-height: 52px;
  padding: 10px 15px;
  box-shadow: var(--shadow-sm);
  transition: border-color 0.18s ease, box-shadow 0.18s ease;
}

.input::placeholder {
  color: #98a2b3;
  font-weight: 500;
}

.input:focus {
  outline: none;
  border-color: var(--fg);
  box-shadow: 0 0 0 4px rgba(12, 13, 16, 0.08);
}

/* Toggle */
.toggle {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.toggle-btn {
  font-family: inherit;
  font-size: clamp(15px, 2.6vw, 17px);
  font-weight: 800;
  min-height: 52px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--fg);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: background 0.18s ease, color 0.18s ease;
}

.toggle-btn.active {
  background: var(--fg);
  color: #fff;
  border-color: var(--fg);
}

/* Filter chips */
.chips {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.chip {
  font-family: inherit;
  font-size: clamp(15px, 2.6vw, 17px);
  font-weight: 800;
  padding: 8px 18px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--fg);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: background 0.18s ease, color 0.18s ease, border-color 0.18s ease;
}

.chip.active {
  background: var(--fg);
  color: #fff;
  border-color: var(--fg);
}

/* Payment rows */
.payment {
  width: 100%;
  text-align: right;
  font-family: inherit;
  cursor: pointer;
  flex-direction: column;
  gap: 8px;
}

.pay-amount {
  font-size: clamp(19px, 3.6vw, 22px);
  font-weight: 800;
  direction: ltr;
}

.total-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border: 1px solid var(--fg);
  border-radius: var(--radius-sm);
  padding: 13px clamp(14px, 3vw, 18px);
  font-size: clamp(18px, 3.2vw, 20px);
  font-weight: 800;
  background: linear-gradient(180deg, #ffffff, #fbfbfc);
  box-shadow: var(--shadow-sm);
}

.total-row .value {
  font-size: clamp(24px, 5vw, 30px);
  font-weight: 800;
  direction: ltr;
  letter-spacing: -0.02em;
}

/* Sync badge */
.sync-badge {
  font-size: clamp(14px, 2.4vw, 15px);
  font-weight: 700;
  padding: 9px 14px;
  border-radius: var(--radius-sm);
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--muted);
  text-align: center;
  box-shadow: var(--shadow-sm);
}

/* Empty state */
.empty {
  border: 1.5px dashed #cfd4dc;
  border-radius: var(--radius);
  padding: 32px 24px;
  text-align: center;
  color: var(--muted);
  font-weight: 700;
  background: rgba(255, 255, 255, 0.6);
}

/* Dialog sheet */
dialog.sheet {
  border: none;
  border-radius: 20px;
  padding: clamp(18px, 4vw, 24px);
  width: min(92vw, 420px);
  display: flex;
  flex-direction: column;
  gap: 14px;
  font-family: inherit;
  box-shadow: var(--shadow-lg);
}

dialog.sheet::backdrop {
  background: rgba(12, 13, 16, 0.5);
  backdrop-filter: blur(2px);
}

.sheet-title {
  margin: 0;
  font-size: clamp(19px, 3.6vw, 22px);
  font-weight: 800;
}

.sheet-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* Login */
.login-head {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  text-align: center;
  margin-bottom: 8px;
}

.logo.lg {
  width: clamp(84px, 22vw, 100px);
  height: clamp(84px, 22vw, 100px);
  border-radius: 24px;
  box-shadow: var(--shadow-md);
}

.divider {
  text-align: center;
  color: var(--muted);
  font-weight: 700;
  position: relative;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--fg);
  color: #fff;
  font-size: clamp(16px, 2.8vw, 18px);
  font-weight: 700;
  padding: 12px 22px;
  border-radius: 999px;
  z-index: 50;
  max-width: 90vw;
  text-align: center;
  box-shadow: var(--shadow-lg);
  animation: toast-in 0.18s ease;
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translate(-50%, 8px);
  }
  to {
    opacity: 1;
    transform: translate(-50%, 0);
  }
}

/* ===== Tablet ===== */
@media (min-width: 640px) {
  :root {
    --content-max: 720px;
  }
}

/* ===== Desktop ===== */
@media (min-width: 1000px) {
  :root {
    --content-max: 1080px;
    --card-h: 150px;
  }

  .app {
    box-shadow: 0 0 0 1px var(--line);
  }

  .view {
    padding: 26px;
    padding-bottom: 44px;
  }

  .screen:has(.login-head) {
    max-width: 460px;
    margin: 6vh auto 0;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 22px;
    padding: 30px;
    box-shadow: var(--shadow-md);
  }
}

@media (min-width: 1400px) {
  :root {
    --content-max: 1200px;
  }
}
