@import url('https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;600&display=swap');

:root {
  --bg-color: #0f172a;
  --text-color: #f8fafc;
  --primary-gradient: linear-gradient(135deg, #6366f1 0%, #a855f7 100%);
  --glass-bg: rgba(255, 255, 255, 0.05);
  --glass-border: rgba(255, 255, 255, 0.1);
  --glass-highlight: rgba(255, 255, 255, 0.15);
  --danger-color: #ef4444;
  --message-out-bg: #4f46e5;
  --message-in-bg: #334155;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: 'Outfit', 'Rubik', sans-serif;
  background-color: var(--bg-color);
  color: var(--text-color);
  height: 100vh;
  /* Fallback */
  height: 100dvh;
  /* Mobile Fix */
  position: relative;
  overflow: hidden;
  /* App-like feel */
}

/* Background Ambient Orbs */
body::before,
body::after {
  content: '';
  position: fixed;
  width: 300px;
  height: 300px;
  border-radius: 50%;
  filter: blur(80px);
  z-index: -1;
  opacity: 0.5;
}

body::before {
  background: #6366f1;
  top: -100px;
  left: -100px;
}

body::after {
  background: #a855f7;
  bottom: -100px;
  right: -100px;
}

#app {
  max-width: 600px;
  margin: 0 auto;
  height: 100%;
  display: flex;
  flex-direction: column;
}

/* RTL Support */
body.rtl {
  direction: rtl;
  font-family: 'Rubik', sans-serif;
  text-align: right;
}

body.rtl header {
  flex-direction: row-reverse;
}

body.rtl .user-tag {
  margin-left: 0;
  margin-right: 8px;
}

body.rtl .message p {
  margin: 0;
  line-height: 1.4;
  white-space: pre-wrap;
  /* Preserve newlines */
}

body.rtl .message.sent {
  border-bottom-right-radius: 16px;
  border-bottom-left-radius: 2px;
}

body.rtl .message.received {
  border-bottom-left-radius: 16px;
  border-bottom-right-radius: 2px;
}

body.rtl .form-control {
  text-align: right;
}

body.rtl .modal-actions {
  justify-content: flex-start;
}

body.rtl .chat-input-wrapper {
  flex-direction: row-reverse;
}

body.rtl .fab {
  right: auto;
  left: 24px;
}

/* Header */
header {
  padding: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--glass-border);
  background: rgba(15, 23, 42, 0.8);
  backdrop-filter: blur(10px);
  z-index: 10;
}

h1 {
  font-weight: 600;
  font-size: 1.5rem;
  background: var(--primary-gradient);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  flex: 1;
}

/* Views */
.view {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  scroll-behavior: smooth;
  display: none;
  /* Hidden by default */
}

.view.active {
  display: block;
  animation: fadeIn 0.3s ease;
}

/* Chat Layout Fix */
#chatView.active {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  /* Internal scrolling only */
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Project List Styles */
.card {
  background: var(--glass-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--glass-border);
  border-radius: 16px;
  padding: 20px;
  margin-bottom: 16px;
  transition: transform 0.2s, background 0.2s;
  cursor: pointer;
  z-index: 10;
  position: relative;
  /* Ensure z-index applies */
}

.card:hover {
  background: var(--glass-highlight);
  transform: translateY(-2px);
}

.status-badge {
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 600;
  background: rgba(99, 102, 241, 0.2);
  color: #818cf8;
  text-transform: uppercase;
}

.edit-btn {
  background: none;
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: #fff;
  padding: 4px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.8rem;
  margin-right: 8px;
}

.edit-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

.delete-btn {
  background: none;
  border: none;
  color: #64748b;
  cursor: pointer;
  padding: 8px;
  z-index: 2;
  /* Ensure clickable */
}

.delete-btn:hover {
  color: var(--danger-color);
}

/* Chat View Styles */
.chat-container {
  display: flex;
  flex-direction: column;
  height: 100%;
  /* Fill view */
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding-bottom: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.message {
  max-width: 80%;
  padding: 12px 16px;
  border-radius: 16px;
  font-size: 0.95rem;
  line-height: 1.5;
  position: relative;
  word-wrap: break-word;
  animation: popIn 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.message p {
  margin: 0;
  white-space: pre-wrap;
}

@keyframes popIn {
  from {
    transform: scale(0.8);
    opacity: 0;
  }

  to {
    transform: scale(1);
    opacity: 1;
  }
}

.message.sent {
  align-self: flex-end;
  background: var(--message-out-bg);
  color: white;
  border-bottom-right-radius: 2px;
}

.message.received {
  /* Placeholder for future multi-user */
  align-self: flex-start;
  background: var(--message-in-bg);
  color: #e2e8f0;
  border-bottom-left-radius: 2px;
}

.message img {
  max-width: 100%;
  border-radius: 8px;
  margin-top: 8px;
  display: block;
}

.message .file-attachment {
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(0, 0, 0, 0.2);
  padding: 8px;
  border-radius: 8px;
  margin-top: 5px;
  color: white;
  text-decoration: none;
}

.timestamp {
  font-size: 0.7rem;
  opacity: 0.7;
  margin-top: 4px;
  display: block;
  text-align: right;
}

/* Chat Input Area */
.chat-input-area {
  margin-top: auto;
  padding: 16px 0 0 0;
  /* sticky bottom inside view */
  background: transparent;
  display: flex;
  gap: 10px;
  align-items: flex-end;
}

.chat-input-wrapper {
  flex: 1;
  background: rgba(255, 255, 255, 0.08);
  border-radius: 24px;
  padding: 8px 16px;
  border: 1px solid var(--glass-border);
  display: flex;
  align-items: center;
  gap: 8px;
}

.chat-input {
  flex: 1;
  background: transparent;
  border: none;
  color: white;
  padding: 8px 0;
  font-family: inherit;
  font-size: 1rem;
  max-height: 100px;
  resize: none;
  /* Auto-expanding logic can be added */
}

.chat-input:focus {
  outline: none;
}

.attach-btn {
  background: none;
  border: none;
  color: #94a3b8;
  cursor: pointer;
  padding: 8px;
  transition: color 0.2s;
}

.attach-btn:hover {
  color: white;
}

.send-btn {
  background: var(--primary-gradient);
  border: none;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.4);
  transition: transform 0.2s;
}

.send-btn:active {
  transform: scale(0.95);
}

/* Common UI */
.fab {
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: var(--primary-gradient);
  border: none;
  color: white;
  font-size: 2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 10px 25px -5px rgba(99, 102, 241, 0.4);
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
  z-index: 100;
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: #64748b;
}

.hidden {
  display: none !important;
}

/* Modals */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  opacity: 0;
  visibility: hidden;
  /* Added for safety */
  transition: opacity 0.3s, visibility 0.3s;
}

.modal-overlay.active {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

.modal {
  background: #1e293b;
  border: 1px solid var(--glass-border);
  border-radius: 24px;
  padding: 30px;
  width: 90%;
  max-width: 400px;
  transform: translateY(20px);
  transition: transform 0.3s;
}

.modal-overlay.active .modal {
  transform: translateY(0);
}

.modal h2 {
  margin-bottom: 20px;
  font-weight: 600;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-size: 0.9rem;
  color: #94a3b8;
}

.form-control {
  width: 100%;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--glass-border);
  border-radius: 12px;
  padding: 12px;
  color: var(--text-color);
  font-size: 1rem;
  font-family: inherit;
  transition: border-color 0.2s;
}

.form-control:focus {
  outline: none;
  border-color: #a855f7;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 30px;
}

.btn {
  padding: 12px 24px;
  border-radius: 12px;
  font-weight: 600;
  border: none;
  cursor: pointer;
}

.btn-secondary {
  background: transparent;
  color: #94a3b8;
}

.btn-primary {
  background: var(--primary-gradient);
  color: white;
}

/* Login & User Management */
.login-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  height: 100vh;
  padding: 40px;
  text-align: center;
}

.login-card {
  background: var(--glass-bg);
  backdrop-filter: blur(20px);
  border: 1px solid var(--glass-border);
  padding: 40px;
  border-radius: 24px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
}

.user-tag {
  display: inline-block;
  font-size: 0.7rem;
  padding: 2px 8px;
  border-radius: 12px;
  margin-left: 8px;
  background: rgba(255, 255, 255, 0.1);
}

.role-admin {
  color: #f472b6;
  border: 1px solid #f472b6;
}

.role-inspector {
  color: #38bdf8;
  border: 1px solid #38bdf8;
}

.role-contractor {
  color: #bef264;
  border: 1px solid #bef264;
}

.logout-btn {
  background: none;
  border: 1px solid var(--glass-border);
  color: #94a3b8;
  padding: 6px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.8rem;
}

.logout-btn:hover {
  color: white;
  border-color: white;
}

/* Print & Export Styles */
.print-only {
  display: none;
}

@media print {

  /* Hides */
  .fab,
  .chat-input-area,
  #headerActions,
  #backBtn,
  #fab,
  button {
    display: none !important;
  }

  /* Reset layout */
  body,
  #app,
  .chat-container,
  .view {
    position: static;
    height: auto;
    overflow: visible;
    background: white;
    color: black;
  }

  body::before,
  body::after {
    display: none;
  }

  /* Show only active chat */
  #listView {
    display: none;
  }

  #chatView {
    display: block;
  }

  header {
    background: white;
    border-bottom: 2px solid #000;
    padding: 0 0 20px 0;
    margin-bottom: 20px;
    position: static;
  }

  h1 {
    background: none;
    -webkit-text-fill-color: black;
    color: black;
    font-size: 24pt;
  }

  /* Print Header Details */
  .print-only {
    display: block;
    margin-bottom: 30px;
    padding: 15px;
    border: 1px solid #ddd;
    border-radius: 8px;
    font-family: sans-serif;
  }

  .print-row {
    display: flex;
    justify-content: space-between;
    margin-bottom: 8px;
    font-size: 12pt;
  }

  .print-label {
    font-weight: bold;
    color: #555;
  }

  .print-val {
    font-family: monospace;
  }

  /* Chat Messages for Print */
  .chat-messages {
    display: block;
  }

  .message {
    max-width: 100%;
    border: 1px solid #eee;
    margin-bottom: 10px;
    padding: 10px;
    background: none !important;
    color: black !important;
    box-shadow: none;
    page-break-inside: avoid;
  }

  .message.sent,
  .message.received {
    align-self: flex-start;
    /* Left align all */
    border-radius: 4px;
    color: black !important;
  }

  .message img {
    max-width: 300px;
    /* Limit image size on paper */
    height: auto;
    display: block;
    margin: 10px 0;
    page-break-inside: avoid;
  }

  .timestamp {
    color: #666;
    text-align: left;
  }

  /* Chat Messages for Print */
  .chat-messages {
    overflow: visible;
    display: block;
    padding: 0;
  }

  .message {
    break-inside: avoid;
    background: white !important;
    border: 1px solid #ccc;
    color: black !important;
    padding: 10px;
    margin-bottom: 15px;
    border-radius: 8px !important;
    max-width: 100%;
  }

  .message.sent,
  .message.received {
    align-self: flex-start;
  }

  .timestamp {
    color: #666;
    font-size: 9pt;
  }

  .message img {
    max-height: 300px;
    border: 1px solid #eee;
  }

  .file-attachment {
    display: flex !important;
    text-decoration: none !important;
    color: #444 !important;
    border: 1px dashed #ccc;
    background: #fafafa;
    pointer-events: none;
    cursor: default;
  }

  /* Hide the download icon for cleaner look? Optional, but let's keep it simple */
  .file-attachment svg {
    display: none;
  }

  .file-attachment span::before {
    content: "(Attachment) ";
    font-weight: bold;
    color: #666;
  }
}