/* =============================================================================
 * css/app.css — CalcNotes design system (shared by every page).
 * -----------------------------------------------------------------------------
 * Loaded by index.php, view.php, archive/index.php and login.php. Page-specific
 * styles stay in each page's own <style> block; everything reusable lives here.
 *
 *   1. Tokens          colours, danger/blue accents, z-scale
 *   2. Base            type, scrollbar, entrance animation
 *   3. Header          brand, search, buttons (.btn / .icon-btn)
 *   4. Grid & cards    2-column even grid of note previews + empty state
 *   5. Modal           overlay (progressive blur) + note window
 *   6. Editor          .ed contenteditable surface: lines, tokens, results
 *   7. Dialog          destructive-action confirm (used by /archive/)
 *   8. Toasts          bottom-right stack (js/toast.js)
 *   9. Responsive      mobile paddings, editor column, tap targets
 *  10. Reduced motion
 * Light mode only. One accent (ink). No gradients.
 * ============================================================================= */

/* ── 1. Tokens ─────────────────────────────────────────────────────────────── */
:root {
  --ink: #171717;            /* neutral-900 — text & the ONE accent           */
  --muted: #737373;          /* neutral-500                                    */
  --faint: #a3a3a3;          /* neutral-400                                    */
  --border: #e5e5e5;         /* neutral-200                                    */
  --border-hover: #d4d4d4;   /* neutral-300                                    */
  --surface-hover: #fafafa;  /* neutral-50                                     */
  --hairline: #f5f5f5;       /* neutral-100 — internal card dividers           */
  --danger: #dc2626;         /* red-600 — destructive actions, inline errors   */
  --danger-strong: #b91c1c;  /* red-700                                        */
  --blue: #2563eb;           /* blue-600 — variable names only                 */

  --z-modal: 70;
  --z-dialog: 80;
  --z-toast: 90;
}

/* ── 2. Base ───────────────────────────────────────────────────────────────── */
* { box-sizing: border-box; margin: 0; }

html, body { height: 100%; }

body {
  font-family: "Inter", ui-sans-serif, system-ui, -apple-system, sans-serif;
  letter-spacing: -0.006em;
  -webkit-font-smoothing: antialiased;
  background: #fff;
  color: var(--ink);
  font-size: 15px;
}

.ti { font-size: 17px; line-height: 1; }
.num { font-variant-numeric: tabular-nums; }
.mono { font-family: ui-monospace, "SF Mono", Menlo, monospace; }

::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 99px; }
::-webkit-scrollbar-thumb:hover { background: var(--border-hover); }

@keyframes enterUp {
  from { opacity: 0; transform: translateY(10px); filter: blur(4px); }
  to   { opacity: 1; transform: translateY(0);    filter: blur(0); }
}
.anim { animation: enterUp .45s ease-out both; }

/* ── 3. Header ─────────────────────────────────────────────────────────────── */
.header {
  max-width: 72rem;
  margin: 0 auto;
  padding: 2.5rem 1.5rem 2rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.brand { display: flex; align-items: center; gap: .625rem; }
.brand-icon {
  width: 36px; height: 36px;
  display: grid; place-items: center;
  border-radius: 12px;
  background: var(--ink);
  color: #fff;
  text-decoration: none;
}
.brand h1 { font-size: 1.25rem; font-weight: 600; text-wrap: balance; }

.header-actions { display: flex; align-items: center; gap: .5rem; }

.search { position: relative; display: none; }
@media (min-width: 640px) { .search { display: block; } }
.search .ti {
  position: absolute; left: 12px; top: 50%;
  transform: translateY(-50%);
  color: var(--faint);
}
.search input {
  height: 36px; width: 224px;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0 12px 0 36px;
  font: inherit; font-size: .875rem;
  color: var(--ink);
  outline: none;
  transition: border-color .18s ease-out;
}
.search input::placeholder { color: var(--faint); }
.search input:hover { border-color: var(--border-hover); }
.search input:focus { border-color: var(--ink); }

.btn {
  display: inline-flex; align-items: center; gap: .375rem;
  height: 36px; padding: 0 .875rem;
  border: 0; border-radius: 10px;
  font: inherit; font-size: .875rem; font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  transition: background-color .15s ease-out, color .15s ease-out;
}
.btn:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }
.btn-primary { background: var(--ink); color: #fff; }
.btn-primary:hover { background: #404040; }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-ghost:hover { background: var(--surface-hover); color: var(--ink); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: var(--danger-strong); }

.icon-btn {
  width: 32px; height: 32px;
  display: grid; place-items: center;
  border: 0; border-radius: 10px;
  background: transparent; color: var(--muted);
  cursor: pointer;
  text-decoration: none;
  transition: background-color .15s ease-out, color .15s ease-out;
}
.icon-btn:hover { background: var(--surface-hover); color: var(--ink); }
.icon-btn:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }

/* ── 4. Grid & cards ───────────────────────────────────────────────────────── */
.grid {
  max-width: 72rem;
  margin: 0 auto;
  padding: 0 1.5rem 5rem;
  display: grid;
  gap: 1.25rem;
  /* minmax(0, 1fr) keeps columns equal — a plain 1fr lets long unbreakable
     lines stretch their column. Two columns, always. */
  grid-template-columns: minmax(0, 1fr);
}
@media (min-width: 640px)  { .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); } }

.card {
  display: flex; flex-direction: column;
  text-decoration: none;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: #fff;
  padding: 1.25rem;
  text-align: left;
  font: inherit; color: inherit;
  cursor: pointer;
  transition: border-color .15s ease-out, box-shadow .15s ease-out, transform .15s ease-out;
}
.card:hover {
  border-color: var(--border-hover);
  box-shadow: 0 4px 12px rgb(0 0 0 / .05);
  transform: translateY(-2px);
}
.card:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }

.card-title {
  font-size: .875rem; font-weight: 600;
  text-wrap: balance;
  margin-bottom: .75rem;
}
.card-lines { flex: 1; display: flex; flex-direction: column; gap: .375rem; font-size: .8125rem; }
.card-line { display: flex; align-items: baseline; justify-content: space-between; gap: .75rem; }
.card-line-text {
  color: var(--muted);
  min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.card-line-res { flex-shrink: 0; font-weight: 500; color: var(--ink); }
.card-more { padding-top: .125rem; font-size: .75rem; color: var(--faint); }
.card-divider { display: block; width: 100%; height: 1px; background: var(--border); margin: 5px 0; }

.card-meta {
  margin-top: .875rem; padding-top: .625rem;
  border-top: 1px solid var(--hairline);
  display: flex; align-items: center; justify-content: space-between;
  font-size: .6875rem; color: var(--faint);
}

.empty {
  grid-column: 1 / -1;
  display: flex; flex-direction: column; align-items: center; gap: .75rem;
  padding: 5rem 0; color: var(--muted);
  font-size: .875rem;
}
.empty .btn { margin-top: .25rem; }

/* Paper-note empty state: a tilted little note with tape. */
.empty-paper {
  position: relative;
  width: 16rem;
  margin-bottom: 1rem;
  padding: 1.5rem 1.25rem 1.25rem;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 1px 2px rgb(0 0 0 / .04), 0 6px 16px rgb(0 0 0 / .05);
  transform: rotate(-2deg);
}
.empty-tape {
  position: absolute;
  top: -10px; left: 50%;
  width: 64px; height: 20px;
  transform: translateX(-50%) rotate(1deg);
  background: rgb(245 245 245 / .85);
  border: 1px solid rgb(229 229 229 / .7);
  border-radius: 3px;
}
.empty-line {
  height: 10px;
  border-radius: 5px;
  background: #f5f5f5;
}
.empty-line + .empty-line { margin-top: .5rem; }

/* ── 5. Modal ──────────────────────────────────────────────────────────────── */
.overlay {
  position: fixed; inset: 0;
  z-index: var(--z-modal);
  background: rgb(255 255 255 / 0);
  backdrop-filter: blur(0px);
  -webkit-backdrop-filter: blur(0px);
  transition: background .28s ease-out,
              backdrop-filter .32s ease-out,
              -webkit-backdrop-filter .32s ease-out;
  pointer-events: none;
}
.overlay.open {
  background: rgb(255 255 255 / .72);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  pointer-events: auto;
}

.overlay-inner {
  height: 100dvh;
  display: flex; align-items: center; justify-content: center;
  padding: 1.5rem;
}

.modal {
  width: 100%; max-width: 42rem;
  max-height: 85dvh;
  display: flex; flex-direction: column;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 20px 25px -5px rgb(0 0 0 / .1), 0 8px 10px -6px rgb(0 0 0 / .1);
  opacity: 0;
  transform: scale(.94) translateY(14px);
  transition: opacity .22s ease-out, transform .24s ease-out;
}
.overlay.open .modal { opacity: 1; transform: scale(1) translateY(0); }

.modal-head {
  display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem;
  padding: 1.75rem 2rem 1rem;
  border-bottom: 1px solid var(--hairline);
}
.modal-head-info { min-width: 0; }
.modal-title {
  font-size: 1.25rem; font-weight: 600;
  text-wrap: balance;
  outline: none;
  border-radius: 8px;
  padding: 0 .25rem; margin: 0 -.25rem;
}
.modal-meta {
  margin-top: .375rem;
  display: flex; flex-wrap: wrap; align-items: center;
  gap: .25rem .75rem;
  font-size: .75rem; color: var(--faint);
}
.modal-meta .ti { font-size: 13px; }
.modal-meta span { display: inline-flex; align-items: center; gap: .25rem; }
.modal-actions { display: flex; align-items: center; gap: .25rem; flex-shrink: 0; }
.save-state {
  font-size: .75rem; color: var(--faint);
  margin-right: .375rem; min-width: 3.5rem; text-align: right;
  display: inline-flex; align-items: center; justify-content: flex-end; gap: .25rem;
  transition: opacity .45s ease-out;
}
.save-state .ti { font-size: 13px; }
.save-state.is-hidden { opacity: 0; }

.modal-body { flex: 1; overflow-y: auto; padding: .75rem 1.25rem 1.5rem; }

/* ── 6. Editor — one contenteditable surface, one .ed-line per line ────────── */
.ed {
  outline: none;
  caret-color: var(--ink);
}
.ed-line {
  position: relative;
  padding: 6px 11rem 6px 12px;
  border-radius: 10px;
  line-height: 1.65;
  min-height: calc(1.65em + 12px);
  color: #404040;
  /* pre-wrap makes every space significant: trailing spaces render, the caret
     advances past them, and Chrome stops deleting "collapsed" spaces on the
     next insertion (it strips insignificant whitespace while editing). */
  white-space: pre-wrap;
  word-break: break-word;
}
.ed-line:hover,
.ed-line.active { background: var(--surface-hover); }

/* Currency codes, crypto codes and tickers: one shade darker, one weight up. */
.ed-tok { color: var(--ink); font-weight: 500; }

/* Variable names in definitions (`name = …`). */
.ed-var { color: var(--blue); font-weight: 500; }

/* `#` comments: verbatim, dimmed, never evaluated. */
.ed-comment { opacity: .4; }

/* `---` divider lines: a rule normally; dim dashes while the caret is there. */
.ed-line.divider::before {
  content: '';
  position: absolute;
  left: 12px; right: 12px; top: 50%;
  border-top: 1px solid var(--border);
}
.ed-line.divider .ed-divtext { opacity: 0; }
.ed-line.divider.active::before { display: none; }
.ed-line.divider.active .ed-divtext { opacity: .2; }
.ed-res {
  position: absolute; right: 12px; top: 6px;
  line-height: 1.65;
  font-variant-numeric: tabular-nums;
  font-weight: 500;
  color: var(--ink);
  user-select: none;
  pointer-events: none;
  max-width: 10rem;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* ── 7. Dialog (destructive confirm) ───────────────────────────────────────── */
.dialog {
  position: fixed; inset: 0;
  z-index: var(--z-dialog);
  display: flex;
  align-items: center; justify-content: center;
  background: rgb(255 255 255 / .6);
  padding: 1.5rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s ease-out;
}
.dialog.open { opacity: 1; pointer-events: auto; }
.dialog-card {
  width: 100%; max-width: 22rem;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 1.5rem;
  box-shadow: 0 20px 25px -5px rgb(0 0 0 / .1);
  transform: scale(.95) translateY(8px);
  transition: transform .2s ease-out;
}
.dialog.open .dialog-card { transform: scale(1) translateY(0); }
.dialog-card h2 { font-size: 1rem; font-weight: 600; text-wrap: balance; }
.dialog-card p { margin-top: .375rem; font-size: .875rem; color: var(--muted); text-wrap: pretty; }
.dialog-buttons { margin-top: 1.25rem; display: flex; justify-content: flex-end; gap: .5rem; }

/* ── Footer ────────────────────────────────────────────────────────────────── */
.footer {
  padding: 2rem 1.5rem 2.5rem;
  text-align: center;
  font-size: .75rem;
  color: var(--faint);
}
.footer a { color: var(--muted); text-decoration: none; }
.footer a:hover { color: var(--ink); }

/* ── 8. Toasts (bottom-right) ──────────────────────────────────────────────── */
.toasts {
  position: fixed;
  right: calc(1.25rem + env(safe-area-inset-right));
  bottom: calc(1.25rem + env(safe-area-inset-bottom));
  z-index: var(--z-toast);
  display: flex; flex-direction: column; align-items: flex-end;
  gap: .5rem;
  pointer-events: none;
}
.toast {
  width: 21rem; max-width: calc(100vw - 2.5rem);
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: .875rem 1rem;
  box-shadow: 0 10px 15px -3px rgb(0 0 0 / .1), 0 4px 6px -4px rgb(0 0 0 / .1);
  pointer-events: auto;
  cursor: pointer;
  animation: enterUp .3s ease-out both;
  transition: opacity .2s ease-out, transform .2s ease-out;
}
.toast.leaving { opacity: 0; transform: translateY(6px); }
.toast-title {
  font-size: .875rem; font-weight: 600; text-wrap: balance;
  display: flex; align-items: center; gap: .375rem;
}
.toast-title .ti { font-size: 15px; color: var(--ink); flex-shrink: 0; }
.toast-body { margin-top: .1875rem; font-size: .8125rem; color: var(--muted); text-wrap: pretty; }

/* Inline evaluation errors (e.g. variable already exists). */
.ed-res-err { color: var(--danger); font-weight: 500; font-size: .8125rem; }
.ed-res-err .ti { font-size: 13px; margin-right: 4px; vertical-align: -2px; }

/* ── 9. Responsive ─────────────────────────────────────────────────────────── */
/* Mobile ≤480px: tighter chrome, and the editor's reserved result column
   shrinks so the text keeps a usable width. */
@media (max-width: 480px) {
  .header { padding: 1.5rem 1rem 1.25rem; }
  .grid { padding: 0 1rem 4rem; }
  .overlay-inner { padding: .75rem; }
  .modal { max-height: 92dvh; }
  .modal-head { padding: 1.25rem 1.25rem .875rem; }
  .modal-body { padding: .625rem .5rem 1rem; }
  .ed-line { padding-right: 7.25rem; }
  .ed-res { max-width: 6.5rem; }
  .footer { padding: 1.5rem 1rem 2rem; }
}

/* Touch devices: comfortable tap targets for icon-only controls. */
@media (pointer: coarse) {
  .icon-btn { width: 40px; height: 40px; }
}

/* ── 10. Reduced motion ────────────────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  .anim { animation: none; }
  .overlay, .modal, .dialog, .dialog-card, .toast, .save-state { transition: none; animation: none; }
}
