* { box-sizing: border-box; }
body {
  font-family: Arial, Helvetica, sans-serif;
  margin: 0;
  padding: 0;
  background: #f5f5f5;
  color: #222;
}
header {
  background: #2c3e50;
  color: #fff;
  padding: 16px 20px;
}
header h1 {
  margin: 0 0 8px 0;
  font-size: 20px;
}
.stats {
  font-size: 13px;
  color: #dfe6e9;
}
.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 12px 20px;
  background: #ecf0f1;
  border-bottom: 1px solid #ccc;
}
.controls input, .controls select, .controls button {
  padding: 6px 10px;
  font-size: 13px;
  border: 1px solid #bbb;
  border-radius: 4px;
}
.controls button {
  background: #2980b9;
  color: #fff;
  cursor: pointer;
  border: none;
}
.controls button:hover { background: #1f6391; }

main { padding: 12px 20px; overflow-x: auto; }

table {
  width: 100%;
  border-collapse: collapse;
  background: #fff;
  font-size: 13px;
  table-layout: fixed;
}
th, td {
  border: 1px solid #ddd;
  padding: 8px 10px;
  text-align: left;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
th {
  background: #34495e;
  color: #fff;
  cursor: default;
  position: sticky;
  top: 0;
}
tbody tr:hover { background: #f0f8ff; cursor: pointer; }
.loading { text-align: center; color: #888; }

th:nth-child(1), td:nth-child(1) { width: 7%; }
th:nth-child(2), td:nth-child(2) { width: 15%; }
th:nth-child(3), td:nth-child(3) { width: 6%; }
th:nth-child(4), td:nth-child(4) { width: 5%; }
th:nth-child(5), td:nth-child(5) { width: 12%; }
th:nth-child(6), td:nth-child(6) { width: 11%; }
th:nth-child(7), td:nth-child(7) { width: 18%; max-width: 180px; }
th:nth-child(8), td:nth-child(8) { width: 9%; }
th:nth-child(9), td:nth-child(9) { width: 9%; }
th:nth-child(10), td:nth-child(10) { width: 8%; }

td:nth-child(7) {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: help;
}

.progress-bar-bg {
  background: #ddd;
  border-radius: 4px;
  width: 100%;
  max-width: 90px;
  height: 14px;
  display: inline-block;
  vertical-align: middle;
  overflow: hidden;
}
.progress-bar-fill {
  height: 100%;
  background: #27ae60;
  text-align: right;
  font-size: 10px;
  color: #fff;
  padding-right: 3px;
  white-space: nowrap;
}

.status-tag {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 3px;
  font-size: 11px;
  font-weight: bold;
  color: #fff;
  background: #7f8c8d;
  white-space: nowrap;
}
.status-zaversheno { background: #27ae60; }
.status-vrabote { background: #27ae60; }
.status-zamorozheno { background: #7f8c8d; }
.status-nenachato { background: #c0392b; }

.modal-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}
.modal-overlay.hidden { display: none; }
.modal {
  background: #fff;
  border-radius: 6px;
  width: 92%;
  max-width: 640px;
  max-height: 88vh;
  padding: 16px 20px;
  position: relative;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal h2 {
  font-size: 16px;
  margin: 4px 0 8px 0;
  padding-right: 24px;
}
.close-btn {
  position: absolute;
  top: 10px; right: 14px;
  background: none;
  border: none;
  font-size: 18px;
  cursor: pointer;
}
.tabs { display: flex; gap: 8px; margin: 12px 0; border-bottom: 1px solid #ddd; flex-shrink: 0; }
.tab-btn {
  padding: 8px 14px;
  border: none;
  background: none;
  cursor: pointer;
  font-size: 13px;
  border-bottom: 2px solid transparent;
}
.tab-btn.active { border-bottom: 2px solid #2980b9; font-weight: bold; color: #2980b9; }
.tab-content { display: none; overflow-y: auto; min-height: 0; flex: 1; }
.tab-content.active { display: block; }

.details-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px 12px;
  font-size: 12.5px;
}
.details-grid .full-width {
  grid-column: 1 / -1;
}
.detail-field {
  background: #f9f9f9;
  border: 1px solid #eee;
  border-radius: 4px;
  padding: 6px 8px;
}
.detail-field .label {
  font-weight: bold;
  color: #555;
  font-size: 11px;
  display: block;
  margin-bottom: 2px;
}
.detail-field .value {
  word-break: break-word;
}

.comments-list { max-height: 35vh; overflow-y: auto; margin-bottom: 12px; }
.comment-item {
  border-bottom: 1px solid #eee;
  padding: 8px 0;
  font-size: 13px;
}
.comment-meta { color: #888; font-size: 11px; }

#commentForm { display: flex; flex-direction: column; gap: 8px; }
#commentForm input, #commentForm textarea {
  padding: 8px;
  border: 1px solid #ccc;
  border-radius: 4px;
  font-size: 13px;
}
#commentForm button {
  background: #27ae60;
  color: #fff;
  border: none;
  padding: 8px;
  border-radius: 4px;
  cursor: pointer;
}
#commentStatus { font-size: 12px; margin-top: 6px; }

footer {
  padding: 10px 20px;
  font-size: 11px;
  color: #888;
  text-align: center;
}


/* ===== Мобильная версия списка квартир ===== */
@media (max-width: 768px) {
  body { min-width: 0; }

  header {
    padding: 14px 16px;
  }
  header h1 {
    font-size: 17px;
    line-height: 1.25;
    margin-bottom: 5px;
  }
  .stats {
    font-size: 12px;
    line-height: 1.45;
  }

  .controls {
    padding: 10px 12px;
    gap: 8px;
  }
  .controls input,
  .controls select,
  .controls button {
    width: 100%;
    min-height: 42px;
    padding: 9px 12px;
    font-size: 15px;
  }

  main {
    padding: 10px 12px 16px;
    overflow-x: hidden;
  }
  #apartmentsTable {
    display: block;
    width: 100%;
    max-width: 100%;
    background: transparent;
  }
  #apartmentsTable thead { display: none; }
  #apartmentsTable tbody { display: block; }

  #apartmentsTable tbody tr {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas:
      "id status"
      "object object"
      "flat floor"
      "stage stage"
      "progress progress";
    gap: 5px 12px;
    width: 100%;
    max-width: 100%;
    margin: 0 0 10px;
    padding: 12px;
    background: #fff;
    border: 1px solid #dfe5e8;
    border-radius: 10px;
    box-shadow: 0 1px 2px rgba(15, 35, 50, 0.06);
    box-sizing: border-box;
  }

  #apartmentsTable tbody td,
  #apartmentsTable tbody td:nth-child(n) {
    display: block;
    width: auto !important;
    min-width: 0;
    max-width: 100% !important;
    margin: 0;
    padding: 0;
    border: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 14px;
    line-height: 1.3;
    box-sizing: border-box;
  }

  #apartmentsTable tbody td:nth-child(1) {
    grid-area: id;
    font-size: 16px;
    font-weight: 700;
    color: #243746;
  }
  #apartmentsTable tbody td:nth-child(2) {
    grid-area: object;
    white-space: normal;
    overflow: hidden;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    font-size: 14px;
    color: #44515a;
  }
  #apartmentsTable tbody td:nth-child(3) { grid-area: flat; }
  #apartmentsTable tbody td:nth-child(4) { grid-area: floor; text-align: right; }
  #apartmentsTable tbody td:nth-child(5) {
    grid-area: stage;
    padding-top: 4px;
    border-top: 1px solid #edf0f2;
    white-space: normal;
    color: #44515a;
  }
  #apartmentsTable tbody td:nth-child(6) {
    grid-area: progress;
    padding-top: 2px;
    overflow: visible;
  }
  #apartmentsTable tbody td:nth-child(7),
  #apartmentsTable tbody td:nth-child(8),
  #apartmentsTable tbody td:nth-child(10) { display: none; }
  #apartmentsTable tbody td:nth-child(9) {
    grid-area: status;
    text-align: right;
    overflow: visible;
  }

  #apartmentsTable tbody td:nth-child(3)::before {
    content: "Квартира ";
    color: #89939a;
    font-size: 11px;
  }
  #apartmentsTable tbody td:nth-child(4)::before {
    content: "Этаж ";
    color: #89939a;
    font-size: 11px;
  }

  .progress-bar-bg {
    display: block;
    width: 100%;
    max-width: none;
    height: 14px;
    margin: 0;
  }
  .progress-bar-fill { font-size: 10px; }
  .status-tag { font-size: 11px; padding: 3px 8px; }

  .modal-overlay { align-items: flex-end; }
  .modal {
    width: 100%;
    max-width: none;
    max-height: 88vh;
    border-radius: 14px 14px 0 0;
    padding: 16px;
  }
  .modal h2 { font-size: 15px; }
  .details-grid { grid-template-columns: 1fr; }
}

@media (max-width: 390px) {
  header h1 { font-size: 16px; }
  #apartmentsTable tbody tr { padding: 10px; }
  #apartmentsTable tbody td:nth-child(1) { font-size: 15px; }
  #apartmentsTable tbody td { font-size: 13px; }
}
