:root {
  --bg: #0f0f23;
  --bg2: #16162a;
  --bg3: #1e1e3a;
  --accent: #0ea5e9;
  --accent2: #10b981;
  --danger: #ef4444;
  --warning: #f59e0b;
  --safe: #22c55e;
  --text: #e2e8f0;
  --text-muted: #64748b;
  --border: rgba(255,255,255,0.08);
  --radius: 12px;
  --bar-h: 52px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; overflow: hidden; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

/* ═══════ Top Bar ═══════ */
#top-bar {
  flex-shrink: 0;
  padding: var(--safe-top) 8px 0;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  z-index: 100;
}
#url-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 0;
  height: var(--bar-h);
}
#menu-toggle, #go-btn {
  width: 36px; height: 36px;
  background: none; border: none; color: var(--text-muted);
  display: flex; align-items: center; justify-content: center;
  border-radius: 8px; cursor: pointer; flex-shrink: 0;
}
#menu-toggle:active, #go-btn:active { background: var(--bg3); }
#url-input-wrap {
  flex: 1; display: flex; align-items: center;
  background: var(--bg); border: 1px solid var(--border);
  border-radius: 10px; padding: 0 10px; height: 36px; gap: 6px;
}
#secure-icon { color: var(--safe); display: flex; flex-shrink: 0; }
#secure-icon.insecure { color: var(--danger); }
#secure-icon.none { color: var(--text-muted); }
#url-input {
  flex: 1; background: none; border: none; color: var(--text);
  font-size: 14px; outline: none; min-width: 0;
  font-family: inherit; -webkit-appearance: none; appearance: none;
  border-radius: 0;
}
#url-input::placeholder { color: var(--text-muted); }

/* ═══════ Browser Frame ═══════ */
#browser-frame {
  flex: 1; position: relative; overflow: hidden;
  background: var(--bg);
}
#web-view {
  width: 100%; height: 100%; border: none;
  background: #fff;
  display: none;
}
#web-view.active { display: block; }

/* Loading Bar */
#loading-bar {
  position: absolute; top: 0; left: 0;
  height: 3px; background: linear-gradient(90deg, var(--accent), var(--accent2));
  z-index: 10; animation: loadProgress 2s ease-in-out infinite;
  width: 0;
}
#loading-bar.active { width: 100%; }
#loading-bar.hidden { display: none; }
@keyframes loadProgress {
  0% { width: 0; }
  50% { width: 70%; }
  100% { width: 100%; }
}

/* ═══════ Home Screen ═══════ */
#home-screen {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  padding: 24px; gap: 28px; z-index: 5;
  background: var(--bg);
}
#home-screen.hidden { display: none; }

.home-logo { text-align: center; }
.home-logo h1 {
  font-size: 24px; font-weight: 800; margin-top: 12px;
  background: linear-gradient(135deg, #e2e8f0, var(--accent));
  -webkit-background-clip: text; -webkit-text-fill-color: transparent;
  background-clip: text;
}
.home-logo p { font-size: 13px; color: var(--text-muted); margin-top: 6px; }

#quick-links {
  display: grid; grid-template-columns: repeat(4, 1fr);
  gap: 12px; width: 100%; max-width: 320px;
}
.quick-link {
  display: flex; flex-direction: column; align-items: center;
  gap: 6px; padding: 14px 8px;
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: var(--radius); cursor: pointer;
  text-decoration: none; color: var(--text); font-size: 24px;
  transition: background 0.2s;
}
.quick-link:active { background: var(--bg3); }
.quick-link span { font-size: 11px; color: var(--text-muted); }

#home-features {
  display: flex; flex-wrap: wrap; gap: 10px;
  justify-content: center; width: 100%; max-width: 340px;
}
.home-feat {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 14px; background: var(--bg2);
  border: 1px solid var(--border); border-radius: 20px;
  font-size: 13px;
}
.feat-icon { font-size: 16px; }
.feat-label { color: var(--text-muted); }
.feat-badge {
  background: var(--safe); color: #000; font-size: 11px;
  font-weight: 700; padding: 1px 7px; border-radius: 10px;
}
.feat-status.safe { color: var(--safe); font-weight: 600; }
.feat-status.warn { color: var(--warning); font-weight: 600; }
.feat-status.danger { color: var(--danger); font-weight: 600; }

/* ═══════ Bottom Bar ═══════ */
#bottom-bar {
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: space-around;
  height: var(--bar-h);
  padding-bottom: var(--safe-bottom);
  background: var(--bg2);
  border-top: 1px solid var(--border);
  z-index: 100;
}
#bottom-bar button {
  position: relative; width: 42px; height: 42px;
  background: none; border: none; color: var(--text-muted);
  display: flex; align-items: center; justify-content: center;
  border-radius: 10px; cursor: pointer;
}
#bottom-bar button:active { background: var(--bg3); }
#bottom-bar button:disabled { opacity: 0.3; }
#bottom-bar button.active { color: var(--accent); }
.toolbar-badge {
  position: absolute; top: 2px; right: 2px;
  min-width: 14px; height: 14px; padding: 0 3px;
  background: var(--safe); color: #000;
  font-size: 8px; font-weight: 700; border-radius: 7px;
  display: flex; align-items: center; justify-content: center;
  line-height: 1;
}
.toolbar-badge.hidden { display: none; }

/* ═══════ Side Menu ═══════ */
#side-menu.hidden { display: none; }
#side-menu { position: fixed; inset: 0; z-index: 500; }
#menu-overlay {
  position: absolute; inset: 0;
  background: rgba(0,0,0,0.5);
  backdrop-filter: blur(2px);
}
#menu-panel {
  position: absolute; top: 0; right: 0;
  width: 280px; max-width: 80vw; height: 100%;
  background: var(--bg2); padding-top: var(--safe-top);
  overflow-y: auto;
  animation: slideIn 0.25s ease-out;
}
@keyframes slideIn { from { transform: translateX(100%); } to { transform: translateX(0); } }
.menu-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 16px 12px; border-bottom: 1px solid var(--border);
}
.menu-header h2 { font-size: 18px; font-weight: 700; }
.menu-header button {
  width: 32px; height: 32px; background: none; border: none;
  color: var(--text-muted); font-size: 18px; cursor: pointer;
  border-radius: 8px;
}
.menu-items { padding: 8px 0; }
.menu-item {
  display: flex; align-items: center; gap: 12px;
  width: 100%; padding: 12px 16px; background: none; border: none;
  color: var(--text); font-size: 15px; cursor: pointer;
  font-family: inherit; text-align: left;
}
.menu-item:active { background: var(--bg3); }
.menu-item span:first-child { font-size: 18px; }
.menu-item small { margin-left: auto; font-size: 11px; padding: 2px 8px; border-radius: 6px; }
.menu-item small.on { background: rgba(34,197,94,0.15); color: var(--safe); }
.menu-item small.off { background: rgba(239,68,68,0.15); color: var(--danger); }
.menu-divider { height: 1px; background: var(--border); margin: 4px 16px; }
.menu-about { padding: 16px; text-align: center; color: var(--text-muted); }
.menu-about small { font-size: 12px; line-height: 1.6; }

/* ═══════ Chat Panel ═══════ */
#chat-panel {
  position: fixed; bottom: 0; left: 0; right: 0;
  height: 60vh; max-height: 420px;
  background: var(--bg2); border-top: 1px solid var(--border);
  border-radius: var(--radius) var(--radius) 0 0;
  display: flex; flex-direction: column;
  z-index: 400; animation: slideUp 0.25s ease-out;
}
#chat-panel.hidden { display: none; }
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
#chat-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
  font-weight: 600; font-size: 15px;
}
#chat-header button {
  width: 28px; height: 28px; background: none; border: none;
  color: var(--text-muted); font-size: 16px; cursor: pointer;
}
#chat-messages {
  flex: 1; overflow-y: auto; padding: 12px 16px;
  display: flex; flex-direction: column; gap: 8px;
}
.chat-msg {
  max-width: 85%; padding: 10px 14px;
  border-radius: var(--radius); font-size: 14px;
  line-height: 1.5; word-break: break-word;
}
.chat-msg.agent {
  align-self: flex-start;
  background: var(--bg3); color: var(--text);
  border-bottom-left-radius: 4px;
}
.chat-msg.user {
  align-self: flex-end;
  background: var(--accent); color: #fff;
  border-bottom-right-radius: 4px;
}
#chat-input-area {
  display: flex; gap: 8px; padding: 10px 12px;
  border-top: 1px solid var(--border);
  padding-bottom: calc(10px + var(--safe-bottom));
}
#chat-input {
  flex: 1; background: var(--bg); border: 1px solid var(--border);
  border-radius: 10px; padding: 8px 12px;
  color: var(--text); font-size: 14px; outline: none;
  font-family: inherit;
}
#chat-send {
  width: 38px; height: 38px;
  background: var(--accent); color: #fff; border: none;
  border-radius: 10px; font-size: 16px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
#chat-send:active { opacity: 0.8; }

/* ═══════ Toast ═══════ */
#toast-container {
  position: fixed; top: calc(var(--safe-top) + 56px);
  left: 10px; right: 10px; z-index: 900;
  display: flex; flex-direction: column; gap: 6px;
  pointer-events: none;
}
.toast {
  padding: 10px 14px; border-radius: 10px;
  font-size: 13px; font-weight: 500;
  animation: toastIn 0.3s ease-out;
  pointer-events: auto;
}
.toast.info { background: rgba(14,165,233,0.9); color: #fff; }
.toast.warning { background: rgba(245,158,11,0.9); color: #000; }
.toast.danger { background: rgba(239,68,68,0.9); color: #fff; }
.toast.success { background: rgba(34,197,94,0.9); color: #000; }
@keyframes toastIn { from { transform: translateY(-20px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

/* ═══════ Bookmarks & History Panel ═══════ */
.list-panel {
  position: fixed; inset: 0; z-index: 400;
  background: var(--bg); padding-top: var(--safe-top);
  display: flex; flex-direction: column;
  animation: slideUp 0.25s ease-out;
}
.list-panel.hidden { display: none; }
.list-panel-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
  font-weight: 700; font-size: 16px;
}
.list-panel-header button {
  width: 32px; height: 32px; background: none; border: none;
  color: var(--text-muted); font-size: 18px; cursor: pointer;
}
.list-panel-body {
  flex: 1; overflow-y: auto; padding: 8px 0;
}
.list-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px; cursor: pointer;
  border-bottom: 1px solid var(--border);
}
.list-item:active { background: var(--bg2); }
.list-item-text { flex: 1; min-width: 0; }
.list-item-title {
  font-size: 14px; font-weight: 500;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.list-item-url {
  font-size: 11px; color: var(--text-muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.list-item-del {
  width: 28px; height: 28px; background: none; border: none;
  color: var(--danger); font-size: 14px; cursor: pointer; flex-shrink: 0;
}
.list-empty {
  text-align: center; padding: 48px 16px;
  color: var(--text-muted); font-size: 14px;
}

/* ═══════ Search Results ═══════ */
#search-results {
  position: absolute; inset: 0;
  background: var(--bg);
  display: flex; flex-direction: column;
  z-index: 6; overflow: hidden;
}
#search-results.hidden { display: none; }
#search-results-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 14px; font-weight: 600; color: var(--accent);
}
#search-results-header button {
  width: 32px; height: 32px; background: none; border: none;
  color: var(--text-muted); font-size: 16px; cursor: pointer;
  border-radius: 8px;
}
#search-results-list {
  flex: 1; overflow-y: auto; padding: 8px 0;
  -webkit-overflow-scrolling: touch;
}
.sr-item {
  display: block; padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer; text-decoration: none; color: inherit;
}
.sr-item:active { background: var(--bg2); }
.sr-title {
  font-size: 15px; font-weight: 600;
  color: var(--accent); margin-bottom: 4px;
  display: -webkit-box; -webkit-line-clamp: 2;
  -webkit-box-orient: vertical; overflow: hidden;
}
.sr-url {
  font-size: 12px; color: var(--accent2);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  margin-bottom: 4px;
}
.sr-snippet {
  font-size: 13px; color: var(--text-muted);
  line-height: 1.5;
  display: -webkit-box; -webkit-line-clamp: 3;
  -webkit-box-orient: vertical; overflow: hidden;
}
.sr-powered {
  text-align: center; padding: 16px;
  font-size: 11px; color: var(--text-muted);
}
.sr-powered a { color: var(--accent); text-decoration: none; }
.sr-retry {
  display: inline-block; text-align: center; padding: 12px 24px;
  border-radius: 10px; background: var(--accent); color: #fff;
  font-size: 14px; font-weight: 600;
  border: none; cursor: pointer; font-family: inherit;
}
.sr-retry:active { opacity: 0.8; }

/* ═══════ Suggestions Dropdown ═══════ */
#suggestions-dropdown {
  position: absolute; top: calc(var(--safe-top) + var(--bar-h));
  left: 8px; right: 8px;
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: 0 0 12px 12px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
  z-index: 150; max-height: 280px; overflow-y: auto;
}
#suggestions-dropdown.hidden { display: none; }
.suggest-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 14px; cursor: pointer;
  border-bottom: 1px solid var(--border);
  font-size: 14px; color: var(--text);
}
.suggest-item:last-child { border-bottom: none; }
.suggest-item:active { background: var(--bg3); }
.suggest-icon { color: var(--text-muted); font-size: 14px; flex-shrink: 0; }
.suggest-text { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ═══════ Trending Section ═══════ */
#trending-section {
  width: 100%; max-width: 340px;
  margin-top: 4px;
}
.trending-title {
  font-size: 12px; color: var(--text-muted);
  font-weight: 600; margin-bottom: 8px;
  text-align: center;
}
#trending-list {
  display: flex; flex-wrap: wrap; gap: 6px;
  justify-content: center;
}
.trending-tag {
  padding: 5px 12px; background: var(--bg2);
  border: 1px solid var(--border); border-radius: 16px;
  font-size: 12px; color: var(--accent);
  cursor: pointer; transition: background 0.2s;
}
.trending-tag:active { background: var(--bg3); }

/* ═══════ Search Result Enhancements ═══════ */
.sr-result-count {
  padding: 8px 16px; font-size: 11px;
  color: var(--text-muted); border-bottom: 1px solid var(--border);
}
.sr-item .sr-favicon {
  width: 16px; height: 16px; border-radius: 3px;
  margin-right: 6px; vertical-align: middle;
}
