/* ─── Reset & Variables ─────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:        #0f1117;
  --surface:   #1a1f2e;
  --surface2:  #232a3b;
  --border:    #2d3550;
  --accent:    #4f8ef7;
  --accent-g:  #00c896;
  --warn:      #f5a623;
  --danger:    #e85d5d;
  --text:      #e8eaf0;
  --muted:     #7a84a0;
  --radius:    10px;
  --shadow:    0 4px 24px rgba(0,0,0,.4);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  font-size: 14px;
  line-height: 1.5;
}

/* ─── Layout ────────────────────────────────────────────────────────────── */
.app-wrapper { display: flex; flex-direction: column; min-height: 100vh; }

header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 28px;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky; top: 0; z-index: 100;
}

.logo { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; }
.logo-icon {
  width: 32px; height: 32px; border-radius: 8px;
  background: linear-gradient(135deg, var(--accent), var(--accent-g));
  display: flex; align-items: center; justify-content: center;
  font-size: 17px;
}

.header-right { display: flex; align-items: center; gap: 12px; }

.key-badge {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 10px;
  font-size: 12px;
  color: var(--muted);
  display: flex; align-items: center; gap: 6px;
  cursor: pointer;
  transition: border-color .2s;
}
.key-badge:hover { border-color: var(--accent); }
.key-badge .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--accent-g); }

main {
  flex: 1;
  max-width: 1280px;
  margin: 0 auto;
  width: 100%;
  padding: 28px;
}

/* ─── Setup Screen ──────────────────────────────────────────────────────── */
#setup-screen {
  display: flex; align-items: center; justify-content: center;
  min-height: calc(100vh - 60px);
}

.setup-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 48px;
  width: 460px;
  text-align: center;
  box-shadow: var(--shadow);
}

.setup-card .icon-big {
  font-size: 52px; margin-bottom: 20px;
}

.setup-card h2 { font-size: 24px; margin-bottom: 8px; }
.setup-card p  { color: var(--muted); margin-bottom: 28px; line-height: 1.6; }

/* ─── Stat Cards ────────────────────────────────────────────────────────── */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 24px;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  display: flex; align-items: center; gap: 16px;
}

.stat-icon {
  width: 42px; height: 42px; border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  font-size: 20px; flex-shrink: 0;
}

.stat-icon.blue   { background: rgba(79,142,247,.15); }
.stat-icon.green  { background: rgba(0,200,150,.15); }
.stat-icon.yellow { background: rgba(245,166,35,.15); }
.stat-icon.red    { background: rgba(232,93,93,.15); }

.stat-label { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.stat-value { font-size: 26px; font-weight: 700; }

/* ─── Upload Area ───────────────────────────────────────────────────────── */
.upload-section { margin-bottom: 24px; }

.drop-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 36px;
  text-align: center;
  cursor: pointer;
  transition: border-color .2s, background .2s;
  background: var(--surface);
}

.drop-zone:hover, .drop-zone.dragover {
  border-color: var(--accent);
  background: rgba(79,142,247,.05);
}

.drop-zone .dz-icon { font-size: 38px; margin-bottom: 10px; }
.drop-zone h3 { font-size: 16px; margin-bottom: 6px; }
.drop-zone p  { color: var(--muted); font-size: 13px; }
.drop-zone input[type=file] { display: none; }

/* ─── Contracts Table ───────────────────────────────────────────────────── */
.table-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 14px;
}

.table-header h2 { font-size: 16px; font-weight: 600; }
.table-actions { display: flex; gap: 10px; }

.table-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow-x: auto;   /* horizontal scroll */
  overflow-y: visible;
}

table { width: max-content; min-width: 100%; border-collapse: collapse; }

td.table-cell {
  max-width: 160px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 13px;
}

thead { background: var(--surface2); }
th {
  text-align: left; padding: 12px 16px;
  font-size: 11px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .05em;
  color: var(--muted); border-bottom: 1px solid var(--border);
}

td {
  padding: 13px 16px;
  border-bottom: 1px solid var(--border);
  color: var(--text);
  font-size: 13px;
}

tr:last-child td { border-bottom: none; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: rgba(255,255,255,.03); }

.filename-cell { font-weight: 500; max-width: 220px; }
.filename-cell .fname {
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  max-width: 200px; display: block;
}

.badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 9px; border-radius: 99px;
  font-size: 11px; font-weight: 600;
}

.badge.pending   { background: rgba(122,132,160,.15); color: var(--muted); }
.badge.processing {
  background: rgba(245,166,35,.15); color: var(--warn);
  animation: pulse 1.5s infinite;
}
.badge.completed { background: rgba(0,200,150,.15);   color: var(--accent-g); }
.badge.failed    { background: rgba(232,93,93,.15);   color: var(--danger); }

@keyframes pulse { 0%,100% { opacity:1 } 50% { opacity:.6 } }

.dot-sm {
  width: 6px; height: 6px; border-radius: 50%;
  background: currentColor; flex-shrink: 0;
}

.empty-state {
  text-align: center; padding: 60px 20px; color: var(--muted);
}
.empty-state .es-icon { font-size: 42px; margin-bottom: 12px; }

/* ─── Buttons ───────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 16px; border-radius: 7px; border: none;
  font-size: 13px; font-weight: 500; cursor: pointer;
  transition: opacity .15s, transform .1s;
  text-decoration: none;
}
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .45; cursor: not-allowed; }

.btn-primary   { background: var(--accent); color: #fff; }
.btn-success   { background: var(--accent-g); color: #0f1117; }
.btn-outline   { background: transparent; border: 1px solid var(--border); color: var(--text); }
.btn-outline:hover { border-color: var(--accent); color: var(--accent); }
.btn-danger    { background: var(--danger); color: #fff; }
.btn-sm        { padding: 5px 10px; font-size: 12px; }
.btn-icon      { padding: 7px; }

/* ─── Forms ─────────────────────────────────────────────────────────────── */
.form-group { margin-bottom: 18px; }
label { display: block; margin-bottom: 6px; font-size: 13px; color: var(--muted); }

input, select {
  width: 100%; padding: 10px 12px;
  background: var(--surface2); color: var(--text);
  border: 1px solid var(--border); border-radius: 7px;
  font-size: 14px; outline: none;
  transition: border-color .2s;
}
input:focus, select:focus { border-color: var(--accent); }

/* ─── Modal ─────────────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.65);
  display: flex; align-items: center; justify-content: center;
  z-index: 200;
  opacity: 0; pointer-events: none;
  transition: opacity .2s;
}
.modal-overlay.open { opacity: 1; pointer-events: all; }

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  width: 780px; max-width: 95vw;
  max-height: 88vh;
  overflow-y: auto;
  transform: translateY(16px);
  transition: transform .2s;
  box-shadow: 0 24px 64px rgba(0,0,0,.5);
}
.modal-overlay.open .modal { transform: translateY(0); }

.modal-header {
  padding: 20px 24px 16px;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: flex-start; justify-content: space-between;
  position: sticky; top: 0; background: var(--surface); z-index: 1;
}
.modal-header h2 { font-size: 17px; }
.modal-header .subtitle { font-size: 12px; color: var(--muted); margin-top: 3px; }

.modal-body { padding: 20px 24px 24px; }

.close-btn {
  background: none; border: none; cursor: pointer;
  color: var(--muted); font-size: 22px; padding: 2px 6px;
  border-radius: 4px;
}
.close-btn:hover { color: var(--text); background: var(--surface2); }

/* ─── Analysis Grid ─────────────────────────────────────────────────────── */
.analysis-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 14px;
  margin-top: 12px;
}

.field-card {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px;
}

.field-card.wide { grid-column: span 2; }

.field-label {
  font-size: 10px; font-weight: 700;
  text-transform: uppercase; letter-spacing: .07em;
  color: var(--muted); margin-bottom: 6px;
}

.field-value {
  font-size: 13px; color: var(--text); line-height: 1.5;
  word-break: break-word;
}

.field-value.na { color: var(--muted); font-style: italic; }

.section-title {
  font-size: 11px; font-weight: 700; text-transform: uppercase;
  letter-spacing: .08em; color: var(--muted);
  grid-column: span 2; margin-top: 8px;
  padding-bottom: 6px; border-bottom: 1px solid var(--border);
}

/* ─── Toast ─────────────────────────────────────────────────────────────── */
#toast {
  position: fixed; bottom: 24px; right: 24px;
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 8px; padding: 12px 18px;
  font-size: 13px; max-width: 320px;
  transform: translateY(80px); opacity: 0;
  transition: transform .25s, opacity .25s;
  z-index: 300;
}
#toast.show { transform: translateY(0); opacity: 1; }
#toast.success { border-color: var(--accent-g); color: var(--accent-g); }
#toast.error   { border-color: var(--danger); color: var(--danger); }

/* ─── Misc ──────────────────────────────────────────────────────────────── */
.text-muted { color: var(--muted); }
.text-sm    { font-size: 12px; }
.mt-2       { margin-top: 8px; }

.upload-progress {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 12px 16px;
  margin-bottom: 8px; display: flex; align-items: center; gap: 12px;
}
.progress-bar-wrap {
  flex: 1; background: var(--surface2);
  border-radius: 99px; height: 6px; overflow: hidden;
}
.progress-bar {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--accent-g));
  border-radius: 99px;
  transition: width .3s;
}
#upload-queue { margin-top: 12px; }

/* ─── Role badges ───────────────────────────────────────────────────────── */
.role-badge {
  display: inline-flex; align-items: center;
  padding: 3px 10px; border-radius: 99px;
  font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em;
}
.role-badge.super_admin { background: rgba(79,142,247,.2);   color: #4f8ef7; }
.role-badge.admin        { background: rgba(0,200,150,.2);   color: #00c896; }
.role-badge.user         { background: rgba(122,132,160,.2); color: #7a84a0; }
.role-badge.viewer       { background: rgba(232,93,93,.15);  color: #e85d5d; }

/* ─── Login modal ───────────────────────────────────────────────────────── */
.login-error {
  color: var(--danger);
  font-size: 13px;
  background: rgba(232,93,93,.1);
  border: 1px solid rgba(232,93,93,.3);
  border-radius: 6px;
  padding: 9px 12px;
  margin-bottom: 14px;
}

/* ─── Viewer notice bar ─────────────────────────────────────────────────── */
.viewer-notice {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--warn);
  border-radius: 8px;
  padding: 10px 16px;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 20px;
}
.viewer-notice a { color: var(--accent); text-decoration: none; }
.viewer-notice a:hover { text-decoration: underline; }

/* ─── Info box ──────────────────────────────────────────────────────────── */
.info-box {
  background: var(--surface2); border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: 8px; padding: 14px; font-size: 13px; line-height: 1.7;
}
.info-box code {
  background: var(--surface); padding: 1px 5px; border-radius: 4px;
  font-size: 12px; color: var(--accent-g);
}

/* ─── Admin users table ─────────────────────────────────────────────────── */
.users-table { width: 100%; border-collapse: collapse; margin-top: 4px; }
.users-table th {
  text-align: left; padding: 9px 12px;
  font-size: 11px; font-weight: 700; text-transform: uppercase;
  letter-spacing: .05em; color: var(--muted);
  background: var(--surface2); border-bottom: 1px solid var(--border);
}
.users-table td {
  padding: 10px 12px; border-bottom: 1px solid var(--border); font-size: 13px;
}
.users-table tr:last-child td { border-bottom: none; }

/* ─── Custom Fields Modal ───────────────────────────────────────────────── */
.add-field-box {
  background: var(--surface2);
  border: 1px dashed var(--border);
  border-radius: 8px;
  padding: 16px;
  margin-top: 8px;
}
.add-field-header {
  margin-bottom: 12px;
  display: flex; align-items: center; justify-content: space-between;
}

.new-field-row {
  display: flex; gap: 10px; align-items: flex-start;
  margin-bottom: 10px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border);
}
.new-field-row:last-child { border-bottom: none; margin-bottom: 0; padding-bottom: 0; }

.nfr-inputs { flex: 1; display: flex; flex-direction: column; gap: 8px; }

.nfr-name {
  font-family: monospace; font-size: 13px;
  background: var(--surface); border: 1px solid var(--border);
  color: var(--text); border-radius: 6px; padding: 8px 10px;
}
.nfr-name:focus { border-color: var(--accent); outline: none; }

.nfr-prompt {
  width: 100%; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px;
  padding: 8px 10px; font-size: 13px; resize: vertical;
  font-family: inherit; line-height: 1.4;
}
.nfr-prompt:focus { border-color: var(--accent); outline: none; }

.existing-field-row {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 12px; margin-bottom: 6px;
  background: var(--surface2);
  border: 1px solid var(--border); border-radius: 8px;
  transition: border-color .15s;
}
.existing-field-row:hover { border-color: var(--accent); }

.ef-names { flex: 1; min-width: 0; }
.ef-name {
  font-family: monospace; font-size: 12px; font-weight: 700;
  color: var(--accent); display: block; margin-bottom: 3px;
}
.ef-prompt {
  font-size: 12px; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  display: block; max-width: 480px;
}

/* ─── Chat ──────────────────────────────────────────────────────────────────── */

.chat-fab {
  position: fixed;
  bottom: 28px;
  right: 28px;
  width: 54px;
  height: 54px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  border: none;
  font-size: 22px;
  cursor: pointer;
  box-shadow: 0 4px 20px rgba(0,0,0,.45);
  z-index: 900;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform .2s, background .2s, box-shadow .2s;
}
.chat-fab:hover  { transform: scale(1.08); box-shadow: 0 6px 24px rgba(0,0,0,.55); }
.chat-fab.active { background: var(--surface2); }

.chat-panel {
  position: fixed;
  right: -440px;
  top: 0;
  bottom: 0;
  width: 420px;
  max-width: 100vw;
  background: var(--bg);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 850;
  transition: right .28s cubic-bezier(.4,0,.2,1);
  box-shadow: -6px 0 28px rgba(0,0,0,.35);
}
.chat-panel.open { right: 0; }

.chat-panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}

/* Scope bar */
.chat-scope-bar {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 9px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--surface2);
  flex-wrap: wrap;
  flex-shrink: 0;
}
.chat-scope-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  flex: 1;
}
.scope-pill {
  font-size: 11px;
  padding: 3px 9px;
  border-radius: 20px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--muted);
  cursor: pointer;
  transition: all .15s;
  white-space: nowrap;
  max-width: 150px;
  overflow: hidden;
  text-overflow: ellipsis;
  user-select: none;
}
.scope-pill:hover  { border-color: var(--accent); color: var(--text); }
.scope-pill.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* Messages */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px 14px 6px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.chat-welcome {
  text-align: center;
  padding: 28px 18px;
  color: var(--text);
}
.chat-suggestions {
  display: flex;
  flex-direction: column;
  gap: 7px;
  text-align: left;
}
.chat-suggestions button {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  color: var(--text);
  font-size: 12px;
  cursor: pointer;
  text-align: left;
  transition: background .15s, border-color .15s;
}
.chat-suggestions button:hover { background: var(--surface); border-color: var(--accent); }

.chat-msg         { display: flex; flex-direction: column; }
.chat-msg.user    { align-items: flex-end; }
.chat-msg.assistant,
.chat-msg.error   { align-items: flex-start; }

.chat-bubble {
  max-width: 90%;
  padding: 10px 13px;
  border-radius: 14px;
  font-size: 13px;
  line-height: 1.6;
  word-break: break-word;
}
.chat-msg.user      .chat-bubble { background: var(--accent); color: #fff; border-bottom-right-radius: 4px; }
.chat-msg.assistant .chat-bubble { background: var(--surface2); color: var(--text); border-bottom-left-radius: 4px; }
.chat-msg.error     .chat-bubble { background: rgba(232,93,93,.12); color: var(--danger); border: 1px solid rgba(232,93,93,.3); }

/* Typing indicator */
.chat-bubble.typing {
  display: flex; gap: 5px; align-items: center;
  padding: 14px 18px;
}
.chat-bubble.typing span {
  width: 7px; height: 7px;
  background: var(--muted);
  border-radius: 50%;
  animation: typingDot .9s infinite ease-in-out;
}
.chat-bubble.typing span:nth-child(2) { animation-delay: .2s; }
.chat-bubble.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes typingDot {
  0%, 80%, 100% { transform: scale(.8); opacity: .5; }
  40%           { transform: scale(1.1); opacity: 1; }
}

/* Markdown formatting inside chat bubbles */
.chat-bubble strong { font-weight: 700; }
.chat-bubble em     { font-style: italic; }
.chat-bubble b      { font-weight: 700; }
.chat-bubble code {
  background: rgba(0,0,0,.18);
  padding: 1px 5px;
  border-radius: 3px;
  font-family: monospace;
  font-size: 12px;
}
.chat-msg.user .chat-bubble code { background: rgba(255,255,255,.2); }
.chat-li {
  padding-left: 14px;
  position: relative;
  margin: 2px 0;
}
.chat-li::before { content: "•"; position: absolute; left: 2px; color: var(--accent); }
.chat-msg.user .chat-li::before { color: rgba(255,255,255,.75); }

/* Input area */
.chat-input-area {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}
.chat-input-area textarea {
  flex: 1;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  padding: 9px 12px;
  font-size: 13px;
  resize: none;
  font-family: inherit;
  line-height: 1.5;
  outline: none;
  transition: border-color .15s;
}
.chat-input-area textarea:focus       { border-color: var(--accent); }
.chat-input-area textarea::placeholder { color: var(--muted); }
#chat-send-btn {
  background: var(--accent);
  border: none;
  border-radius: 10px;
  color: #fff;
  width: 42px;
  font-size: 17px;
  cursor: pointer;
  flex-shrink: 0;
  transition: background .15s, opacity .15s;
}
#chat-send-btn:hover    { background: #5b7ff0; }
#chat-send-btn:disabled { opacity: .4; cursor: not-allowed; }

@media (max-width: 900px) {
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .analysis-grid { grid-template-columns: 1fr; }
  .analysis-grid .field-card.wide { grid-column: span 1; }
  .analysis-grid .section-title { grid-column: span 1; }
  .chat-panel { width: 100vw; }
}
@media (max-width: 600px) {
  main { padding: 16px; }
  .stats-grid { grid-template-columns: 1fr 1fr; }
  header { padding: 0 16px; }
}
