:root {
  --primary: #4b7bec;
  --accent: #20bf6b;
  --danger: #eb3b5a;
  --bg-light: #f7faff;
  --bg-dark: #181d23;
  --fg-light: #222;
  --fg-dark: #eee;
  --border: #d1d8e0;
  --radius: 12px;
  --shadow: 0 2px 12px rgba(0,0,0,0.09);
  --topbar-height: 54px;
}

/* Safe-area para iOS notch */
body {
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
  min-width: 0;
}

/* Reset CSS básico cross-browser */
html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p,
blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn,
em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var,
b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas,
details, embed, figure, figcaption, footer, header, hgroup, menu, nav,
output, ruby, section, summary, time, mark, audio, video {
  margin:0; padding:0; border:0; font-size:100%; font:inherit; vertical-align:baseline; box-sizing: border-box;
}

html {
  height: 100%;
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body {
  background: var(--bg-light);
  color: var(--fg-light);
  font-family: 'Segoe UI', Arial, sans-serif;
  min-height: 100vh;
  min-width: 0;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  transition: background .15s, color .15s;
}

body.dark-theme {
  background: var(--bg-dark);
  color: var(--fg-dark);
}

/* HEADER */
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: .8rem 1.1rem;
  background: var(--primary);
  color: #fff;
  box-shadow: var(--shadow);
  min-height: var(--topbar-height);
  height: var(--topbar-height);
  position: relative;
  z-index: 5;
}
.app-title {
  font-size: 1.28rem;
  font-weight: 600;
  letter-spacing: .5px;
  user-select: none;
}
.header-controls {
  display: flex;
  align-items: center;
  gap: .7em;
}
header button {
  margin-left: .2em;
  background: none;
  border: none;
  color: inherit;
  cursor: pointer;
  font-size: 1.15rem;
  line-height: 1;
  padding: 0.25em 0.4em;
  border-radius: 2em;
  transition: background .15s;
}
header button:focus {
  outline: 2px solid #fff;
  outline-offset: 2px;
  background: rgba(0,0,0,0.10);
}

/* NAVIGATION */
nav {
  display: flex;
  justify-content: center;
  background: #f5f6fa;
  border-bottom: 1.5px solid #e1e5ee;
  flex-wrap: wrap;
  min-height: 44px;
}
nav button {
  flex: 1;
  min-width: 60px;
  padding: .75rem 0;
  font-size: 1.02rem;
  background: none;
  border: none;
  outline: none;
  border-bottom: 2.5px solid transparent;
  transition: background 0.2s, border-bottom 0.2s, color 0.15s;
  cursor: pointer;
  color: #444;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: .45em;
  font-weight: 500;
  user-select: none;
}
nav button:focus {
  background: #e6eefd;
  outline: 2px solid var(--primary);
  outline-offset: -2px;
}
nav button.active {
  background: #fff;
  border-bottom: 2.5px solid var(--primary);
  color: var(--primary);
  font-weight: 600;
}

/* MAIN */
main {
  max-width: 1000px;
  margin: 2.2rem auto 1.5rem auto;
  background: #fff;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.6rem 1.2rem 1.1rem 1.2rem;
  width: 98vw;
  min-width: 0;
  transition: background .15s;
  overflow-x: hidden;
}
body.dark-theme main {
  background: #23272f;
}
@media (max-width: 900px) {
  main {
    padding: 1rem 0.3rem 0.8rem 0.3rem;
    margin: 1rem auto;
    width: 100vw;
    border-radius: 0;
  }
  header {
    padding: .6rem .6rem;
    min-height: 44px;
    height: 44px;
  }
  nav button {
    font-size: .96rem;
    padding: .6rem 0;
  }
}
@media (max-width: 600px) {
  main { padding: .6rem 0 .5rem 0; }
  nav { min-height: 38px; }
  nav button { font-size: .89rem; min-width: 38px; padding: .47rem 0; }
  header { min-height: 40px; height: 40px; }
}

/* LOGIN */
.login-container {
  max-width: 340px;
  margin: 11vh auto;
  padding: 2.2rem 2.5rem;
  background: #fff;
  box-shadow: var(--shadow);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  align-items: center;
}
body.dark-theme .login-container { background: #222; }
.login-container h2 {
  text-align: center;
  margin-bottom: 1.7rem;
  color: var(--primary);
  font-weight: 600;
}
.login-container form {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.login-container label {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  width: 100%;
  margin-bottom: 1.2rem;
  font-size: 1rem;
}
.login-container input[type="text"],
.login-container input[type="password"] {
  width: 100%;
  padding: .7rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-top: 0.3rem;
  font-size: 1.06rem;
  background: #fcfcfc;
  box-sizing: border-box;
}
.login-container input[type="text"]:focus,
.login-container input[type="password"]:focus {
  outline: 2px solid var(--primary);
  border-color: var(--primary);
}
.login-container button {
  width: 100%;
  padding: .85rem;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 1.1rem;
  margin-top: 1rem;
  cursor: pointer;
  transition: background .2s;
}
.login-container button:focus {
  outline: 2px solid var(--accent);
}
.login-container button:hover {
  background: var(--accent);
}
.error-msg {
  color: var(--danger);
  margin-top: 1rem;
  text-align: center;
}

/* TABS */
.tab { display: none; }
.tab.active { display: block; }

/* CHAT */
.chat-history {
  max-height: 58vh;
  min-height: 160px;
  overflow-y: auto;
  border: 1px solid var(--border);
  background: #f3f6fd;
  border-radius: var(--radius);
  padding: .92rem;
  margin-bottom: .6rem;
  font-size: 1.03em;
  width: 100%;
  box-sizing: border-box;
  word-break: break-word;
  transition: background .15s;
}
body.dark-theme .chat-history {
  background: #23272f;
  border: 1px solid #222;
}
.message { display: flex; margin-bottom: 0.7rem; }
.message.user { justify-content: flex-end; }
.message.assistant { justify-content: flex-start; }
.bubble {
  max-width: 70vw;
  padding: .7em 1em;
  border-radius: 18px;
  background: #eaf0fb;
  color: #222;
  font-size: 1.04em;
  box-shadow: 0 2px 6px rgba(0,0,0,0.03);
  white-space: pre-wrap;
  word-break: break-word;
  overflow-x: auto;
}
.message.user .bubble {
  background: var(--primary);
  color: #fff;
  border-bottom-right-radius: 3px;
}
.message.assistant .bubble {
  background: #f1f3f6;
  color: #333;
  border-bottom-left-radius: 3px;
}
.avatar {
  width: 28px; height: 28px;
  background: #eee;
  border-radius: 50%;
  margin-right: 0.6em;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.05em;
  user-select: none;
}
.message.user .avatar { order: 2; margin-left: 0.6em; margin-right: 0; }
.chat-loader { display: flex; align-items: center; gap: .6em; margin-bottom: .7em; color: var(--primary);}
.chat-loader.hidden { display: none; }
.chat-controls {
  display: flex;
  flex-direction: column;
  gap: .6rem;
  width: 100%;
}
#input {
  width: 100%;
  min-height: 48px;
  border-radius: var(--radius);
  padding: .7rem;
  border: 1px solid var(--border);
  font-size: 1em;
  resize: vertical;
  box-sizing: border-box;
}
#input:focus { outline: 2px solid var(--primary); border-color: var(--primary);}
.chat-buttons {
  display: flex; gap: 1rem; justify-content: flex-end; width: 100%;
}
.chat-buttons button {
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 1.2em;
  padding: .53rem 1.5rem;
  cursor: pointer;
  font-size: .98em;
  transition: background .2s;
  min-width: 100px;
}
.chat-buttons button:focus { outline: 2px solid var(--accent);}
.chat-buttons button:hover { background: var(--accent);}
@media (max-width: 600px) {
  .chat-history { min-height: 120px; font-size: .99em; padding: .65rem;}
  #input { min-height: 38px; font-size: .96em; }
  .chat-buttons button { min-width: 80px; font-size: .95em; padding: .48rem 1.2rem;}
}

/* UPLOAD */
.upload-container { display: grid; grid-template-columns: 1fr; row-gap: .7rem; }
#upload-form { display: flex; gap: .65rem; align-items: center; margin-bottom: 0; flex-wrap: wrap;}
#file-input[type="file"] { max-width: 200px; }
#upload-btn { min-width: 90px; }
#file-search {
  width: 100%; max-width: 320px; border-radius: var(--radius); border: 1px solid var(--border);
  padding: .48em .8em; font-size: .98rem; margin-top: 0.45rem;
}
#file-search:focus { outline: 2px solid var(--primary); }

/* FILE GRID */
.file-grid {
  display: grid;
  grid-template-columns: 2.6fr .9fr 1.1fr 1.7fr 1.2fr;
  gap: 0.22rem 0.7rem;
  align-items: center;
  margin-top: .8rem;
  width: 100%;
  font-size: .97em;
}
.file-header {
  font-weight: 600;
  background: #f3f6fd;
  border-radius: 6px;
  padding: 0.38em 0.6em;
  color: var(--primary);
  font-size: .98rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
}
.file-cell {
  padding: 0.32em 0.5em;
  background: #f7faff;
  border-radius: 6px;
  font-size: 0.96em;
  word-break: break-all;
  min-height: 30px;
  display: flex; align-items: center;
  color: #222;
  transition: background .15s, color .15s;
}
.file-cell.actions {
  display: flex; flex-direction: row; gap: 0.5em; justify-content: flex-start;
}
.file-cell.actions button {
  white-space: nowrap;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 7px;
  padding: 0.38em 1em;
  cursor: pointer;
  font-size: .97em;
  min-width: 80px;
  min-height: 30px;
  transition: background .16s;
  box-shadow: 0 1px 2px rgba(0,0,0,0.09);
}
.file-cell.actions button:focus { outline: 2px solid var(--accent);}
.file-cell.actions button:hover { background: var(--accent);}
body.dark-theme .file-header {
  background: #22252a;
  color: #7ea7fa;
  border-bottom: 1px solid #393f4b;
}
body.dark-theme .file-cell {
  background: #23272f;
  color: #f5f6fa;
}
body.dark-theme .file-cell.actions button {
  background: #2956b9;
  color: #fff;
}
body.dark-theme .file-cell.actions button:hover {
  background: #20bf6b;
  color: #fff;
}
@media (max-width: 800px) {
  .file-grid {
    grid-template-columns: 1.7fr .8fr .8fr 1.2fr .9fr;
    font-size: .93em;
  }
  .file-header, .file-cell { padding: 0.21em 0.31em;}
}
@media (max-width: 550px) {
  .file-grid { display: block; }
  .file-header, .file-cell {
    display: block; width: 100%; border-radius: 5px; margin-bottom: 2px;
    font-size: .97em;
  }
  .file-header { background: #e3eaf7; }
  .file-cell { background: #f7faff; }
  .file-cell.actions { flex-direction: column; gap: 0.2em; margin-bottom: 0.5em;}
  body.dark-theme .file-header { background: #22252a; }
  body.dark-theme .file-cell { background: #23272f; }
}

/* SETTINGS */
#settings label {
  display: block;
  margin: .82rem 0 .18rem;
  font-size: .97em;
}
#settings input, #settings select {
  width: 100%;
  padding: .6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: .98em;
  background: #fff;
  margin-bottom: .1rem;
  box-sizing: border-box;
  max-width: 100%;
  overflow-x: auto;
}
body.dark-theme #settings input,
body.dark-theme #settings select {
  background: #23272f;
  color: #f5f6fa;
  border: 1px solid #444;
}
#settings input:focus, #settings select:focus {
  outline: 2px solid var(--primary);
}
#save-settings {
  margin-left: 0 !important;
  background: var(--accent);
  border-radius: 1.2em;
  font-size: 1.04em;
}
#save-settings:focus { outline: 2px solid var(--primary);}
#save-settings:hover { background: var(--primary);}
#reindex-btn {
  margin-left: 0 !important;
  background: var(--danger); color: #fff; border-radius: 1.1em;
}
#reindex-btn:focus { outline: 2px solid var(--danger);}
fieldset {
  border: 1px solid #e1e5ee; border-radius: var(--radius); margin-bottom: .7rem; padding: .8em .9em;
  min-width: 0;
  max-width: 100%;
  box-sizing: border-box;
  overflow-x: auto;
}
body.dark-theme fieldset {
  border: 1px solid #444;
  background: #181d23;
}
legend { color: var(--primary); font-size: .99em; font-weight: 500;}
.note {
  background: #e3eaf7; color: #333; border-radius: 7px; margin: .7em 0 0.5em 0; padding: .5em .7em; font-size: .97em;
}
body.dark-theme .note {
  background: #23272f;
  color: #7ea7fa;
}
.settings-buttons {
  display: flex;
  gap: .7em;
  align-items: center;
  margin-top: .5em;
  justify-content: flex-end;
}
@media (max-width: 1050px) {
  main { padding-left: 0.2rem; padding-right: 0.2rem;}
}
@media (max-width: 700px) {
  fieldset { padding: .6em .5em; }
  #settings input, #settings select { padding: .5rem; font-size: .95em;}
  #save-settings, #reindex-btn { font-size: .98em; padding: .6em 1.1em;}
}

/* LOADER & TOAST */
#loading-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(251,251,255,0.7);
  z-index: 99;
  display: flex; align-items: center; justify-content: center;
}
#loading-overlay.hidden { display: none; }
.spinner {
  width: 35px; height: 35px;
  border: 5px solid #eee;
  border-top: 5px solid var(--primary);
  border-radius: 50%;
  animation: spin .75s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed;
  left: 50%; bottom: 2.2rem;
  transform: translateX(-50%);
  background: var(--primary);
  color: #fff;
  padding: .73rem 1.5rem;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index: 999;
  font-size: 1.06em;
  opacity: .97;
  pointer-events: none;
  transition: opacity .3s;
  min-width: 140px;
  text-align: center;
}
.toast.hidden { display: none; }

/* SEARCH PÚBLICA */
.search-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 1.2rem 0;
  width: 100%;
  justify-content: center;
}
.search-container form {
  display: flex;
  align-items: center;
  width: auto;
  justify-content: center;
  flex-wrap: wrap;
  margin-left: auto;
  margin-right: auto;
}
#search-input {
  width: 450px; max-width: 97vw; min-width: 110px;
  padding: 0.65rem .9rem;
  border: 1px solid var(--border);
  border-radius: 22px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.07);
  font-size: 1.01rem;
  transition: width .2s;
}
#search-input:focus { outline: 2px solid var(--primary); }
@media (max-width: 900px) { #search-input { width: 92vw; max-width: 97vw; } }
@media (max-width: 600px) {
  .search-container form { flex-direction: column; align-items: stretch; }
  #search-input, #search-btn { width: 100%; margin: 0 0 .5rem 0; min-width: 0;}
  #search-btn { margin-left: 0; }
}
#search-btn {
  margin-left: 0.35rem;
  padding: 0.7rem 1.2rem;
  border: none;
  border-radius: 22px;
  background: var(--primary);
  color: #fff;
  font-size: .99rem;
  cursor: pointer;
  transition: background 0.17s;
}
#search-btn:focus { outline: 2px solid var(--accent);}
#search-btn:hover { background: var(--accent);}
#search-results { width: 96%; max-width: 800px; margin-top: 1.1rem;}
.search-result-item { margin-bottom: 1.2rem;}
.search-result-item a {
  font-size: 1.06rem;
  font-weight: 600;
  color: var(--primary);
  text-decoration: none;
  word-break: break-all;
  transition: color .15s;
}
.search-result-item a:focus { outline: 2px solid var(--primary);}
.search-result-item a:hover { text-decoration: underline;}
.search-result-snippet {
  font-size: .97rem;
  color: var(--fg-light);
  margin-top: 0.21rem;
  word-break: break-word;
  transition: color .15s;
}
body.dark-theme #search-results,
body.dark-theme .search-result-item {
  color: #d4dcfa;
}
body.dark-theme .search-result-item a {
  color: #7ea7fa;
}
body.dark-theme .search-result-snippet {
  color: #b0bad9;
}

/* PAGINAÇÃO */
.pagination { display: flex; justify-content: center; margin: .8rem 0; flex-wrap: wrap;}
.pagination button {
  margin: 0 0.15rem;
  padding: 0.43rem 0.63rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: none;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  font-size: .97em;
}
.pagination button:focus { outline: 2px solid var(--primary);}
.pagination button.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
.pagination button:hover:not(.active) {
  background: var(--accent);
  color: #fff;
}

/* CÓDIGO/COPY */
.code-block-wrap {
  position: relative;
  margin: .7em 0 .7em 0;
  background: #24292e;
  border-radius: 6px;
  overflow: auto;
}
.copy-btn {
  position: absolute;
  top: 7px;
  right: 7px;
  z-index: 2;
  font-size: .99em;
  border: none;
  background: #1b1f232d;
  color: #fff;
  border-radius: 4px;
  padding: .18em .7em;
  cursor: pointer;
  transition: background .16s;
}
.copy-btn:focus { outline: 2px solid #e3eaf7;}
.copy-btn:hover { background: #20bf6b; color: #fff;}
pre { margin: 0; }
.hljs { background: #23272f !important; border-radius: 6px; padding: .6em .7em; }

::-webkit-scrollbar { width: 8px; background: #ebebeb;}
::-webkit-scrollbar-thumb { background: #c7c7c7; border-radius: 7px;}
::-webkit-scrollbar-thumb:hover { background: #b8b8b8;}
@media (max-width: 500px) { .hljs { font-size: .93em; padding: .45em .5em;}}
