/* ── App Grid ── */
#app {
  display: grid;
  grid-template-columns: var(--nav-w) 1fr;
  grid-template-rows: 100vh;
  height: 100vh;
  opacity: 0;
  transition: opacity 0.3s;
}
#app.ready { opacity: 1; }

/* ── Sidebar ── */
#sidebar {
  grid-column: 1;
  grid-row: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg2);
  border-right: 1px solid var(--border);
  overflow: hidden;
  height: 100vh;
}

.sidebar-logo {
  height: var(--header-h);
  display: flex; align-items: center; gap: 10px;
  padding: 0 20px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.logo-mark {
  width: 30px; height: 30px;
  background: linear-gradient(135deg, var(--accent), #0055cc);
  border-radius: 7px;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 800; color: #fff;
  box-shadow: 0 0 16px var(--accent-glow);
  flex-shrink: 0;
}
.logo-text { font-size: 17px; font-weight: 800; letter-spacing: -0.5px; }
.logo-text span { color: var(--accent); }

.nav-section { padding: 16px 12px 8px; flex-shrink: 0; }
.nav-label { font-size: 10px; font-weight: 700; letter-spacing: 1.2px; text-transform: uppercase; color: var(--text3); padding: 0 8px; margin-bottom: 4px; }

.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 12px; border-radius: 8px;
  font-size: 13px; font-weight: 600;
  color: var(--text2); cursor: pointer;
  transition: background 0.15s, color 0.15s;
  user-select: none;
}
.nav-item:hover { background: var(--bg3); color: var(--text); }
.nav-item.active { background: var(--accent-dim); color: var(--accent); }
.nav-item svg { width: 15px; height: 15px; flex-shrink: 0; opacity: 0.7; }
.nav-item.active svg { opacity: 1; }

.sidebar-footer {
  margin-top: auto;
  border-top: 1px solid var(--border);
  padding: 12px;
  flex-shrink: 0;
}
.user-badge {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; border-radius: 8px;
  cursor: pointer; transition: background 0.15s;
}
.user-badge:hover { background: var(--bg3); }
.user-avatar {
  width: 28px; height: 28px; border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), #0055cc);
  display: flex; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 700; color: #fff; flex-shrink: 0;
}
.user-info { flex: 1; min-width: 0; }
.user-name { font-size: 12px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-role { font-size: 10px; color: var(--text3); font-family: 'DM Mono', monospace; }
.logout-btn {
  background: none; border: none; cursor: pointer;
  color: var(--text3); padding: 4px; border-radius: 4px;
  transition: color 0.15s;
}
.logout-btn:hover { color: var(--red); }

/* ── Main area ── */
#main {
  grid-column: 2;
  grid-row: 1;
  display: grid;
  grid-template-rows: var(--header-h) 1fr;
  height: 100vh;
  overflow: hidden;
}

/* ── Top bar ── */
#topbar {
  grid-row: 1;
  height: var(--header-h);
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 16px;
  padding: 0 24px;
}
.page-title { font-size: 15px; font-weight: 700; flex: 1; }
.topbar-search {
  display: flex; align-items: center; gap: 8px;
  background: var(--bg3); border: 1px solid var(--border);
  border-radius: 8px; padding: 0 12px; height: 34px;
  width: 220px;
}
.topbar-search input {
  background: none; border: none; outline: none;
  font-family: 'DM Mono', monospace; font-size: 13px;
  color: var(--text); width: 100%;
}
.topbar-search input::placeholder { color: var(--text3); }
.topbar-search svg { color: var(--text3); flex-shrink: 0; }
.market-status {
  display: flex; align-items: center; gap: 6px;
  font-family: 'DM Mono', monospace; font-size: 11px; color: var(--text2);
}
.status-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--green); animation: pulse 2s ease-in-out infinite; }
@keyframes pulse { 0%,100%{opacity:1} 50%{opacity:0.4} }

#log-toggle-btn {
  margin-left: 8px; background: transparent; border: 1px solid var(--border);
  color: var(--text2); padding: 2px 8px; border-radius: 4px; cursor: pointer;
  font-size: 11px; display: flex; align-items: center; gap: 4px;
}
#log-badge {
  background: #3b82f6; color: #fff; border-radius: 3px;
  padding: 0 4px; font-size: 10px; min-width: 16px; text-align: center;
}

/* ── Content ── */
#content {
  grid-row: 2;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 24px;
  padding-bottom: 48px;
  -webkit-overflow-scrolling: touch;
}
#content::-webkit-scrollbar { width: 6px; }
#content::-webkit-scrollbar-track { background: transparent; }
#content::-webkit-scrollbar-thumb { background: var(--border2); border-radius: 3px; }

/* ── Log Panel ── */
#app-log-panel {
  position: fixed; bottom: 0; left: var(--nav-w); right: 0; height: 260px;
  background: color-mix(in srgb, var(--card) 94%, white 6%); border-top: 2px solid var(--accent);
  box-shadow: 0 -10px 30px rgba(15, 23, 42, 0.16);
  flex-direction: column; z-index: 100;
}

.log-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 12px; border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg3) 92%, white 8%);
  color: var(--text);
  font-size: 12px; font-weight: 700;
}

.log-actions { display: flex; gap: 4px; }
.log-actions button {
  background: var(--bg3); border: 1px solid var(--border); color: var(--text2);
  padding: 2px 8px; border-radius: 4px; cursor: pointer; font-size: 10px;
}
.log-actions button:hover { color: var(--text); }
#app-log-body {
  flex: 1; overflow-y: auto; padding: 4px 0;
  color: var(--text);
  background: transparent;
  font-family: 'DM Mono', monospace; font-size: 11px;
}

.sidebar-toggle-btn,.topbar-nav-toggle,.nav-group-toggle{background:none;border:1px solid var(--border);color:var(--text2);border-radius:8px;cursor:pointer;transition:all .15s ease;font-family:'DM Mono',monospace;}
.sidebar-toggle-btn,.topbar-nav-toggle{width:32px;height:32px;display:inline-flex;align-items:center;justify-content:center;flex-shrink:0;}
.sidebar-toggle-btn:hover,.topbar-nav-toggle:hover,.nav-group-toggle:hover{color:var(--text);border-color:var(--accent);}
.topbar-nav-toggle{margin-right:4px;}
.nav-section-header{display:flex;align-items:center;justify-content:space-between;gap:8px;}
.nav-group-toggle{padding:1px 7px;font-size:11px;line-height:1.4;}
.nav-section-items{display:flex;flex-direction:column;gap:2px;}
.nav-group-collapsed .nav-section-items{display:none;}
#app.sidebar-collapsed{grid-template-columns:72px 1fr;}
#app.sidebar-collapsed .logo-text,#app.sidebar-collapsed .nav-item-text,#app.sidebar-collapsed .nav-label-text,#app.sidebar-collapsed .user-info,#app.sidebar-collapsed .nav-group-toggle{display:none;}
#app.sidebar-collapsed .sidebar-logo{padding:0 14px;justify-content:center;gap:8px;}
#app.sidebar-collapsed .nav-section{padding:14px 10px 6px;}
#app.sidebar-collapsed .nav-label{justify-content:center;padding:0;}
#app.sidebar-collapsed .nav-item{justify-content:center;padding:10px 0;}
#app.sidebar-collapsed .sidebar-footer{padding:10px;}
#app.sidebar-collapsed .user-badge{justify-content:center;padding:8px 4px;}
#app.sidebar-collapsed .logout-btn{display:none;}
#app.sidebar-collapsed #app-log-panel{left:72px;}

body.nav-open { overflow: hidden; }
#sidebarBackdrop {
  position: fixed;
  inset: 0;
  background: rgba(2, 6, 12, 0.72);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 110;
}
#sidebarBackdrop.is-visible {
  opacity: 1;
  pointer-events: auto;
}

@media (max-width: 1200px) {
  #topbar { padding: 0 18px; }
  .topbar-search { width: 180px; }
  #content { padding: 18px; padding-bottom: 36px; }
}

@media (max-width: 1024px), (orientation: portrait) and (max-width: 1366px) {
  :root {
    --header-h: 54px;
    --nav-w: 260px;
  }

  html, body {
    height: 100%;
    overflow: hidden;
  }

  body.nav-open { overflow: hidden; }
  #sidebarBackdrop,
  #topbarSidebarToggle {
    display: none !important;
  }

  #app {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(56px, auto) 1fr;
    width: 100%;
    height: 100vh;
    overflow: hidden;
  }

  #sidebar {
    grid-column: 1;
    grid-row: 1;
    position: relative;
    inset: auto;
    width: 100%;
    height: auto;
    min-height: 56px;
    z-index: 20;
    transform: none;
    transition: none;
    box-shadow: none;
    border-right: none;
    border-bottom: 1px solid var(--border);
    flex-direction: row;
    align-items: stretch;
    gap: 6px;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 6px 10px 6px;
    white-space: nowrap;
    scrollbar-width: thin;
  }

  #sidebar::-webkit-scrollbar {
    height: 6px;
  }

  #sidebar::-webkit-scrollbar-thumb {
    background: rgba(255,255,255,0.16);
    border-radius: 999px;
  }

  .sidebar-logo {
    height: auto;
    min-height: 38px;
    padding: 0 10px 0 2px;
    border-bottom: none;
    border-right: 1px solid var(--border);
    flex: 0 0 auto;
    align-self: stretch;
  }

  .mobile-nav-current {
    display: none;
    align-items: center;
    min-height: 38px;
    padding: 0 14px;
    border: 1px solid var(--border);
    border-radius: 999px;
    color: var(--text2);
    font-size: 13px;
    font-weight: 700;
    white-space: nowrap;
    background: var(--bg3);
  }

  #sidebarCollapseBtn {
    display: inline-flex !important;
  }

  .sidebar-footer {
    display: none;
  }

  .nav-section {
    padding: 0;
    display: flex;
    flex: 0 0 auto;
    align-items: stretch;
    gap: 6px;
    min-height: 38px;
  }

  .nav-label,
  .nav-group-toggle {
    display: none !important;
  }

  .nav-section-items {
    display: flex !important;
    flex-direction: row;
    align-items: stretch;
    gap: 6px;
  }

  .nav-group-collapsed .nav-section-items {
    display: flex !important;
  }

  .nav-item {
    flex: 0 0 auto;
    min-width: clamp(210px, 46vw, 280px);
    height: 58px;
    min-height: 38px;
    justify-content: center;
    padding: 0 18px;
    border: 1px solid var(--border);
    background: var(--bg2);
    white-space: nowrap;
    text-align: center;
  }

  .nav-item.active {
    border-color: var(--accent);
    box-shadow: inset 0 0 0 1px rgba(60, 179, 113, 0.12);
  }

  #app.mobile-nav-minimized {
    grid-template-rows: minmax(50px, auto) 1fr;
  }

  #app.mobile-nav-minimized #sidebar {
    min-height: 50px;
    padding: 5px 8px;
    gap: 0;
    justify-content: flex-start;
  }

  #app.mobile-nav-minimized .sidebar-logo {
    min-height: 40px;
    padding-right: 0;
    border-right: none;
    gap: 8px;
  }

  #app.mobile-nav-minimized .logo-text,
  #app.mobile-nav-minimized .nav-section {
    display: none;
  }

  #app.mobile-nav-minimized .mobile-nav-current {
    display: inline-flex;
  }

  #main {
    grid-column: 1;
    grid-row: 2;
    width: 100%;
    min-width: 0;
    min-height: 0;
    overflow: hidden;
  }

  #topbar {
    flex-wrap: wrap;
    align-items: center;
    gap: 10px;
    padding: 10px 14px;
    height: auto;
    min-height: var(--header-h);
  }

  .page-title {
    order: 1;
    flex: 1 1 auto;
    min-width: 0;
  }

  .topbar-search {
    order: 3;
    width: 100%;
    flex: 1 1 100%;
  }

  .market-status {
    order: 2;
    margin-left: auto;
  }

  #log-toggle-btn {
    order: 2;
    margin-left: 0;
  }

  #content {
    padding: 14px;
    padding-bottom: 28px;
  }

  #app-log-panel {
    left: 0 !important;
    right: 0;
    width: 100%;
    height: min(42vh, 320px);
  }
}

@media (max-width: 640px) {
  :root { --header-h: 52px; }

  #app {
    grid-template-rows: minmax(50px, auto) 1fr;
  }

  #sidebar {
    min-height: 50px;
    gap: 6px;
    padding: 5px 8px 5px;
  }

  .sidebar-logo {
    min-height: 35px;
    padding-right: 6px;
  }

  .mobile-nav-current {
    min-height: 35px;
    padding: 0 12px;
    font-size: 12px;
  }

  .nav-section {
    min-height: 35px;
  }

  .logo-text { font-size: 15px; }
  .nav-item {
    min-width: clamp(190px, 58vw, 250px);
    height: 35px;
    min-height: 35px;
    padding: 0 12px;
    font-size: 12px;
    gap: 6px;
  }
  .page-title { font-size: 14px; }
  .topbar-search { height: 36px; }
  .market-status { font-size: 10px; }
  #log-toggle-btn { font-size: 10px; padding: 2px 6px; }
  #content { padding: 12px; padding-bottom: 24px; }
}

html,
body {
  width: 100%;
  min-width: 0;
}

#app,
#main,
#content {
  min-height: 0;
}

#app {
  height: 100dvh;
  min-height: 100dvh;
  grid-template-rows: minmax(0, 100dvh);
}

#main {
  min-width: 0;
}

#content {
  height: 100%;
  max-height: 100%;
  overflow-y: auto !important;
  overflow-x: hidden;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
}

#content > * {
  max-width: 100%;
}

#sidebar {
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
}

.nav-section {
  flex-shrink: 0;
}

.nav-importance {
  margin-left: auto;
  color: var(--yellow);
  font-family: 'DM Mono', monospace;
  font-size: 9px;
  letter-spacing: -1px;
  opacity: .9;
  white-space: nowrap;
}

.nav-item-text + .nav-importance {
  margin-left: auto;
}

#app.sidebar-collapsed .nav-importance {
  display: none;
}

@media (max-width: 1024px), (orientation: portrait) and (max-width: 1366px) {
  #app {
    height: 100dvh;
    min-height: 100dvh;
  }

  #main {
    min-height: 0;
  }

  #content {
    min-height: 0;
    height: 100%;
    max-height: 100%;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
  }

  #sidebar {
    overflow-x: auto;
    overflow-y: hidden;
  }

  .nav-importance {
    margin-left: 4px;
  }
}

