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

:root {
  --bg: #1a1510;
  --bg-card: rgba(255,255,255,0.08);
  --bg-card-hover: rgba(255,255,255,0.12);
  --border: rgba(255,255,255,0.12);
  --gold: #d4a843;
  --gold-dim: #a88530;
  --brown: #7c6a4f;
  --brown-light: #9a8a6a;
  --text: #e8e0d4;
  --text-dim: #a89a84;
  --text-muted: #6e6050;
  --danger: #c0392b;
  --danger-dim: #922b20;
  --success: #4a7c59;
  --bubble-own: #7c6a4f;
  --bubble-other: rgba(255,255,255,0.1);
  --input-bg: rgba(255,255,255,0.06);
  --input-border: rgba(255,255,255,0.15);
  --input-focus: #d4a843;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* === Screens === */
.screen {
  height: 100%;
}
.hidden {
  display: none !important;
}

/* === Login === */
#login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 20px;
}

.login-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 40px 32px;
  width: 100%;
  max-width: 360px;
  text-align: center;
}

.login-title {
  font-size: 1.6rem;
  font-weight: 600;
  color: var(--gold);
  margin-bottom: 28px;
  letter-spacing: 1px;
}

#login-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

#login-form input {
  background: var(--input-bg);
  border: 1px solid var(--input-border);
  border-radius: 10px;
  padding: 12px 16px;
  color: var(--text);
  font-size: 0.95rem;
  outline: none;
  transition: border-color 0.2s;
}

#login-form input:focus {
  border-color: var(--input-focus);
}

#login-form input::placeholder {
  color: var(--text-muted);
}

.login-error {
  color: var(--danger);
  font-size: 0.85rem;
  min-height: 1.2em;
}

/* === Buttons === */
.btn {
  border: none;
  border-radius: 10px;
  cursor: pointer;
  font-size: 0.95rem;
  font-weight: 500;
  padding: 10px 20px;
  transition: background 0.2s, opacity 0.2s;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--gold);
  color: #1a1510;
}

.btn-primary:hover:not(:disabled) {
  background: var(--gold-dim);
}

.btn-small {
  font-size: 0.8rem;
  padding: 6px 12px;
  border-radius: 8px;
}

.btn-outline {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
}

.btn-outline:hover:not(:disabled) {
  background: var(--bg-card);
  color: var(--text);
}

.btn-danger {
  border-color: var(--danger-dim);
  color: var(--danger);
}

.btn-danger:hover:not(:disabled) {
  background: rgba(192, 57, 43, 0.15);
}

/* === App Layout === */
#app-screen {
  display: flex;
  flex-direction: column;
  height: 100vh;
  max-width: 600px;
  margin: 0 auto;
}

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

.app-title {
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--gold);
  letter-spacing: 0.5px;
}

.logout-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 6px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  transition: color 0.2s, background 0.2s;
}

.logout-btn:hover {
  color: var(--text);
  background: var(--bg-card);
}

/* === Tab Bar === */
.tab-bar {
  display: flex;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.tab {
  flex: 1;
  background: none;
  border: none;
  padding: 12px 0;
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--text-dim);
  cursor: pointer;
  position: relative;
  transition: color 0.2s;
}

.tab:hover {
  color: var(--text);
}

.tab.active {
  color: var(--gold);
}

.tab.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 20%;
  width: 60%;
  height: 2px;
  background: var(--gold);
  border-radius: 2px 2px 0 0;
}

/* === Tab Panels === */
.tab-panels {
  flex: 1;
  overflow: hidden;
  position: relative;
}

.tab-panel {
  display: none;
  flex-direction: column;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.tab-panel.active {
  display: flex;
}

/* === Grocery Tab === */
.grocery-input-row {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.grocery-input {
  flex: 1;
  background: var(--input-bg);
  border: 1px solid var(--input-border);
  border-radius: 10px;
  padding: 10px 14px;
  color: var(--text);
  font-size: 0.9rem;
  outline: none;
  min-width: 0;
}

.grocery-input:focus {
  border-color: var(--input-focus);
}

.grocery-input::placeholder {
  color: var(--text-muted);
}

.grocery-qty-input {
  width: 56px;
  background: var(--input-bg);
  border: 1px solid var(--input-border);
  border-radius: 10px;
  padding: 10px 8px;
  color: var(--text);
  font-size: 0.9rem;
  text-align: center;
  outline: none;
}

.grocery-qty-input:focus {
  border-color: var(--input-focus);
}

.grocery-qty-input::placeholder {
  color: var(--text-muted);
}

.btn-add {
  padding: 10px 16px;
  white-space: nowrap;
}

.grocery-actions {
  display: flex;
  gap: 8px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.grocery-list {
  list-style: none;
  overflow-y: auto;
  flex: 1;
  padding: 4px 0;
}

.grocery-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  transition: background 0.15s;
}

.grocery-item:hover {
  background: var(--bg-card);
}

.grocery-item.checked {
  opacity: 0.5;
}

.grocery-item.checked .grocery-item-name {
  text-decoration: line-through;
}

.grocery-checkbox {
  width: 22px;
  height: 22px;
  border-radius: 6px;
  border: 2px solid var(--border);
  background: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: border-color 0.2s, background 0.2s;
}

.grocery-checkbox:hover {
  border-color: var(--gold);
}

.grocery-checkbox.checked {
  background: var(--gold);
  border-color: var(--gold);
}

.grocery-checkbox.checked::after {
  content: '';
  width: 10px;
  height: 6px;
  border-left: 2px solid #1a1510;
  border-bottom: 2px solid #1a1510;
  transform: rotate(-45deg) translateY(-1px);
}

.grocery-item-info {
  flex: 1;
  min-width: 0;
}

.grocery-item-name {
  font-size: 0.95rem;
  color: var(--text);
}

.grocery-item-meta {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.grocery-item-qty {
  font-size: 0.8rem;
  color: var(--text-dim);
  background: var(--bg-card);
  padding: 2px 8px;
  border-radius: 6px;
  flex-shrink: 0;
}

.grocery-delete-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px;
  border-radius: 6px;
  font-size: 1.1rem;
  line-height: 1;
  transition: color 0.2s, background 0.2s;
  flex-shrink: 0;
}

.grocery-delete-btn:hover {
  color: var(--danger);
  background: rgba(192, 57, 43, 0.15);
}

.empty-state {
  text-align: center;
  color: var(--text-muted);
  padding: 40px 20px;
  font-size: 0.9rem;
}

/* === Chat Tab === */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.chat-bubble {
  max-width: 80%;
  padding: 10px 14px;
  border-radius: 14px;
  font-size: 0.9rem;
  line-height: 1.45;
  word-wrap: break-word;
  position: relative;
}

.chat-bubble.own {
  background: var(--bubble-own);
  color: #fff;
  align-self: flex-end;
  border-bottom-right-radius: 4px;
}

.chat-bubble.other {
  background: var(--bubble-other);
  color: var(--text);
  align-self: flex-start;
  border-bottom-left-radius: 4px;
}

.chat-sender {
  font-size: 0.75rem;
  font-weight: 600;
  margin-bottom: 3px;
  opacity: 0.8;
}

.chat-text {
  white-space: pre-wrap;
}

.chat-image {
  max-width: 100%;
  max-height: 300px;
  border-radius: 8px;
  margin-top: 6px;
  cursor: pointer;
}

.chat-time {
  font-size: 0.65rem;
  opacity: 0.5;
  margin-top: 4px;
  text-align: right;
}

.chat-input-row {
  display: flex;
  gap: 8px;
  padding: 10px 16px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.chat-input {
  flex: 1;
  background: var(--input-bg);
  border: 1px solid var(--input-border);
  border-radius: 20px;
  padding: 10px 16px;
  color: var(--text);
  font-size: 0.9rem;
  outline: none;
  min-width: 0;
}

.chat-input:focus {
  border-color: var(--input-focus);
}

.chat-input::placeholder {
  color: var(--text-muted);
}

.btn-send {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.load-more-btn {
  align-self: center;
  margin-bottom: 8px;
}

/* === AI Tab === */
.ai-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.ai-project-select {
  flex: 1;
  background: var(--input-bg);
  border: 1px solid var(--input-border);
  border-radius: 10px;
  padding: 8px 12px;
  color: var(--text);
  font-size: 0.85rem;
  outline: none;
  min-width: 0;
}

.ai-project-select:focus {
  border-color: var(--input-focus);
}

.ai-project-select option {
  background: #2a2218;
  color: var(--text);
}

.ai-token-count {
  font-size: 0.75rem;
  color: var(--text-muted);
  white-space: nowrap;
}

.ai-messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.ai-message {
  padding: 12px 16px;
  border-radius: 12px;
  font-size: 0.9rem;
  line-height: 1.55;
  word-wrap: break-word;
}

.ai-message.user {
  background: var(--bubble-own);
  color: #fff;
  align-self: flex-end;
  max-width: 85%;
  border-bottom-right-radius: 4px;
}

.ai-message.assistant {
  background: var(--bubble-other);
  color: var(--text);
  align-self: flex-start;
  max-width: 95%;
  border-bottom-left-radius: 4px;
}

.ai-message.assistant p {
  margin-bottom: 0.5em;
}

.ai-message.assistant p:last-child {
  margin-bottom: 0;
}

.ai-message.assistant pre {
  background: rgba(0,0,0,0.3);
  border-radius: 8px;
  padding: 10px 12px;
  overflow-x: auto;
  margin: 8px 0;
  font-size: 0.82rem;
}

.ai-message.assistant code {
  background: rgba(0,0,0,0.25);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 0.85em;
}

.ai-message.assistant pre code {
  background: none;
  padding: 0;
}

.ai-message.assistant ul, .ai-message.assistant ol {
  margin: 6px 0;
  padding-left: 20px;
}

.ai-message.assistant blockquote {
  border-left: 3px solid var(--gold-dim);
  padding-left: 12px;
  margin: 8px 0;
  color: var(--text-dim);
}

.ai-message.assistant h1,
.ai-message.assistant h2,
.ai-message.assistant h3 {
  margin: 10px 0 4px;
  color: var(--gold);
}

.ai-message.assistant h1 { font-size: 1.15rem; }
.ai-message.assistant h2 { font-size: 1.05rem; }
.ai-message.assistant h3 { font-size: 0.95rem; }

.ai-message .ai-msg-role {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.ai-streaming-cursor::after {
  content: '\25CF';
  animation: blink 1s step-end infinite;
  color: var(--gold);
  margin-left: 2px;
}

@keyframes blink {
  50% { opacity: 0; }
}

.ai-input-row {
  display: flex;
  gap: 8px;
  padding: 10px 16px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.ai-input {
  flex: 1;
  background: var(--input-bg);
  border: 1px solid var(--input-border);
  border-radius: 20px;
  padding: 10px 16px;
  color: var(--text);
  font-size: 0.9rem;
  outline: none;
  min-width: 0;
}

.ai-input:focus {
  border-color: var(--input-focus);
}

.ai-input::placeholder {
  color: var(--text-muted);
}

/* === Scrollbar === */
::-webkit-scrollbar {
  width: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,0.15);
  border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
  background: rgba(255,255,255,0.25);
}

/* === Image lightbox === */
.lightbox-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  cursor: pointer;
}

.lightbox-overlay img {
  max-width: 95%;
  max-height: 95%;
  border-radius: 8px;
}

/* === Confirm dialog === */
.confirm-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 500;
}

.confirm-dialog {
  background: #2a2218;
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 24px;
  max-width: 320px;
  width: 90%;
  text-align: center;
}

.confirm-dialog p {
  margin-bottom: 18px;
  font-size: 0.95rem;
}

.confirm-dialog .confirm-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
}

/* === Responsive === */
@media (max-width: 600px) {
  #app-screen {
    max-width: 100%;
  }

  .login-card {
    padding: 30px 24px;
  }

  .chat-bubble {
    max-width: 88%;
  }

  .ai-message.assistant {
    max-width: 100%;
  }
}
