:root {
  --brand: #3f8f7a;
  --brand-dark: #2f6f5f;
  --brand-soft: #e6f2ee;
  --bg: #f6f7f5;
  --surface: #ffffff;
  --text: #1f2b28;
  --text-soft: #667a74;
  --border: #e4e8e6;
  --shadow: 0 2px 8px rgba(31, 43, 40, 0.06);
  --shadow-lg: 0 8px 26px rgba(31, 43, 40, 0.14);
  --radius: 15px;
  --radius-sm: 11px;
  --header-h: 54px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

[data-theme="dark"] {
  --brand: #5bbfa4;
  --brand-dark: #7fd3bc;
  --brand-soft: #1c2c28;
  --bg: #121816;
  --surface: #1b2320;
  --text: #e9efec;
  --text-soft: #93a39d;
  --border: #2a3430;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
  --shadow-lg: 0 8px 26px rgba(0, 0, 0, 0.5);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html { font-size: 15px; }
html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Tajawal", "Cairo", system-ui, sans-serif;
  overscroll-behavior-y: none;
}

body {
  min-height: 100dvh;
  padding-bottom: calc(84px + var(--safe-bottom));
}

.ico { width: 20px; height: 20px; fill: currentColor; }

/* Header */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: calc(9px + var(--safe-top)) 14px 8px;
  background: var(--bg);
  border-bottom: 1px solid transparent;
  transition: border-color .2s;
}
.app-header.scrolled { border-color: var(--border); }

.brand { display: flex; align-items: center; gap: 9px; min-width: 0; }
.brand-logo { width: 36px; height: 36px; object-fit: contain; }
.brand-text { display: flex; flex-direction: column; line-height: 1.1; min-width: 0; }
.brand-text h1 { margin: 0; font-size: 19px; font-weight: 800; color: var(--brand); }
.brand-tagline { font-size: 10px; color: var(--text-soft); }

.header-actions { display: flex; gap: 2px; }
.icon-btn {
  display: grid; place-items: center;
  width: 36px; height: 36px;
  border: none; border-radius: 11px;
  background: transparent; color: var(--text);
  cursor: pointer; transition: background .15s;
}
.icon-btn:active { background: var(--brand-soft); }

/* Search */
.search-wrap { padding: 2px 14px 8px; position: sticky; top: calc(var(--header-h) + var(--safe-top)); z-index: 15; background: var(--bg); }
.search-box {
  display: flex; align-items: center; gap: 9px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0 14px;
  height: 44px;
  box-shadow: var(--shadow);
}
.search-ico { width: 18px; height: 18px; fill: var(--text-soft); flex-shrink: 0; }
#searchInput {
  flex: 1; border: none; background: none; outline: none;
  font-family: inherit; font-size: 15px; color: var(--text);
}
.clear-search { border: none; background: none; color: var(--text-soft); font-size: 15px; cursor: pointer; padding: 4px; }

/* Category chips */
.chips-row {
  display: flex; gap: 7px; overflow-x: auto;
  padding: 2px 14px 10px;
  scrollbar-width: none;
}
.chips-row::-webkit-scrollbar { display: none; }
.chip {
  flex-shrink: 0;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-soft);
  border-radius: 999px;
  padding: 7px 13px;
  font-family: inherit; font-size: 13px; font-weight: 600;
  cursor: pointer; white-space: nowrap;
  display: flex; align-items: center; gap: 5px;
  transition: all .15s;
}
.chip.active { background: var(--brand); color: #fff; border-color: var(--brand); }
.chip .chip-count { font-size: 11px; opacity: .8; }

/* View header (breadcrumb when a category is open) */
.notes-area { padding: 0 10px; }
.view-header { display: flex; align-items: center; gap: 10px; padding: 2px 4px 12px; }
.back-btn {
  display: flex; align-items: center; gap: 4px;
  border: none; background: none; color: var(--brand-dark);
  font-family: inherit; font-weight: 700; font-size: 14px; cursor: pointer;
  padding: 4px 6px; border-radius: 8px;
}
.back-btn:active { background: var(--brand-soft); }
.view-header h2 { margin: 0; font-size: 15px; font-weight: 800; color: var(--text); }
.view-header .vh-count { color: var(--text-soft); font-weight: 600; font-size: 12px; margin-inline-start: auto; }

/* Notes grid / list */
.notes-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  align-items: start;
}
.notes-list { display: flex; flex-direction: column; gap: 10px; }

/* Shared card look */
.note-card, .stack-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.note-card {
  position: relative;
  cursor: pointer;
  animation: cardIn .3s cubic-bezier(.2,.7,.3,1) both;
}
@keyframes cardIn {
  from { opacity: 0; transform: translateY(12px) scale(.95); }
  to { opacity: 1; transform: none; }
}
.note-card.removing { animation: cardOut .26s ease forwards; pointer-events: none; }
@keyframes cardOut {
  to { opacity: 0; transform: scale(.68) translateY(10px) rotate(-4deg); }
}

.note-card .pin-badge {
  position: absolute; top: 9px; inset-inline-start: 9px;
  width: 18px; height: 18px; fill: var(--brand-dark); opacity: .85; z-index: 2;
}
.card-del {
  position: absolute; top: 6px; inset-inline-end: 6px; z-index: 3;
  width: 27px; height: 27px; border: none; border-radius: 9px;
  background: rgba(0,0,0,.05); color: #c0433a;
  display: grid; place-items: center; cursor: pointer; opacity: .65;
  transition: opacity .15s, background .15s;
}
.card-del svg { width: 15px; height: 15px; fill: currentColor; }
.card-del:active { background: rgba(192,67,58,.16); opacity: 1; }
[data-theme="dark"] .card-del { background: rgba(255,255,255,.07); }

.note-title {
  margin: 0 0 5px; font-size: 15px; font-weight: 700;
  color: var(--text); word-break: break-word; padding-inline-end: 22px;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.note-body {
  margin: 0; font-size: 13.5px; line-height: 1.65; color: var(--text-soft);
  white-space: pre-wrap; word-break: break-word;
  display: -webkit-box; -webkit-line-clamp: 7; -webkit-box-orient: vertical; overflow: hidden;
}
.note-tags { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 9px; }
.note-tag {
  font-size: 10.5px; font-weight: 600;
  background: rgba(0,0,0,.05); color: var(--text-soft);
  padding: 3px 8px; border-radius: 999px;
}
[data-theme="dark"] .note-tag { background: rgba(255,255,255,.08); }
.note-meta {
  display: flex; align-items: center; justify-content: space-between;
  margin-top: 9px; font-size: 10.5px; color: var(--text-soft);
}
.note-cat { display: flex; align-items: center; gap: 4px; font-weight: 600; }

/* Stacked cards (per-category) */
.stacks-wrap {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px 10px;
  align-items: start;
}
.stack {
  position: relative;
  cursor: pointer;
  animation: cardIn .32s cubic-bezier(.2,.7,.3,1) both;
}
.stack:active .stack-card { transform: translateY(1px); }
.stack-card {
  position: relative; z-index: 3;
  transition: transform .12s;
}
.stack-peek {
  position: absolute; inset: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  transform-origin: top center;
}
.stack-peek.p1 { transform: translateY(6px) scaleX(.955); z-index: 2; }
.stack-peek.p2 { transform: translateY(12px) scaleX(.905); z-index: 1; }
.stack-count {
  position: absolute; top: 8px; inset-inline-start: 8px; z-index: 4;
  background: var(--brand); color: #fff;
  min-width: 22px; height: 22px; padding: 0 7px;
  border-radius: 999px; font-size: 11px; font-weight: 800;
  display: grid; place-items: center; box-shadow: var(--shadow);
}

/* Empty state */
.empty-state { text-align: center; padding: 54px 20px; color: var(--text-soft); grid-column: 1 / -1; }
.empty-emoji { font-size: 48px; margin-bottom: 10px; }
.empty-title { font-size: 17px; font-weight: 700; color: var(--text); margin: 0 0 5px; }
.empty-sub { font-size: 13px; margin: 0; }

/* Footer / copyright link */
.app-footer { text-align: center; padding: 30px 24px 8px; }
.rights-link {
  display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--border); background: var(--surface);
  color: var(--text-soft); font-family: inherit; font-size: 12px; font-weight: 600;
  padding: 8px 15px; border-radius: 999px; cursor: pointer;
  transition: color .15s, border-color .15s;
}
.rights-link:active { color: var(--brand); border-color: var(--brand); }
.rights-ico { width: 15px; height: 15px; fill: var(--brand); opacity: .9; }

/* Rights sheet */
.rights-sheet {
  width: 100%; max-width: 640px; background: var(--bg);
  border-radius: 22px 22px 0 0; max-height: 88dvh;
  display: flex; flex-direction: column;
  padding-bottom: var(--safe-bottom);
  animation: slideUp .25s ease;
}
.rights-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 4px 14px 10px; border-bottom: 1px solid var(--border);
}
.rights-head h2 { margin: 0; font-size: 16px; font-weight: 800; color: var(--text); }
.rights-body { overflow-y: auto; padding: 16px 18px 24px; }
.rights-brand { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; }
.rights-brand img { width: 46px; height: 46px; object-fit: contain; }
.rights-brand strong { display: block; font-size: 17px; font-weight: 800; color: var(--brand); }
.rights-brand span { font-size: 12px; color: var(--text-soft); }
.rights-copy { font-weight: 700; color: var(--text); font-size: 14px; margin: 0 0 12px; }
.rights-body p { margin: 0 0 12px; font-size: 13px; line-height: 1.85; color: var(--text-soft); }
.rights-body p:last-child { margin-bottom: 0; }

/* FAB */
.fab {
  position: fixed;
  inset-inline-end: 18px;
  bottom: calc(18px + var(--safe-bottom));
  width: 54px; height: 54px;
  border: none; border-radius: 18px;
  background: var(--brand); color: #fff;
  display: grid; place-items: center;
  box-shadow: 0 8px 22px rgba(63, 143, 122, .45);
  cursor: pointer; z-index: 30;
  transition: transform .12s;
}
.fab:active { transform: scale(.92); }
.fab svg { width: 25px; height: 25px; fill: currentColor; }

/* Sheet / editor */
.sheet-overlay, .menu-overlay {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(0,0,0,.4);
  display: flex; align-items: flex-end; justify-content: center;
  animation: fade .2s;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.sheet {
  width: 100%; max-width: 640px;
  background: var(--bg);
  border-radius: 22px 22px 0 0;
  max-height: 92%;
  display: flex; flex-direction: column;
  animation: slideUp .25s ease;
  padding-bottom: var(--safe-bottom);
}
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }

.sheet-handle { width: 40px; height: 5px; border-radius: 3px; background: var(--border); margin: 9px auto 3px; }
.editor-top { display: flex; align-items: center; justify-content: space-between; padding: 5px 12px 9px; }
.editor-top-actions { display: flex; align-items: center; gap: 6px; }
#pinToggle.active { color: var(--brand); background: var(--brand-soft); }
.btn-save {
  border: none; background: var(--brand); color: #fff;
  font-family: inherit; font-weight: 700; font-size: 14px;
  padding: 8px 20px; border-radius: 11px; cursor: pointer;
}
.btn-save:active { background: var(--brand-dark); }

.editor-body { flex: 1 1 auto; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 4px 16px 20px; }
.note-title-input {
  width: 100%; border: none; background: none; outline: none;
  font-family: inherit; font-size: 20px; font-weight: 800; color: var(--text);
  padding: 7px 0;
}
.note-body-input {
  width: 100%; min-height: 120px; resize: vertical;
  border: none; background: none; outline: none;
  font-family: inherit; font-size: 16px; line-height: 1.85; color: var(--text);
  padding: 4px 0 14px;
}
.note-title-input::placeholder, .note-body-input::placeholder { color: var(--text-soft); opacity: .7; }

.field { margin-top: 14px; }
.field-label { display: block; font-size: 12.5px; font-weight: 700; color: var(--text-soft); margin-bottom: 7px; }
.field-label .hint { font-weight: 400; opacity: .8; }
.field-input, .field-select {
  width: 100%; height: 44px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--text);
  font-family: inherit; font-size: 14px; padding: 0 13px; outline: none;
}
.field-select { appearance: none; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%23667a74'%3E%3Cpath d='M7 10l5 5 5-5z'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: left 12px center; }

.color-row { display: flex; gap: 9px; flex-wrap: wrap; }
.color-dot {
  width: 34px; height: 34px; border-radius: 50%;
  border: 2px solid var(--border); cursor: pointer;
  display: grid; place-items: center;
}
.color-dot.selected { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
.color-dot .check { width: 16px; height: 16px; fill: #2f6f5f; opacity: 0; }
.color-dot.selected .check { opacity: 1; }

.editor-footer {
  display: flex; gap: 8px; padding: 9px 16px; border-top: 1px solid var(--border);
  overflow-x: auto; scrollbar-width: none;
}
.editor-footer::-webkit-scrollbar { display: none; }
.footer-btn {
  flex-shrink: 0; display: flex; align-items: center; gap: 6px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text-soft);
  border-radius: 999px; padding: 8px 13px; font-family: inherit; font-size: 12.5px; font-weight: 600;
  cursor: pointer;
}
.footer-btn.danger { color: #c0433a; }
.footer-btn .ico { width: 17px; height: 17px; }

/* Sort menu */
.menu-overlay { align-items: flex-end; }
.menu-sheet {
  width: 100%; max-width: 640px; background: var(--surface);
  border-radius: 20px 20px 0 0; padding: 8px 8px calc(12px + var(--safe-bottom));
  animation: slideUp .22s ease;
}
.menu-title { padding: 13px 16px 7px; font-size: 12.5px; font-weight: 700; color: var(--text-soft); }
.menu-item {
  display: block; width: 100%; text-align: start;
  border: none; background: none; color: var(--text);
  font-family: inherit; font-size: 15px; padding: 13px 16px; border-radius: 11px; cursor: pointer;
}
.menu-item:active, .menu-item.active { background: var(--brand-soft); color: var(--brand-dark); font-weight: 700; }

/* Toast */
.toast {
  position: fixed; inset-inline: 16px; bottom: calc(84px + var(--safe-bottom)); z-index: 60;
  background: #2a3430; color: #fff;
  border-radius: 13px; padding: 13px 15px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  box-shadow: var(--shadow-lg); max-width: 480px; margin-inline: auto;
  animation: slideUp .2s ease; font-size: 13.5px;
}
.toast-action {
  border: none; background: none; color: var(--brand-dark); font-family: inherit;
  font-weight: 800; font-size: 13.5px; cursor: pointer; white-space: nowrap;
}

@media (min-width: 620px) {
  .notes-grid, .stacks-wrap { grid-template-columns: repeat(3, 1fr); }
  .notes-area, .search-wrap, .chips-row, .app-header { max-width: 900px; margin-inline: auto; }
}
