@import url('https://fonts.bunny.net/css?family=inter:300,400,500,600,700|jetbrains-mono:400,500,600&display=swap');

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; -webkit-text-size-adjust: 100%; }

:root {
  --font-sans: 'Inter', -apple-system, system-ui, sans-serif;
  --font-mono: 'JetBrains Mono', ui-monospace, monospace;
  --gold: #d4a017;
  --gold-bright: #f0c040;
  --gold-dim: #8a6f10;
  --emerald: #1f4a2c;
  --emerald-light: #2a6b40;
  --ok: #34d399;
  --warn: #fbbf24;
  --err: #f87171;
  --info: #60a5fa;
  --r-sm: 6px;
  --r: 10px;
  --r-lg: 14px;
  --shadow-sm: 0 1px 2px rgba(0,0,0,.06), 0 1px 3px rgba(0,0,0,.1);
  --shadow: 0 4px 12px rgba(0,0,0,.08), 0 2px 4px rgba(0,0,0,.06);
  --shadow-lg: 0 12px 32px rgba(0,0,0,.12), 0 4px 12px rgba(0,0,0,.08);
  --ease: cubic-bezier(.4, 0, .2, 1);
  --t: .18s var(--ease);
  --t-slow: .3s var(--ease);
}

/* ─── Theme: night ─── */
:root[data-theme=night] {
  --bg: #0a0e10;
  --bg-2: #11151a;
  --bg-3: #161c22;
  --bg-elev: #1a212a;
  --bg-hover: #1f2731;
  --border: #232b35;
  --border-strong: #2f3a47;
  --text: #f1f2ed;
  --text-2: #b9beb1;
  --text-3: #7c8479;
  --text-4: #4a5048;
  --accent: var(--gold);
  --link: #d4a04f;
  --backdrop: rgba(0,0,0,.7);
}
:root[data-theme=light] {
  --bg: #fafaf6;
  --bg-2: #ffffff;
  --bg-3: #f4f4ef;
  --bg-elev: #ffffff;
  --bg-hover: #ececdf;
  --border: #e3e3d8;
  --border-strong: #c5c5b8;
  --text: #1a1d18;
  --text-2: #4d5147;
  --text-3: #7d8175;
  --text-4: #b0b3a5;
  --accent: #8b6d0b;
  --link: #74590a;
  --backdrop: rgba(20,22,18,.5);
}

body {
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  -webkit-tap-highlight-color: transparent;
}
code, pre, .mono { font-family: var(--font-mono); }
a { color: var(--link); text-decoration: none; transition: color var(--t); }
a:hover { color: var(--accent); }
::selection { background: var(--gold); color: #000; }

/* ─── Topbar (mobile-first) ─── */
.topbar {
  position: sticky; top: 0; z-index: 100;
  background: var(--bg-2);
  border-bottom: 1px solid var(--border);
  height: 56px;
  padding: 0 1rem;
  display: flex;
  align-items: center;
  gap: 1rem;
  backdrop-filter: blur(10px);
}
.hamburger {
  background: transparent; border: 0; padding: .5rem; cursor: pointer;
  display: flex; flex-direction: column; gap: 4px;
  width: 36px; height: 36px;
  align-items: center; justify-content: center;
}
.hamburger span {
  width: 20px; height: 2px; background: var(--text); border-radius: 2px;
  transition: var(--t);
}
.brand-mini { display: flex; align-items: center; gap: .5rem; }
.brand-mini .brand-mark { width: 30px; height: 30px; font-size: 1rem; }
.brand-mini .brand-name { font-size: 1rem; }
.user-area { margin-left: auto; }
.user-pill {
  display: flex; align-items: center; gap: .6rem;
  padding: .35rem .6rem; border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg-3);
  transition: var(--t);
  color: var(--text);
}
.user-pill:hover { border-color: var(--gold); background: var(--bg-hover); color: var(--text); }
.user-avatar {
  width: 30px; height: 30px; border-radius: 50%;
  background: linear-gradient(135deg, var(--gold), var(--gold-dim));
  color: #0a0e10;
  display: grid; place-items: center;
  font-weight: 600; font-size: .75rem; letter-spacing: .02em;
}
.user-info { display: none; }

/* ─── App layout ─── */
.app { min-height: 100vh; display: flex; flex-direction: column; }
.sidebar {
  position: fixed; top: 56px; left: 0; bottom: 0;
  width: 260px;
  background: var(--bg-2);
  border-right: 1px solid var(--border);
  padding: 1.25rem 1rem 1rem;
  z-index: 50;
  transform: translateX(-100%);
  visibility: hidden;
  transition: transform var(--t-slow), visibility var(--t-slow);
  display: flex; flex-direction: column;
  overflow-y: auto;
}
.sidebar.open { transform: translateX(0); visibility: visible; box-shadow: var(--shadow-lg); }
.brand { display: none; }
.nav-item {
  display: flex; align-items: center; gap: .8rem;
  padding: .7rem .9rem;
  border-radius: var(--r);
  color: var(--text-2);
  cursor: pointer;
  transition: var(--t);
  font-size: .9rem;
  min-height: 44px;
  position: relative;
}
.nav-item:hover { background: var(--bg-hover); color: var(--text); }
.nav-item.active {
  background: var(--bg-3); color: var(--accent);
}
.nav-item.active::before {
  content: ''; position: absolute; left: 0; top: 25%; bottom: 25%;
  width: 3px; border-radius: 0 3px 3px 0; background: var(--gold);
}
.nav-icon { width: 20px; flex-shrink: 0; opacity: .85; font-size: 1rem; }
.nav-label { flex: 1; }
.nav-spacer { flex: 1; }
.nav-foot { padding-top: 1rem; border-top: 1px solid var(--border); margin-top: 1rem; }
.theme-toggle {
  background: transparent; border: 1px solid var(--border);
  color: var(--text-2);
  padding: .55rem .7rem; border-radius: var(--r);
  cursor: pointer; width: 100%;
  font-family: var(--font-sans); font-size: .8rem;
  transition: var(--t);
  display: flex; align-items: center; justify-content: center; gap: .4rem;
}
.theme-toggle:hover { border-color: var(--gold); color: var(--accent); }

.main {
  flex: 1;
  padding: 1.25rem 1rem 3rem;
  overflow-x: hidden;
}
.page-head {
  display: flex; align-items: baseline; justify-content: space-between;
  margin-bottom: 1.25rem;
  padding-bottom: .9rem;
  border-bottom: 1px solid var(--border);
  gap: 1rem;
}
.page-title {
  font-size: 1.15rem; letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--text);
  font-weight: 500;
}
.page-sub {
  color: var(--text-3); font-size: .75rem;
  font-family: var(--font-mono);
  letter-spacing: .02em;
}

/* ─── Brand mark ─── */
.brand-mark {
  width: 36px; height: 36px;
  border-radius: 50%;
  border: 1.5px solid var(--gold);
  display: grid; place-items: center;
  font-family: 'Brush Script MT', 'Lucida Handwriting', cursive;
  color: var(--gold);
  font-size: 1.2rem;
  background: radial-gradient(circle, rgba(31,74,44,.6), transparent);
  flex-shrink: 0;
}
.brand-name {
  font-size: 1.05rem; letter-spacing: .3em;
  color: var(--gold); font-weight: 300;
}

/* ─── Cards ─── */
.grid { display: grid; gap: 1rem; }
.grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
.card {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 1.1rem 1.2rem;
  transition: border-color var(--t);
}
.card:hover { border-color: var(--border-strong); }
.card h3 {
  color: var(--text-3);
  font-size: .68rem;
  font-weight: 600;
  letter-spacing: .15em;
  text-transform: uppercase;
  margin-bottom: .9rem;
}
.card-head {
  display: flex; align-items: center; justify-content: space-between;
  padding-bottom: .8rem;
  border-bottom: 1px solid var(--border);
  margin-bottom: 1rem;
  gap: 1rem;
  flex-wrap: wrap;
}
.card-head h3 { margin: 0; }

.metric {
  font-size: 1.8rem;
  color: var(--text);
  font-family: var(--font-mono);
  font-weight: 300;
  line-height: 1;
}
.metric .unit { font-size: .7rem; color: var(--text-3); margin-left: .35rem; font-weight: 400; }
.metric-sub { color: var(--text-3); font-size: .72rem; margin-top: .35rem; font-family: var(--font-mono); }

/* ─── Tables (mobile-friendly) ─── */
table { width: 100%; border-collapse: collapse; font-size: .85rem; }
.table-wrap, table { overflow-x: auto; -webkit-overflow-scrolling: touch; }
th {
  text-align: left; padding: .55rem .45rem;
  border-bottom: 1px solid var(--border-strong);
  color: var(--text-3); font-weight: 600;
  font-size: .65rem;
  text-transform: uppercase; letter-spacing: .12em;
  white-space: nowrap;
}
td {
  padding: .55rem .45rem;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
  font-family: var(--font-mono);
  color: var(--text-2);
  font-size: .8rem;
}
tr:hover td { background: var(--bg-hover); color: var(--text); }
td.name { color: var(--text); }

/* ─── Buttons & inputs ─── */
.btn {
  display: inline-flex; align-items: center; gap: .4rem;
  padding: .65rem 1.1rem;
  background: var(--gold);
  color: #0a0e10;
  border: 0; border-radius: var(--r);
  cursor: pointer; font-weight: 600;
  font-size: .85rem; letter-spacing: .02em;
  font-family: var(--font-sans);
  transition: var(--t);
  min-height: 40px;
  white-space: nowrap;
}
.btn:hover { background: var(--gold-bright); transform: translateY(-1px); box-shadow: var(--shadow); }
.btn:active { transform: translateY(0); }
.btn.ghost {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--border);
}
.btn.ghost:hover { border-color: var(--gold); background: var(--bg-hover); transform: none; box-shadow: none; }
.btn.sm { padding: .4rem .8rem; font-size: .78rem; min-height: 32px; }
.btn.danger { background: var(--err); color: white; }

input[type=text], input[type=email], input[type=password], input[type=search],
input[type=number], textarea, select {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: .65rem .85rem;
  border-radius: var(--r);
  font-family: var(--font-mono);
  font-size: .9rem;
  width: 100%;
  transition: var(--t);
  min-height: 44px;
}
input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--gold);
  box-shadow: 0 0 0 3px rgba(212, 160, 23, .15);
}
textarea { font-family: var(--font-mono); resize: vertical; min-height: 120px; line-height: 1.6; }
label {
  display: block;
  margin: 1rem 0 .35rem;
  font-size: .72rem;
  color: var(--text-3);
  text-transform: uppercase;
  letter-spacing: .12em;
  font-weight: 600;
}

/* ─── Badges ─── */
.badge {
  display: inline-flex; align-items: center;
  padding: .15rem .55rem;
  border-radius: 4px;
  font-size: .68rem; font-weight: 600;
  letter-spacing: .08em; text-transform: uppercase;
  font-family: var(--font-mono);
  white-space: nowrap;
}
.badge.ok { background: rgba(52,211,153,.15); color: var(--ok); }
.badge.warn { background: rgba(251,191,36,.15); color: var(--warn); }
.badge.err { background: rgba(248,113,113,.15); color: var(--err); }
.badge.info { background: rgba(96,165,250,.15); color: var(--info); }
.badge.pulse { animation: pulse 1.5s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .5; } }

/* ─── Login page ─── */
body.login { background: var(--bg); }
.login {
  min-height: 100vh; display: grid; place-items: center;
  background:
    radial-gradient(ellipse at top, rgba(31, 74, 44, .15), transparent 60%),
    radial-gradient(ellipse at bottom right, rgba(212, 160, 23, .08), transparent 60%),
    var(--bg);
  padding: 1.5rem;
}
.login-card {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 2.5rem 2rem 2rem;
  width: 100%;
  max-width: 380px;
  text-align: center;
  box-shadow: var(--shadow-lg);
}
.login-mark {
  width: 84px; height: 84px;
  border-radius: 50%;
  border: 2px solid var(--gold);
  display: grid; place-items: center;
  margin: 0 auto 1.25rem;
  font-family: 'Brush Script MT', 'Lucida Handwriting', cursive;
  color: var(--gold);
  font-size: 2.3rem;
  background:
    radial-gradient(circle at 30% 30%, rgba(212,160,23,.15), transparent 60%),
    radial-gradient(circle, rgba(31,74,44,.5), transparent 70%);
  box-shadow:
    inset 0 0 24px rgba(212, 160, 23, .1),
    0 0 0 4px rgba(212, 160, 23, .05);
}
.login-name {
  font-size: 1.6rem;
  letter-spacing: .35em;
  color: var(--gold);
  font-weight: 300;
  margin-bottom: .25rem;
}
.login-sub {
  color: var(--text-3);
  font-size: .72rem;
  letter-spacing: .15em;
  text-transform: uppercase;
  margin-bottom: 2rem;
  font-family: var(--font-mono);
}
.login form { display: flex; flex-direction: column; gap: .9rem; }
.login form input { text-align: center; }
.login .err { font-size: .8rem; }

/* ─── Markdown content ─── */
.md { line-height: 1.7; color: var(--text); }
.md h1, .md h2, .md h3 { color: var(--accent); margin: 1.4rem 0 .7rem; letter-spacing: .02em; font-weight: 500; }
.md h1 { font-size: 1.4rem; }
.md h2 { font-size: 1.15rem; border-bottom: 1px solid var(--border); padding-bottom: .3rem; }
.md h3 { font-size: .95rem; color: var(--text); }
.md p { margin: .6rem 0; }
.md ul, .md ol { padding-left: 1.25rem; margin: .5rem 0; }
.md li { margin: .25rem 0; }
.md code { background: var(--bg-3); padding: .12rem .35rem; border-radius: 3px; font-size: .85em; color: var(--accent); }
.md pre { background: var(--bg-3); padding: 1rem; border-radius: var(--r); overflow-x: auto; margin: .7rem 0; }
.md pre code { background: transparent; padding: 0; color: var(--text); }
.md blockquote { border-left: 3px solid var(--gold); padding-left: 1rem; color: var(--text-2); margin: .7rem 0; font-style: italic; }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 1.3rem 0; }

/* ─── Memory page ─── */
.memory-layout { display: grid; grid-template-columns: 1fr; gap: 1rem; min-height: 70vh; }
.mem-list-card { padding: 0; overflow: hidden; }
.mem-list-card > .card-head { padding: 1rem 1.2rem; border-bottom: 1px solid var(--border); margin: 0; }
.mem-list-card #mem-list { padding: .4rem; max-height: 50vh; overflow-y: auto; }
.mem-item {
  display: flex; align-items: center; gap: .6rem;
  padding: .55rem .7rem;
  border-radius: var(--r-sm);
  cursor: pointer;
  color: var(--text-2);
  transition: var(--t);
  min-height: 44px;
}
.mem-item:hover { background: var(--bg-hover); color: var(--text); }
.mem-item.active { background: var(--bg-3); color: var(--accent); }
.mem-icon { font-size: .9rem; opacity: .6; }
.mem-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .85rem; font-family: var(--font-mono); }
.mem-size { font-size: .7rem; color: var(--text-3); font-family: var(--font-mono); }

.mem-view-card { min-height: 60vh; }

/* ─── Conversations ─── */
.chat-row {
  display: flex; align-items: center; gap: 1rem;
  padding: .9rem 1rem;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: var(--t);
  border-radius: var(--r-sm);
}
.chat-row:hover { background: var(--bg-hover); }
.chat-meta { flex: 1; min-width: 0; }
.chat-handle { color: var(--text); font-weight: 500; font-size: .9rem; }
.chat-preview { margin-top: .15rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-toggle { color: var(--text-3); font-size: 1rem; }
.chat-thread { padding: 0 1rem 1rem; background: var(--bg-3); border-radius: 0 0 var(--r) var(--r); margin-bottom: .8rem; }
.msg { padding: .7rem 0; border-bottom: 1px solid var(--border); }
.msg:last-child { border-bottom: 0; }
.msg-meta { margin-bottom: .25rem; }
.msg-body { font-size: .9rem; color: var(--text); line-height: 1.6; white-space: pre-wrap; word-break: break-word; }
.msg-in .msg-body { color: var(--text); }
.msg-out .msg-body { color: var(--text-2); padding-left: .6rem; border-left: 2px solid var(--gold); }

/* ─── Modal ─── */
#preview-modal {
  position: fixed; inset: 0;
  background: var(--backdrop);
  z-index: 200;
  padding: 1rem;
  overflow-y: auto;
  align-items: center; justify-content: center;
  backdrop-filter: blur(4px);
}
.modal-body {
  background: var(--bg-2); border: 1px solid var(--border);
  border-radius: var(--r-lg);
  max-width: 900px; width: 100%;
  padding: 1.25rem 1.5rem 1.5rem;
  margin: auto;
  max-height: 90vh; overflow-y: auto;
  box-shadow: var(--shadow-lg);
}

/* ─── Utility ─── */
.row { display: flex; gap: .8rem; align-items: center; }
.col { display: flex; flex-direction: column; gap: .8rem; }
.gap-sm { gap: .4rem; }
.muted { color: var(--text-3); }
.small { font-size: .8rem; }
.mt-2 { margin-top: 1rem; }
.mb-2 { margin-bottom: 1rem; }
.empty {
  padding: 3rem 1rem; text-align: center;
  color: var(--text-3); font-family: var(--font-mono);
  font-size: .85rem;
  border: 1px dashed var(--border);
  border-radius: var(--r);
  background: var(--bg-3);
}
.flex { display: flex; }
.flex-1 { flex: 1; min-width: 0; }
.desktop-only { display: none; }
.tag {
  display: inline-block;
  background: var(--bg-3); color: var(--text-2);
  padding: .1rem .45rem;
  border-radius: 3px;
  font-size: .68rem;
  margin-right: .2rem;
  font-family: var(--font-mono);
  border: 1px solid var(--border);
}

/* ─── Scrollbars ─── */
* { scrollbar-width: thin; scrollbar-color: var(--border-strong) transparent; }
*::-webkit-scrollbar { width: 8px; height: 8px; }
*::-webkit-scrollbar-track { background: transparent; }
*::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 4px; }
*::-webkit-scrollbar-thumb:hover { background: var(--text-3); }

/* ─── Focus ─── */
*:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; border-radius: var(--r-sm); }

/* ═══════════════════════════════════════════════════════════ */
/* ─── Tablet (>= 640px) ─── */
@media (min-width: 640px) {
  .main { padding: 1.75rem 2rem 3rem; }
  .grid-2 { grid-template-columns: repeat(2, 1fr); }
  .page-title { font-size: 1.3rem; }
  .metric { font-size: 2.1rem; }
  .user-info { display: block; line-height: 1.2; }
  .user-name { font-size: .85rem; font-weight: 500; color: var(--text); }
  .user-role { font-size: .68rem; color: var(--text-3); text-transform: uppercase; letter-spacing: .08em; }
}

/* ─── Desktop (>= 1024px) ─── */
@media (min-width: 900px) {
  .topbar { display: none; }
  .app { display: grid; grid-template-columns: 260px 1fr; min-height: 100vh; }
  .sidebar {
    position: relative; top: 0; transform: none; height: auto;
    visibility: visible;
    padding: 1.5rem 1rem 1rem;
  }
  .brand { display: flex; align-items: center; gap: .65rem; padding: 0 .5rem 1.5rem; border-bottom: 1px solid var(--border); margin-bottom: 1rem; }
  .main { padding: 2rem 2.5rem 3rem; }
  .grid-3 { grid-template-columns: repeat(3, 1fr); }
  .grid-4 { grid-template-columns: repeat(4, 1fr); }
  .memory-layout { grid-template-columns: 300px 1fr; }
  .mem-list-card #mem-list { max-height: 65vh; }
  .desktop-only { display: flex; }
  .hamburger, .brand-mini { display: none; }
  .user-area {
    position: fixed; top: 1rem; right: 2rem;
    z-index: 60;
  }
}

/* ─── Print friendly ─── */
@media print {
  .sidebar, .topbar, .user-area, .btn { display: none !important; }
  .main { padding: 0; }
  .card { border: 1px solid #ccc; box-shadow: none; }
}

/* ─── Chips & table (comercial) ─── */
.chips { display: flex; flex-wrap: wrap; gap: .4rem; }
.chip {
  display: inline-flex; align-items: center; gap: .35rem;
  padding: .35rem .7rem; border-radius: 999px;
  background: var(--bg-3); color: var(--text-2);
  border: 1px solid var(--border);
  font-size: .82rem; text-decoration: none;
  transition: background .15s, color .15s, border-color .15s;
}
.chip:hover { background: var(--bg-hover); color: var(--text); }
.chip.active { background: var(--accent); color: var(--bg); border-color: var(--accent); }
.chip b { font-weight: 600; opacity: .85; }

table.t { width: 100%; border-collapse: collapse; font-size: .88rem; }
table.t th, table.t td { padding: .55rem .6rem; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
table.t th { font-weight: 600; font-size: .75rem; opacity: .7; text-transform: uppercase; letter-spacing: .03em; }
table.t tbody tr { cursor: pointer; transition: background .12s; }
table.t tbody tr:hover { background: var(--bg-3); }
