:root {
  --dark: #1f2937;
  --darker: #111827;
  --red: #e30613;
  --blue: #2563eb;
  --light: #f4f6f8;
  --card: #ffffff;
  --border: #d9dee7;
  --text: #1f2937;
  --muted: #6b7280;
}

body {
  margin: 0;
  font-family: Arial, sans-serif;
  background: var(--light);
  color: var(--text);
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
}

* {
  box-sizing: border-box;
}

img,
video,
canvas {
  max-width: 100%;
}

.header {
  background: var(--dark);
  color: white;
  padding: 22px 28px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 22px;
}

.logo {
  height: 78px;
  background: white;
  padding: 12px;
  border-radius: 3px;
}

.header h1 {
  margin: 0;
  font-size: 32px;
}

.header p {
  margin: 8px 0 0;
  color: #d1d5db;
}

.nav {
  margin-top: 22px;
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.nav button {
  background: #374151;
  border: 1px solid #4b5563;
}

.nav button:hover {
  background: var(--red);
}

#nav-export-backup {
  display: none;
}

.user-bar {
  margin-top: 14px;
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}

.user-bar span {
  font-weight: 700;
}

.user-bar button {
  background: #4b5563;
}

.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-panel {
  width: min(420px, 100%);
  background: white;
  padding: 28px;
  border-radius: 10px;
  box-shadow: 0 4px 18px rgba(0,0,0,0.12);
}

.login-logo {
  height: 80px;
  display: block;
  margin-bottom: 16px;
}

#login-message {
  color: #991b1b;
  font-weight: 700;
}

main {
  padding: 24px;
}

.toolbar {
  background: var(--card);
  padding: 16px;
  border-radius: 12px;
  margin-bottom: 22px;
  display: flex;
  gap: 12px;
  align-items: center;
  box-shadow: 0 3px 12px rgba(0,0,0,0.06);
}

button {
  background: var(--blue);
  color: white;
  border: none;
  padding: 11px 16px;
  border-radius: 7px;
  cursor: pointer;
  font-weight: 600;
  min-height: 42px;
  touch-action: manipulation;
}

button:hover {
  background: #1d4ed8;
}

.layout {
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: 22px;
}

.machine-list,
.details-panel {
  background: var(--card);
  padding: 22px;
  border-radius: 14px;
  box-shadow: 0 3px 14px rgba(0,0,0,0.07);
}

.machine-list h2,
.details-panel h2 {
  margin-top: 0;
}

.machine-item {
  border-bottom: 1px solid var(--border);
  padding: 14px 10px;
  cursor: pointer;
  border-radius: 8px;
}

.machine-item:hover {
  background: #f1f5f9;
}

input,
textarea,
select {
  width: 100%;
  padding: 10px;
  margin: 6px 0 14px;
  box-sizing: border-box;
  border: 1px solid var(--border);
  border-radius: 6px;
  font: inherit;
  min-height: 42px;
}

textarea {
  min-height: 90px;
}

#tabs {
  border-top: 1px solid var(--border);
  padding-top: 18px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

#tabs button {
  background: #334155;
}

#tabs button:hover {
  background: var(--red);
}

table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 14px;
  background: white;
  table-layout: auto;
}

th {
  background: #111827;
  color: white;
  text-align: left;
  padding: 10px;
}

td {
  border-bottom: 1px solid var(--border);
  padding: 10px;
  overflow-wrap: anywhere;
}

.photo-preview {
  max-width: 180px;
  margin: 8px;
  border-radius: 8px;
  border: 1px solid #ccc;
}

@media (max-width: 900px) {
  .layout {
    grid-template-columns: 1fr;
  }

  .brand {
    flex-direction: column;
    align-items: flex-start;
  }
}

.dashboard {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 18px;
  margin-bottom: 22px;
}

.dashboard-card {
  background: white;
  border-radius: 14px;
  padding: 20px;
  text-align: center;
  box-shadow: 0 3px 14px rgba(0,0,0,0.07);
}

.dashboard-card h3 {
  font-size: 42px;
  margin: 0;
  color: #2563eb;
}

.dashboard-card p {
  margin-top: 8px;
  color: #6b7280;
  font-weight: 600;
}

@media (max-width: 900px) {
  .dashboard {
    grid-template-columns: repeat(2,1fr);
  }
}

.status-badge {
  display: inline-block;
  padding: 4px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: bold;
  margin-bottom: 6px;
}

.running {
  background: #dcfce7;
  color: #166534;
}

.limited-operation {
  background: #fef3c7;
  color: #92400e;
}

.down {
  background: #fee2e2;
  color: #991b1b;
}

.waiting-for-parts {
  background: #ede9fe;
  color: #5b21b6;
}

.fault-card {
  background: white;
  border: 1px solid var(--border);
  border-left: 6px solid #2563eb;
  padding: 16px;
  border-radius: 10px;
  margin-bottom: 14px;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}

.fault-card:hover {
  background: #f8fafc;
}

.fault-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.fault-status,
.priority-badge {
  display: inline-block;
  padding: 4px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: bold;
}

.fault-status.open {
  background: #fee2e2;
  color: #991b1b;
}

.fault-status.closed {
  background: #e5e7eb;
  color: #374151;
}

.priority-badge.low {
  background: #dcfce7;
  color: #166534;
}

.priority-badge.medium {
  background: #fef3c7;
  color: #92400e;
}

.priority-badge.high {
  background: #ffedd5;
  color: #9a3412;
}

.priority-badge.critical {
  background: #fee2e2;
  color: #991b1b;
}

.photo-actions {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 18px;
}

#photo-upload-input {
  display: none;
}

#document-upload-input {
  display: none;
}

.document-actions {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 18px;
}

.document-table td,
.document-table th {
  vertical-align: middle;
}

.document-table td:first-child {
  overflow-wrap: anywhere;
}

.photo-gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 16px;
}

.photo-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  background: white;
  display: grid;
  gap: 8px;
  align-content: start;
}

.photo-card strong,
.photo-card span {
  overflow-wrap: anywhere;
}

.photo-card span {
  color: var(--muted);
  font-size: 13px;
}

.photo-thumb-button {
  padding: 0;
  border: 0;
  background: transparent;
  border-radius: 6px;
  overflow: hidden;
  aspect-ratio: 4 / 3;
}

.photo-thumb-button:hover {
  background: transparent;
}

.photo-thumb-button img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.photo-placeholder {
  aspect-ratio: 4 / 3;
  border: 1px dashed var(--border);
  border-radius: 6px;
  display: grid;
  place-items: center;
  color: var(--muted);
  background: #f8fafc;
  font-weight: 600;
}

.danger-button {
  background: #b91c1c;
}

.danger-button:hover {
  background: #991b1b;
}

.pm-section {
  border-top: 1px solid var(--border);
  padding-top: 16px;
  margin-top: 20px;
}

.pm-section .inventory-card button {
  margin: 4px 6px 4px 0;
}

.inventory-card {
  background: white;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
  margin: 12px 0;
  overflow-wrap: anywhere;
}

.inventory-form {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
  margin: 16px 0;
  background: #f8fafc;
}

.inventory-form h3 {
  margin-top: 0;
}

.report-toolbar,
.report-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin: 14px 0;
}

.report-section {
  margin-top: 18px;
}

.report-summary {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
  margin: 14px 0 18px;
}

.report-summary-card {
  box-shadow: none;
  border: 1px solid var(--border);
}

.report-table td {
  vertical-align: top;
}

@media (max-width: 768px) {
  .header {
    padding: 16px;
  }

  .brand {
    gap: 12px;
  }

  .logo {
    height: 58px;
    padding: 8px;
  }

  .header h1 {
    font-size: 24px;
    line-height: 1.15;
  }

  .header p {
    font-size: 14px;
  }

  .nav,
  .user-bar,
  .toolbar,
  .report-toolbar,
  .report-actions,
  .fault-view-buttons,
  .photo-actions,
  .document-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
    width: 100%;
  }

  #nav-export-backup {
    display: block;
  }

  #nav-report-fault {
    order: 1;
    background: var(--red);
    border-color: #b91c1c;
  }

  #nav-machines {
    order: 2;
  }

  #nav-faults {
    order: 3;
  }

  #nav-jobs {
    order: 4;
  }

  #nav-inventory {
    order: 5;
  }

  #nav-pm {
    order: 6;
  }

  #nav-users {
    order: 7;
  }

  #nav-reports {
    order: 8;
  }

  #nav-dashboard {
    order: 9;
  }

  #nav-export-backup {
    order: 10;
  }

  body[data-mobile-section] .toolbar,
  body[data-mobile-section] .dashboard,
  body[data-mobile-section] .machine-list,
  body[data-mobile-section] .details-panel {
    display: none;
  }

  body[data-mobile-section="dashboard"] .toolbar {
    display: grid;
  }

  body[data-mobile-section="dashboard"] .dashboard {
    display: grid;
  }

  body[data-mobile-section="dashboard"] .details-panel,
  body[data-mobile-section="machines"] .machine-list,
  body[data-mobile-section="machines"] .details-panel,
  body[data-mobile-section="report-fault"] .details-panel,
  body[data-mobile-section="faults"] .details-panel,
  body[data-mobile-section="jobs"] .details-panel,
  body[data-mobile-section="inventory"] .details-panel,
  body[data-mobile-section="pm"] .details-panel,
  body[data-mobile-section="reports"] .details-panel,
  body[data-mobile-section="users"] .details-panel {
    display: block;
  }

  body[data-mobile-section="report-fault"] .layout,
  body[data-mobile-section="faults"] .layout,
  body[data-mobile-section="jobs"] .layout,
  body[data-mobile-section="inventory"] .layout,
  body[data-mobile-section="pm"] .layout,
  body[data-mobile-section="reports"] .layout,
  body[data-mobile-section="users"] .layout {
    display: block;
  }

  .user-bar span,
  .toolbar input[type="file"],
  .photo-actions input[type="file"],
  .document-actions input[type="file"] {
    grid-column: 1 / -1;
    min-width: 0;
  }

  .nav button,
  .user-bar button,
  .toolbar button,
  .report-toolbar button,
  .report-actions button,
  .fault-view-buttons button,
  .photo-actions button,
  .document-actions button,
  .details-panel button,
  .inventory-form button,
  .inventory-card button {
    width: 100%;
    min-height: 46px;
    padding: 12px 10px;
    white-space: normal;
  }

  main {
    padding: 14px;
  }

  .toolbar {
    align-items: stretch;
    border-radius: 10px;
  }

  .layout {
    grid-template-columns: 1fr;
    gap: 14px;
  }

  .machine-list,
  .details-panel {
    padding: 16px;
    border-radius: 10px;
  }

  .machine-list {
    max-height: 45vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .machine-item {
    padding: 14px 10px;
    width: 100%;
  }

  .status-badge,
  .fault-status,
  .priority-badge {
    font-size: 12px;
    line-height: 1.2;
    white-space: normal;
  }

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

  .dashboard-card {
    padding: 16px 10px;
    border-radius: 10px;
  }

  .dashboard-card h3 {
    font-size: 34px;
  }

  .dashboard-card p {
    font-size: 14px;
  }

  #tabs {
    margin-top: 16px !important;
    padding-top: 14px;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
  }

  #tabs button {
    margin: 0;
    width: 100%;
    min-height: 46px;
  }

  input,
  textarea,
  select {
    font-size: 16px;
    padding: 12px;
  }

  textarea {
    min-height: 120px;
  }

  .fault-header {
    align-items: flex-start;
    flex-direction: column;
    gap: 8px;
  }

  .fault-card,
  .inventory-card,
  .inventory-form,
  .mobile-fault-panel,
  .photo-card {
    border-radius: 8px;
    padding: 14px;
  }

  .mobile-fault-panel {
    background: #f8fafc;
    border: 1px solid var(--border);
  }

  .mobile-fault-panel h2 {
    margin-top: 0;
  }

  .mobile-fault-panel button {
    margin-top: 6px;
  }

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

  .photo-preview {
    max-width: 100%;
    margin: 8px 0;
  }

  .report-summary {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  table,
  thead,
  tbody,
  tr,
  th,
  td {
    display: block;
  }

  table {
    border: 1px solid var(--border);
    border-radius: 8px;
    overflow: hidden;
  }

  thead {
    display: none;
  }

  tr {
    border-bottom: 1px solid var(--border);
    padding: 8px 0;
  }

  tr:last-child {
    border-bottom: 0;
  }

  td {
    border-bottom: 0;
    display: grid;
    grid-template-columns: minmax(105px, 38%) 1fr;
    gap: 10px;
    padding: 8px 12px;
    align-items: start;
  }

  td::before {
    content: attr(data-label);
    font-weight: 700;
    color: var(--muted);
    overflow-wrap: anywhere;
  }

  td:not([data-label]) {
    display: block;
  }

  .login-page {
    padding: 14px;
  }

  .login-panel {
    padding: 22px;
    border-radius: 8px;
  }

  .login-panel h1 {
    font-size: 24px;
  }
}

@media (max-width: 480px) {
  .header {
    padding: 14px 12px;
  }

  .header h1 {
    font-size: 21px;
  }

  .logo {
    height: 52px;
  }

  .nav,
  .user-bar,
  .toolbar,
  .report-toolbar,
  .report-actions,
  .fault-view-buttons,
  .photo-actions,
  .document-actions,
  #tabs {
    grid-template-columns: 1fr;
  }

  main {
    padding: 10px;
  }

  .machine-list,
  .details-panel,
  .toolbar {
    padding: 12px;
  }

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

  .dashboard-card h3 {
    font-size: 30px;
  }

  .dashboard-card p {
    font-size: 13px;
    margin-top: 6px;
  }

  .photo-gallery,
  .report-summary {
    grid-template-columns: 1fr;
  }

  td {
    grid-template-columns: 1fr;
    gap: 4px;
  }

  td::before {
    font-size: 12px;
    text-transform: uppercase;
  }

  .document-table td,
  .document-table th {
    width: 100%;
  }
}

@media print {
  body {
    background: white;
  }

  .header,
  .toolbar,
  .dashboard,
  .machine-list,
  .report-toolbar,
  .report-actions,
  #tabs {
    display: none !important;
  }

  main,
  .layout,
  .details-panel {
    display: block;
    padding: 0;
    box-shadow: none;
    border-radius: 0;
  }
}
