:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --primary-light: #3b82f6;
  --bg: #f8fafc;
  --bg-card: #ffffff;
  --text: #1e293b;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --error: #dc2626;
  --success: #16a34a;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
  --shadow: 0 1px 3px rgba(0,0,0,0.1), 0 1px 2px rgba(0,0,0,0.06);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.1), 0 4px 6px -2px rgba(0,0,0,0.05);
  --radius: 12px;
  --radius-sm: 8px;
}

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

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  min-height: 100dvh;
  overscroll-behavior: none;
}

#app {
  min-height: 100vh;
  min-height: 100dvh;
}

.hidden {
  display: none !important;
}

/* Login Screen */
.login-container {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 24px;
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
}

.login-header {
  text-align: center;
  margin-bottom: 32px;
  color: white;
}

.logo {
  font-size: 48px;
  margin-bottom: 16px;
}

.login-header h1 {
  font-size: 2rem;
  font-weight: 700;
  margin-bottom: 8px;
}

.login-header h1 span {
  font-weight: 400;
  opacity: 0.9;
}

.login-header p {
  opacity: 0.85;
  font-size: 0.95rem;
}

.login-form {
  width: 100%;
  max-width: 320px;
  background: var(--bg-card);
  padding: 24px;
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
}

.login-form .form-group {
  margin-bottom: 20px;
}

.login-form label {
  display: block;
  font-weight: 500;
  margin-bottom: 8px;
  color: var(--text);
}

.login-form input {
  width: 100%;
  padding: 14px 16px;
  font-size: 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  transition: border-color 0.2s, box-shadow 0.2s;
}

.login-form input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.error-message {
  color: var(--error);
  font-size: 0.875rem;
  margin-top: 12px;
  text-align: center;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 20px;
  font-size: 1rem;
  font-weight: 500;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.2s;
  -webkit-tap-highlight-color: transparent;
}

.btn-primary {
  background: var(--primary);
  color: white;
}

.btn-primary:hover, .btn-primary:active {
  background: var(--primary-dark);
}

.btn-danger {
  background: var(--error);
}

.btn-danger:hover, .btn-danger:active {
  background: #b91c1c;
}

.btn-ghost {
  background: transparent;
  color: var(--text);
  padding: 8px;
}

.btn-ghost:hover {
  background: var(--border);
}

.btn-icon {
  padding: 8px;
}

.btn-sm {
  padding: 6px 12px;
  font-size: 0.875rem;
}

.btn-full {
  width: 100%;
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* App Header */
.app-header {
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  background: var(--primary);
  color: white;
  box-shadow: var(--shadow);
}

.app-header h1 {
  font-size: 1.25rem;
  font-weight: 600;
}

.app-header .btn-ghost {
  color: white;
}

.app-header .btn-ghost:hover {
  background: rgba(255,255,255,0.15);
}

/* Screen Header */
.screen-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 50;
}

.screen-header h2 {
  flex: 1;
  font-size: 1.125rem;
  font-weight: 600;
}

/* Main Screen */
.main-screen {
  padding-bottom: 80px;
}

/* Search Bar */
.search-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
}

.search-bar svg {
  color: var(--text-muted);
  flex-shrink: 0;
}

.search-bar input {
  flex: 1;
  border: none;
  background: transparent;
  font-size: 1rem;
  outline: none;
  color: var(--text);
}

.search-bar input::placeholder {
  color: var(--text-muted);
}

/* List */
.list {
  padding: 8px 0;
}

.list-item {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 16px 20px;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.list-item:active {
  background: var(--bg);
}

.list-item-content {
  flex: 1;
  min-width: 0;
}

.list-item-title {
  font-weight: 500;
  margin-bottom: 4px;
}

.list-item-subtitle {
  font-size: 0.875rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.list-item-arrow {
  color: var(--text-muted);
  flex-shrink: 0;
}

.list-item-actions {
  display: flex;
  gap: 8px;
}

.list-item-actions .btn-ghost {
  color: var(--text-muted);
}

.list-item-actions .btn-ghost:hover {
  color: var(--error);
}

/* Documents List */
.documents-list .list-item {
  cursor: default;
}

.documents-list .list-item-icon {
  width: 40px;
  height: 40px;
  border-radius: var(--radius-sm);
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  flex-shrink: 0;
}

.documents-list .list-item-icon.pdf {
  background: #fee2e2;
  color: #dc2626;
}

.documents-list .list-item-icon.image {
  background: #dbeafe;
  color: #2563eb;
}

/* Empty State */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 48px 24px;
  text-align: center;
  color: var(--text-muted);
}

.empty-state svg {
  width: 64px;
  height: 64px;
  margin-bottom: 16px;
  opacity: 0.5;
}

.empty-state p {
  font-size: 1rem;
}

.empty-state.small {
  padding: 24px;
}

.empty-state.small svg {
  width: 32px;
  height: 32px;
  margin-bottom: 8px;
}

.empty-state.small p {
  font-size: 0.875rem;
}

/* FAB */
.fab {
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--primary);
  color: white;
  border: none;
  box-shadow: var(--shadow-lg);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.2s, background 0.2s;
  -webkit-tap-highlight-color: transparent;
  z-index: 50;
}

.fab:hover {
  background: var(--primary-dark);
}

.fab:active {
  transform: scale(0.95);
}

/* Form */
.form-content {
  padding: 20px;
  padding-bottom: 100px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  font-weight: 500;
  margin-bottom: 8px;
  color: var(--text);
}

.form-group input,
.form-group textarea {
  width: 100%;
  padding: 14px 16px;
  font-size: 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-card);
  color: var(--text);
  transition: border-color 0.2s, box-shadow 0.2s;
  font-family: inherit;
}

.form-group input:focus,
.form-group textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.form-group textarea {
  resize: vertical;
  min-height: 100px;
}

.form-group input::placeholder,
.form-group textarea::placeholder {
  color: var(--text-muted);
}

.field-hint {
  display: block;
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 4px;
}

.pesel-hint-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 4px;
  gap: 8px;
}

.pesel-hint-row .field-hint {
  margin-top: 0;
}

.pesel-status {
  font-size: 0.8rem;
  font-weight: 500;
  padding: 2px 8px;
  border-radius: 4px;
  white-space: nowrap;
}

.pesel-status.valid {
  background: rgba(22, 163, 74, 0.1);
  color: var(--success);
}

.pesel-status.invalid {
  background: rgba(220, 38, 38, 0.1);
  color: var(--error);
}

/* Rich Text Editor */
.editor-toolbar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: var(--radius-sm) var(--radius-sm) 0 0;
  flex-wrap: wrap;
}

.editor-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 8px 10px;
  border: none;
  background: transparent;
  border-radius: 4px;
  cursor: pointer;
  color: var(--text);
  transition: background 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.editor-btn:hover {
  background: var(--border);
}

.editor-btn:active, .editor-btn.active {
  background: var(--primary-light);
  color: white;
}

.editor-btn .btn-text {
  font-size: 0.8rem;
  font-weight: 500;
}

.editor-separator {
  width: 1px;
  height: 24px;
  background: var(--border);
  margin: 0 4px;
}

.rich-editor {
  width: 100%;
  min-height: 250px;
  padding: 14px 16px;
  font-size: 1rem;
  border: 1px solid var(--border);
  border-top: none;
  border-radius: 0 0 var(--radius-sm) var(--radius-sm);
  background: var(--bg-card);
  color: var(--text);
  line-height: 1.6;
  overflow-y: auto;
}

.rich-editor:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.rich-editor:empty:before {
  content: attr(placeholder);
  color: var(--text-muted);
  pointer-events: none;
}

.rich-editor img {
  max-width: 100%;
  height: auto;
  border-radius: var(--radius-sm);
  margin: 8px 0;
}

.rich-editor b, .rich-editor strong {
  font-weight: 600;
}

/* Patient Info */
.patient-info {
  padding: 20px;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
}

.patient-info .patient-name {
  font-size: 1.5rem;
  font-weight: 600;
  margin-bottom: 12px;
}

.patient-info .info-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  color: var(--text-muted);
  font-size: 0.95rem;
}

.patient-info .info-row svg {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
}

.patient-info .info-row.pesel {
  font-family: 'SF Mono', Monaco, Consolas, monospace;
  letter-spacing: 1px;
}

.patient-info .patient-notes {
  margin-top: 16px;
  padding: 12px;
  background: var(--bg);
  border-radius: var(--radius-sm);
  font-size: 0.95rem;
  color: var(--text);
  white-space: pre-wrap;
}

/* Section Header */
.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px 8px;
  background: var(--bg);
}

.section-header h3 {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

/* Visit Detail */
.visit-detail-content {
  padding: 20px;
}

.visit-detail-content .visit-date-header {
  font-size: 1.25rem;
  font-weight: 600;
  margin-bottom: 20px;
  color: var(--primary);
}

.visit-detail-content .visit-notes {
  background: var(--bg-card);
  padding: 16px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  line-height: 1.6;
}

.visit-detail-content .visit-notes img {
  max-width: 100%;
  height: auto;
  border-radius: var(--radius-sm);
  margin: 8px 0;
}

.visit-detail-content .empty-notes {
  color: var(--text-muted);
  font-style: italic;
}

/* Loading */
.loading {
  position: fixed;
  inset: 0;
  background: rgba(255,255,255,0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Toast */
.toast {
  position: fixed;
  bottom: 90px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: white;
  padding: 12px 24px;
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  z-index: 1000;
  animation: slideUp 0.3s ease;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateX(-50%) translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
}

.toast.error {
  background: var(--error);
}

.toast.success {
  background: var(--success);
}

/* Confirm Dialog */
.dialog-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
}

.dialog {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 24px;
  max-width: 320px;
  width: 100%;
  box-shadow: var(--shadow-lg);
}

.dialog p {
  margin-bottom: 20px;
  text-align: center;
}

.dialog-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
}

.dialog-actions .btn {
  flex: 1;
}

/* Responsive adjustments for larger screens */
@media (min-width: 640px) {
  .login-container {
    padding: 40px;
  }
  
  .login-form {
    padding: 32px;
  }
  
  .app-header {
    padding: 16px 24px;
  }
  
  .search-bar {
    padding: 12px 24px;
  }
  
  .list-item {
    padding: 16px 24px;
  }
  
  .form-content {
    padding: 24px;
    max-width: 600px;
    margin: 0 auto;
  }
  
  .patient-info {
    padding: 24px;
  }
  
  .fab {
    bottom: 32px;
    right: 32px;
  }
}

@media (min-width: 768px) {
  #main-app {
    max-width: 768px;
    margin: 0 auto;
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
    min-height: 100vh;
    background: var(--bg);
  }
}

/* Safe area insets for notched phones */
@supports (padding: max(0px)) {
  .app-header {
    padding-top: max(16px, env(safe-area-inset-top));
  }
  
  .fab {
    bottom: max(24px, calc(env(safe-area-inset-bottom) + 16px));
    right: max(24px, env(safe-area-inset-right));
  }
  
  .form-content {
    padding-bottom: max(100px, calc(env(safe-area-inset-bottom) + 80px));
  }
}

/* Filter Tabs */
.filter-tabs {
  display: flex;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  padding: 0 16px;
}

.filter-tab {
  flex: 1;
  padding: 12px 16px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.95rem;
  font-weight: 500;
  cursor: pointer;
  position: relative;
  transition: color 0.2s;
  -webkit-tap-highlight-color: transparent;
}

.filter-tab.active {
  color: var(--primary);
}

.filter-tab.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 16px;
  right: 16px;
  height: 2px;
  background: var(--primary);
  border-radius: 2px 2px 0 0;
}

/* Header Actions */
.header-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

/* Archived Badge */
.archived-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  background: var(--text-muted);
  color: white;
  font-size: 0.75rem;
  font-weight: 500;
  border-radius: 4px;
  margin-left: 8px;
}

/* Modal Overlay */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 16px;
}

.modal {
  background: var(--bg-card);
  border-radius: var(--radius);
  width: 100%;
  max-width: 600px;
  max-height: 90vh;
  max-height: 90dvh;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.modal-header h3 {
  font-size: 1.1rem;
  font-weight: 600;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding-right: 8px;
}

.modal-footer {
  display: flex;
  gap: 12px;
  padding: 16px 20px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.modal-footer .btn {
  flex: 1;
  justify-content: center;
}

/* Document Preview Modal */
.document-preview-modal {
  max-width: 90vw;
  max-height: 95vh;
  max-height: 95dvh;
}

.preview-content {
  flex: 1;
  overflow: auto;
  padding: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  min-height: 200px;
}

.preview-content img {
  max-width: 100%;
  max-height: 60vh;
  max-height: 60dvh;
  object-fit: contain;
  border-radius: var(--radius-sm);
}

.preview-content iframe,
.preview-content embed,
.preview-content object {
  width: 100%;
  height: 60vh;
  height: 60dvh;
  border: none;
  border-radius: var(--radius-sm);
}

.preview-content .preview-error {
  text-align: center;
  color: var(--text-muted);
  padding: 40px 20px;
}

.preview-content .preview-error svg {
  width: 48px;
  height: 48px;
  margin-bottom: 16px;
  opacity: 0.5;
}

.preview-content .preview-error p {
  margin-bottom: 16px;
}

.preview-note-section {
  padding: 12px 20px;
  border-top: 1px solid var(--border);
  background: var(--bg-card);
  flex-shrink: 0;
}

.preview-note-section label {
  display: block;
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.note-input-row {
  display: flex;
  gap: 8px;
}

.note-input-row input {
  flex: 1;
  padding: 10px 12px;
  font-size: 0.95rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
}

.note-input-row input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.1);
}

/* Document Note in List */
.doc-note {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.doc-note svg {
  width: 12px;
  height: 12px;
  flex-shrink: 0;
}

.doc-note-text {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Document list item clickable */
.documents-list .list-item {
  cursor: pointer;
}

.documents-list .list-item:active {
  background: var(--bg);
}

/* Preview button style */
.doc-view svg {
  color: var(--primary);
}

.documents-list .list-item-actions .doc-view:hover {
  color: var(--primary-dark);
  background: rgba(37, 99, 235, 0.1);
}

@media (min-width: 640px) {
  .document-preview-modal {
    max-width: 800px;
  }
  
  .preview-content img {
    max-height: 70vh;
    max-height: 70dvh;
  }
  
  .preview-content iframe,
  .preview-content embed,
  .preview-content object {
    height: 70vh;
    height: 70dvh;
  }
}
