/* ── Reset & base ───────────────────────────────────────────────────────── */

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg:         #0f1117;
  --surface:    #1a1d27;
  --surface-2:  #22263a;
  --border:     #2e3347;
  --accent:     #5b6af0;
  --accent-h:   #4a59e0;
  --danger:     #e05b5b;
  --text:       #e4e6f0;
  --text-muted: #7a7f9a;
  --green:      #4caf7d;
  --radius:     8px;
  --shadow:     0 2px 12px rgba(0,0,0,.45);
  font-size: 15px;
}

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

a { color: var(--accent); }

/* ── Login page ─────────────────────────────────────────────────────────── */

.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 1rem;
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 2.5rem 2rem;
  width: 100%;
  max-width: 360px;
}

.login-card h1 {
  font-size: 1.4rem;
  font-weight: 600;
  margin-bottom: 1.75rem;
  color: var(--text);
}

.field {
  display: flex;
  flex-direction: column;
  gap: .35rem;
  margin-bottom: 1.1rem;
}

.field label {
  font-size: .8rem;
  font-weight: 500;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .04em;
}

input[type="text"],
input[type="password"],
input[type="number"],
.add-field {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: .95rem;
  padding: .55rem .75rem;
  outline: none;
  transition: border-color .15s;
  width: 100%;
}

input[type="text"]:focus,
input[type="password"]:focus,
input[type="number"]:focus,
.add-field:focus {
  border-color: var(--accent);
}

.error-msg {
  color: var(--danger);
  font-size: .85rem;
  min-height: 1.2em;
  margin-bottom: .5rem;
}

/* ── Buttons ────────────────────────────────────────────────────────────── */

button {
  cursor: pointer;
  font-family: inherit;
  font-size: .9rem;
  border: none;
  border-radius: var(--radius);
  transition: background .15s, opacity .15s;
}

button:disabled {
  opacity: .45;
  cursor: not-allowed;
}

#login-btn,
.btn-primary {
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  padding: .6rem 1.2rem;
}

#login-btn {
  width: 100%;
  margin-top: .25rem;
  padding: .7rem;
}

#login-btn:hover:not(:disabled),
.btn-primary:hover:not(:disabled) {
  background: var(--accent-h);
}

.btn-ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
  padding: .6rem 1rem;
}

.btn-ghost:hover:not(:disabled) {
  border-color: var(--text-muted);
  color: var(--text);
}

.btn-danger {
  background: transparent;
  border: none;
  color: var(--danger);
  padding: .3rem .6rem;
  font-size: .8rem;
  border-radius: 4px;
}

.btn-danger:hover { background: rgba(224,91,91,.12); }

/* ── App shell ──────────────────────────────────────────────────────────── */

.app-page {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

.app-header {
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}

.app-nav {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0 1.25rem;
  height: 52px;
}

.app-brand {
  font-weight: 700;
  font-size: 1rem;
  color: var(--text);
  margin-right: .5rem;
  white-space: nowrap;
}

.nav-links {
  display: flex;
  gap: .25rem;
}

.nav-btn {
  background: transparent;
  color: var(--text-muted);
  border: none;
  padding: .4rem .9rem;
  border-radius: var(--radius);
  font-size: .9rem;
  font-weight: 500;
}

.nav-btn:hover { background: var(--surface-2); color: var(--text); }
.nav-btn.active { background: var(--surface-2); color: var(--text); }

.nav-right {
  display: flex;
  align-items: center;
  gap: .75rem;
  margin-left: auto;
}

.nav-user {
  font-size: .85rem;
  color: var(--text-muted);
}

.logout-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
  font-size: .8rem;
  padding: .3rem .75rem;
  border-radius: var(--radius);
}

.logout-btn:hover { border-color: var(--text-muted); color: var(--text); }

.app-main {
  flex: 1;
  overflow: hidden;
  position: relative;
}

.section {
  position: absolute;
  inset: 0;
  overflow-y: auto;
  padding: 1.25rem;
}

.section.hidden { display: none; }

/* ── Encryption warning ─────────────────────────────────────────────────── */

.encryption-warning {
  background: rgba(224,91,91,.12);
  border: 1px solid var(--danger);
  border-radius: var(--radius);
  color: var(--danger);
  padding: .75rem 1rem;
  margin-bottom: 1rem;
  font-size: .88rem;
  line-height: 1.5;
}

.encryption-warning code {
  background: rgba(255,255,255,.07);
  border-radius: 3px;
  padding: .1em .35em;
  font-size: .85em;
  word-break: break-all;
}

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

#chat-section {
  display: flex;
  flex-direction: column;
  padding: 0;
}

.chat-layout {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 1rem 1.25rem 1.25rem;
  gap: .75rem;
}

.chat-status-bar {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: .5rem;
}

.presence-indicator {
  font-size: .82rem;
  color: var(--text-muted);
}

.presence-indicator.online::before {
  content: '';
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--green);
  margin-right: .4rem;
  vertical-align: middle;
}

.message-list {
  flex: 1;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: .6rem;
  scroll-behavior: smooth;
}

.chat-hint {
  font-size: .8rem;
  color: var(--text-muted);
  text-align: center;
  margin: auto;
}

.message-bubble {
  max-width: 72%;
  padding: .55rem .85rem;
  border-radius: 12px;
  font-size: .92rem;
  line-height: 1.5;
  word-break: break-word;
  white-space: pre-wrap;
}

.message-bubble.mine {
  background: var(--accent);
  color: #fff;
  align-self: flex-end;
  border-bottom-right-radius: 3px;
}

.message-bubble.theirs {
  background: var(--surface-2);
  color: var(--text);
  align-self: flex-start;
  border-bottom-left-radius: 3px;
}

.message-bubble .msg-meta {
  display: block;
  font-size: .72rem;
  opacity: .65;
  margin-top: .25rem;
}

.message-bubble.mine .msg-meta { text-align: right; }

.chat-form {
  display: flex;
  gap: .6rem;
  align-items: flex-end;
  flex-shrink: 0;
}

.chat-input {
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: inherit;
  font-size: .95rem;
  padding: .65rem .85rem;
  resize: none;
  outline: none;
  max-height: 140px;
  overflow-y: auto;
  transition: border-color .15s;
  line-height: 1.45;
}

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

.send-btn {
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  padding: .65rem 1.25rem;
  flex-shrink: 0;
  align-self: flex-end;
}

.send-btn:hover:not(:disabled) { background: var(--accent-h); }

/* ── Inventory ──────────────────────────────────────────────────────────── */

.inventory-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
}

.section-title {
  font-size: 1.05rem;
  font-weight: 600;
}

.add-item-form {
  display: flex;
  flex-wrap: wrap;
  gap: .6rem;
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .85rem 1rem;
  margin-bottom: 1rem;
}

.add-item-form .add-field {
  width: auto;
  flex: 1;
  min-width: 110px;
}

.table-wrap {
  overflow-x: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.inventory-table {
  width: 100%;
  border-collapse: collapse;
  font-size: .9rem;
}

.inventory-table th {
  background: var(--surface-2);
  color: var(--text-muted);
  font-size: .75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .05em;
  padding: .65rem 1rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.inventory-table td {
  padding: .6rem 1rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

.inventory-table tbody tr:last-child td {
  border-bottom: none;
}

.inventory-table tbody tr:hover td {
  background: rgba(255,255,255,.02);
}

/* Inline editable cells */
.editable {
  outline: none;
  border-radius: 4px;
  padding: .2rem .35rem;
  min-width: 60px;
  cursor: text;
  transition: background .12s;
}

.editable:hover { background: var(--surface-2); }
.editable:focus { background: var(--bg); box-shadow: 0 0 0 2px var(--accent); }

/* Stock controls */
.stock-cell {
  display: flex;
  align-items: center;
  gap: .4rem;
  white-space: nowrap;
}

.stock-btn {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  width: 24px;
  height: 24px;
  border-radius: 4px;
  font-size: .95rem;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.stock-btn:hover:not(:disabled) { background: var(--surface); border-color: var(--accent); }

.stock-value {
  min-width: 2.5rem;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

/* Summary row */
.summary-row td {
  background: var(--surface-2);
  border-top: 2px solid var(--border);
  border-bottom: none;
  font-size: .88rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.summary-row td:first-child {
  color: var(--text);
}

.inventory-empty {
  padding: 2rem;
  text-align: center;
  color: var(--text-muted);
  font-size: .9rem;
}

/* ── Utilities ──────────────────────────────────────────────────────────── */

.hidden { display: none !important; }

/* Scrollbar styling */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ── Mobile ─────────────────────────────────────────────────────────────── */

@media (max-width: 640px) {

  /* Use dvh so the app shell doesn't extend behind the mobile URL bar */
  .app-page {
    height: 100dvh;
    height: 100vh; /* fallback for older browsers */
  }

  /* ── Nav ── */

  .app-nav {
    height: 48px;
    padding: 0 .75rem;
    gap: .4rem;
  }

  .app-brand { display: none; }

  .nav-btn {
    padding: .4rem .75rem;
    font-size: .85rem;
  }

  .nav-user { display: none; }

  .logout-btn {
    font-size: .75rem;
    padding: .3rem .6rem;
  }

  /* ── Section padding ── */

  .section { padding: .85rem; }

  /* ── Encryption warning ── */

  .encryption-warning {
    font-size: .82rem;
    padding: .65rem .85rem;
  }

  /* ── Chat ── */

  .chat-layout {
    padding: .75rem;
    gap: .5rem;
  }

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

  /* Prevent iOS from zooming in on textarea focus */
  .chat-input { font-size: 16px; }

  /* ── Inputs — 16px minimum prevents iOS auto-zoom ── */

  input[type="text"],
  input[type="password"],
  input[type="number"],
  .add-field {
    font-size: 16px;
    padding: .65rem .75rem;
  }

  /* ── Stock buttons — bigger touch targets ── */

  .stock-btn {
    width: 36px;
    height: 36px;
    font-size: 1.1rem;
    border-radius: 6px;
  }

  /* ── Add item form — stack fields vertically ── */

  .add-item-form {
    flex-direction: column;
    gap: .5rem;
  }

  .add-item-form .add-field {
    width: 100%;
    flex: none;
    min-width: unset;
  }

  .add-item-form .btn-primary,
  .add-item-form .btn-ghost {
    width: 100%;
    padding: .7rem;
    font-size: .95rem;
    text-align: center;
  }

  /* ── Inventory — card layout ── */

  /* Hide the standard column headers */
  .inventory-table thead { display: none; }

  /* Each row becomes a self-contained card */
  .inventory-table,
  .inventory-table tbody,
  .inventory-table tbody tr {
    display: block;
    width: 100%;
  }

  .inventory-table tbody tr {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: .65rem;
    padding: .65rem .85rem;
  }

  .inventory-table tbody tr:hover td { background: transparent; }

  .inventory-table tbody td {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: .35rem 0;
    border-bottom: 1px solid var(--border);
  }

  .inventory-table tbody td:last-child { border-bottom: none; }

  /* Label from data-label attribute */
  .inventory-table tbody td::before {
    content: attr(data-label);
    font-size: .73rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: .05em;
    flex-shrink: 0;
    margin-right: .75rem;
  }

  /* Name cell: label on its own line, value below */
  .inventory-table tbody td[data-label="Name"] {
    flex-direction: column;
    align-items: flex-start;
    gap: .2rem;
  }

  .inventory-table tbody td[data-label="Name"]::before {
    margin-right: 0;
  }

  /* Delete button cell: no label, right-aligned */
  .inventory-table tbody td[data-label=""] {
    justify-content: flex-end;
    padding-top: .45rem;
  }

  .inventory-table tbody td[data-label=""]::before {
    display: none;
  }

  .btn-danger {
    padding: .4rem .8rem;
    font-size: .82rem;
  }

  /* Editable cells — full width on mobile */
  .editable { min-width: unset; }

  /* Summary row — show as a compact card below the table */
  .inventory-table tfoot { display: block; }

  .inventory-table tfoot tr {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: .5rem;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: .75rem .85rem;
  }

  .inventory-table tfoot td {
    display: flex;
    flex-direction: column;
    border-bottom: none;
    padding: 0;
    font-size: .8rem;
  }

  .inventory-table tfoot td:first-child {
    grid-column: 1 / -1;
    font-size: .85rem;
    padding-bottom: .35rem;
    border-bottom: 1px solid var(--border);
    margin-bottom: .1rem;
  }

  .inventory-table tfoot td:last-child { display: none; }

  /* tfoot value label */
  .inventory-table tfoot td[id]::before {
    content: attr(data-label);
    font-size: .7rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: .05em;
    margin-bottom: .15rem;
  }
}

/* Very small screens */
@media (max-width: 360px) {
  .nav-btn { padding: .35rem .55rem; font-size: .8rem; }
  .message-bubble { max-width: 94%; font-size: .88rem; }
}
