:root {
  color-scheme: light;
  --bg: #eef3f8;
  --panel: #ffffff;
  --line: #d7e1ee;
  --text: #132033;
  --muted: #66758a;
  --blue: #2563eb;
  --blue-dark: #1d4ed8;
  --blue-soft: #e8f1ff;
  --green: #15803d;
  --green-soft: #eaf7ef;
  --danger: #b91c1c;
  --shadow: 0 16px 45px rgba(15, 23, 42, .08);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Segoe UI", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

button, input, textarea, select { font: inherit; }

.hidden { display: none !important; }

.app {
  min-height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 12px;
  padding: 12px;
}

.topbar {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: var(--shadow);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 14px;
  padding: 12px 14px;
}

.brand { font-size: 18px; font-weight: 650; letter-spacing: .01em; }
.subtitle { color: var(--muted); font-size: 13px; }
.session { color: #42526a; font-size: 16px; font-weight: 500; }

.top-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.btn {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px 13px;
  background: #fff;
  color: var(--text);
  font-weight: 600;
  cursor: pointer;
}

.btn.primary {
  background: var(--blue);
  border-color: var(--blue);
  color: #fff;
}

.btn.primary:hover { background: var(--blue-dark); }
.btn.ghost { background: #fff; }
.btn:disabled { opacity: .6; cursor: not-allowed; }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: var(--shadow);
}

.login-card {
  width: min(520px, calc(100vw - 24px));
  margin: 42px auto;
  padding: 20px;
  display: grid;
  gap: 14px;
}

.login-card h1 { margin: 0; font-size: 22px; }

.password-card {
  margin: 0 0 12px;
  padding: 14px;
  display: grid;
  gap: 10px;
}

.password-card h1 {
  margin: 0;
  font-size: 18px;
}

.password-grid {
  display: grid;
  grid-template-columns: minmax(180px, 1fr) minmax(180px, 1fr) auto;
  align-items: end;
  gap: 10px;
}

label {
  display: grid;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
}

input, textarea, select {
  width: 100%;
  border: 1px solid #c8d5e6;
  border-radius: 8px;
  padding: 10px 11px;
  background: #fff;
  color: #0f172a;
}

textarea {
  min-height: 120px;
  resize: vertical;
  line-height: 1.45;
}

.message {
  min-height: 18px;
  color: var(--danger);
  font-size: 13px;
}

.workspace {
  height: calc(100vh - 98px);
  min-height: 0;
  display: grid;
  grid-template-columns: 500px minmax(760px, 1fr);
  gap: 12px;
}

.list-panel, .details-panel {
  min-height: 0;
  overflow: auto;
}

.list-head {
  position: sticky;
  top: 0;
  z-index: 2;
  background: #fbfdff;
  border-bottom: 1px solid var(--line);
  padding: 14px 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
  letter-spacing: .01em;
}

.section-toggle {
  width: 100%;
  border: 0;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  text-align: left;
  cursor: pointer;
  color: inherit;
}

.section-toggle .chevron {
  display: inline-flex;
  margin-left: 6px;
  color: var(--muted);
  font-size: 13px;
}

.section-toggle.open .chevron {
  transform: rotate(90deg);
}

.owner-notice-panel {
  border-bottom: 1px solid var(--line);
  background: #fff7ed;
}

.notice-head {
  position: sticky;
  top: 0;
  background: #fffbeb;
  color: #92400e;
}

.notice-pill {
  background: #fde68a;
  color: #92400e;
}

.ready-head {
  position: sticky;
  top: 0;
  background: #ecfdf5;
  color: #047857;
}

.ready-pill {
  background: #bbf7d0;
  color: #047857;
}

.task.owner-notice {
  background: #fffbeb;
}

.task.owner-notice.active {
  background: #fef3c7;
  box-shadow: inset 3px 0 0 #f59e0b;
}

.pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 26px;
  height: 24px;
  padding: 0 8px;
  border-radius: 999px;
  background: var(--blue-soft);
  color: var(--blue-dark);
  font-weight: 600;
  font-size: 12px;
}

.task {
  border-bottom: 1px solid #e4ebf5;
  padding: 15px 16px;
  cursor: pointer;
  background: #fff;
  transition: background .12s ease, box-shadow .12s ease;
}

.task:hover {
  background: #f8fafc;
}

.task.active {
  background: #f8fbff;
  box-shadow: inset 3px 0 0 var(--blue);
}

.task.accounting-hold {
  background: #fff8df;
}

.task.accounting-hold:hover {
  background: #fff4c4;
}

.task.accounting-hold.active {
  background: #fff0b8;
  box-shadow: inset 3px 0 0 #f59e0b;
}

.task.accounting-ready {
  background: #f0fdf4;
}

.task.accounting-ready:hover {
  background: #dcfce7;
}

.task.accounting-ready.active {
  background: #d1fae5;
  box-shadow: inset 3px 0 0 #16a34a;
}

.task.answered { color: #475569; }

.task-title {
  font-weight: 600;
  font-size: 15px;
  line-height: 1.3;
  letter-spacing: .005em;
  margin-bottom: 4px;
}

.task-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: end;
  margin-top: 10px;
  font-size: 14px;
  color: #334155;
}

.muted { color: var(--muted); font-size: 12px; }
.amount { color: #047857; font-weight: 600; white-space: nowrap; }
.task-row strong { font-weight: 600; }

.status {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 2px 8px;
  font-size: 11px;
  font-weight: 600;
  background: #eef4ff;
  color: #1d4ed8;
}

.status.answered, .status.closed {
  background: var(--green-soft);
  color: var(--green);
}

.status.on_hold {
  background: #fef3c7;
  color: #92400e;
}

.status.to_accounting {
  background: #e0f2fe;
  color: #0369a1;
}

.empty {
  height: 100%;
  display: grid;
  place-items: center;
  text-align: center;
  color: var(--muted);
  padding: 24px;
}

.details-head {
  border-bottom: 1px solid var(--line);
  padding: 10px 14px;
  display: grid;
  gap: 6px;
  background: #fbfdff;
}

.details-title {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  align-items: flex-start;
}

.details-title strong { font-size: 17px; }

.mobile-detail-bar {
  display: none;
}

.invoice-meta-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 5px 16px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
}

.invoice-meta-strip span {
  min-width: 0;
  max-width: 360px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.invoice-meta-strip b {
  color: #64748b;
  font-weight: 600;
}

.invoice-meta-strip .overdue-meta,
.invoice-meta-strip .overdue-meta b {
  color: var(--danger);
}

.preview {
  background: #e8eef6;
  padding: 12px;
  overflow: visible;
  display: flex;
  justify-content: center;
}

.invoice-embed {
  width: min(100%, 1180px);
  min-height: 420px;
  border: 1px solid #c9d5e5;
  border-radius: 8px;
  background: #fff;
  overflow: hidden;
}

.invoice-frame {
  display: block;
  width: 100%;
  min-height: 280px;
  border: 0;
  background: #fff;
}

.preview-fallback {
  border: 1px solid #c9d5e5;
  border-radius: 8px;
  background: #fff;
  padding: 0;
  overflow: hidden;
  font-family: inherit;
  font-size: 14px;
  white-space: normal;
}

.fallback-invoice {
  display: grid;
  gap: 18px;
  padding: 22px;
  color: var(--text);
}

.fallback-invoice-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--line);
}

.fallback-invoice h2 {
  margin: 3px 0 4px;
  font-size: clamp(22px, 3vw, 34px);
  line-height: 1.05;
}

.fallback-label {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.fallback-total {
  min-width: 190px;
  text-align: right;
  border-radius: 16px;
  background: #eef7ff;
  padding: 14px 16px;
}

.fallback-total span,
.fallback-summary span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
}

.fallback-total strong {
  display: block;
  margin-top: 4px;
  color: #007a5a;
  font-size: clamp(22px, 3vw, 36px);
  line-height: 1.05;
}

.fallback-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.fallback-box {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #fbfdff;
  padding: 14px;
}

.fallback-box strong {
  display: block;
  margin-top: 5px;
  overflow-wrap: anywhere;
}

.fallback-summary {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  border-radius: 14px;
  background: #f3f6fb;
  padding: 12px;
}

.fallback-summary div {
  border-radius: 10px;
  background: #fff;
  padding: 12px;
}

.fallback-summary strong {
  display: block;
  margin-top: 4px;
  font-size: 18px;
  overflow-wrap: anywhere;
}

.chat {
  border-top: 1px solid var(--line);
  padding: 14px;
  display: grid;
  gap: 10px;
  background: #fbfdff;
}

.messages {
  max-height: 260px;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: stretch;
}

.messages > .chat-message {
  border: 1px solid #d9e4f1;
  border-radius: 12px 12px 12px 4px;
  padding: 5px 10px 6px;
  background: #fff;
  font-size: 13px;
  line-height: 1.24;
  white-space: normal;
  width: fit-content;
  max-width: min(84%, 640px);
  min-height: 0;
  align-self: flex-start;
  display: block !important;
  gap: 3px;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.05);
  overflow-wrap: anywhere;
  word-break: break-word;
  box-sizing: border-box;
  height: auto !important;
  max-height: none !important;
  overflow: visible !important;
  position: relative !important;
  contain: none !important;
}

.messages > .chat-message > .chat-meta {
  display: block;
  margin-bottom: 0;
  font-size: 11px;
  line-height: 1.12;
  position: static !important;
}

.messages > .chat-message > .chat-text {
  display: block;
  font-size: 15px;
  line-height: 1.22;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
  position: static !important;
  margin: 0;
  min-width: 0;
  padding-top: 3px;
  clear: both;
}

.messages > .chat-message.me {
  background: #dbeafe;
  border-color: #93c5fd;
  border-radius: 12px 12px 4px 12px;
  align-self: flex-end;
}

.chat-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}

.accounting-hold-note {
  display: grid;
  gap: 5px;
  max-width: 720px;
  border: 1px solid #f0d88b;
  border-radius: 14px;
  background: #fff8dc;
  color: #7a4b00;
  padding: 12px 14px;
  line-height: 1.35;
}

.accounting-hold-note strong {
  color: #92400e;
}

.toast {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 20;
  max-width: min(420px, calc(100vw - 32px));
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid #93c5fd;
  background: #dbeafe;
  color: #1e3a8a;
  box-shadow: var(--shadow);
  font-weight: 700;
}

.version {
  position: fixed;
  left: 12px;
  bottom: 8px;
  z-index: 10;
  color: var(--muted);
  font-size: 11px;
  background: rgba(255, 255, 255, .82);
  border: 1px solid rgba(215, 225, 238, .8);
  border-radius: 999px;
  padding: 3px 8px;
  pointer-events: none;
}

@media (max-width: 980px) {
  html, body {
    max-width: 100%;
    overflow-x: hidden;
    font-size: 13px;
  }
  .app {
    width: 100%;
    max-width: 100vw;
    overflow-x: hidden;
    padding: 6px;
    gap: 8px;
  }
  .topbar {
    width: 100%;
    min-width: 0;
    align-items: flex-start;
    display: grid;
    grid-template-columns: minmax(96px, 1fr) minmax(0, auto);
    gap: 8px;
    padding: 10px 12px;
    border-radius: 8px;
  }
  .brand {
    font-size: 16px;
    line-height: 1.15;
  }
  .subtitle {
    font-size: 12px;
    line-height: 1.25;
  }
  .top-actions {
    min-width: 0;
    gap: 5px;
    display: grid;
    grid-template-columns: 1fr 1fr;
    justify-items: stretch;
    align-items: stretch;
  }
  .session {
    grid-column: 1 / -1;
    width: 100%;
    text-align: right;
    font-size: 12px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .btn {
    min-width: 0;
    padding: 7px 8px;
    border-radius: 7px;
    font-size: 12px;
    line-height: 1.15;
  }
  #notifyBtn {
    grid-column: 1 / -1;
  }
  .workspace {
    width: 100%;
    max-width: 100%;
    height: auto;
    min-height: calc(100vh - 92px);
    grid-template-columns: 1fr;
  }
  .card {
    max-width: 100%;
    border-radius: 8px;
  }
  .details-panel {
    display: none;
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
  }
  .workspace.mobile-detail-open .list-panel {
    display: none;
  }
  .details-panel.mobile-visible {
    display: block;
  }
  .list-panel {
    width: 100%;
    max-width: 100%;
    overflow: visible;
  }
  .list-head {
    padding: 10px 12px;
    font-size: 14px;
  }
  .pill {
    min-width: 24px;
    height: 22px;
    font-size: 12px;
  }
  .task {
    width: 100%;
    max-width: 100%;
    padding: 10px 12px;
    overflow: hidden;
  }
  .task-title {
    font-size: 14px;
    line-height: 1.22;
    overflow-wrap: anywhere;
    word-break: break-word;
  }
  .task-row {
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
    margin-top: 7px;
    font-size: 13px;
  }
  .amount {
    font-size: 13px;
  }
  .status {
    font-size: 10px;
    padding: 2px 7px;
  }
  .muted {
    font-size: 11px;
  }
  .mobile-detail-bar {
    position: sticky;
    top: 0;
    z-index: 5;
    display: flex;
    justify-content: flex-end;
    padding: 8px;
    background: #fbfdff;
    border-bottom: 1px solid var(--line);
  }
  .mobile-detail-bar .btn {
    width: auto;
    min-width: 90px;
  }
  .details-view,
  #detailsView {
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
  }
  .details-head {
    padding: 10px;
    gap: 6px;
  }
  .details-title strong {
    font-size: 15px;
    overflow-wrap: anywhere;
    word-break: break-word;
  }
  .invoice-meta-strip {
    gap: 4px 10px;
    font-size: 11px;
  }
  .invoice-meta-strip span {
    max-width: 100%;
    white-space: normal;
  }
  .preview {
    max-width: 100%;
    padding: 6px;
    overflow-x: hidden;
  }
  .invoice-embed {
    max-width: 100%;
    min-height: 260px;
    overflow: hidden;
    border-radius: 7px;
  }
  .invoice-frame {
    width: 100%;
    min-height: 220px;
  }
  .fallback-invoice {
    padding: 14px;
    gap: 12px;
  }
  .fallback-invoice-top {
    display: grid;
    gap: 10px;
  }
  .fallback-total {
    min-width: 0;
    text-align: left;
  }
  .fallback-grid,
  .fallback-summary {
    grid-template-columns: 1fr;
  }
  .fallback-invoice h2 {
    font-size: 22px;
  }
  .fallback-total strong {
    font-size: 24px;
  }
  .chat {
    padding: 8px;
    gap: 7px;
  }
  .messages {
    max-height: 150px;
    gap: 4px;
  }
  .messages > .chat-message {
    width: fit-content;
    max-width: calc(100% - 12px);
    padding: 4px 9px 5px;
    border-radius: 9px 9px 9px 3px;
    font-size: 13px;
    line-height: 1.18;
  }
  .messages > .chat-message > .chat-meta {
    font-size: 11px;
    line-height: 1.08;
    margin-bottom: 0;
  }
  .messages > .chat-message > .chat-text {
    font-size: 15px;
    line-height: 1.16;
  }
  .messages > .chat-message.me {
    border-radius: 9px 9px 3px 9px;
  }
  textarea {
    min-height: 84px;
    padding: 8px;
    font-size: 13px;
  }
  .chat-actions { flex-direction: column; align-items: stretch; }
  .chat-actions .btn { width: 100%; }
  .password-grid {
    grid-template-columns: 1fr;
  }
  .version {
    position: static;
    width: fit-content;
    margin: 0 0 4px 4px;
  }
}

@media (display-mode: standalone) {
  .topbar { padding-top: max(12px, env(safe-area-inset-top)); }
}
