* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Arial, sans-serif;
  background: #f5f6f8;
  color: #1f2937;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid #d1d5db;
  background: #ffffff;
}

.crm-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 1rem;
  padding: 1rem;
  min-width: 0;
}

.panel {
  border: 2px solid #111827;
  background: #ffffff;
  min-height: 220px;
  min-width: 0;
  padding: 1rem;
}

.panel-field1 {
  max-width: 100%;
  min-height: auto;
}

.panel-field2 {
  min-height: 180px;
}

.panel-field3 {
  min-height: 240px;
  min-width: 0;
}

.panel h2 {
  margin-top: 0;
  font-size: 1rem;
}

.field1-row {
  margin-bottom: 0.75rem;
}

.field1-row:last-child {
  margin-bottom: 0;
}

.field1-toolbar {
  display: flex;
  flex-wrap: nowrap;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
}

.field1-heading {
  font-weight: 700;
  font-size: 0.95rem;
  line-height: 1.2;
  flex: 1 1 auto;
  min-width: 0;
}

.field1-actions {
  display: flex;
  flex-shrink: 0;
  gap: 0.35rem;
}

.btn-tight {
  width: auto;
  margin-bottom: 0;
  padding: 0.35rem 0.55rem;
  font-size: 0.8rem;
  white-space: nowrap;
}

.field-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
}

.header-actions {
  display: flex;
  gap: 0.35rem;
}

.contacts-table {
  border: 1px solid #d1d5db;
}

.contacts-head,
.contact-row {
  display: grid;
  grid-template-columns:
    minmax(110px, 1.25fr) minmax(82px, 0.95fr) minmax(82px, 0.95fr) minmax(128px, 1.25fr) minmax(72px, 0.85fr) minmax(100px, 1.15fr);
  gap: 0.5rem;
  align-items: center;
}

.contacts-head {
  font-size: 0.8rem;
  font-weight: 700;
  background: #f3f4f6;
  padding: 0.45rem 0.55rem;
  border-bottom: 1px solid #d1d5db;
}

.contacts-head span,
.contact-row span {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.contacts-body {
  max-height: 260px;
  overflow-y: auto;
}

.contact-row {
  width: 100%;
  text-align: left;
  border: none;
  border-bottom: 1px solid #f3f4f6;
  background: #fff;
  padding: 0.45rem 0.55rem;
  margin: 0;
  font: inherit;
  font-size: 0.86rem;
}

.contact-row:last-child {
  border-bottom: none;
}

.contact-row.active,
.contact-row:hover {
  background: #eff6ff;
}

.ticket-hint {
  margin: 0 0 0.5rem;
}

/* One shared column grid: header + all data cells are direct grid children (via display: contents) so columns line up */
.tickets-scroll {
  border: 1px solid #d1d5db;
  width: 100%;
  min-width: 0;
  max-height: 280px;
  overflow: auto;
}

.tickets-table {
  display: grid;
  grid-template-columns:
    max-content
    max-content
    max-content
    max-content
    minmax(0, 1fr)
    max-content
    max-content
    max-content;
  column-gap: clamp(0.2rem, 1.2vw, 0.45rem);
  row-gap: 0;
  align-items: center;
  min-width: 100%;
  width: max-content;
  box-sizing: border-box;
}

.tickets-head,
.tickets-body {
  display: contents;
}

.ticket-row.muted {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: 1fr;
  width: 100%;
  box-sizing: border-box;
  padding: clamp(0.3rem, 0.8vw, 0.4rem) clamp(0.3rem, 1vw, 0.5rem);
  margin: 0;
  border-bottom: 1px solid #f3f4f6;
  font-size: clamp(0.7rem, 0.4vw + 0.65rem, 0.84rem);
  background: #fff;
}

/* Header row: first 8 grid cells */
.tickets-table > *:nth-child(-n + 8) {
  font-size: clamp(0.68rem, 0.35vw + 0.62rem, 0.78rem);
  font-weight: 700;
  background: #f3f4f6;
  padding: clamp(0.28rem, 0.8vw, 0.4rem) clamp(0.2rem, 0.9vw, 0.45rem);
  border-bottom: 1px solid #d1d5db;
  box-sizing: border-box;
}

/* Data cells */
.tickets-table > .ticket-date,
.tickets-table > .ticket-closed,
.tickets-table > .ticket-openfor,
.tickets-table > .ticket-user,
.tickets-table > .ticket-desc,
.tickets-table > .ticket-creator,
.tickets-table > .ticket-time,
.tickets-table > .ticket-status {
  padding: clamp(0.3rem, 0.8vw, 0.4rem) clamp(0.2rem, 0.9vw, 0.45rem);
  margin: 0;
  border-bottom: 1px solid #f3f4f6;
  font-size: clamp(0.7rem, 0.4vw + 0.65rem, 0.84rem);
  background: #fff;
  box-sizing: border-box;
}

/* Details column fills its track; other columns hug content at the shared grid level */
.ticket-desc,
.ticket-sort[data-sort="details"] {
  min-width: 0;
  width: 100%;
  justify-self: stretch;
  text-align: left;
}

.tickets-table > .ticket-date,
.tickets-table > .ticket-closed,
.tickets-table > .ticket-openfor,
.tickets-table > .ticket-user,
.tickets-table > .ticket-creator,
.tickets-table > .ticket-time {
  justify-self: start;
  white-space: nowrap;
  overflow: visible;
  text-overflow: clip;
}

.tickets-table > .ticket-status {
  justify-self: end;
  white-space: nowrap;
  overflow: visible;
  text-overflow: clip;
}

.ticket-data-cell {
  cursor: pointer;
}

.ticket-data-cell:hover {
  background: #f9fafb;
}

.ticket-sort {
  margin: 0;
  padding: 0.15rem 0.2rem;
  border: none;
  background: transparent;
  font: inherit;
  font-weight: 700;
  color: inherit;
  cursor: pointer;
  text-align: left;
  white-space: nowrap;
  overflow: visible;
  text-overflow: clip;
  border-radius: 4px;
}

.ticket-sort[data-sort="date"],
.ticket-sort[data-sort="user"],
.ticket-sort[data-sort="status"] {
  justify-self: start;
  width: max-content;
  max-width: max-content;
}

.ticket-sort:hover {
  background: rgba(0, 0, 0, 0.06);
}

.ticket-sort--active {
  color: #1d4ed8;
}

.ticket-sort--asc::after {
  content: " ▲";
  font-size: 0.65em;
  font-weight: 800;
}

.ticket-sort--desc::after {
  content: " ▼";
  font-size: 0.65em;
  font-weight: 800;
}

.ticket-head-time,
.ticket-head-label {
  white-space: nowrap;
  overflow: visible;
  text-overflow: clip;
  justify-self: start;
  width: max-content;
}

.ticket-desc {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ticket-status-pill {
  display: inline-block;
  padding: 0.15rem clamp(0.3rem, 1.5vw, 0.5rem);
  border-radius: 999px;
  font-size: clamp(0.65rem, 0.35vw + 0.58rem, 0.76rem);
  font-weight: 700;
  line-height: 1.25;
  white-space: nowrap;
  overflow: visible;
  box-sizing: border-box;
  vertical-align: middle;
}

.ticket-status-pill--open {
  background: #dc2626;
  color: #ffffff;
}

.ticket-status-pill--waiting {
  background: #eab308;
  color: #1c1917;
}

.ticket-status-pill--closed {
  background: #16a34a;
  color: #ffffff;
}

.ticket-view-meta .ticket-status-pill {
  vertical-align: middle;
  font-size: 0.8rem;
}

.ticket-row.muted .ticket-muted-fill {
  white-space: normal;
}

.ticket-view-meta {
  margin: 0 0 0.85rem;
  padding: 0.6rem 0.65rem;
  font-size: 0.82rem;
  background: #f3f4f6;
  border-radius: 6px;
  line-height: 1.45;
}

.ticket-view-meta .ticket-meta-line {
  margin: 0.2rem 0;
}

.ticket-view-meta .ticket-meta-line:first-child {
  margin-top: 0;
}

.ticket-view-meta .ticket-meta-line:last-child {
  margin-bottom: 0;
}

@media (max-width: 720px) {
  .tickets-table {
    column-gap: 0.2rem;
  }

  .ticket-sort {
    padding: 0.1rem 0.08rem;
  }
}

@media (max-width: 1200px) {
  .contacts-head,
  .contact-row {
    grid-template-columns:
      minmax(98px, 1.15fr) minmax(74px, 0.9fr) minmax(74px, 0.9fr) minmax(115px, 1.1fr) minmax(68px, 0.8fr) minmax(92px, 1.05fr);
    gap: 0.35rem;
  }

  .contacts-head {
    font-size: 0.75rem;
  }

  .contact-row {
    font-size: 0.8rem;
    padding: 0.4rem 0.45rem;
  }
}

@media (max-width: 900px) {
  .contacts-head,
  .contact-row {
    grid-template-columns:
      minmax(88px, 1.1fr) minmax(66px, 0.85fr) minmax(66px, 0.85fr) minmax(100px, 1.05fr) minmax(62px, 0.75fr) minmax(84px, 1fr);
    gap: 0.25rem;
  }

  .contacts-head {
    font-size: 0.72rem;
    padding: 0.35rem 0.4rem;
  }

  .contact-row {
    font-size: 0.76rem;
    padding: 0.35rem 0.4rem;
  }
}

.customer-combobox-wrap {
  position: relative;
}

.customer-combobox-wrap input {
  margin-bottom: 0;
}

.customer-dropdown {
  position: absolute;
  left: 0;
  right: 0;
  top: calc(100% + 2px);
  max-height: 260px;
  overflow-y: auto;
  background: #ffffff;
  border: 1px solid #d1d5db;
  border-radius: 4px;
  box-shadow: 0 8px 24px rgba(17, 24, 39, 0.12);
  z-index: 200;
}

.customer-dropdown.hidden {
  display: none;
}

.dropdown-item {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  width: 100%;
  padding: 0.5rem 0.65rem;
  border: none;
  border-bottom: 1px solid #f3f4f6;
  background: #fff;
  cursor: pointer;
  text-align: left;
  font: inherit;
  margin: 0;
}

.dropdown-item:last-child {
  border-bottom: none;
}

.dropdown-item:hover,
.dropdown-item:focus {
  background: #eff6ff;
  outline: none;
}

.dropdown-meta {
  display: block;
  font-size: 0.8rem;
  color: #6b7280;
  margin-top: 0.15rem;
}

.dropdown-empty {
  padding: 0.65rem;
  color: #6b7280;
  font-size: 0.9rem;
}

.customer-details-three .info-line {
  font-size: 0.9rem;
  line-height: 1.35;
}

.customer-details-three .info-name {
  margin-bottom: 0.2rem;
}

.customer-details-three .info-meta {
  color: #4b5563;
  font-size: 0.85rem;
}

.muted {
  color: #9ca3af;
  font-size: 0.9rem;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.actions-row {
  display: flex;
  gap: 0.5rem;
}

.actions-row > * {
  flex: 1;
}

input,
textarea,
button {
  width: 100%;
  padding: 0.5rem;
  margin-bottom: 0.5rem;
}

select {
  width: 100%;
  padding: 0.5rem;
  margin-bottom: 0.5rem;
}

button {
  cursor: pointer;
}

.btn-secondary {
  display: block;
  text-align: center;
  text-decoration: none;
  width: 100%;
  padding: 0.5rem;
  border: 1px solid #9ca3af;
  color: #111827;
  background: #f3f4f6;
  margin-bottom: 0.5rem;
}

.list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.list li {
  padding: 0.5rem;
  border: 1px solid #d1d5db;
  margin-bottom: 0.5rem;
  cursor: pointer;
}

.list li.active {
  border-color: #2563eb;
  background: #eff6ff;
}

.details {
  font-size: 0.9rem;
}

.hidden {
  display: none;
}

.customer-dialog {
  border: none;
  padding: 0;
  border-radius: 6px;
  max-width: min(520px, calc(100vw - 2rem));
  box-shadow: 0 24px 48px rgba(0, 0, 0, 0.2);
}

.customer-dialog::backdrop {
  background: rgba(17, 24, 39, 0.5);
}

.customer-dialog.dialog-polyfill {
  position: fixed;
  inset: 0;
  margin: auto;
  z-index: 100000;
  background: #ffffff;
  border: 1px solid #d1d5db;
  max-height: calc(100vh - 2rem);
  overflow: auto;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
  padding: 1rem 1rem 0;
}

.modal-header h3 {
  margin: 0;
  font-size: 1.1rem;
}

.btn-close {
  width: auto;
  margin: 0;
  padding: 0.2rem 0.55rem;
  font-size: 1.25rem;
  line-height: 1;
  border: none;
  background: transparent;
  color: #6b7280;
  cursor: pointer;
}

.btn-close:hover {
  color: #111827;
}

.modal-content .customer-form {
  padding: 0 1rem 1rem;
}

.modal-content .customer-form input {
  margin-bottom: 0.5rem;
}

.customer-form textarea {
  min-height: 85px;
}

.ticket-status-field {
  margin: 0.5rem 0 0.35rem;
  padding: 0.5rem 0.65rem;
  border: 1px solid #e5e7eb;
  border-radius: 6px;
  background: #fafafa;
}

.ticket-status-field legend {
  font-size: 0.85rem;
  font-weight: 600;
  padding: 0 0.25rem;
}

.ticket-status-option {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  margin-right: 1rem;
  font-size: 0.9rem;
  cursor: pointer;
}

.ticket-status-option input {
  margin: 0;
}

.login-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
}

.card {
  width: 100%;
  max-width: 420px;
  border: 1px solid #d1d5db;
  background: #ffffff;
  padding: 1rem;
}

.hint {
  font-size: 0.85rem;
  color: #6b7280;
}

.error {
  background: #fee2e2;
  color: #991b1b;
  border: 1px solid #fca5a5;
  padding: 0.5rem;
}
