/* ================================
   iShelf - Enhanced Spreadsheet Design
   ================================ */

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

:root {
    --color-bg: #f8f9fa;
    --color-surface: #ffffff;
    --color-border: #e1e4e8;
    --color-text: #24292e;
    --color-text-secondary: #586069;
    --color-text-tertiary: #959da5;
    --color-primary: #0366d6;
    --color-primary-hover: #0256c5;
    --color-success: #28a745;
    --color-warning: #ffc107;
    --color-danger: #dc3545;
    --color-reading: #6f42c1;
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.08);
    --shadow-md: 0 4px 12px rgba(0,0,0,0.1);
    --shadow-lg: 0 8px 24px rgba(0,0,0,0.12);
    --font-system: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
}

body {
    font-family: var(--font-system);
    background: var(--color-bg);
    color: var(--color-text);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

.app-wrapper {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* ================================
   Header
   ================================ */

.app-header {
    background: var(--color-surface);
    border-bottom: 1px solid var(--color-border);
    padding: 24px 32px;
    position: sticky;
    top: 0;
    z-index: 100;
}

.header-content {
    max-width: 1400px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 16px;
}

.app-title {
    font-size: 24px;
    font-weight: 600;
    color: var(--color-text);
    margin: 0;
}

.app-subtitle {
    font-size: 14px;
    color: var(--color-text-secondary);
    margin: 0;
}

.header-actions {
    display: flex;
    gap: 12px;
}

/* ================================
   Buttons
   ================================ */

.btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    border: 1px solid var(--color-border);
    border-radius: 6px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    font-family: var(--font-system);
    text-decoration: none;
}

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

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

.btn-secondary {
    background: var(--color-surface);
    color: var(--color-text);
}

.btn-secondary:hover {
    background: var(--color-bg);
    border-color: var(--color-text-tertiary);
}

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

.btn-icon {
    width: 32px;
    height: 32px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    border-radius: 6px;
    color: var(--color-text-secondary);
    cursor: pointer;
    transition: all 0.2s ease;
}

.btn-icon:hover {
    background: var(--color-bg);
    color: var(--color-text);
}

.btn-delete:hover {
    background: rgba(220, 53, 69, 0.1);
    color: var(--color-danger);
}

/* ================================
   Main Container
   ================================ */

.main-container {
    flex: 1;
    max-width: 1400px;
    width: 100%;
    margin: 0 auto;
    padding: 32px;
}

/* ================================
   Empty State
   ================================ */

.empty-state {
    text-align: center;
    padding: 80px 24px;
    background: var(--color-surface);
    border-radius: 12px;
    border: 1px solid var(--color-border);
}

.empty-icon {
    font-size: 64px;
    margin-bottom: 16px;
    opacity: 0.5;
}

.empty-state h2 {
    font-size: 24px;
    font-weight: 600;
    margin-bottom: 8px;
    color: var(--color-text);
}

.empty-state p {
    font-size: 16px;
    color: var(--color-text-secondary);
}

/* ================================
   Spreadsheet Table
   ================================ */

.spreadsheet-container {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: 12px;
    overflow: hidden;
    box-shadow: var(--shadow-sm);
}

.spreadsheet-table {
    width: 100%;
    border-collapse: collapse;
}

.spreadsheet-table thead {
    background: var(--color-bg);
    border-bottom: 2px solid var(--color-border);
}

.spreadsheet-table th {
    padding: 12px 16px;
    text-align: left;
    font-size: 13px;
    font-weight: 600;
    color: var(--color-text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.col-cover {
    width: 60px;
}

.col-title {
    width: 35%;
}

.col-author {
    width: 20%;
}

.col-rating {
    width: 140px;
}

.col-status {
    width: 140px;
}

.col-actions {
    width: 60px;
    text-align: right;
}

.spreadsheet-table tbody tr.table-row {
    border-bottom: 1px solid var(--color-border);
    transition: background 0.15s ease;
    cursor: pointer;
}

.spreadsheet-table tbody tr.table-row:hover {
    background: #f6f8fa;
}

.spreadsheet-table tbody tr.table-row.row-expanded {
    background: #fff8e1;
}

.spreadsheet-table td {
    padding: 12px 16px;
    font-size: 14px;
    color: var(--color-text);
    vertical-align: middle;
}

/* Book Cover */
.cell-cover {
    cursor: default !important;
}

.book-cover {
    width: 40px;
    height: 60px;
    object-fit: cover;
    border-radius: 4px;
    box-shadow: var(--shadow-sm);
}

.book-cover-placeholder {
    width: 40px;
    height: 60px;
    background: var(--color-bg);
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    opacity: 0.5;
}

/* Title & Author */
.cell-title,
.cell-author {
    cursor: pointer;
}

.cell-content {
    padding: 4px 0;
    position: relative;
}

.editable-cell {
    padding: 8px 12px;
    border-radius: 6px;
    transition: background 0.2s ease;
}

.editable-cell:hover {
    background: rgba(3, 102, 214, 0.05);
}

.editable-cell .edit-hint {
    display: none;
    font-size: 11px;
    color: var(--color-primary);
    font-weight: 500;
    margin-top: 4px;
}

.editable-cell:hover .edit-hint {
    display: block;
}

.book-title {
    font-weight: 500;
    color: var(--color-text);
    display: block;
    margin-bottom: 4px;
}

.page-count {
    font-size: 12px;
    color: var(--color-text-tertiary);
    display: block;
}

.book-author {
    color: var(--color-text-secondary);
}

.cell-edit {
    padding: 8px 0;
}

.edit-input {
    width: 100%;
    padding: 8px 12px;
    border: 2px solid var(--color-primary);
    border-radius: 6px;
    font-size: 14px;
    font-family: var(--font-system);
    outline: none;
    margin-bottom: 8px;
}

.edit-actions {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
}

.btn-edit-cancel,
.btn-edit-save {
    padding: 4px 12px;
    border-radius: 4px;
    font-size: 12px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    border: none;
    font-family: var(--font-system);
}

.btn-edit-cancel {
    background: var(--color-bg);
    color: var(--color-text-secondary);
}

.btn-edit-cancel:hover {
    background: #ddd;
}

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

.btn-edit-save:hover {
    background: var(--color-primary-hover);
}

/* Star Rating */
.star-rating {
    display: flex;
    gap: 4px;
}

.star-btn {
    background: none;
    border: none;
    font-size: 20px;
    color: #ddd;
    cursor: pointer;
    padding: 0;
    line-height: 1;
    transition: all 0.2s ease;
}

.star-btn:hover {
    transform: scale(1.2);
}

.star-btn.star-filled {
    color: #ffc107;
}

/* Status Select */
.status-select {
    padding: 6px 32px 6px 12px;
    border: 1px solid transparent;
    border-radius: 6px;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    appearance: none;
    background-repeat: no-repeat;
    background-position: right 8px center;
    background-size: 16px;
    background-image: url("data:image/svg+xml,%3Csvg width='16' height='16' viewBox='0 0 16 16' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M4 6l4 4 4-4' stroke='%23586069' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    transition: all 0.2s ease;
}

.status-select:hover {
    border-color: var(--color-border);
}

.status-reading {
    background-color: rgba(111, 66, 193, 0.1);
    color: var(--color-reading);
}

.status-completed {
    background-color: rgba(40, 167, 69, 0.1);
    color: var(--color-success);
}

.status-want-to-read {
    background-color: rgba(255, 193, 7, 0.1);
    color: #e6a800;
}

.cell-actions {
    text-align: right;
    padding-right: 12px;
}

/* ================================
   Expanded Row
   ================================ */

.expanded-row {
    border-bottom: 1px solid var(--color-border) !important;
    background: #fefcf8 !important;
}

.expanded-cell {
    padding: 0 !important;
    background: #fefcf8 !important;
}

.expanded-content {
    padding: 32px;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 32px;
    animation: expandIn 0.3s ease;
}

@keyframes expandIn {
    from {
        opacity: 0;
        transform: translateY(-10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.summary-section,
.notes-section {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: 8px;
    padding: 20px;
}

.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
}

.section-header h4 {
    font-size: 16px;
    font-weight: 600;
    color: var(--color-text);
    margin: 0;
}

.summary-content {
    margin-top: 16px;
}

.summary-content p {
    margin-bottom: 12px;
    line-height: 1.6;
    color: var(--color-text-secondary);
}

.summary-actions {
    display: flex;
    gap: 8px;
    margin-top: 16px;
    padding-top: 16px;
    border-top: 1px solid var(--color-border);
}

.spinner-sm {
    display: inline-block;
    width: 12px;
    height: 12px;
    border: 2px solid rgba(255,255,255,0.3);
    border-top-color: white;
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}

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

/* Notes */
.notes-display {
    min-height: 100px;
}

.notes-text {
    white-space: pre-wrap;
    font-family: var(--font-system);
    font-size: 14px;
    line-height: 1.6;
    color: var(--color-text);
}

.notes-empty {
    color: var(--color-text-tertiary);
    font-style: italic;
    font-size: 14px;
}

.notes-textarea {
    width: 100%;
    padding: 12px;
    border: 1px solid var(--color-border);
    border-radius: 6px;
    font-family: var(--font-system);
    font-size: 14px;
    resize: vertical;
    outline: none;
}

.notes-textarea:focus {
    border-color: var(--color-primary);
}

.notes-actions {
    display: flex;
    gap: 8px;
    margin-top: 12px;
    justify-content: flex-end;
}

/* ================================
   Chat Widget
   ================================ */

.chat-widget {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 1000;
}

.chat-toggle {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: var(--color-primary);
    color: white;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow-lg);
    transition: all 0.3s ease;
    position: relative;
}

.chat-toggle:hover {
    transform: scale(1.1);
    background: var(--color-primary-hover);
}

.chat-toggle.hidden {
    display: none;
}

.chat-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    background: var(--color-danger);
    color: white;
    width: 24px;
    height: 24px;
    border-radius: 12px;
    font-size: 12px;
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid var(--color-surface);
}

.chat-panel {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: 400px;
    height: 600px;
    background: var(--color-surface);
    border-radius: 16px;
    box-shadow: var(--shadow-lg);
    display: flex;
    flex-direction: column;
    border: 1px solid var(--color-border);
    overflow: hidden;
}

.chat-enter {
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.chat-enter-start {
    opacity: 0;
    transform: translateY(20px) scale(0.95);
}

.chat-enter-end {
    opacity: 1;
    transform: translateY(0) scale(1);
}

.chat-leave {
    transition: all 0.2s ease;
}

.chat-leave-start {
    opacity: 1;
}

.chat-leave-end {
    opacity: 0;
    transform: translateY(20px);
}

.chat-header {
    padding: 20px;
    border-bottom: 1px solid var(--color-border);
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    background: var(--color-bg);
}

.chat-header-content h3 {
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 4px;
    color: var(--color-text);
}

.chat-header-content p {
    font-size: 13px;
    color: var(--color-text-secondary);
    margin: 0;
}

.chat-header-actions {
    display: flex;
    gap: 8px;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    background: var(--color-surface);
}

.chat-messages::-webkit-scrollbar {
    width: 6px;
}

.chat-messages::-webkit-scrollbar-track {
    background: transparent;
}

.chat-messages::-webkit-scrollbar-thumb {
    background: var(--color-border);
    border-radius: 3px;
}

.chat-empty {
    text-align: center;
    padding: 40px 20px;
    color: var(--color-text-secondary);
}

.chat-empty p {
    font-size: 15px;
    font-weight: 500;
    margin-bottom: 16px;
}

.chat-empty ul {
    list-style: none;
    padding: 0;
    text-align: left;
    display: inline-block;
}

.chat-empty li {
    padding: 8px 0;
    font-size: 14px;
}

.chat-empty li:before {
    content: "•";
    color: var(--color-primary);
    font-weight: bold;
    display: inline-block;
    width: 1em;
    margin-left: -1em;
}

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

.message {
    margin-bottom: 12px;
    animation: messageSlide 0.3s ease;
}

@keyframes messageSlide {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.user-message {
    display: flex;
    justify-content: flex-end;
}

.user-message .message-content {
    background: var(--color-primary);
    color: white;
    padding: 10px 14px;
    border-radius: 12px;
    border-bottom-right-radius: 4px;
    max-width: 80%;
    font-size: 14px;
    line-height: 1.4;
    word-wrap: break-word;
}

.assistant-message .message-content {
    background: var(--color-bg);
    color: var(--color-text);
    padding: 10px 14px;
    border-radius: 12px;
    border-bottom-left-radius: 4px;
    max-width: 85%;
    font-size: 14px;
    line-height: 1.4;
    border: 1px solid var(--color-border);
    word-wrap: break-word;
}

.chat-input-container {
    padding: 16px;
    border-top: 1px solid var(--color-border);
    background: var(--color-surface);
}

.chat-input-form {
    display: flex;
    gap: 8px;
    align-items: center;
}

.chat-input {
    flex: 1;
    padding: 10px 14px;
    border: 1px solid var(--color-border);
    border-radius: 8px;
    font-size: 14px;
    font-family: var(--font-system);
    outline: none;
    transition: border-color 0.2s ease;
}

.chat-input:focus {
    border-color: var(--color-primary);
}

.chat-input:disabled {
    background: var(--color-bg);
    cursor: not-allowed;
}

.chat-submit {
    width: 40px;
    height: 40px;
    padding: 0;
    border: none;
    border-radius: 8px;
    background: var(--color-primary);
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
}

.chat-submit:hover:not(:disabled) {
    background: var(--color-primary-hover);
}

.chat-submit:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* ================================
   Modal
   ================================ */

.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2000;
    padding: 24px;
}

.modal-content {
    background: var(--color-surface);
    border-radius: 12px;
    max-width: 480px;
    width: 100%;
    box-shadow: var(--shadow-lg);
    animation: modalPop 0.3s ease;
}

@keyframes modalPop {
    from {
        opacity: 0;
        transform: scale(0.9);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

.modal-header {
    padding: 24px 24px 16px;
    border-bottom: 1px solid var(--color-border);
}

.modal-header h3 {
    font-size: 20px;
    font-weight: 600;
    margin-bottom: 8px;
    color: var(--color-text);
}

.modal-header p {
    font-size: 14px;
    color: var(--color-text-secondary);
    margin: 0;
}

.modal-body {
    padding: 24px;
}

.modal-cover {
    text-align: center;
    margin-bottom: 20px;
}

.modal-cover img {
    max-width: 120px;
    border-radius: 8px;
    box-shadow: var(--shadow-md);
}

.modal-field {
    display: flex;
    padding: 12px 0;
    border-bottom: 1px solid var(--color-border);
}

.modal-field:last-child {
    border-bottom: none;
}

.field-label {
    font-size: 13px;
    font-weight: 600;
    color: var(--color-text-secondary);
    min-width: 80px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.field-value {
    font-size: 14px;
    color: var(--color-text);
    font-weight: 500;
    flex: 1;
}

.modal-actions {
    padding: 16px 24px 24px;
    display: flex;
    gap: 12px;
    justify-content: flex-end;
}

/* ================================
   Responsive Design
   ================================ */

@media (max-width: 768px) {
    .app-header {
        padding: 16px 20px;
    }
    
    .header-content {
        flex-direction: column;
        gap: 16px;
        align-items: flex-start;
    }
    
    .main-container {
        padding: 16px;
    }
    
    .spreadsheet-container {
        overflow-x: auto;
    }
    
    .spreadsheet-table {
        min-width: 700px;
    }
    
    .expanded-content {
        grid-template-columns: 1fr;
        gap: 20px;
    }
    
    .chat-panel {
        width: calc(100vw - 32px);
        height: calc(100vh - 100px);
    }
}