/**
 * カスタムダッシュボード - スタイル
 * Phase 1 MVP
 */

/* ========== Dashboard Grid (12-Column Manual Layout) ========== */
#dashboard-grid {
    display: grid;
    /* Responsive 12-column grid with minimum 80px per column */
    grid-template-columns: repeat(12, minmax(80px, 1fr));
    grid-auto-rows: 120px;
    /* Reduced from 200px for compact layout */
    gap: 16px;
    padding: 20px;
    min-height: 600px;
    /* Responsive width - fits within viewport */
    max-width: 100%;
    box-sizing: border-box;
}

/* ========== Widget Container ========== */
.dashboard-widget {
    position: relative;
    /* Required for absolute-positioned resize handles */
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    min-height: 300px;
    min-width: 0;
    /* グリッド内で内部コンテンツによる伸長を防ぐ */
    display: flex;
    flex-direction: column;
    /* Manual positioning via grid-column/grid-row set in JS */
    z-index: 1;
    /* Establish stacking context to prevent overlap issues */
}

/* ========== Widget Header ========== */
.widget-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    border-bottom: 1px solid #e5e7eb;
    background: #f9fafb;
}

.widget-title {
    font-size: 16px;
    font-weight: 600;
    margin: 0;
    color: #111827;
    /* Darkened for better contrast */
}

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

.btn-widget-action {
    border: none;
    background: transparent;
    cursor: pointer;
    font-size: 16px;
    padding: 4px 8px;
    border-radius: 4px;
    color: #111827;
    /* High contrast for edit/delete buttons */
    transition: background 0.2s;
}

.btn-widget-action:hover {
    background: #e5e7eb;
}

/* ========== Widget Body ========== */
.widget-body {
    flex: 1;
    padding: 16px;
    position: relative;
    min-width: 0;
    /* フレックスコンテナ内でのshrink許可 */
    min-height: 0;
    /* フレックスコンテナ内でのshrink許可 */
    overflow: hidden;
    /* 子要素がはみ出さないようにする */
}

.widget-body canvas {
    max-width: 100% !important;
    height: auto !important;
}

.widget-loading,
.widget-error {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: #4b5563;
    /* Darkened for better contrast */
    font-size: 14px;
}

.widget-error {
    color: #ef4444;
}

/* ========== Drag and Drop Visual Feedback ========== */
.dashboard-widget.dragging {
    opacity: 0.4;
    transform: scale(0.95);
    transition: opacity 0.2s, transform 0.2s;
}

.dashboard-widget.drag-over {
    border: 3px dashed #3b82f6;
    background: #eff6ff;
    transition: all 0.2s;
}

.widget-header[draggable="true"] {
    cursor: move !important;
    user-select: none;
}

.widget-header[draggable="true"]:hover {
    background: #f3f4f6;
}

/* ========== Resize Handles ========== */
.resize-handle {
    position: absolute;
    background: #3b82f6;
    opacity: 0;
    transition: opacity 0.2s;
    z-index: 10;
}

.dashboard-widget:hover .resize-handle {
    opacity: 0.7;
}

.resize-handle:hover {
    opacity: 1 !important;
    background: #2563eb;
}

/* Corner resize handles - 12px squares */
.resize-se {
    bottom: -4px;
    right: -4px;
    width: 12px;
    height: 12px;
    cursor: nwse-resize;
    border-radius: 2px;
}

/* Scenario selection checkboxes */
.scenario-checkboxes {
    display: flex;
    gap: 16px;
}

.scenario-checkboxes label {
    display: flex;
    align-items: center;
    gap: 6px;
    cursor: pointer;
    font-size: 14px;
}

.scenario-checkboxes input[type="checkbox"] {
    cursor: pointer;
    width: 16px;
    height: 16px;
}

.resize-ne {
    top: -4px;
    right: -4px;
    width: 12px;
    height: 12px;
    cursor: ne-resize;
    border-radius: 2px;
}

.resize-sw {
    bottom: -4px;
    left: -4px;
    width: 12px;
    height: 12px;
    cursor: sw-resize;
    border-radius: 2px;
}

.resize-nw {
    top: -4px;
    left: -4px;
    width: 12px;
    height: 12px;
    cursor: nw-resize;
    border-radius: 2px;
}

/* ========== Add Widget Button ========== */
.dashboard-widget-add {
    min-height: 300px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.btn-add-widget {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    height: 100%;
    border: 2px dashed #cbd5e1;
    background: transparent;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s;
    color: #64748b;
    font-size: 14px;
}

.btn-add-widget:hover {
    border-color: #3b82f6;
    color: #3b82f6;
    background: #eff6ff;
}

.plus-icon {
    font-size: 32px;
    font-weight: 300;
}

/* ========== Widget Config Modal ========== */
#widget-config-modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    align-items: center;
    justify-content: center;
}

.widget-modal-content {
    background: white;
    border-radius: 8px;
    padding: 24px;
    max-width: 600px;
    width: 90%;
    max-height: 80vh;
    overflow-y: auto;
}

.widget-modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

.widget-modal-header h2 {
    margin: 0;
    font-size: 20px;
    color: #111827;
}

.btn-modal-close {
    border: none;
    background: transparent;
    font-size: 24px;
    cursor: pointer;
    color: #4b5563;
    padding: 0;
    width: 32px;
    height: 32px;
    line-height: 1;
}

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

.widget-form-group {
    margin-bottom: 16px;
}

.widget-form-group label {
    display: block;
    margin-bottom: 4px;
    font-size: 14px;
    font-weight: 500;
    color: #111827;
    /* Darkened for better contrast */
}

.widget-form-group input,
.widget-form-group select {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid #d1d5db;
    border-radius: 4px;
    font-size: 14px;
}

.widget-form-group input:focus,
.widget-form-group select:focus {
    outline: none;
    border-color: #3b82f6;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.widget-modal-actions {
    display: flex;
    gap: 12px;
    justify-content: flex-end;
    margin-top: 24px;
}

.btn-widget-cancel,
.btn-widget-save {
    padding: 8px 16px;
    border-radius: 4px;
    font-size: 14px;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-widget-cancel {
    border: 1px solid #d1d5db;
    background: white;
    color: #111827;
}

.btn-widget-cancel:hover {
    background: #f9fafb;
}

.btn-widget-save {
    border: none;
    background: #3b82f6;
    color: white;
}

.btn-widget-save:hover {
    background: #2563eb;
}

/* ========== Table Orientation Toggle ========== */
.btn-toggle {
    padding: 6px 12px;
    border: 1px solid #d1d5db;
    background: white;
    color: #111827;
    cursor: pointer;
    font-size: 13px;
    border-radius: 4px;
    transition: all 0.2s;
}

.btn-toggle:hover {
    background: #f9fafb;
}

.btn-toggle.active {
    background: #3b82f6;
    border-color: #3b82f6;
    color: white;
}

/* ========== Layout Preset Selector ========== */
.layout-preset-selector {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 16px 20px;
    background: #f9fafb;
    border-bottom: 1px solid #e5e7eb;
    margin-bottom: 20px;
}

.layout-preset-selector label {
    font-size: 14px;
    font-weight: 500;
    color: #111827;
}

#preset-select {
    padding: 6px 12px;
    border: 1px solid #d1d5db;
    border-radius: 4px;
    font-size: 14px;
    min-width: 200px;
    cursor: pointer;
}

#preset-select:focus {
    outline: none;
    border-color: #3b82f6;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.btn-apply-preset {
    padding: 6px 16px;
    background: #3b82f6;
    color: white;
    border: none;
    border-radius: 4px;
    font-size: 14px;
    cursor: pointer;
    transition: background 0.2s;
}

.btn-apply-preset:hover {
    background: #2563eb;
}

.btn-apply-preset:disabled {
    background: #9ca3af;
    cursor: not-allowed;
}

/* ========== 指標選択UI ========== */
.metrics-list {
    display: flex;
    flex-direction: column;
    gap: 16px;
    margin-bottom: 16px;
    max-height: 400px;
    overflow-y: auto;
}

.metric-row {
    background: #f9fafb;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    padding: 16px;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    position: relative;
}

.metric-row-header {
    grid-column: 1 / -1;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
}

.metric-row-title {
    font-weight: 600;
    color: #111827;
    font-size: 14px;
}

.btn-remove-metric {
    background: #ef4444;
    color: white;
    border: none;
    border-radius: 4px;
    padding: 4px 12px;
    font-size: 12px;
    cursor: pointer;
    transition: background 0.2s;
}

.btn-remove-metric:hover {
    background: #dc2626;
}

.metric-form-group {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.metric-form-group label {
    font-size: 13px;
    font-weight: 500;
    color: #4b5563;
}

.metric-form-group select,
.metric-form-group input {
    padding: 8px 12px;
    border: 1px solid #d1d5db;
    border-radius: 4px;
    font-size: 14px;
}

.metric-form-group select:focus,
.metric-form-group input:focus {
    outline: none;
    border-color: #3b82f6;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.btn-add-metric {
    width: 100%;
    padding: 12px;
    background: #f3f4f6;
    border: 2px dashed #d1d5db;
    border-radius: 8px;
    color: #4b5563;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-add-metric:hover {
    background: #e5e7eb;
    border-color: #3b82f6;
    color: #3b82f6;
}

/* 色選択プレビュー */
input[type="color"] {
    width: 60px;
    height: 38px;
    border: 1px solid #d1d5db;
    border-radius: 4px;
    cursor: pointer;
}

/* ========== Fixed Add Widget Button (Pill-Shaped with Label) ========== */
.btn-add-widget-fixed {
    position: fixed;
    bottom: 32px;
    right: 32px;
    width: 180px;
    height: 56px;
    border-radius: 28px;
    background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
    color: white;
    border: none;
    box-shadow: 0 4px 12px rgba(59, 130, 246, 0.4);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    font-size: 15px;
    font-weight: 600;
    z-index: 100;
    transition: all 0.3s ease;
    padding: 0 24px;
}

.btn-add-widget-fixed:hover {
    background: linear-gradient(135deg, #2563eb 0%, #1d4ed8 100%);
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(59, 130, 246, 0.5);
}

.btn-add-widget-fixed .icon {
    font-size: 20px;
    line-height: 1;
}

.btn-add-widget-fixed .label {
    font-size: 15px;
    line-height: 1;
    white-space: nowrap;
}

/* ========== Smooth Drag-and-Drop Transitions ========== */
.dashboard-widget {
    transition: grid-column 0.3s ease, grid-row 0.3s ease, opacity 0.2s ease, transform 0.2s ease;
}

.dashboard-widget.dragging {
    opacity: 0.5;
    z-index: 1000;
}

/* Drag preview ghost */
.drag-preview-ghost {
    position: fixed;
    pointer-events: none;
    z-index: 999;
    background: rgba(59, 130, 246, 0.2);
    border: 2px dashed #3b82f6;
    border-radius: 8px;
    transition: all 0.1s ease;
}

/* ========== Table Widget Styles ========== */
.widget-table-wrapper {
    width: 100%;
    height: 100%;
    overflow: auto;
}

.widget-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}

.widget-table thead {
    background: #f9fafb;
    position: sticky;
    top: 0;
    z-index: 1;
}

.widget-table th {
    padding: 10px 12px;
    text-align: left;
    font-weight: 600;
    color: #111827;
    /* Darkened for better contrast */
    border-bottom: 2px solid #e5e7eb;
    white-space: nowrap;
    /* Prevent text wrapping and force horizontal */
    writing-mode: horizontal-tb;
    /* Force horizontal text even in narrow columns */
}

.widget-table td {
    padding: 8px 12px;
    border-bottom: 1px solid #f3f4f6;
    color: #111827;
    white-space: nowrap;
    /* Prevent text wrapping in cells */
    writing-mode: horizontal-tb;
    /* Force horizontal text in all cells */
}

.widget-table tbody tr:hover {
    background: #f9fafb;
}

.widget-table .cell-period {
    font-weight: 500;
    color: #4b5563;
}

.widget-table .cell-number {
    text-align: right;
    font-family: 'Courier New', monospace;
}

/* フィルタセクションのスタイル */
.filter-dropdown-group {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 16px;
    padding: 16px;
    background: #f9fafb;
    border: 1px solid #e5e7eb;
    border-radius: 6px;
}

.filter-item {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.filter-item-label {
    font-weight: 600;
    color: #111827;
    font-size: 13px;
}

.filter-select {
    padding: 6px 8px;
    border: 1px solid #d1d5db;
    border-radius: 4px;
    font-size: 14px;
    background: white;
    color: #374151;
    cursor: pointer;
    transition: border-color 0.2s;
}

.filter-select:focus {
    outline: none;
    border-color: #3b82f6;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.filter-select option {
    padding: 6px;
}

.filter-select option:checked {
    background: #3b82f6;
    color: white;
}

.filter-hint {
    margin-top: 8px;
    font-size: 13px;
    color: #6b7280;
    font-style: italic;
}

/* 指標ごとのフィルタスタイル */
.metric-filters {
    margin-top: 16px;
    padding-top: 16px;
    border-top: 1px solid #e5e7eb;
}

.metric-filters-label {
    display: block;
    font-size: 13px;
    font-weight: 600;
    color: #6b7280;
    margin-bottom: 8px;
}

.metric-filters-row {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 12px;
}

.metric-filter-item {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.metric-filter-item label {
    font-size: 12px;
    color: #6b7280;
    font-weight: 500;
}

.metric-filter-item select {
    padding: 6px 8px;
    border: 1px solid #d1d5db;
    border-radius: 4px;
    font-size: 13px;
    background: white;
    color: #374151;
    cursor: pointer;
    transition: border-color 0.2s;
}

.metric-filter-item select:focus {
    outline: none;
    border-color: #3b82f6;
    box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.1);
}

/* ========== Dashboard Loading Spinner ========== */
#dashboard-grid .loading-spinner {
    grid-column: 1 / -1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 80px 20px;
    gap: 16px;
    min-height: 400px;
}

#dashboard-grid .loading-spinner .spinner {
    width: 48px;
    height: 48px;
    border: 4px solid #e5e7eb;
    border-top-color: #3b82f6;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

#dashboard-grid .loading-spinner .loading-text {
    font-size: 16px;
    color: #374151;
    font-weight: 500;
}

#dashboard-grid .loading-spinner .loading-subtext {
    font-size: 13px;
    color: #9ca3af;
    margin-top: -8px;
}

/* ========== Dashboard Empty State ========== */
.dashboard-empty-state {
    grid-column: 1 / -1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 80px 20px;
    min-height: 400px;
    background: #f9fafb;
    border: 2px dashed #e5e7eb;
    border-radius: 12px;
}

.dashboard-empty-state p {
    margin: 0;
    font-size: 18px;
    color: #6b7280;
    font-weight: 500;
}

.dashboard-empty-state .empty-state-hint {
    font-size: 14px;
    color: #9ca3af;
    margin-top: 8px;
    font-weight: 400;
}
