:root {
  --background: #171b20;
  --surface: #1e232a;
  --surface-hover: #252c34;
  --border: rgba(255,255,255,0.09);
  --text: #edf1f5;
  --text-muted: #98a2ad;
  --accent: #2f8f46;
  --accent-hover: #3ca457;
  --accent-dim: rgba(47, 143, 70, 0.25);
  --danger: #e85d5d;
  --radius-sm: 10px;
  --radius-md: 14px;
  --radius-lg: 18px;
  --input-font: clamp(14px, 3.9vw, 16px);
  --body-font: clamp(13px, 3.6vw, 15px);
}

* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body { height: 100%; overscroll-behavior: none; -webkit-text-size-adjust: 100%; }
html.is-share-view, html.is-share-view body { background: #171b20; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--background);
  color: var(--text);
  height: 100vh; height: 100dvh;
  overflow: hidden;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
#app { display: flex; height: 100vh; height: 100dvh; width: 100vw; overflow: hidden; }

button, .icon-button, .icon-button-sm, .new-chat-btn, .auth-btn, .send-button,
.modal-close, .chip, .chat-item, .account-tab, .context-menu button,
.attachment-chip button, .version-controls button, .link-btn {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  user-select: none;
}
button:active, .icon-button:active, .icon-button-sm:active,
.new-chat-btn:active, .auth-btn:active, .send-button:active { transform: scale(0.94); }

input, textarea, select { font-size: var(--input-font) !important; }

#sidebar {
  width: 280px; background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column; flex-shrink: 0;
  transition: width 0.24s cubic-bezier(0.2, 0.8, 0.2, 1);
  overflow: hidden; z-index: 10;
}
#sidebar.collapsed { width: 72px; }
.sidebar-header { display: flex; justify-content: space-between; align-items: center; padding: .75rem 1rem; border-bottom: 1px solid var(--border); min-height: 60px; }
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 700; font-size: 1.1rem; white-space: nowrap; }
.logo-img { height: 32px; width: auto; }
.brand-name { background: linear-gradient(135deg, #58a6ff, #3fb950); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
#sidebar.collapsed .brand-name,
#sidebar.collapsed .new-chat-btn span,
#sidebar.collapsed .chat-history,
#sidebar.collapsed .sidebar-footer span { display: none; }
#sidebar.collapsed .sidebar-footer .auth-btn { justify-content: center; padding: .3rem; }
#sidebar.collapsed #sidebarToggle { margin: 0; position: relative; top: 0; }

.icon-button {
  width: 44px; height: 44px; border: none; border-radius: var(--radius-sm);
  background: transparent; color: var(--text-muted); cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  transition: background .16s, color .16s, transform .16s;
}
.icon-button:hover { background: var(--surface-hover); color: var(--text); }
#sidebarToggle { margin-left: auto; }

.new-chat-btn {
  margin: .75rem 1rem; padding: .7rem 1rem;
  background: var(--accent-dim); color: var(--text);
  border: 1px solid rgba(255,255,255,.08); border-radius: var(--radius-sm);
  font-weight: 600; font-size: .9rem; cursor: pointer;
  display: flex; align-items: center; gap: .5rem; white-space: nowrap;
  min-height: 44px;
}
.new-chat-btn:hover { background: var(--accent); color: #fff; }
#sidebar.collapsed .new-chat-btn { justify-content: center; padding: .6rem; }

.chat-history { flex: 1; overflow-y: auto; padding: .25rem .5rem; -webkit-overflow-scrolling: touch; }
.chat-history::-webkit-scrollbar { width: 4px; }
.chat-history::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

.chat-item { display: flex; justify-content: space-between; align-items: center; padding: .6rem .6rem; border-radius: var(--radius-sm); cursor: pointer; transition: background .15s; color: var(--text-muted); min-height: 44px; }
.chat-item:hover, .chat-item.active { background: var(--surface-hover); color: var(--text); }
.chat-item .title { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: .9rem; display: flex; align-items: center; gap: .25rem; }
.chat-item .actions { display: flex; gap: .15rem; }
.chat-item .actions button { background: none; border: none; color: var(--text-muted); cursor: pointer; padding: .3rem .35rem; border-radius: 4px; min-width: 36px; min-height: 36px; display: inline-flex; align-items: center; justify-content: center; }
.chat-item .actions button:hover { background: var(--border); color: var(--text); }

.pinned-section { border-bottom: 1px solid var(--border); margin-bottom: .5rem; padding-bottom: .5rem; }
.pinned-label { font-size: .7rem; text-transform: uppercase; color: var(--text-muted); letter-spacing: .5px; padding: .2rem .6rem; }

.sidebar-footer { border-top: 1px solid var(--border); padding: .6rem 1rem; margin-top: auto; }
.auth-btn { background: none; border: 1px solid var(--border); border-radius: var(--radius-sm); padding: .6rem 1rem; cursor: pointer; color: var(--text-muted); display: flex; align-items: center; gap: .5rem; font-size: .85rem; width: 100%; justify-content: center; min-height: 44px; }
.auth-btn:hover { background: var(--surface-hover); color: var(--text); }

#mainContent { flex: 1; display: flex; flex-direction: column; background: var(--background); min-width: 0; overflow: hidden; }
#topBar { display: flex; align-items: center; padding: .5rem 1rem; border-bottom: 1px solid var(--border); gap: .75rem; min-height: 56px; flex-shrink: 0; }
#openSidebarBtn { display: none; }
#chatContainer { flex: 1; overflow-y: auto; padding: 1rem 1.5rem; scroll-behavior: smooth; -webkit-overflow-scrolling: touch; }
#chatContainer::-webkit-scrollbar { width: 6px; }
#chatContainer::-webkit-scrollbar-thumb { background: var(--border); border-radius: 8px; }

#welcomeScreen { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; gap: .75rem; padding: 1rem; }
.welcome-logo { height: 80px; width: auto; max-width: 40vw; }
#welcomeScreen h1 { font-size: clamp(1.2rem, 5vw, 1.6rem); font-weight: 600; }
#welcomeScreen p { color: var(--text-muted); font-size: clamp(.85rem, 3vw, .95rem); }
.suggestion-chips { display: flex; flex-wrap: wrap; gap: .5rem; justify-content: center; margin-top: .5rem; max-width: 90vw; }
.chip { background: var(--surface); border: 1px solid var(--border); border-radius: 20px; padding: .5rem 1rem; font-size: clamp(.75rem, 3vw, .85rem); cursor: pointer; color: var(--text-muted); transition: all .15s; min-height: 40px; }
.chip:hover { background: var(--surface-hover); border-color: var(--accent); color: var(--text); }

#messageList { flex: 1; display: none; flex-direction: column; gap: 1.5rem; }
.message-row { display: flex; flex-direction: column; width: 100%; }
.message-row.user { align-items: flex-end; }
.message-row.assistant { align-items: flex-start; }
.assistant-header-row { display: flex; align-items: center; gap: .4rem; margin-bottom: .2rem; font-weight: 500; font-size: .85rem; color: var(--accent); }
.assistant-avatar { height: 20px; width: auto; border-radius: 4px; }
.assistant-name { color: var(--text); font-weight: 600; }

.message { max-width: min(780px, 85%); padding: .6rem 1rem; border-radius: var(--radius-md); line-height: 1.6; word-wrap: break-word; animation: fadeIn .2s ease; font-size: var(--body-font); }
.message.user { background: var(--accent-dim); color: var(--text); border: 1px solid rgba(255,255,255,.06); border-bottom-right-radius: 4px; }
.message.assistant { background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 4px; color: var(--text); width: 100%; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: translateY(0); } }

.message-content { font-size: 1em; line-height: 1.7; }
.message-content h1, .message-content h2, .message-content h3 { margin: .6rem 0 .3rem 0; font-weight: 600; color: var(--text); }
.message-content h1 { font-size: 1.4rem; } .message-content h2 { font-size: 1.2rem; } .message-content h3 { font-size: 1.05rem; }
.message-content p { margin: .25rem 0; }
.message-content ul, .message-content ol { padding-left: 1.2rem; margin: .2rem 0; }
.message-content li { margin: .1rem 0; }
.message-content table { border-collapse: collapse; width: 100%; margin: .4rem 0; font-size: .9rem; overflow-x: auto; display: block; }
.message-content th, .message-content td { border: 1px solid var(--border); padding: .2rem .4rem; text-align: left; }
.message-content th { background: var(--surface-hover); }
.message-content code { background: var(--surface-hover); padding: .05rem .3rem; border-radius: 4px; font-family: monospace; font-size: .9rem; }
.message-content pre { background: var(--surface-hover); padding: .5rem; border-radius: var(--radius-sm); overflow-x: auto; }
.message-content blockquote { border-left: 3px solid var(--accent); padding-left: .6rem; margin: .2rem 0; color: var(--text-muted); }
.message-content hr { display: none; }

.thinking-indicator { display: flex; align-items: center; gap: .5rem; color: var(--text-muted); padding: .2rem 0; }
.thinking-indicator .spinner { width: 16px; height: 16px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.message-actions-row { display: flex; gap: .3rem; margin-top: .2rem; flex-wrap: wrap; opacity: .9; align-items: center; }
.icon-button-sm { background: none; border: none; color: var(--text-muted); cursor: pointer; padding: .35rem .4rem; border-radius: 6px; display: inline-flex; align-items: center; justify-content: center; transition: background .15s, color .15s; min-width: 36px; min-height: 36px; }
.icon-button-sm:hover { background: var(--surface-hover); color: var(--text); }

.icon-button-sm.liked { background-color: #2f8f46 !important; color: #ffffff !important; }
.icon-button-sm.disliked { background-color: #e85d5d !important; color: #ffffff !important; }
.icon-button-sm.copied { background-color: #2f8f46 !important; color: #ffffff !important; }

.version-controls { display: flex; align-items: center; gap: .4rem; margin-top: .3rem; font-size: .8rem; color: var(--text-muted); }
.version-controls button { background: none; border: none; cursor: pointer; color: var(--text-muted); padding: .3rem; border-radius: 4px; display: inline-flex; align-items: center; min-width: 36px; min-height: 36px; }
.version-controls button:hover { background: var(--surface-hover); color: var(--text); }
.version-controls button:disabled { opacity: .3; cursor: not-allowed; }

.attachments-above { display: flex; flex-wrap: wrap; gap: .4rem; margin-bottom: .55rem; }
.user-message-attachment { width: 80px; height: 80px; border-radius: 8px; overflow: hidden; border: 1px solid var(--border); cursor: pointer; transition: transform .15s; background: var(--surface-hover); }
.user-message-attachment:hover { transform: scale(1.04); }
.user-message-attachment img { width: 100%; height: 100%; object-fit: cover; display: block; }
.user-message-attachment .file-icon { display: flex; align-items: center; justify-content: center; width: 100%; height: 100%; color: var(--text-muted); }

#composerArea { flex-shrink: 0; padding: .5rem 1rem .75rem; border-top: 1px solid var(--border); background: var(--background); padding-bottom: calc(.75rem + env(safe-area-inset-bottom)); }
#composer { max-width: 900px; margin: 0 auto; }
#attachmentPreview { display: flex; flex-wrap: wrap; gap: .4rem; padding: .25rem 0; }
.attachment-chip { display: inline-flex; align-items: center; gap: .3rem; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: .25rem .5rem .25rem .3rem; }
.attachment-chip img { width: 36px; height: 36px; object-fit: cover; border-radius: 6px; cursor: pointer; }
.attachment-chip i { width: 20px; height: 20px; }
.attachment-chip button { background: none; border: none; color: var(--text-muted); cursor: pointer; padding: .25rem; display: inline-flex; align-items: center; min-width: 32px; min-height: 32px; }
.attachment-chip button:hover { color: var(--text); }

.composer-main { display: flex; align-items: flex-end; gap: .4rem; background: var(--surface); border-radius: var(--radius-md); padding: .2rem .2rem .2rem .5rem; border: 2px solid #2e7d32; transition: border-color .2s; }
.composer-main:focus-within { border-color: #4caf50; }
.composer-main textarea {
  flex: 1;
  background: transparent;
  border: none;
  resize: none;
  padding: .5rem .3rem;
  font-size: var(--input-font) !important;
  outline: none;
  color: var(--text);
  max-height: min(50vh, 400px);
  font-family: inherit;
  min-height: 44px;
  line-height: 1.4;
  white-space: pre-wrap;
  word-wrap: break-word;
  min-width: 0;
  overflow-y: auto;
}
.composer-main textarea::placeholder {
  color: var(--text-muted);
  font-size: var(--input-font);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  opacity: 0.85;
}

.send-button { background: var(--accent); color: #fff; border: none; border-radius: var(--radius-sm); cursor: pointer; display: flex; align-items: center; justify-content: center; min-width: 44px; min-height: 44px; width: 44px; height: 44px; opacity: 1; transition: opacity .2s, background .2s; flex-shrink: 0; }
.send-button:disabled { background: var(--accent-dim); color: var(--text-muted); cursor: default; opacity: .3; }
.send-button:not(:disabled):hover { background: var(--accent-hover); }
.send-button.generating { background: #e85d5d !important; color: #fff !important; opacity: 1 !important; animation: pulseStop 1.2s ease-in-out infinite; }
.send-button.generating:hover { background: #c0392b !important; }
@keyframes pulseStop { 0%,100% { box-shadow: 0 0 0 0 rgba(232,93,93,.55); } 50% { box-shadow: 0 0 0 7px rgba(232,93,93,0); } }
.send-icon, .stop-icon { width: 18px; height: 18px; }

.footer-note { text-align: center; font-size: .7rem; color: var(--text-muted); margin-top: .4rem; }

.modal { position: fixed; inset: 0; background: rgba(0,0,0,.7); display: flex; align-items: center; justify-content: center; z-index: 1000; backdrop-filter: blur(4px); padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }
.modal.hidden { display: none; }
.modal-content { background: var(--surface); padding: 1.5rem; border-radius: var(--radius-md); max-width: 420px; width: 92%; position: relative; max-height: 90vh; overflow-y: auto; border: 1px solid var(--border); }
.modal-close { position: absolute; top: .5rem; right: .75rem; background: none; border: none; font-size: 1.5rem; cursor: pointer; color: var(--text-muted); min-width: 44px; min-height: 44px; display: inline-flex; align-items: center; justify-content: center; }
.modal-close:hover { color: var(--text); }
.modal-content h2 { margin-bottom: 1rem; color: var(--text); font-size: 1.3rem; }
.modal-content h3 { margin: 1rem 0 .4rem 0; color: var(--text); font-size: 1rem; }
.modal-content label { display: block; margin-top: .6rem; font-weight: 500; font-size: .85rem; color: var(--text-muted); }
.modal-content input { width: 100%; padding: .65rem; margin-top: .15rem; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--background); color: var(--text); font-size: var(--input-font) !important; min-height: 44px; }
.modal-content input:focus { outline: none; border-color: var(--accent); }
.modal-content .btn-primary { width: 100%; margin-top: 1rem; padding: .75rem; background: var(--accent); color: white; border: none; border-radius: var(--radius-sm); font-weight: 600; font-size: .95rem; cursor: pointer; min-height: 44px; }
.modal-content .btn-primary:hover { background: var(--accent-hover); }
.modal-content .btn-primary:disabled { opacity: .6; cursor: not-allowed; }
.modal-content .btn-danger { background: var(--danger); }
.modal-content .btn-danger:hover { background: #c0392b; }
.modal-content .toggle-link { margin-top: .75rem; text-align: center; color: var(--accent); cursor: pointer; font-size: .9rem; padding: .5rem; }
.modal-content .toggle-link:hover { text-decoration: underline; }
.modal-content .error-msg { color: var(--danger); font-size: .85rem; margin-top: .3rem; }
.modal-content .status-msg { color: var(--text-muted); font-size: .85rem; margin-top: .3rem; }
.modal-content hr { border: none; border-top: 1px solid var(--border); margin: 1rem 0; }

.link-btn { background: none; border: none; color: var(--accent); cursor: pointer; margin-top: .5rem; font-size: .9rem; padding: .5rem; min-height: 40px; }
.link-btn:hover { text-decoration: underline; }
.link-btn:disabled { color: var(--text-muted); cursor: not-allowed; text-decoration: none; opacity: .7; }

.account-modal .modal-content { max-width: 700px; width: 95%; padding: 1.5rem; }
.account-modal-layout { display: flex; gap: 1.5rem; margin-top: .5rem; }
.account-tabs { display: flex; flex-direction: column; gap: .2rem; min-width: 140px; border-right: 1px solid var(--border); padding-right: 1rem; }
.account-tab { display: flex; align-items: center; gap: .5rem; padding: .7rem .8rem; border-radius: var(--radius-sm); cursor: pointer; color: var(--text-muted); font-size: .9rem; min-height: 44px; }
.account-tab:hover { background: var(--surface-hover); color: var(--text); }
.account-tab.active { background: var(--accent-dim); color: var(--text); }
.account-tab.logout-tab { color: var(--danger); margin-top: auto; }
.account-tab.logout-tab:hover { background: rgba(232,93,93,.12); }
.account-content { flex: 1; padding-right: .5rem; overflow-y: auto; max-height: 60vh; min-width: 0; }
.account-content h2 { margin-bottom: 1rem; font-size: 1.2rem; }
.account-content .profile-info p { margin: .3rem 0; font-size: .95rem; word-break: break-word; }
.account-content .account-section { display: flex; flex-direction: column; gap: .5rem; }
.account-content .account-section button { width: auto; align-self: flex-start; padding: .5rem 1rem; min-height: 40px; }
.account-content .security-section { display: flex; flex-direction: column; gap: .5rem; }
.account-content .sessions-list p { margin: .3rem 0; font-size: .9rem; word-break: break-word; }
.twofa-setup { display: flex; flex-direction: column; align-items: center; gap: 1rem; }
.twofa-setup img.qr-code { width: 200px; height: 200px; max-width: 80vw; border-radius: 8px; background: white; padding: 8px; }
.twofa-setup input { width: 100%; max-width: 200px; text-align: center; letter-spacing: 4px; }

.context-menu { position: fixed; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: .3rem; min-width: 160px; z-index: 2000; box-shadow: 0 8px 24px rgba(0,0,0,.5); }
.context-menu.hidden { display: none; }
.context-menu button { display: flex; align-items: center; gap: .5rem; width: 100%; padding: .6rem .8rem; background: none; border: none; color: var(--text); cursor: pointer; border-radius: var(--radius-sm); font-size: .9rem; min-height: 44px; }
.context-menu button:hover { background: var(--surface-hover); }
.context-menu button.danger { color: var(--danger); }
.context-menu button.danger:hover { background: rgba(232,93,93,.12); }

.lightbox-overlay { animation: fadeIn .2s ease; }

html.is-share-view body, body.share-view { background: var(--background) !important; overflow: hidden; }
html.is-share-view #sidebar,
html.is-share-view #topBar,
html.is-share-view #composerArea,
html.is-share-view #welcomeScreen,
html.is-share-view .welcome-logo,
html.is-share-view .suggestion-chips,
html.is-share-view .footer-note,
body.share-view #sidebar,
body.share-view #topBar,
body.share-view #composerArea,
body.share-view #welcomeScreen,
body.share-view .welcome-logo,
body.share-view .suggestion-chips,
body.share-view .footer-note { display: none !important; }

html.is-share-view #app, body.share-view #app { display: block; width: 100vw; height: 100dvh; }
html.is-share-view #mainContent, body.share-view #mainContent { width: 100%; max-width: 100%; height: 100dvh; display: block; }
html.is-share-view #chatContainer, body.share-view #chatContainer {
  padding: 4.25rem 1rem 2rem;
  max-width: 900px; margin: 0 auto; width: 100%;
  height: 100dvh; overflow-y: auto; box-sizing: border-box;
}
html.is-share-view #messageList, body.share-view #messageList { display: flex !important; gap: 1.5rem; flex-direction: column; }

html.is-share-view body::before, body.share-view::before {
  content: "Shared Chat — AgriDeepAI";
  display: block; position: fixed; top: 0; left: 0; right: 0;
  padding: .75rem 1rem; background: var(--surface);
  border-bottom: 1px solid var(--border);
  color: var(--text-muted); font-size: .9rem;
  text-align: center; z-index: 50;
}

@media (max-width: 380px) {
  :root { --input-font: 14px; --body-font: 13px; }
  #chatContainer { padding: .5rem .5rem; }
  .message { max-width: 96%; padding: .5rem .75rem; }
  #composerArea { padding: .4rem .5rem .5rem; padding-bottom: calc(.5rem + env(safe-area-inset-bottom)); }
  .composer-main { padding-left: .5rem; gap: .3rem; }
  .composer-main textarea { min-height: 40px; padding: .4rem .3rem; max-height: min(50vh, 300px); }
  .send-button { min-width: 40px; min-height: 40px; width: 40px; height: 40px; }
  .icon-button { width: 40px; height: 40px; }
  .chip { padding: .4rem .7rem; }
  .welcome-logo { height: 60px; }
  .modal-content { padding: 1rem; }
  .account-modal .modal-content { padding: .75rem; }
  .user-message-attachment { width: 60px; height: 60px; }
}

@media (max-width: 768px) {
  #sidebar { position: fixed; top: 0; left: 0; height: 100%; width: min(85vw, 320px); transform: translateX(-105%); transition: transform .22s ease; border-right: none; box-shadow: 4px 0 24px rgba(0,0,0,.5); z-index: 100; }
  #sidebar.mobile-open { transform: translateX(0); }
  #openSidebarBtn { display: block; }
  #sidebarToggle { display: none; }
  .brand-name, .new-chat-btn span, .auth-btn span { display: inline !important; }
  #sidebar.collapsed { width: min(85vw, 320px); }
  #sidebar.collapsed .chat-history { display: block; }
  .message { max-width: 92%; }
  .account-modal .modal-content { max-width: 95%; padding: 1rem; }
  .account-modal-layout { flex-direction: column; gap: 1rem; }
  .account-tabs { flex-direction: row; overflow-x: auto; border-right: none; border-bottom: 1px solid var(--border); padding-bottom: .5rem; padding-right: 0; scrollbar-width: none; }
  .account-tabs::-webkit-scrollbar { display: none; }
  .account-tab { white-space: nowrap; padding: .5rem .7rem; }
  .account-content { max-height: none; }
  body.share-view #chatContainer, html.is-share-view #chatContainer { padding: 4.25rem .75rem 1rem; }
  .user-message-attachment { width: 64px; height: 64px; }
  .composer-main textarea { max-height: min(45vh, 320px); }
}

@media (max-width: 900px) and (orientation: landscape) {
  .message { max-width: 88%; }
  #composerArea { padding: .4rem 1rem .5rem; }
  .modal-content { max-height: 95vh; }
  #welcomeScreen { padding: .5rem; }
  .welcome-logo { height: 50px; }
  #chatContainer { padding: .5rem 1rem; }
  .composer-main textarea { max-height: min(40vh, 220px); }
}

@media (min-width: 769px) {
  #sidebar.collapsed .brand-name,
  #sidebar.collapsed .new-chat-btn span,
  #sidebar.collapsed .auth-btn span { display: none; }
}
@media (min-width: 1440px) {
  :root { --input-font: 16px; --body-font: 15px; }
  .message { max-width: min(900px, 75%); }
  #chatContainer { padding: 1.5rem 2rem; }
}

@media (min-width: 1000px) and (max-height: 600px) {
  #welcomeScreen { gap: .4rem; }
  .welcome-logo { height: 50px; }
  .composer-main textarea { max-height: min(35vh, 200px); }
}

.manual-secret { margin-top: .75rem; width: 100%; }
.secret-box { display: flex; align-items: center; gap: .4rem; background: var(--background); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: .5rem .6rem; margin-top: .3rem; }
.secret-box code { flex: 1; font-family: monospace; font-size: .75rem; word-break: break-all; color: var(--text); user-select: all; }
.copy-secret-btn { background: var(--accent); color: #fff; border: none; border-radius: 6px; padding: .3rem .6rem; font-size: .75rem; font-weight: 600; cursor: pointer; white-space: nowrap; }
.copy-secret-btn:hover { background: var(--accent-hover); }

.session-item { padding: .5rem; border-radius: 6px; background: var(--surface-hover); margin-bottom: .4rem; font-size: .85rem; }
.session-item p { margin: .15rem 0; word-break: break-word; }

/* ===== Edit-message: match the original bubble's size & position ===== */
.message.user.message-editing {
  width: min(780px, 85%);
  max-width: min(780px, 85%);
  min-width: min(360px, 80vw);
}
.edit-area { width: 100%; box-sizing: border-box; }
.edit-textarea {
  width: 100%;
  min-width: 100%;
  min-height: 44px;
  padding: .6rem .8rem;
  border-radius: 10px;
  background: var(--background);
  color: var(--text);
  border: 1px solid var(--border);
  resize: none;
  font-family: inherit;
  font-size: var(--body-font);
  line-height: 1.6;
  box-sizing: border-box;
  overflow-y: hidden;
  field-sizing: content;
}
.edit-textarea:focus { outline: none; border-color: var(--accent); }
.edit-actions {
  display: flex; gap: .5rem; margin-top: .5rem; justify-content: flex-end;
}
.edit-btn-cancel {
  padding: .5rem 1.1rem; background: transparent;
  border: 1px solid var(--border); border-radius: 10px;
  color: var(--text); cursor: pointer; font-weight: 500; font-size: .9rem;
}
.edit-btn-send {
  padding: .5rem 1.4rem; background: var(--accent); color: #fff;
  border: none; border-radius: 10px; font-weight: 600; font-size: .9rem;
  cursor: pointer; box-shadow: 0 2px 8px rgba(47,143,70,.35);
}
@media (max-width: 480px) {
  .message.user.message-editing { width: 92%; max-width: 92%; min-width: 0; }
}

/* ===== Touch-device fixes: prevent hover-induced tap delay ===== */
.icon-button svg, .icon-button-sm svg, .new-chat-btn svg, .send-button svg,
.auth-btn svg, .chip svg, .modal-close svg, .account-tab svg,
.context-menu button svg, .link-btn svg, .attachment-chip button svg,
.version-controls button svg, .copy-secret-btn svg, .toggle-link svg {
  pointer-events: none;
}

@media (hover: none) {
  /* Neutralise hover states so first tap activates instantly */
  .icon-button:hover { background: transparent; color: var(--text-muted); }
  .chat-item:hover { background: transparent; color: var(--text-muted); }
  .chat-item.active:hover { background: var(--surface-hover); color: var(--text); }
  .chip:hover { background: var(--surface); border-color: var(--border); color: var(--text-muted); }
  .auth-btn:hover { background: none; color: var(--text-muted); }
  .new-chat-btn:hover { background: var(--accent-dim); color: var(--text); }
  .send-button:not(:disabled):hover { background: var(--accent); }
  .account-tab:hover { background: transparent; color: var(--text-muted); }
  .account-tab.active:hover { background: var(--accent-dim); color: var(--text); }
  .modal-close:hover { color: var(--text-muted); }
  .modal-content .btn-primary:hover { background: var(--accent); }
  .modal-content .btn-danger:hover { background: var(--danger); }
  .link-btn:hover { text-decoration: none; }
  .context-menu button:hover { background: none; }
  .version-controls button:hover { background: none; color: var(--text-muted); }
  .attachment-chip button:hover { color: var(--text-muted); }
  .copy-secret-btn:hover { background: var(--accent); }

  /* Add :active feedback for touch devices */
  .icon-button:active { background: var(--surface-hover); color: var(--text); transform: scale(0.96); }
  .chat-item:active { background: var(--surface-hover); color: var(--text); }
  .chip:active { background: var(--surface-hover); border-color: var(--accent); color: var(--text); }
  .auth-btn:active { background: var(--surface-hover); color: var(--text); }
  .account-tab:active { background: var(--surface-hover); color: var(--text); }
  .context-menu button:active { background: var(--surface-hover); }
}

/* ===== Session list with per-row logout ===== */
.sessions-list .sessions-actions {
  display: flex;
  justify-content: flex-end;
  margin-bottom: .75rem;
}
.sessions-list .sessions-logout-all {
  display: inline-flex;
  align-items: center;
  gap: .4rem;
  padding: .55rem 1rem;
  font-size: .85rem;
  font-weight: 600;
  border-radius: var(--radius-sm);
  width: auto;
  align-self: flex-end;
}
.sessions-list .sessions-logout-all svg { width: 16px; height: 16px; }
.sessions-list .sessions-logout-all:disabled { opacity: .6; cursor: not-allowed; }

.session-item {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: .75rem;
  padding: .65rem .75rem;
  border-radius: 8px;
  background: var(--surface-hover);
  margin-bottom: .5rem;
  font-size: .85rem;
}
.session-item .session-info { flex: 1; min-width: 0; }
.session-item .session-info p { margin: .15rem 0; word-break: break-word; }

.session-logout-btn {
  flex-shrink: 0;
  align-self: center;
  display: inline-flex;
  align-items: center;
  gap: .35rem;
  background: transparent;
  color: var(--danger);
  border: 1px solid rgba(232,93,93,.35);
  border-radius: 8px;
  padding: .4rem .7rem;
  font-size: .8rem;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s, border-color .15s;
  min-height: 36px;
}
.session-logout-btn:hover { background: rgba(232,93,93,.12); border-color: var(--danger); }
.session-logout-btn:disabled { opacity: .55; cursor: not-allowed; }

@media (max-width: 480px) {
  .session-item { flex-direction: column; align-items: stretch; }
  .session-logout-btn { align-self: flex-start; }
  .sessions-list .sessions-logout-all { width: 100%; justify-content: center; align-self: stretch; }
}

/* ===== Code blocks: dark theme + modern top-right copy button ===== */
.code-block-wrap {
  position: relative;
  margin: .65rem 0;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: #0f1318;
}

.code-block-wrap pre,
.message-content .code-block-wrap pre {
  margin: 0 !important;
  padding: 2.35rem .9rem .85rem !important;
  background: #0f1318 !important;
  color: #e6edf3 !important;
  border: none !important;
  border-radius: 0 !important;
  overflow-x: auto;
  font-family: 'JetBrains Mono', 'Fira Code', Consolas, Monaco, 'Courier New', monospace;
  font-size: .84rem;
  line-height: 1.55;
}

.code-block-wrap pre code,
.message-content .code-block-wrap pre code {
  background: transparent !important;
  color: inherit !important;
  padding: 0 !important;
  font-size: inherit;
}

/* Modern top-right copy button */
.code-copy-btn {
  position: absolute;
  top: .5rem;
  right: .5rem;
  z-index: 2;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 30px;
  min-height: 30px;
  padding: .3rem;
  background: rgba(255, 255, 255, .08);
  color: #c9d1d9;
  border: 1px solid rgba(255, 255, 255, .15);
  border-radius: 7px;
  cursor: pointer;
  transition: background .15s ease, color .15s ease, border-color .15s ease, transform .12s ease;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.code-copy-btn svg { display: block; width: 14px; height: 14px; pointer-events: none; }
.code-copy-btn:hover {
  background: rgba(255, 255, 255, .16);
  color: #fff;
  border-color: rgba(255, 255, 255, .3);
}
.code-copy-btn:active { transform: scale(.94); }
.code-copy-btn.copied {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

/* Responsive: keep the button comfortably tappable on small screens */
@media (max-width: 480px) {
  .code-copy-btn { min-width: 34px; min-height: 34px; top: .45rem; right: .45rem; }
  .code-block-wrap pre { padding: 2.5rem .75rem .75rem !important; font-size: .8rem; }
}

/* Remove the old plain <pre> styling so it doesn't fight the wrapper */
.message-content > pre { margin: .65rem 0; padding: .85rem; background: #0f1318; border: 1px solid var(--border); border-radius: 10px; overflow-x: auto; }
